Detect Face
Table of Contents
Overview
The zoho.ai.detectFace task detects all the faces in the image and returns the bounding box coordinates of the faces detected. Additionally, the emotion, gender, and pose attributes of the face can also be detected.
For example, using this task, you can predict if a seminar was engaging based on the audience's facial expressions. Learn more.
- The prediction results may not be accurate, which is the case with any AI prediction. However, we are working on improving this.
- The prediction results are dynamic. The same script may produce different outcomes based on the machine's knowledge.
Syntax
<response> = zoho.ai.detectFace(<input_image>, <is_dominant_face>, <attribute_list>);
where:
Params | Data type | Description |
<response> | KEY-VALUE | Specifies the predicted face coordinates, emotion, gender, and position of all faces in the image. |
<input_image> | File | Specifies the file object that contains the image. Note:
|
<is_dominant_face> | Boolean (true/false) | Specifies the condition to return the data of prominent face or all faces in the image. true - Returns bounding box of prominent face. false - Returns bounding boxes of all faces in the image. |
<attribute_list> (optional) | List | Specifies the list of values to return. The supported values are:
|
Example 1: Detect faces in an image
The following script detects the face in the image and returns its coordinates.
image_file = invokeurl [ url : "https://c0.wallpaperflare.com/preview/52/764/33/twin-boys.jpg" ]; response = zoho.ai.detectFace(image_file, false);
Where:
"https://c0.wallpaperflare.com/preview/52/764/33/twin-boys.jpg"
response
image_file
false
Example 2: Detect additional attributes like gender, emotion and pose.
The following script detects the faces in the image and returns its response with face coordinates, gender, emotion, and pose.
image_file = invokeurl [ url :"https://c0.wallpaperflare.com/preview/52/764/33/twin-boys.jpg" ]; attribute_list = list(); attribute_list.add("gender"); attribute_list.add("emotion"); attribute_list.add("pose"); response = zoho.ai.detectFace(image_file, false, attribute_list);
Where:
attribute_list
Example 3: Apply zoho.ai.detectface task on an image uploaded in a Zoho Creator field
The below code downloads the image from the field through API and returns its response with zoho.ai.detectFace task. The zoho.ai.detectFace task cannot be used with Zoho Creator’s form fields. Before that, you need to establish a connection within the application. In order to download the uploaded file in the form, you need download file API to use it with the zoho.ai.detectFace task.
// Replace <account_owner_name> with the user name of the Creator accounts owner. // Replace <app_link_name> the link name of the target application. // Replace <report_link_name> with the link name of the target report. // Replace <record_ID > with the ID of the record from which you want to download the file. // Replace <field_link_name> with the link name of the target file upload or image field. image_file = invokeurl [ url: "https://creator.zoho.com/api/v2/<account_owner_name>/<app_link_name>/report/<report_link_name>/<record_ID>/<field_link_name>/download" type : GET connection: "creator_oauth_connection" ]; attribute_list = List(); attribute_list.add("gender"); attribute_list.add("emotion"); attribute_list.add("pose"); response = zoho.ai.detectFace(image_file, false, attribute_list);
Where:
"https://creator.zoho.com/api/v2/<account_owner_name>/
<app_link_name>/report/<report_link_name>/<record_id>
/<field_link_name>/download</field_link_name
</record_id></report_link_name></app_link_name></account_owner_name>"
creator_oauth_connection
Scenario 1: Fetch required data from the response
Consider an image where you need to know the total count of people. This can be done by detecting the number of faces in the image. The zoho.ai.detectFace task stores the number of faces found data in the response as numFacesFound. This particular data can be fetched using the get() function. Refer to the following script for better understanding.
image_file = invokeurl [ url : "https://ylpapp.isb.edu/user/Passport_photo_samples/Sample-9---wrong.jpg" ]; // Create a variable to store the response. response = zoho.ai.detectFace(image_file, false); // Fetch the "numFacesFound" data from response using get() function. number_of_faces = response.get("numFacesFound");
Scenario 2: Predict, whether the program was engaging based on the facial expression of the audience
Consider an image with a group of people sitting in an auditorium. By analyzing the facial emotions of the people in the picture, we can conclude whether the program is engaging or not.
If the number of happy faces exceeds 50% of the total face count, we can conclude that the program is engaging, otherwise it is not engaging.
The following script fulfils the above-said scenario.
image_file = invokeurl [ url : "https://media.istockphoto.com/photos/diverse-human-faces-picture-id1187245319?k=20&m=1187245319&s=612x612&w=0&h=byaOZWGwYLZI3YoGagQEiFds05DHjmcRfSynTeTrPe4=" type : GET ]; // Create a variable -face_data to store the data as list by using get() and toList() function. face_data = zoho.ai.detectFace(image_file, false, {"emotion"}).get("data").toList(); count = 0; // Create a variable - total_face to stores the total face count in the image using size() function. total_face = face_data.size(); for each index face in face_data { detect_emotion = face_data.get(face).get("emotion").get("prediction"); if(detect_emotion == "happy") { count = count + 1; } } if(count > total_face / 2) { response = "The seminar was engaging"; } else { response = "The seminar was not engaging"; }
Scenario 3: Populate Zoho Creator field with predicted emotion
Let’s consider a situation where you need to auto-populate predicted emotion on a single line field based on the image uploaded in the file upload / image field. The emotion can be fetched from the response of zoho.ai.detectFace task using get() function. This action can be performed with the help of Zoho Creator - Download File API.
The following script detects the emotion from the uploaded image and updates another Zoho Creator field with the predicted emotion:
// url - Contains the download file API I URL to download the uploaded image // account_owner_name - Replace with the user name of the Creator account. // app_link_name - Replace with the link name of the target application. // report_link_name - Replace with the link name of the target report. // record_ID - Replace with the ID of the record from which you want to download the file. // field_link_name - Replace with the link name of the target file upload or image field. image_file = invokeurl [ url: "https://creator.zoho.com/api/v2/<account_owner_name>/<app_link_name>/report/<report_link_name>/<record_ID>/<field_link_name>/download" type : GET connection: "zoho_oauth_connection" ]; // Create a variable to store the data of prominent face detect_emotion = zoho.ai.detectFace(image_file,true,{"emotion"}).getJson("data").getJson("emotion").getJson("prediction"); // Specify a form field where the emotion needs to be populated. // Replace "predicted_emotion" with your single line field_link_name . input.predicted_emotion = detect_emotion;
Response Format
The success response will be returned in the following format.
{ "data": [
{
"faceCoordinate": [
"0.565",
"0.273",
"0.760",
"0.590"
],
"gender": {
"prediction": "male",
"confidence": {
"female": "0.028",
"male": "0.972"
}
},
"emotion": {
"prediction": "happy",
"confidence": {
"angry": "0.0",
"fear": "0.0",
"happy": "0.999",
"neutral": "0.0",
"sad": "0.0",
"surprise": "0.0"
}
},
"pose": {
"yaw": "-4.6450",
"pitch": "-7.8978",
"roll": "-1.0229"
}
}
],
"numFacesFound": 1,
"executionMessage": "Faces found",
"message": "success"
}
The error message when the file size exceeded the allowed limit will be returned in the following format:
"The zoho.ai.detectFace integration task failed because the size of the supplied input file exceeded the allowed limit of 5MB."
The failure response when the image is not in the specified format(.png, .jpg,or .jpeg).
{ "error":{
"code": 400,
"message": "null file format is not allowed"
},
"status": "Failed"
}