Face recognition with Amazon Rekognition

Using Amazon Rekognition, I tried the basic process of enclosing the face in the input image with a rectangle.

What is Amazon Rekognition?

Amazon Rekognition is one of AWS's AI services that supports image recognition. In addition, AWS's AI service covers various areas such as image recognition and natural language processing, so machine learning can be incorporated into applications without deep machine learning skills, and machine learning can be used from API just by preparing data. There are features such as.

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_detect.py)

import boto3
import sys
from PIL import Image,ImageDraw

Check arguments

if len(sys.argv) != 2:

print ('Please specify the image file as an argument.') exit()

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

Execute detect_faces with image file as an argument

with open(sys.argv[1],'rb') as image:
	response = client.detect_faces(Image={'Bytes':image.read()},Attributes=['ALL'])

If the face is not recognized, the process ends

if len(response['FaceDetails'])==0:

print ('The face was not recognized.') else: #Create an image file for the rectangle set based on the input image file img = Image.open(sys.argv[1]) imgWidth,imgHeight = img.size draw = ImageDraw.Draw(img)

Perform rectangle set processing for the number of recognized faces

	for faceDetail in response['FaceDetails']:

Get face position / size information from #BoundingBox box = faceDetail['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,top),
			(left + width,top + height)
		)

Enclose the face in a rectangle

		draw.rectangle(points,outline='lime')

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

#Display image file img.show()

Brief commentary

The outline is as follows.

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

Execution result

command

python face_detect.py ichiro1.jpg

Input image (ichiro1.jpg)

ichiro1.jpg

Output image (detected_ichiro1.jpg)

detected_ichiro1.jpg

Not only Ichiro, but also the people in the spectators are aware of it.

Summary

Not limited to Rekognition, AWS AI service is a convenient service that allows you to easily use machine learning from the API. Also, this time I tried only rectangles, but since there are various Jsons returned from Rekognition such as gender and age, I think that I can try various other things.

Recommended Posts

Face recognition with Amazon Rekognition
Face recognition with Edison
Build serverless facial recognition with Terraform (Amazon Rekognition)
Face recognition with Python's OpenCV
Face recognition / cutting with OpenCV
Try face recognition with Python
Face detection with Lambda (Python) + Rekognition
Try face recognition with Generated Photos
Try face recognition with python + OpenCV
First Anime Face Recognition with Chainer
Face recognition with camera with opencv3 + python2.7
I tried face recognition with OpenCV
Face recognition of anime characters with Keras
[python, openCV] base64 Face recognition with images
Serverless face recognition API made with Python
[Python3] [Ubuntu16] [Docker] Try face recognition with OpenFace
Image recognition with keras
Now, let's try face recognition with Chainer (prediction phase)
Easy face recognition try with Jetson Nano and webcam
Now, let's try face recognition with Chainer (learning phase)
Real-time face recognition with video acquired by getUserMedia [HTML5, openCV]
I tried face recognition using Face ++
Face detection with Python + dlib
Face detection with Python + OpenCV
Object co-localization for face recognition
Face detection with Haar Cascades
Achieve "Bals" with Amazon Echo
Image recognition with Keras + OpenCV
Anime face detection with OpenCV
Replace your face with Twitter icon with openCV face recognition and do ZOOM
Number recognition in images with Python
Prepare pipenv environment with amazon Linux 2
Same person detection by Amazon Rekognition
Face recognition using principal component analysis
[OpenCV] Personal identification with face photo
python x tensoflow x image face recognition
I tried face detection with MTCNN
Cut out face with Python + OpenCV
Let's recognize emotions with Azure Face
Automatically send emails with Amazon SES
Object recognition with openCV by traincascade
Subtitle data created with Amazon Transcribe