Samantha Hipple commited on
Commit
eb7cd31
1 Parent(s): 91c8e31
Files changed (2) hide show
  1. app.py +21 -3
  2. emodeepface.py +19 -20
app.py CHANGED
@@ -3,10 +3,13 @@ import streamlit as st
3
  from beluga import load_model, process_emotions, generate_prompt
4
  from emodeepface import check_image_rotation, process_photo
5
 
6
- # Title webpage
 
 
 
7
  st.title("Affective Journaling Assistant")
8
 
9
- # User instructions
10
  st.write("""
11
  Welcome to the Affective Journaling Assistant!
12
 
@@ -20,5 +23,20 @@ To proceed:
20
  Let's get started!
21
  """)
22
 
23
- # User image upload
24
  file_name = st.file_uploader("Please upload your photo.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  from beluga import load_model, process_emotions, generate_prompt
4
  from emodeepface import check_image_rotation, process_photo
5
 
6
+ # begin loading beluga model and tokenizer
7
+ model, tokenizer = load_model()
8
+
9
+ # title webpage
10
  st.title("Affective Journaling Assistant")
11
 
12
+ # provide user instructions
13
  st.write("""
14
  Welcome to the Affective Journaling Assistant!
15
 
 
23
  Let's get started!
24
  """)
25
 
26
+ # request user image upload
27
  file_name = st.file_uploader("Please upload your photo.")
28
+
29
+ # once an image has been uploaded
30
+ if file_name is not None:
31
+ # capture image with intended rotation
32
+ image = check_image_rotation(file_name)
33
+ # display the image directly
34
+ st.image(image, use_column_width=True)
35
+ # process uploaded image
36
+ emotion_predictions = process_photo(file_name)
37
+ # process emotion predictions
38
+ result = process_emotions(model, tokenizer, emotion_predictions)
39
+ # generate affective journaling prompt based on emotion predictions
40
+ prompt = generate_prompt(result)
41
+ # display journal prompt
42
+ st.write(prompt)
emodeepface.py CHANGED
@@ -32,24 +32,23 @@ def process_photo(file_name):
32
  backends = ['opencv', 'mtcnn', 'retinaface', 'mediapipe', 'ssd']
33
  attempt = 0
34
 
35
- if file_name is not None:
36
- image = check_image_rotation(file_name)
37
- image_data = np.array(image)
38
 
39
- while attempt < len(backends):
40
- try:
41
- predictions = DeepFace.analyze(image_data, actions=['emotion'], detector_backend=backends[attempt])
42
- if len(predictions) > 1:
43
- faces = [(face, face['region']['w'] * face['region']['h']) for face in predictions]
44
- new_predictions = sorted(faces, key=lambda x: x[1], reverse=True)[0][0]
45
- emotion_dict = new_predictions['emotion']
46
- return emotion_dict
47
- return predictions['emotion']
48
- except Exception as e:
49
- if attempt == len(backends) - 1:
50
- error_message = f"Failed to analyze image after attempting all detector backends available. Please upload a new image."
51
- raise ImageProcessingError(error_message)
52
- else:
53
- # log the error message for each failed backend here:
54
- print(f"Retrying with backend `{backends[attempt+1]}` due to error: {str(e)}")
55
- attempt += 1
 
32
  backends = ['opencv', 'mtcnn', 'retinaface', 'mediapipe', 'ssd']
33
  attempt = 0
34
 
35
+ image = check_image_rotation(file_name)
36
+ image_data = np.array(image)
 
37
 
38
+ while attempt < len(backends):
39
+ try:
40
+ predictions = DeepFace.analyze(image_data, actions=['emotion'], detector_backend=backends[attempt])
41
+ if len(predictions) > 1:
42
+ faces = [(face, face['region']['w'] * face['region']['h']) for face in predictions]
43
+ new_predictions = sorted(faces, key=lambda x: x[1], reverse=True)[0][0]
44
+ emotion_dict = new_predictions['emotion']
45
+ return emotion_dict
46
+ return predictions['emotion']
47
+ except Exception as e:
48
+ if attempt == len(backends) - 1:
49
+ error_message = f"Failed to analyze image after attempting all detector backends available. Please upload a new image."
50
+ raise ImageProcessingError(error_message)
51
+ else:
52
+ # log the error message for each failed backend here:
53
+ print(f"Retrying with backend `{backends[attempt+1]}` due to error: {str(e)}")
54
+ attempt += 1