Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files- app.py +85 -0
- haarcascade_frontalface_default.xml +0 -0
- model_78.h5 +3 -0
- model_weights_78.h5 +3 -0
- requirements.txt +9 -0
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import the rquired libraries.
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
from keras.models import load_model
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from tensorflow import keras
|
| 7 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
| 8 |
+
from streamlit_webrtc import webrtc_streamer, VideoTransformerBase, RTCConfiguration, VideoProcessorBase, WebRtcMode
|
| 9 |
+
|
| 10 |
+
emotion_labels = ['Angry','Disgust','Fear','Happy','Neutral', 'Sad', 'Surprise']
|
| 11 |
+
|
| 12 |
+
classifier =load_model('model_78.h5')
|
| 13 |
+
|
| 14 |
+
classifier.load_weights("model_weights_78.h5")
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 18 |
+
except Exception:
|
| 19 |
+
st.write("Error loading cascade classifiers")
|
| 20 |
+
|
| 21 |
+
class VideoTransformer(VideoTransformerBase):
|
| 22 |
+
def transform(self, frame):
|
| 23 |
+
img = frame.to_ndarray(format="bgr24")
|
| 24 |
+
|
| 25 |
+
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 26 |
+
faces = face_cascade.detectMultiScale(
|
| 27 |
+
image=img_gray, scaleFactor=1.3, minNeighbors=5)
|
| 28 |
+
for (x, y, w, h) in faces:
|
| 29 |
+
cv2.rectangle(img=img, pt1=(x, y), pt2=(
|
| 30 |
+
x + w, y + h), color=(0, 255, 255), thickness=2)
|
| 31 |
+
roi_gray = img_gray[y:y + h, x:x + w]
|
| 32 |
+
roi_gray = cv2.resize(roi_gray, (48, 48), interpolation=cv2.INTER_AREA)
|
| 33 |
+
if np.sum([roi_gray]) != 0:
|
| 34 |
+
roi = roi_gray.astype('float') / 255.0
|
| 35 |
+
roi = img_to_array(roi)
|
| 36 |
+
roi = np.expand_dims(roi, axis=0)
|
| 37 |
+
prediction = classifier.predict(roi)[0]
|
| 38 |
+
maxindex = int(np.argmax(prediction))
|
| 39 |
+
finalout = emotion_labels[maxindex]
|
| 40 |
+
output = str(finalout)
|
| 41 |
+
label_position = (x, y-10)
|
| 42 |
+
cv2.putText(img, output, label_position, cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
| 43 |
+
|
| 44 |
+
return img
|
| 45 |
+
|
| 46 |
+
def main():
|
| 47 |
+
# Face Analysis Application #
|
| 48 |
+
st.title("Real Time Face Emotion Detection Application ๐ ๐คฎ๐จ๐๐๐๐ฎ")
|
| 49 |
+
activiteis = ["Home", "Live Face Emotion Detection"]
|
| 50 |
+
choice = st.sidebar.selectbox("Select Activity", activiteis)
|
| 51 |
+
|
| 52 |
+
# Homepage.
|
| 53 |
+
if choice == "Home":
|
| 54 |
+
html_temp_home1 = """<div style="background-color:#FC4C02;padding:0.5px">
|
| 55 |
+
<h4 style="color:white;text-align:center;">
|
| 56 |
+
Start Real Time Face Emotion Detection.
|
| 57 |
+
</h4>
|
| 58 |
+
</div>
|
| 59 |
+
</br>"""
|
| 60 |
+
|
| 61 |
+
st.markdown(html_temp_home1, unsafe_allow_html=True)
|
| 62 |
+
st.write("""
|
| 63 |
+
How to use...
|
| 64 |
+
1. Click the dropdown list in the top left corner and select Live Face Emotion Detection.
|
| 65 |
+
2. This takes you to a page which will tell if it recognizes your emotions.
|
| 66 |
+
""")
|
| 67 |
+
|
| 68 |
+
# Live Face Emotion Detection.
|
| 69 |
+
elif choice == "Live Face Emotion Detection":
|
| 70 |
+
st.header("Webcam Live Feed")
|
| 71 |
+
st.subheader('''
|
| 72 |
+
Welcome to the other side of the SCREEN!!!
|
| 73 |
+
* Get ready with all the emotions you can express.
|
| 74 |
+
''')
|
| 75 |
+
st.write("1. Click Start to open your camera and give permission for prediction")
|
| 76 |
+
st.write("2. This will predict your emotion.")
|
| 77 |
+
st.write("3. When you done, click stop to end.")
|
| 78 |
+
webrtc_streamer(key="example", video_processor_factory=VideoTransformer)
|
| 79 |
+
|
| 80 |
+
else:
|
| 81 |
+
pass
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
main()
|
haarcascade_frontalface_default.xml
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
model_78.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ae8de71c7742aa40cf3a5b685d95e56070592a65b9e419bdd2b633a720c0e0f3
|
| 3 |
+
size 32828272
|
model_weights_78.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0d45e7f0123004cbc60a1cd51e6860bd18c03a9d42238b507ee281a984ae51ce
|
| 3 |
+
size 10966264
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
streamlit==1.9.0
|
| 3 |
+
tensorflow-cpu==2.9.0
|
| 4 |
+
opencv-python-headless==4.5.5.64
|
| 5 |
+
streamlit-webrtc==0.37.0
|
| 6 |
+
protobuf~=3.19.0
|
| 7 |
+
pandas
|
| 8 |
+
seaborn
|
| 9 |
+
pyngrok
|