[PYTHON] Same person detection by Amazon Rekognition

Continuing from Last time, I tried to detect the same person using Amazon Rekogniton.


The same person is detected using the API called compare_faces of Amazon Rekognition. I input two images in compare_faces, the first is the image of the person I want to detect, and the second is the image to be detected.

Execution environment

OS:Windows10 Language: Python 3.7

Advance preparation

Set the following authentication information in AWS CLI (aws configure).

AWS Access Key ID AWS Secret Access Key Default region name Default output format

Source code (face_compare.py)


import boto3
import sys
from PIL import Image,ImageDraw

#Argument check
if len(sys.argv) != 3:
	print('Specify two image files as arguments.')

#Create a Rekognition client
client = boto3.client('rekognition')

#Compare with two image files as arguments_Run faces
with open(sys.argv[1],'rb') as source:
	with open(sys.argv[2],'rb') as target:
		response = client.compare_faces(SourceImage={'Bytes':source.read()},TargetImage={'Bytes':target.read()})

#If the same person is not detected, the process ends
if len(response['FaceMatches'])==0:
	print('The same person was not detected.')
	#Create an image file for the rectangle set based on the second image file
	img = Image.open(sys.argv[2])
	imgWidth,imgHeight = img.size
	draw = ImageDraw.Draw(img)

	#Perform rectangle set processing for the number of detected faces
	for faceMatch in response['FaceMatches']:

		#Get face position / size information from BoundingBox
		box = faceMatch['Face']['BoundingBox']
		left = imgWidth * box['Left']
		top = imgHeight * box['Top']
		width = imgWidth * box['Width']
		height = imgHeight * box['Height']		

		#Set the position and size information of the rectangle
		points = (
			(left + width,top + height)

		#Enclose the face in a rectangle

		#Save image file
		img.save('detected_' + sys.argv[2]) 	

		#View image file

Brief commentary

The outline is as follows.

(1) Get the two image files to be input to Rekognition from the arguments at the time of program execution. (2) Execute Rekognition's compare_faces with the image file in (1) above as an argument. (3) Obtain the recognized face position / size information from Json's Face Matches / Bounding Box returned from Rekognition. ④ Create an image file with a rectangle from ③ above and display it.

Execution result


python face_compare.py ichiro1.jpg ichiro2.jpg

Input image 1 (ichiro1.jpg)


Input image 2 (ichiro2.jpg)


Output image (detected_ichiro2.jpg)


He detected Ichiro.


Similar to detect_faces in Last time, you can easily perform image recognition using API. By using compare_faces, you can easily extract the person you want to find from a large number of images. According to the AWS site, it is used by newspaper companies and photo service companies in Japan.

Recommended Posts

Same person detection by Amazon Rekognition
Face recognition with Amazon Rekognition
Machine translation by Amazon Translate