Quantcast
Channel: My Handbook
Viewing all articles
Browse latest Browse all 76

AWS Rekognition + OpenCV

$
0
0

Here is how you can use OpenCV with AWS rekognition

  1. Enable your configured aws account on rekognition and S3
  2. Create a rekognition collection this will be the collection of faces the camera stream is matched against > #aws rekognition create-collection –collection-id “faces” –region us-east-1
  3. List collections to make sure that the collection was created #aws rekognition list-collections –region us-east-1
  4. Create an S3 bucket and upload the photo with the face you’d like your camera to rekognize
  5. Index the faces in your S3 Bucket
    aws rekognition index-faces \
    –image ‘{“S3Object”:{“Bucket”:”BucketName“,”Name”:”FileName.png“}}’ \
    –collection-id “faces” \
    –region us-east-1
  6. The following python code should work
import boto3
import cv2
from PIL import Image
import io
frame_skip = 12 # analyze every 100 frames to cut down on Rekognition API calls
threshold=80
vidcap = cv2.VideoCapture(0)
cur_frame = 0
success = True
BUCKET = “BUCKETNAME” ### This is the bucket where the face your trying to match lives
COLLECTION = “COLLECTIONNAME” #### This is the collection of faces that you have already taught the ai to store
def search_faces_by_image(bin_img, key, collection_id, threshold=80, region=”us-east-1″):
rekognition = boto3.client(“rekognition”, region)
try:
response = rekognition.search_faces_by_image(
Image={‘Bytes’: bin_img},
CollectionId=collection_id,
FaceMatchThreshold=threshold,
)
return response[‘FaceMatches’]
except:
print(“no faces found”)
return
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()
face = search_faces_by_image(bin_img, KEY, COLLECTION)
print(face)
cur_frame += 1
Advertisements

Viewing all articles
Browse latest Browse all 76

Trending Articles