How to detect people in a photo in Python
2 min readAug 30, 2019
Today we will be looking at using an API to automatically detect people in an image. If you would rather locate faces or facial features, note that we have a separate function that encompasses that.
First, pip install our Image Recognition Client:
pip install git+https://github.com/Cloudmersive/Cloudmersive.APIClient.Python.ImageRecognition.git
Then call recognize_detect_people:
from __future__ import print_function
import time
import cloudmersive_image_api_client
from cloudmersive_image_api_client.rest import ApiException
from pprint import pprint# Configure API key authorization: Apikey
configuration = cloudmersive_image_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API class
api_instance = cloudmersive_image_api_client.RecognizeApi(cloudmersive_image_api_client.ApiClient(configuration))
image_file = '/path/to/file' # file | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.try:
# Detect people, including locations, in an image
api_response = api_instance.recognize_detect_people(image_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling RecognizeApi->recognize_detect_people: %s\n" % e)
And you’re done! It’s really that easy. Here is an example image:
And the result:
{
"Successful": true,
"Objects": [
{
"ObjectClassName": "person",
"Height": 1364,
"Width": 1587,
"Score": 0.9817148447036743,
"X": 1105,
"Y": 493
},
{
"ObjectClassName": "person",
"Height": 1253,
"Width": 1159,
"Score": 0.9773844480514526,
"X": 245,
"Y": 472
}
],
"ObjectCount": 2
}
We are provided with the location of each person, their dimensions, and a confidence score.