Using Amazon Rekognition, I tried the basic process of enclosing the face in the input image with a rectangle.
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.
OS:Windows10 Language: Python 3.7
Set the following authentication information in AWS CLI (aws configure).
AWS Access Key ID AWS Secret Access Key Default region name Default output format
import boto3
import sys
from PIL import Image,ImageDraw
if len(sys.argv) != 2:
print ('Please specify the image file as an argument.') exit()
Create a client for #Rekognition client = boto3.client('rekognition')
with open(sys.argv[1],'rb') as image:
response = client.detect_faces(Image={'Bytes':image.read()},Attributes=['ALL'])
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)
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']
points = (
(left,top),
(left + width,top + height)
)
draw.rectangle(points,outline='lime')
#Save image file img.save('detected_' + sys.argv[1])
#Display image file img.show()
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.
python face_detect.py ichiro1.jpg
Not only Ichiro, but also the people in the spectators are aware of it.
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