File size: 2,611 Bytes
2b87fc4
dcdc943
 
a2167ac
20dfbab
 
2b87fc4
dcdc943
e8c712c
dcdc943
 
e8c712c
f2c7501
dcdc943
e8c712c
 
92c22ec
e8c712c
 
20dfbab
 
f2c7501
 
e8c712c
92c22ec
f2c7501
a2167ac
ac6d752
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
# from transformers import pipeline
from deepface import DeepFace
import numpy as np
# import custom helper functions
from backend import check_image_rotation

# pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")

st.title("Your Emotions? Or Nah?")
# st.title("Hot Dog? Or Not?")

file_name = st.file_uploader("Upload a photo of your face")
# file_name = st.file_uploader("Upload a hot dog candidate image")

if file_name is not None:
    # make two columns
    col1, col2 = st.columns(2)

    # capture image with intended rotation
    image = check_image_rotation(file_name)

    # display image in left column
    col1.image(image, use_column_width=True)

    # capture image data for face analysis
    image_data = np.array(image)
    # define a list of backends in case face cannot be detected 
    backends = ['opencv', 'mtcnn', 'retinaface', 'mediapipe', 'ssd']
    # attempt tracker
    attempt = 0

    # retry loop
    while True:
        try:
            # capture predictions from deepface emotion model
            predictions = DeepFace.analyze(image_data, actions=['emotion'], detector_backend=backends[attempt])  
            # ensure only the main prediction object is processed,
            if len(predictions) > 1:
                # when more than one face is detected by the backend,
                faces = [(face, face['region']['w'] * face['region']['h']) for face in predictions]
                # by using the predictions connected to the largest bounding box
                new_predictions = sorted(faces, key=lambda x: x[1], reverse=True)[0][0]
                emotion_dict = new_predictions['emotion']
            else:
                emotion_dict = predictions['emotion']
            # capture desired prediction data
            emotions = list(emotion_dict.keys())
            probabilities = list(emotion_dict.values())
            # display in the right column...
            col2.header("Emotion Probabilities")
            # ...each emotion category and its probability
            for i in range(len(emotions)):
                col2.subheader(f"{emotions[i]}: {probabilities[i]:.2f}%")
            break
        except Exception as e:
            # if the analysis fails to detect a face, try a different backend
            attempt += 1
            if attempt < len(backends):
                print(f"Retrying with backend `{backends[attempt]}` due to error: {str(e)}")
            else:
                print(f"Failed to analyze image after attempting all detector backends available. Please upload a new image.")