Spaces:
Runtime error
Runtime error
Commit
•
73db645
1
Parent(s):
4678ace
Updated app.py
Browse files
app.py
CHANGED
@@ -8,22 +8,8 @@ import time
|
|
8 |
|
9 |
st.set_page_config(layout="wide")
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
16 |
|
17 |
-
import tempfile
|
18 |
-
import os
|
19 |
-
|
20 |
-
weights_paths = {
|
21 |
-
'age': '/home/appuser/.deepface/weights/age_model_weights.h5',
|
22 |
-
'gender': '/home/appuser/.deepface/weights/gender_model_weights.h5',
|
23 |
-
'race': '/home/appuser/.deepface/weights/race_model_single_batch.h5',
|
24 |
-
'emotion': '/home/appuser/.deepface/weights/facial_expression_model_weights.h5'
|
25 |
-
}
|
26 |
-
|
27 |
def upload():
|
28 |
image=None
|
29 |
initial_image = st.camera_input('Take a picture')
|
@@ -39,46 +25,34 @@ def upload():
|
|
39 |
|
40 |
|
41 |
def main(options):
|
42 |
-
col1,col2=st.columns(2)
|
43 |
-
image=None
|
44 |
-
original_image=None
|
45 |
-
|
46 |
|
|
|
47 |
|
48 |
|
49 |
-
|
50 |
-
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
image, original_image= upload()
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
with col2:
|
60 |
-
if original_image is not None and original_image is not None and st.button('Detect'):
|
61 |
-
|
62 |
-
|
63 |
st.warning('Wait for few seconds!!')
|
64 |
progress_bar = st.progress(0.0)
|
65 |
status_text = st.empty()
|
66 |
-
|
67 |
result = DeepFace.analyze(image,detector_backend=options,actions=['age','gender','emotion','race'])
|
68 |
-
|
69 |
for i in range(100):
|
70 |
progress_bar.progress((i + 1) / 100)
|
71 |
status_text.text(f"Processing {i+1}%")
|
72 |
time.sleep(0.01)
|
73 |
-
|
74 |
progress_bar.empty()
|
75 |
gray_frame = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
76 |
faces = cascade.detectMultiScale(gray_frame, 1.1, 3)
|
77 |
-
|
78 |
|
79 |
for x,y,w,h in faces:
|
80 |
-
|
81 |
-
|
82 |
cv2.rectangle(image, (x, y), (x+w, y+h), (4, 29, 255), 2, cv2.LINE_4)
|
83 |
user_selected_items = list(result[0].keys())
|
84 |
if 'age' in user_selected_items:
|
@@ -87,15 +61,15 @@ def main(options):
|
|
87 |
if 'dominant_gender' in user_selected_items:
|
88 |
gender_label='Gender: '+str(result[0]['dominant_gender'])
|
89 |
cv2.putText(image, gender_label, (x, y+h+70), cv2.FONT_ITALIC,1, (0,255,255), 2)
|
90 |
-
|
91 |
if 'dominant_emotion' in user_selected_items:
|
92 |
emotion_label='Emotion: '+str(result[0]['dominant_emotion']).title()
|
93 |
cv2.putText(image, emotion_label, (x, y+h+110), cv2.FONT_ITALIC,1 ,(255,0,255), 2)
|
94 |
|
95 |
if 'dominant_race' in user_selected_items:
|
96 |
emotion_label='Race: '+str(result[0]['dominant_race']).title()
|
97 |
-
cv2.putText(image, emotion_label, (x, y+h+150), cv2.FONT_ITALIC,1 ,(
|
98 |
-
|
99 |
st.image(image, channels='BGR')
|
100 |
|
101 |
|
|
|
8 |
|
9 |
st.set_page_config(layout="wide")
|
10 |
|
|
|
|
|
|
|
|
|
11 |
cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def upload():
|
14 |
image=None
|
15 |
initial_image = st.camera_input('Take a picture')
|
|
|
25 |
|
26 |
|
27 |
def main(options):
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
|
30 |
|
31 |
|
32 |
+
|
33 |
+
if st.checkbox('Take a picture for prediction'):
|
34 |
|
35 |
+
image, original_image= upload()
|
36 |
+
if original_image is not None and original_image is not None and st.button('Prediction'): # Check if original_image is not None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
st.warning('Wait for few seconds!!')
|
38 |
progress_bar = st.progress(0.0)
|
39 |
status_text = st.empty()
|
40 |
+
|
41 |
result = DeepFace.analyze(image,detector_backend=options,actions=['age','gender','emotion','race'])
|
42 |
+
|
43 |
for i in range(100):
|
44 |
progress_bar.progress((i + 1) / 100)
|
45 |
status_text.text(f"Processing {i+1}%")
|
46 |
time.sleep(0.01)
|
47 |
+
|
48 |
progress_bar.empty()
|
49 |
gray_frame = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
50 |
faces = cascade.detectMultiScale(gray_frame, 1.1, 3)
|
51 |
+
|
52 |
|
53 |
for x,y,w,h in faces:
|
54 |
+
|
55 |
+
|
56 |
cv2.rectangle(image, (x, y), (x+w, y+h), (4, 29, 255), 2, cv2.LINE_4)
|
57 |
user_selected_items = list(result[0].keys())
|
58 |
if 'age' in user_selected_items:
|
|
|
61 |
if 'dominant_gender' in user_selected_items:
|
62 |
gender_label='Gender: '+str(result[0]['dominant_gender'])
|
63 |
cv2.putText(image, gender_label, (x, y+h+70), cv2.FONT_ITALIC,1, (0,255,255), 2)
|
64 |
+
|
65 |
if 'dominant_emotion' in user_selected_items:
|
66 |
emotion_label='Emotion: '+str(result[0]['dominant_emotion']).title()
|
67 |
cv2.putText(image, emotion_label, (x, y+h+110), cv2.FONT_ITALIC,1 ,(255,0,255), 2)
|
68 |
|
69 |
if 'dominant_race' in user_selected_items:
|
70 |
emotion_label='Race: '+str(result[0]['dominant_race']).title()
|
71 |
+
cv2.putText(image, emotion_label, (x, y+h+150), cv2.FONT_ITALIC,1 ,(0,0,102), 2)
|
72 |
+
|
73 |
st.image(image, channels='BGR')
|
74 |
|
75 |
|