hipples commited on
Commit
f2c7501
·
1 Parent(s): a2167ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -11
app.py CHANGED
@@ -9,7 +9,7 @@ from PIL import Image
9
  st.title("Your Emotions? Or Nah?")
10
  # st.title("Hot Dog? Or Not?")
11
 
12
- file_name = st.file_uploader("Upload a photo of your face.")
13
  # file_name = st.file_uploader("Upload a hot dog candidate image")
14
 
15
  if file_name is not None:
@@ -18,18 +18,29 @@ if file_name is not None:
18
 
19
  # capture image
20
  image = Image.open(file_name)
21
- # to display in in column 1
 
22
  col1.image(image, use_column_width=True)
23
 
24
- # capture image data for deepface
25
  image_data = np.array(image)
26
- # capture predictions from deepface
27
- predictions = DeepFace.analyze(image_data, actions=['emotion'])['emotion']
28
- # predictions = pipeline(image)
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- # to display in column 2
31
  col2.header("Emotion Probabilities")
32
- # for p in predictions:
33
- for emotion in predictions:
34
- # col2.subheader(f"{ p['label'] }: { round(p['score'] * 100, 1)}%")
35
- col2.subheader(f"{emotion}")
 
9
  st.title("Your Emotions? Or Nah?")
10
  # st.title("Hot Dog? Or Not?")
11
 
12
+ file_name = st.file_uploader("Upload a photo of your face")
13
  # file_name = st.file_uploader("Upload a hot dog candidate image")
14
 
15
  if file_name is not None:
 
18
 
19
  # capture image
20
  image = Image.open(file_name)
21
+
22
+ # display image in left column
23
  col1.image(image, use_column_width=True)
24
 
25
+ # capture image data for face analysis
26
  image_data = np.array(image)
27
+ # capture predictions from deepface emotion model
28
+ predictions = DeepFace.analyze(image_data, actions=['emotion'])
29
+ # ensure only the main prediction object is processed,
30
+ if len(predictions) > 1:
31
+ # when more than one face is detected by the backend,
32
+ faces = [(face, face['region']['w'] * face['region']['h']) for face in predictions]
33
+ # by using the predictions connected to the largest bounding box
34
+ new_predictions = sorted(faces, key=lambda x: x[1], reverse=True)[0][0]
35
+ emotion_dict = new_predictions['emotion']
36
+ else:
37
+ emotion_dict = predictions['emotion']
38
+ # capture desired prediction data
39
+ emotions = list(emotion_dict.keys())
40
+ probabilities = list(emotion_dict.values())
41
 
42
+ # display in the right column...
43
  col2.header("Emotion Probabilities")
44
+ # ...each emotion category and its probability
45
+ for i in range(len(emotions)):
46
+ col2.subheader(f"{emotions[i]}: {probabilities[i]:.2f}%")