codevlogger2003 commited on
Commit
b3e744e
1 Parent(s): 1cbfb8f

Add necessary files

Browse files
Files changed (2) hide show
  1. app.py +93 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pandas as pd
3
+ from deepface import DeepFace
4
+ import streamlit as st
5
+ import cv2
6
+ import base64
7
+ 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')
30
+ original_image = initial_image
31
+ temp_path = None
32
+ if initial_image is not None:
33
+ bytes_data = initial_image.getvalue()
34
+ image = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
35
+
36
+ return image, original_image
37
+
38
+
39
+
40
+
41
+ def main(options):
42
+
43
+
44
+
45
+
46
+
47
+ if st.checkbox('Take a picture for prediction'):
48
+
49
+ image, original_image= upload()
50
+ if original_image is not None and original_image is not None and st.button('Prediction'): # Check if original_image is not None
51
+ st.warning('Wait for few seconds!!')
52
+ progress_bar = st.progress(0.0)
53
+ status_text = st.empty()
54
+
55
+ result = DeepFace.analyze(image,detector_backend=options,actions=['age','gender','emotion'])
56
+
57
+ for i in range(100):
58
+ progress_bar.progress((i + 1) / 100)
59
+ status_text.text(f"Processing {i+1}%")
60
+ time.sleep(0.01)
61
+
62
+ progress_bar.empty()
63
+ gray_frame = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
64
+ faces = cascade.detectMultiScale(gray_frame, 1.1, 3)
65
+
66
+
67
+ for x,y,w,h in faces:
68
+
69
+
70
+ cv2.rectangle(image, (x, y), (x+w, y+h), (4, 29, 255), 2, cv2.LINE_4)
71
+ user_selected_items = list(result[0].keys())
72
+ if 'age' in user_selected_items:
73
+ age_label='Age: '+str(result[0]['age'])
74
+ cv2.putText(image, age_label, (x ,y+h+30), cv2.FONT_ITALIC,1 ,(255,255,0), 2)
75
+ if 'dominant_gender' in user_selected_items:
76
+ gender_label='Gender: '+str(result[0]['dominant_gender'])
77
+ cv2.putText(image, gender_label, (x, y+h+70), cv2.FONT_ITALIC,1, (0,255,255), 2)
78
+
79
+ if 'dominant_emotion' in user_selected_items:
80
+ emotion_label='Emotion: '+str(result[0]['dominant_emotion']).title()
81
+ cv2.putText(image, emotion_label, (x, y+h+110), cv2.FONT_ITALIC,1 ,(255,0,255), 2)
82
+
83
+ st.image(image, channels='BGR')
84
+
85
+
86
+
87
+ if __name__ == '__main__':
88
+ def get_options():
89
+ actions = ['opencv','mtcnn','retinaface']
90
+ option2 = st.selectbox('Choose the following backend:', actions)
91
+ return option2
92
+
93
+ main(get_options())
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ opencv-contrib-python-headless
2
+ deepface
3
+ mtcnn