Detecting faces in images
Warning
The Vision OCR features listed below are legacy features and will be discontinued on May 14, 2024.
To detect faces in a photo, use the Face Detection.
To do this in the batchAnalyze method, set the type
property to FACE_DETECTION
.
Examples
Getting started
To use the examples, install cURL
Get your account data for authentication:
-
Get an IAM token for your Yandex account or federated account.
-
Get the ID of the folder for which your account has the
ai.vision.user
role or higher. -
When accessing Vision OCR via the API, provide the received parameters in each request:
-
For the Vision API and Classifier API:
Specify the IAM token in the
Authorization
header in the following format:Authorization: Bearer <IAM token>
Specify the folder ID in the request body in the
folderId
parameter. -
For the OCR API:
- Specify the IAM token in the
Authorization
header. - Specify the folder ID in the
x-folder-id
header.
Authorization: Bearer <IAM_token> x-folder-id <folder_ID>
- Specify the IAM token in the
-
Vision OCR supports two authentication methods based on service accounts:
-
Using an IAM token:
-
Specify the obtained IAM token in the
Authorization
header in the following format:Authorization: Bearer <IAM token>
-
With API keys.
API keys do not expire. This means that this authentication method is simpler, but less secure. Use it if you can't automatically request an IAM token.
-
Specify the obtained API key in the
Authorization
header in the following format:Authorization: Api-Key <API key>
Do not specify the folder ID in your requests, as YandexGPT uses the folder in which the service account was created.
Find faces in an image
-
Prepare an image file that meets the requirements:
- The supported file formats are JPEG, PNG, and PDF. Specify the MIME type
of the file in themime_type
property. The default value isimage
. - The maximum file size is 1 MB.
- The image size should not exceed 20 MP (height × width).
Note
Need an image? Download a sample
. - The supported file formats are JPEG, PNG, and PDF. Specify the MIME type
-
Encode the file as Base64:
UNIXWindowsPowerShellPythonNode.jsJavaGobase64 -i input.jpg > output.txt
C:> Base64.exe -e input.jpg > output.txt
[Convert]::ToBase64String([IO.File]::ReadAllBytes("./input.jpg")) > output.txt
# Import a library for encoding files in Base64 import base64 # Create a function that will encode a file and return results. def encode_file(file_path): with open(file_path, "rb") as fid: file_content = fid.read() return base64.b64encode(file_content).decode("utf-8")
// Read the file contents to memory. var fs = require('fs'); var file = fs.readFileSync('/path/to/file'); // Get the file contents in Base64 format. var encoded = Buffer.from(file).toString('base64');
// Import a library for encoding files in Base64. import org.apache.commons.codec.binary.Base64; // Get the file contents in Base64 format. byte[] fileData = Base64.encodeBase64(yourFile.getBytes());
import ( "bufio" "encoding/base64" "io/ioutil" "os" ) // Open the file. f, _ := os.Open("/path/to/file") // Read the file contents. reader := bufio.NewReader(f) content, _ := ioutil.ReadAll(reader) // Get the file contents in Base64 format. base64.StdEncoding.EncodeToString(content)
-
Create a file with the request body, e.g.,
body.json
:body.json:
{ "folderId": "b1gvmob95yys********", "analyze_specs": [{ "content": "iVBORw0KGgo...", "features": [{ "type": "FACE_DETECTION" }] }] }
Where:
folderId
: ID of any folder for which your account has theai.vision.user
role or higher.analyze_specs: content
: Image encoded in Base64.
-
Send a request using the batchAnalyze method and save the response to a file, e.g.,
output.json
:BashCMDPowerShellexport IAM_TOKEN=<IAM token> curl -X POST \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${IAM_TOKEN}" \ -d '@body.json' \ https://vision.api.cloud.yandex.net/vision/v1/batchAnalyze > output.json
set IAM_TOKEN=<IAM token> curl -X POST ^ -H "Content-Type: application/json" ^ -H "Authorization: Bearer %IAM_TOKEN%" ^ -d "@body.json" ^ https://vision.api.cloud.yandex.net/vision/v1/batchAnalyze > output.json
$Env:IAM_TOKEN="<IAM token>" curl -X POST ` -H "Content-Type: application/json" ` -H "Authorization: Bearer $Env:IAM_TOKEN" ` -d '@body.json' ` https://vision.api.cloud.yandex.net/vision/v1/batchAnalyze > output.json
Where
IAM_TOKEN
is the IAM token received before starting.
Ready-to-use function for sending requests in bash
The example below is intended to be run in MacOS and Linux. To run it in Windows, see how to work with Bash in Microsoft Windows.
-
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
-
Copy the function to the terminal:
vision_face_detection() { curl -H "Authorization: Bearer `yc iam create-token`" \ "https://vision.api.cloud.yandex.net/vision/v1/batchAnalyze" \ -d @<(cat << EOF { "folderId": "`yc config get folder-id`", "analyze_specs": [{ "content": "`base64 -i $1`", "features": [{ "type": "FACE_DETECTION" }] }] } EOF ) }
Explanations:
yc iam create-token
: get an IAM token.-d @<(cat EOF ... EOF)
: create a request body.yc config get folder-id
: get the ID of the default folder selected in the CLI.base64 -i $1
: Base64 encoding of the image passed in the function arguments.
-
Now you can call this function by passing the image path in the arguments:
vision_face_detection path/to/image.jpg