udaytag commited on
Commit
dbe049b
1 Parent(s): f304ac2

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +112 -0
  2. model.json +0 -0
  3. model_weights.h5 +3 -0
  4. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import streamlit as st
3
+ from streamlit_webrtc import VideoTransformerBase, webrtc_streamer
4
+ from tensorflow.keras.preprocessing.image import img_to_array
5
+ from tensorflow.keras.applications.efficientnet_v2 import preprocess_input
6
+ from PIL import Image
7
+ import numpy as np
8
+ import tensorflow as tf
9
+
10
+ # Load your emotion classification model
11
+ def load_model(model_path, weights_path):
12
+ with open(model_path, 'r') as json_file:
13
+ model_json = json_file.read()
14
+ loaded_model = tf.keras.models.model_from_json(model_json)
15
+ loaded_model.load_weights(weights_path)
16
+ return loaded_model
17
+
18
+ # Preprocessing function for the image
19
+ def preprocess_image(img, target_size=(224, 224)):
20
+ if img.mode != 'RGB':
21
+ img = img.convert('RGB')
22
+ img = img.resize(target_size)
23
+ img_array = img_to_array(img)
24
+ img_array = np.expand_dims(img_array, axis=0)
25
+ img_array = preprocess_input(img_array)
26
+ return img_array
27
+
28
+ # Load the model
29
+ model = load_model('model.json', 'model_weights.h5')
30
+
31
+ # Load the pre-trained face detection model
32
+ faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
33
+
34
+ class_names = ['Ahegao', 'Angry', 'Happy', 'Neutral', 'Sad', 'Surprise']
35
+
36
+ class VideoTransformer(VideoTransformerBase):
37
+ def __init__(self):
38
+ self.i = 0
39
+
40
+ def transform(self, frame):
41
+ img = frame.to_ndarray(format="bgr24")
42
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
43
+ faces = faceCascade.detectMultiScale(gray, 1.3, 5)
44
+ i = self.i + 1
45
+
46
+ for (x, y, w, h) in faces:
47
+ face_img = img[y:y + h, x:x + w]
48
+ face_img = cv2.cvtColor(face_img, cv2.COLOR_BGR2RGB)
49
+ pil_img = Image.fromarray(face_img)
50
+ preprocessed_img = preprocess_image(pil_img)
51
+ predictions = model.predict(preprocessed_img)
52
+ predicted_class = np.argmax(predictions, axis=1)[0]
53
+
54
+ cv2.rectangle(img, (x, y), (x + w, y + h), (95, 207, 30), 3)
55
+ cv2.rectangle(img, (x, y - 40), (x + w, y), (95, 207, 30), -1)
56
+ cv2.putText(img, class_names[predicted_class], (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 0), 2)
57
+
58
+ return img
59
+
60
+ def main():
61
+ st.title("Facial Emotion Detection")
62
+ st.sidebar.title("Dashboard")
63
+ st.sidebar.markdown(
64
+ """ Developed by Uday Singh
65
+ Model: EfficientnetV2
66
+ LinkedIn: https://www.linkedin.com/in/uday-singh-45563a209/""")
67
+ option = st.sidebar.selectbox("Choose an option",
68
+ ["Home", "Real-time Facial Emotion Analysis", "Emotion Analysis for Local Images",
69
+ "About"])
70
+ if option == "Home":
71
+ html_temp_home1 = """<div style="background-color:#6D7B8D;padding:10px">
72
+ <h4 style="color:white;text-align:center;">
73
+ Face Emotion detection application using OpenCV, EfficientNetV2 and Streamlit.</h4>
74
+ </div>
75
+ </br>"""
76
+ st.markdown(html_temp_home1, unsafe_allow_html=True)
77
+ st.write("""
78
+ The application has two functionalities.
79
+
80
+ 1. Real time face detection using web cam feed.
81
+
82
+ 2. Real time facial emotion recognition.
83
+
84
+ 3. Emotion Prediction for local Images
85
+
86
+ """)
87
+
88
+ elif option == "Real-time Facial Emotion Analysis":
89
+ webrtc_streamer(key="example", video_transformer_factory=VideoTransformer)
90
+
91
+ elif option == "Emotion Analysis for Local Images":
92
+ st.write("Emotion Analysis for a Single Image")
93
+ single_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
94
+ if single_image is not None:
95
+ img = Image.open(single_image)
96
+ preprocessed_img = preprocess_image(img)
97
+ predictions = model.predict(preprocessed_img)
98
+ predicted_class = np.argmax(predictions, axis=1)[0]
99
+ st.write("Predicted Emotion for Uploaded Image:", class_names[predicted_class])
100
+
101
+ elif option == "About":
102
+ st.subheader("About this app")
103
+ html_temp_about1 = """<div style="padding:10px"> Real time face emotion detection application using OpenCV,
104
+ Custom Trained CNN model and Streamlit. <br><br>
105
+ This Application is developed by Uday Singh using Streamlit Framework, Opencv, Tensorflow and Keras library for demonstration purpose.
106
+ If you're on LinkedIn and want to connect, just click on the link in sidebar and shoot me a request.<br><br>
107
+ Thanks for Visiting... </div> </br> """
108
+ st.markdown(html_temp_about1, unsafe_allow_html=True)
109
+
110
+
111
+ if __name__ == "__main__":
112
+ main()
model.json ADDED
The diff for this file is too large to render. See raw diff
 
model_weights.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:241fdd3caecf80777c170c7f3cdf51283eeef53e745e9384b609955fd9c532e1
3
+ size 214930856
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ numpy
3
+ tensorflow==2.13.0
4
+ keras
5
+ pillow
6
+ opencv-python-headless
7
+ streamlit-webrtc