facedetecthttps://api.pixlab.io/facedetect
Description
Output the rectangle coordinates for each detected human face in a given image or video frame. A plenty of useful endpoints can be mixed with facedetect such as crop for face extraction, drawrectangles for marking faces, mogrify for blurring and so forth.
If you need to extract the facial shapes besides the rectangle coordinates, refer to the facelandmarks endpoint. For further deep facial analysis including age, gender and emotion pattern extraction, facemotion do perform such task.
HTTP Methods
GET, POST
Request Parameters
Required
Fields | Type | Description |
---|---|---|
img | URL | Input media URL. If you want to upload your image directly from your app, then submit a multipart/form-data POST request. Only JPEG, PNG & BMP format are allowed. |
key | String | Your PixLab API Key. You can also embed your key in the WWW-Authenticate: HTTP header and omit this parameter if you want to. |
Optional
Fields | Type | Description |
---|---|---|
count | Boolean | Return the total number of detected faces instead of their rectangle coordinates. |
POST Request Body (If you plan to use POST instead of a simple GET request)
Allowed Content-Type:
multipart/form-data
application/json
Use multipart/form-data if you want to upload your media file directly (refer to the sample set for a working example). If you are using JSON, then the media file must be already uploaded somewhere. Call store if you want to upload an image for example before invoking this endpoint.
Response
application/json.
This command always return a JSON object after each call. The field of interest here is the faces array which hold the rectangle coordinates and the ID of each detected face. The following are the JSON fields returned in response body:
Fields | Type | Description |
---|---|---|
status | Integer | Status code 200 indicates success, any other code indicates failure. |
faces | Array | JSON array holding the rectangle coordinates of each detected face (see below for fields). If the optional count parameter is set, then this field is ignored. |
count | Integer | Total detected faces. This field is returned only if the optional count parameter is set in your request. Otherwise, it is always ignored. |
error | String | Error message if status != 200. |
The following are the object fields returned for each detected face in the faces array from the JSON response:
Fields | Type | Description |
---|---|---|
face_id | Integer | Detected face ID. |
left | Integer | Rectangle left coordinate: X. |
top | Integer | Rectangle top coordinate: Y. |
width | Integer | Rectangle width. |
height | Integer | Rectangle height. |
For additional processing, pass any field value you want to the next command such as crop for face extraction, drawrectangles for marking, mogrify for blurring, etc.
Python Example
import requests
import json
img = 'http://cf.broadsheet.ie/wp-content/uploads/2015/03/jeremy-clarkson_3090507b.jpg'
# Detect all human faces first and extract each one of them via crop later.
req = requests.get('https://api.pixlab.io/facedetect',params={
'img': img,
'key':'My_Pix_Key',
})
reply = req.json()
if reply['status'] != 200:
print (reply['error'])
exit();
total = len(reply['faces']) # Total detected faces
print(str(total)+" face(s) were detected")
# Extract each face via crop now
for face in reply['faces']:
req = requests.get('https://api.pixlab.io/crop',params={
'img':img,
'key':'My_Pix_Key',
'width': face['width'],
'height': face['height'],
'x': face['left'],
'y': face['top']
})
reply = req.json()
if reply['status'] != 200:
print (reply['error'])
else:
print ("Face #"+str(face['face_id'])+" location: "+ reply['link'])
See Also
header, nsfw, sfw, ocr, crop, drawrectangles, mogrify, meme, facelandmarks, facecompare, facelookup, docscan