File size: 2,533 Bytes
0462bcc
 
 
 
 
 
6abc907
 
4ae6f5b
6e1a3ad
6abc907
0462bcc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6abc907
49a3d88
 
 
 
 
 
 
 
 
 
 
6abc907
6e1a3ad
 
0462bcc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6abc907
 
285b8ac
6456e27
0462bcc
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import gradio as gr
import boto3
from botocore.exceptions import BotoCoreError, ClientError
from PIL import Image
import numpy as np
import io
import pyttsx3
import time
import os
import subprocess


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 speak_text(text):
    engine = pyttsx3.init()
    try:
        engine.endLoop()
        del engine
        engine = pyttsx3.init()
    except:
        pass
        engine.startLoop()
        engine.setProperty('rate', 185)
        engine.say(text)




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']


    speak_text(f'This person is {max_emotion}')
    #espeak(f'This person is {max_emotion}')
    
    # 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()