Skip to content

Instantly share code, notes, and snippets.

@jakechen
Created September 20, 2017 15:38
Show Gist options
  • Save jakechen/614200257a954f0e5bb23723b9e8ad8c to your computer and use it in GitHub Desktop.
Save jakechen/614200257a954f0e5bb23723b9e8ad8c to your computer and use it in GitHub Desktop.
Using opencv to parse frames for Amazon Rekognition to analyze. This example uses Rekognition's celebrity recognition feature as an example.
# With help from https://aws.amazon.com/blogs/ai/build-your-own-face-recognition-service-using-amazon-rekognition/
frame_skip = 100 # analyze every 100 frames to cut down on Rekognition API calls
import boto3
import cv2
from PIL import Image
import io
rekog = boto3.client('rekognition')
vidcap = cv2.VideoCapture('./video_clip.mp4') # Load clip from storage. Can modify this to input from camera.
cur_frame = 0
success = True
while success:
success, frame = vidcap.read() # get next frame from video
if cur_frame % frame_skip == 0: # only analyze every n frames
print('frame: {}'.format(cur_frame))
pil_img = Image.fromarray(frame) # convert opencv frame (with type()==numpy) into PIL Image
stream = io.BytesIO()
pil_img.save(stream, format='JPEG') # convert PIL Image to Bytes
bin_img = stream.getvalue()
response = rekog.recognize_celebrities(Image={'Bytes': bin_img}) # call Rekognition
if response['CelebrityFaces']: # print celebrity name if a celebrity is detected
for face in response['CelebrityFaces']:
print('Celebrity is {} with confidence of {}'.format(face['Name'], face['MatchConfidence']))
cur_frame += 1
@amittarkar
Copy link

kudos for the awesome code!! but how to redisplay the image after recognition with all the celebrity info.

@jakechen
Copy link
Author

kudos for the awesome code!! but how to redisplay the image after recognition with all the celebrity info.

Not sure I 100% understand your question, but if you're talking about plotting the bounding boxes on top of the faces, then:

  1. Grab the bounding box info from the API response. If you check out boto3's rekognition API for recognize_celebrities(), this would be `face['Face']['BoundingBox'] if using my variable above.
  2. Plot the bounding box on top of the image using PIL by using the rectangle method in PIL's ImageDraw class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment