Dhrumit1314 commited on
Commit
7967a8c
1 Parent(s): 517aea1

Upload 6 files

Browse files
age_model_3epochs.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:62190e322612ef0631205e2d1b9a97ef0f328ee807385cddca465b3d680cd07d
3
+ size 274399416
app.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Sun Mar 24 02:15:10 2024
4
+
5
+ @author: Dhrumit Patel
6
+ """
7
+
8
+ import streamlit as st
9
+ from keras.models import load_model
10
+ from keras_preprocessing.image import img_to_array
11
+ import cv2
12
+ import numpy as np
13
+ import datetime
14
+
15
+ # Load models
16
+ face_classifier = cv2.CascadeClassifier('pretrained_haarcascade_classifier/haarcascade_frontalface_default.xml')
17
+ emotion_model = load_model('models/emotion_detection_model_50epochs.h5')
18
+ age_model = load_model('models/age_model_3epochs.h5')
19
+ gender_model = load_model('models/gender_model_3epochs.h5')
20
+
21
+ class_labels = ['Angry', 'Disgust', 'Fear', 'Happy', 'Neutral', 'Sad', 'Surprise']
22
+ gender_labels = ['Male', 'Female']
23
+
24
+ # Streamlit app
25
+ st.title("Live Face Detection")
26
+ st.write("This app detects face, emotion, and gender of a person")
27
+
28
+ start_button = st.button('Start Webcam', key='start')
29
+ stop_button = st.button('Stop Webcam', key='stop')
30
+
31
+ webcam_on = start_button
32
+
33
+ if webcam_on:
34
+ # Start webcam
35
+ cap = cv2.VideoCapture(0)
36
+
37
+ frameST = st.empty()
38
+
39
+ while webcam_on and not stop_button:
40
+ ret, frame = cap.read()
41
+ labels = []
42
+
43
+ gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
44
+ faces=face_classifier.detectMultiScale(gray,1.3,5)
45
+
46
+ for (x,y,w,h) in faces:
47
+ cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
48
+ roi_gray=gray[y:y+h,x:x+w]
49
+ roi_gray=cv2.resize(roi_gray,(48,48),interpolation=cv2.INTER_AREA)
50
+
51
+ # Get image ready for prediction
52
+ roi=roi_gray.astype('float')/255.0 # Scaling the image
53
+ roi=img_to_array(roi)
54
+ roi=np.expand_dims(roi,axis=0) # Expand dims to get it ready for prediction (1, 48, 48, 1)
55
+
56
+ preds=emotion_model.predict(roi)[0] # One hot encoded result for 7 classes
57
+ label=class_labels[preds.argmax()] # Find the label
58
+ label_position=(x,y)
59
+ cv2.putText(frame,label,label_position,cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)
60
+
61
+ # Gender
62
+ roi_color=frame[y:y+h,x:x+w]
63
+ roi_color=cv2.resize(roi_color,(200,200),interpolation=cv2.INTER_AREA)
64
+ gender_predict = gender_model.predict(np.array(roi_color).reshape(-1,200,200,3))
65
+ gender_predict = (gender_predict>= 0.5).astype(int)[:,0]
66
+ gender_label=gender_labels[gender_predict[0]]
67
+ gender_label_position=(x,y+h+50) # 50 pixels below to move the label outside the face
68
+ cv2.putText(frame,gender_label,gender_label_position,cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)
69
+
70
+ # Age
71
+ age_predict = age_model.predict(np.array(roi_color).reshape(-1,200,200,3))
72
+ age = round(age_predict[0,0])
73
+ age_label_position=(x+h,y+h)
74
+ cv2.putText(frame,"Age="+str(age),age_label_position,cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)
75
+
76
+ # Display the resulting frame
77
+ frameST.image(frame, channels="BGR")
78
+
79
+ # Update the webcam status
80
+ webcam_on = not stop_button
81
+
82
+ cap.release()
83
+ cv2.destroyAllWindows()
84
+
85
+
86
+ # Write information file of what does this app do
87
+ st.write("## Information")
88
+
89
+ st.title("Facial Emotion Detection, Age, and Gender Recognition with OpenCV")
90
+
91
+ st.write("""
92
+ In this project, a multi-faceted facial analysis system has been implemented, combining state-of-the-art deep learning models with the powerful computer vision library, OpenCV. The system is designed to accurately detect faces in images or video streams and provide insights into facial emotions, age, and gender.
93
+ """)
94
+
95
+ st.header("Project Overview")
96
+
97
+ st.write("""
98
+ - **Face Detection**: Utilizing the robust face detection capabilities of OpenCV, the system efficiently locates and isolates faces within images or video frames. This forms the foundational step for subsequent analyses, ensuring accurate and reliable results.
99
+ - **Facial Emotion Detection**: The emotion detection model has been trained to recognize a range of human emotions, including happiness, sadness, anger, surprise, and more. Leveraging a deep learning approach, the model can analyze facial expressions in real-time, providing valuable insights into the emotional states of individuals in the captured media.
100
+ - **Age and Gender Recognition**: The age and gender recognition models have been fine-tuned to estimate the age and gender of detected faces. This adds another layer of demographic information, allowing for more comprehensive analysis and applications. The age estimation model provides an approximate age range, while the gender recognition model accurately classifies faces into male or female categories.
101
+ - **Integration with OpenCV**: The entire system is seamlessly integrated with OpenCV, a widely-used computer vision library. OpenCV streamlines the image and video processing pipeline, facilitating real-time analysis and enhancing the system's efficiency. The combination of deep learning models and OpenCV ensures a robust and scalable solution for facial analysis tasks.
102
+ """)
103
+
104
+ st.header("Usage")
105
+
106
+ st.write("""
107
+ The information about models is written in the information text file attached along with the GitHub files. The information about the dataset for each training is written in each Python file.
108
+
109
+ To run the application:
110
+ 1. Download the models from the link provided in the text file.
111
+ 2. Save the models in your respective system.
112
+ 3. Then modify the path of the downloaded models in `live_face_detection.py` file and run it.
113
+ """)
114
+
115
+ st.header("Practical Applications")
116
+
117
+ st.write("""
118
+ This facial analysis system has a wide range of practical applications, including but not limited to:
119
+ - Human-computer interaction
120
+ - Sentiment analysis
121
+ - Audience engagement measurement
122
+ - Security and surveillance
123
+ - Personalized user experiences
124
+ """)
125
+
126
+ st.header("Future Enhancements")
127
+
128
+ st.write("""
129
+ As technology evolves, there is room for further improvements and enhancements to the system. This may involve fine-tuning the models with additional data, exploring novel architectures, or integrating with other cutting-edge computer vision techniques.
130
+ """)
131
+
132
+ st.header("Conclusion")
133
+
134
+ st.write("""
135
+ In conclusion, the facial emotion detection, age, and gender recognition system presented here showcases the synergy between deep learning models and OpenCV, offering a powerful and versatile tool for understanding and analyzing facial attributes in diverse scenarios.
136
+ """)
137
+
138
+ st.markdown(f"© {datetime.datetime.now().year} Dhrumit Patel. All rights reserved.")
emotion_detection_model_50epochs.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dde478237261277246ed989749189f0eda75c8bfd9904928622ef59a8279f344
3
+ size 29933488
gender_model_3epochs.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6bb2821e39ea29aaaa4f2ad8d0d06a0cc10282e3de90d81b566012341e558695
3
+ size 47248096
haarcascade_frontalface_default.xml ADDED
The diff for this file is too large to render. See raw diff
 
live_face_detection.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Thu Feb 29 17:46:17 2024
4
+
5
+ @author: Dhrumit Patel
6
+ """
7
+
8
+ from keras.models import load_model
9
+ from time import sleep
10
+ from keras_preprocessing.image import img_to_array
11
+ from keras_preprocessing import image
12
+ import cv2
13
+
14
+ import numpy as np
15
+
16
+ face_classifier = cv2.CascadeClassifier('pretrained_haarcascade_classifier/haarcascade_frontalface_default.xml')
17
+ emotion_model = load_model('models/emotion_detection_model_50epochs.h5')
18
+ age_model = load_model('models/age_model_3epochs.h5')
19
+ gender_model = load_model('models/gender_model_3epochs.h5')
20
+
21
+ class_labels = ['Angry', 'Disgust', 'Fear', 'Happy', 'Neutral', 'Sad', 'Surprise']
22
+ gender_labels = ['Male', 'Female']
23
+
24
+ cap = cv2.VideoCapture(0)
25
+
26
+ while True:
27
+ ret, frame = cap.read()
28
+ labels = []
29
+
30
+ gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
31
+ faces=face_classifier.detectMultiScale(gray,1.3,5)
32
+
33
+ for (x,y,w,h) in faces:
34
+ cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
35
+ roi_gray=gray[y:y+h,x:x+w]
36
+ roi_gray=cv2.resize(roi_gray,(48,48),interpolation=cv2.INTER_AREA)
37
+
38
+ # Get image ready for prediction
39
+ roi=roi_gray.astype('float')/255.0 # Scaling the image
40
+ roi=img_to_array(roi)
41
+ roi=np.expand_dims(roi,axis=0) # Expand dims to get it ready for prediction (1, 48, 48, 1)
42
+
43
+ preds=emotion_model.predict(roi)[0] # One hot encoded result for 7 classes
44
+ label=class_labels[preds.argmax()] # Find the label
45
+ label_position=(x,y)
46
+ cv2.putText(frame,label,label_position,cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)
47
+
48
+ #Gender
49
+ roi_color=frame[y:y+h,x:x+w]
50
+ roi_color=cv2.resize(roi_color,(200,200),interpolation=cv2.INTER_AREA)
51
+ gender_predict = gender_model.predict(np.array(roi_color).reshape(-1,200,200,3))
52
+ gender_predict = (gender_predict>= 0.5).astype(int)[:,0]
53
+ gender_label=gender_labels[gender_predict[0]]
54
+ gender_label_position=(x,y+h+50) # 50 pixels below to move the label outside the face
55
+ cv2.putText(frame,gender_label,gender_label_position,cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)
56
+
57
+ #Age
58
+ age_predict = age_model.predict(np.array(roi_color).reshape(-1,200,200,3))
59
+ age = round(age_predict[0,0])
60
+ age_label_position=(x+h,y+h)
61
+ cv2.putText(frame,"Age="+str(age),age_label_position,cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)
62
+
63
+
64
+ cv2.imshow('Live Face Detection', frame)
65
+ if cv2.waitKey(1) & 0xFF == ord('q'):
66
+ break
67
+
68
+ cap.release()
69
+ cv2.destroyAllWindows()
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+
83
+
84
+
85
+
86
+
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+