princeml commited on
Commit
a6f3f2a
1 Parent(s): 6db0567

Upload 5 files

Browse files
Files changed (5) hide show
  1. Procfile +1 -0
  2. app.py +108 -0
  3. haarcascade_frontalface_default.xml +0 -0
  4. runtime.txt +1 -0
  5. setup.sh +9 -0
Procfile ADDED
@@ -0,0 +1 @@
 
 
1
+ web: sh setup.sh && streamlit run app.py
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import cv2
3
+ import streamlit as st
4
+ from tensorflow import keras
5
+ from keras.models import model_from_json
6
+ from keras.preprocessing.image import img_to_array
7
+ from streamlit_webrtc import webrtc_streamer, VideoTransformerBase, RTCConfiguration, VideoProcessorBase, WebRtcMode
8
+
9
+ # load model
10
+ emotion_dict = {0:'angry', 1 :'happy', 2: 'neutral', 3:'sad', 4: 'surprise'}
11
+ # load json and create model
12
+ json_file = open('emotion_model1.json', 'r')
13
+ loaded_model_json = json_file.read()
14
+ json_file.close()
15
+ classifier = model_from_json(loaded_model_json)
16
+
17
+ # load weights into new model
18
+ classifier.load_weights("emotion_model1.h5")
19
+
20
+ #load face
21
+ try:
22
+ face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
23
+ except Exception:
24
+ st.write("Error loading cascade classifiers")
25
+
26
+ RTC_CONFIGURATION = RTCConfiguration({"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]})
27
+
28
+ class Faceemotion(VideoTransformerBase):
29
+ def transform(self, frame):
30
+ img = frame.to_ndarray(format="bgr24")
31
+
32
+ #image gray
33
+ img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
34
+ faces = face_cascade.detectMultiScale(
35
+ image=img_gray, scaleFactor=1.3, minNeighbors=5)
36
+ for (x, y, w, h) in faces:
37
+ cv2.rectangle(img=img, pt1=(x, y), pt2=(
38
+ x + w, y + h), color=(255, 0, 0), thickness=2)
39
+ roi_gray = img_gray[y:y + h, x:x + w]
40
+ roi_gray = cv2.resize(roi_gray, (48, 48), interpolation=cv2.INTER_AREA)
41
+ if np.sum([roi_gray]) != 0:
42
+ roi = roi_gray.astype('float') / 255.0
43
+ roi = img_to_array(roi)
44
+ roi = np.expand_dims(roi, axis=0)
45
+ prediction = classifier.predict(roi)[0]
46
+ maxindex = int(np.argmax(prediction))
47
+ finalout = emotion_dict[maxindex]
48
+ output = str(finalout)
49
+ label_position = (x, y)
50
+ cv2.putText(img, output, label_position, cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
51
+
52
+ return img
53
+
54
+ def main():
55
+ # Face Analysis Application #
56
+ st.title("Real Time Face Emotion Detection Application")
57
+ activiteis = ["Home", "Webcam Face Detection", "About"]
58
+ choice = st.sidebar.selectbox("Select Activity", activiteis)
59
+ st.sidebar.markdown(
60
+ """ Developed by Mohammad Juned Khan
61
+ Email : Mohammad.juned.z.khan@gmail.com
62
+ [LinkedIn] (https://www.linkedin.com/in/md-juned-khan)""")
63
+ if choice == "Home":
64
+ html_temp_home1 = """<div style="background-color:#6D7B8D;padding:10px">
65
+ <h4 style="color:white;text-align:center;">
66
+ Face Emotion detection application using OpenCV, Custom CNN model and Streamlit.</h4>
67
+ </div>
68
+ </br>"""
69
+ st.markdown(html_temp_home1, unsafe_allow_html=True)
70
+ st.write("""
71
+ The application has two functionalities.
72
+
73
+ 1. Real time face detection using web cam feed.
74
+
75
+ 2. Real time face emotion recognization.
76
+
77
+ """)
78
+ elif choice == "Webcam Face Detection":
79
+ st.header("Webcam Live Feed")
80
+ st.write("Click on start to use webcam and detect your face emotion")
81
+ webrtc_streamer(key="example", mode=WebRtcMode.SENDRECV, rtc_configuration=RTC_CONFIGURATION,
82
+ video_processor_factory=Faceemotion)
83
+
84
+ elif choice == "About":
85
+ st.subheader("About this app")
86
+ html_temp_about1= """<div style="background-color:#6D7B8D;padding:10px">
87
+ <h4 style="color:white;text-align:center;">
88
+ Real time face emotion detection application using OpenCV, Custom Trained CNN model and Streamlit.</h4>
89
+ </div>
90
+ </br>"""
91
+ st.markdown(html_temp_about1, unsafe_allow_html=True)
92
+
93
+ html_temp4 = """
94
+ <div style="background-color:#98AFC7;padding:10px">
95
+ <h4 style="color:white;text-align:center;">This Application is developed by Mohammad Juned Khan using Streamlit Framework, Opencv, Tensorflow and Keras library for demonstration purpose. If you're on LinkedIn and want to connect, just click on the link in sidebar and shoot me a request. If you have any suggestion or wnat to comment just write a mail at Mohammad.juned.z.khan@gmail.com. </h4>
96
+ <h4 style="color:white;text-align:center;">Thanks for Visiting</h4>
97
+ </div>
98
+ <br></br>
99
+ <br></br>"""
100
+
101
+ st.markdown(html_temp4, unsafe_allow_html=True)
102
+
103
+ else:
104
+ pass
105
+
106
+
107
+ if __name__ == "__main__":
108
+ main()
haarcascade_frontalface_default.xml ADDED
The diff for this file is too large to render. See raw diff
 
runtime.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ python-3.7.11
setup.sh ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ mkdir -p ~/.streamlit/
2
+
3
+ echo "\
4
+ [server]\n\
5
+ headless = true\n\
6
+ port = $PORT\n\
7
+ enableCORS = false\n\
8
+ \n\
9
+ " > ~/.streamlit/config.toml