Member-only story

Mood Detection Using AWS Rekognition

Vinayak Pandey
Apr 18, 2021

Given code will capture your picture unsing webcam and then use AWS Rekognition to detect your mood. Based on that, it will play a song on Youtube.

Pre-Requsite: Need to create an S3 bucket and IAM credentials with appropriate permissions should be configured.

Code: Save this code as script.py

import webbrowser
import cv2
import boto3
def main():
capture = capture_write()
play_music()
def capture_write(filename="image.jpeg", port=0, ramp_frames=30, x=1280, y=720):
camera = cv2.VideoCapture(port)
# Set Resolution
camera.set(3, x)
camera.set(4, y)
# Adjust camera lighting
for i in range(ramp_frames):
temp = camera.read()
retval, im = camera.read()
cv2.imwrite(filename,im)
del(camera)
return True
def play_music(filename="image.jpeg",bucket="vinytest1989"):
s3 = boto3.resource('s3')
s3.Bucket(bucket)
s3.Bucket(bucket).upload_file(filename ,"image.jpeg")
rek = boto3.client('rekognition')
response_face = rek.detect_faces(Image={'S3Object': {'Bucket': bucket,'Name': filename,}},Attributes = ['ALL'])
if response_face['FaceDetails'][0]['Smile']['Value'] == False:
webbrowser.open("https://www.youtube.com/watch?v=7TgSCEQYUpI")
else:
webbrowser.open("https://www.youtube.com/watch?v=llWum9z6nto")
if __name__ == '__main__':
main()

Change bucket name in the code and execute python3 script.py

--

--

Vinayak Pandey
Vinayak Pandey

Written by Vinayak Pandey

Experienced Cloud Engineer with a knack of automation. Linkedin profile: https://www.linkedin.com/in/vinayakpandeyit/

No responses yet