Skip to content

Instantly share code, notes, and snippets.

@Nevin243
Created May 18, 2020 20:42
Show Gist options
  • Save Nevin243/91880e42ba989a9a8b52fbccb69a7b6c to your computer and use it in GitHub Desktop.
Save Nevin243/91880e42ba989a9a8b52fbccb69a7b6c to your computer and use it in GitHub Desktop.
Open CV and rekognition demo for emotions on face capture
import numpy as np
import cv2
import boto3
import json
# Rekognition Detect faces
def detect_faces(photo):
client=boto3.client('rekognition')
response = client.detect_faces(
Image={
'Bytes': photo
},
Attributes=[
'ALL'
]
)
return response
cam = cv2.VideoCapture(1)
cv2.namedWindow("test")
while True:
ret, frame = cam.read()
cv2.imshow("test", frame)
if not ret:
break
k = cv2.waitKey(1)
if k%256 == 27:
# ESC pressed
print("Escape hit, closing...")
break
elif k%256 == 32:
# SPACE pressed
response = detect_faces(cv2.imencode('.jpg', frame)[1].tostring())
for faceDetail in response['FaceDetails']:
print('Emotions: \t Confidence')
for emotion in faceDetail['Emotions']:
print(str(emotion['Type']) + '\t\t' + str(emotion['Confidence']))
# Release control of the webcam and close window
cam.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment