SocialEase / app.py
ergosumdre's picture
Create app.py
0462bcc
raw
history blame
2.13 kB
import gradio as gr
import boto3
from botocore.exceptions import BotoCoreError, ClientError
from PIL import Image
import numpy as np
import io
aws_key_id = os.environ['aws_access_key_id']
aws_secret = os.environ['aws_secret_access_key']
# Initialize AWS Rekognition client
try:
client = boto3.client('rekognition',
region_name='us-east-1',
aws_access_key_id= aws_key_id,
aws_secret_access_key = aws_secret)
except (BotoCoreError, ClientError) as error:
print('Error: ', error)
def recognize_emotions(image):
"""
This function takes an image as input, and returns the emotion with the highest confidence level in the face using AWS Rekognition
"""
# Convert the NumPy array to PIL image
pil_image = Image.fromarray(np.uint8(image))
# Convert the PIL image to bytes
with io.BytesIO() as output:
pil_image.save(output, format="JPEG")
contents = output.getvalue()
# Perform detection on the image using AWS Rekognition
response = client.detect_faces(
Image={
'Bytes': contents
},
Attributes=['ALL']
)
# If no faces are detected, return None
if not response['FaceDetails']:
return None
# Extract the emotions detected in the face
emotions = response['FaceDetails'][0]['Emotions']
# Find the emotion with the highest confidence level
max_confidence = 0
max_emotion = ''
for emotion in emotions:
if emotion['Confidence'] > max_confidence:
max_confidence = emotion['Confidence']
max_emotion = emotion['Type']
# Return the emotion with the highest confidence level as a string
return str(max_emotion)
# Create Gradio interface
iface = gr.Interface(recognize_emotions,
inputs=gr.Image(source="webcam", streaming=True),
outputs="text",
title="How does this person feel?",
description="Helping you understand what others think")
# Launch the interface
iface.launch()