Created
May 18, 2020 20:42
-
-
Save Nevin243/91880e42ba989a9a8b52fbccb69a7b6c to your computer and use it in GitHub Desktop.
Open CV and rekognition demo for emotions on face capture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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