Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +114 -0
- emotion_model.h5 +3 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import io
|
| 4 |
+
import PIL
|
| 5 |
+
from base64 import b64decode, b64encode
|
| 6 |
+
from keras.models import load_model
|
| 7 |
+
import streamlit as st
|
| 8 |
+
from streamlit_webrtc import webrtc_streamer, VideoProcessorBase
|
| 9 |
+
|
| 10 |
+
# Initialize the Haar Cascade face detection model
|
| 11 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 12 |
+
model = load_model('emotion_model.h5',compile=False)
|
| 13 |
+
emotion_dict = {0: "Angry", 1: "Disgust", 2: "Fear", 3: "Happy", 4: "Neutral", 5: "Sad", 6: "Surprised"}
|
| 14 |
+
|
| 15 |
+
# Define functions to convert between JavaScript image reply and OpenCV image
|
| 16 |
+
def js_to_image(js_reply):
|
| 17 |
+
image_bytes = b64decode(js_reply.split(',')[1])
|
| 18 |
+
jpg_as_np = np.frombuffer(image_bytes, dtype=np.uint8)
|
| 19 |
+
img = cv2.imdecode(jpg_as_np, flags=1)
|
| 20 |
+
return img
|
| 21 |
+
|
| 22 |
+
def bbox_to_bytes(bbox_array):
|
| 23 |
+
bbox_PIL = PIL.Image.fromarray(bbox_array, 'RGBA')
|
| 24 |
+
iobuf = io.BytesIO()
|
| 25 |
+
bbox_PIL.save(iobuf, format='png')
|
| 26 |
+
bbox_bytes = 'data:image/png;base64,{}'.format((str(b64encode(iobuf.getvalue()), 'utf-8')))
|
| 27 |
+
return bbox_bytes
|
| 28 |
+
|
| 29 |
+
# Define function to process each frame from the video stream
|
| 30 |
+
def process_frame(frame):
|
| 31 |
+
# Convert frame to grayscale
|
| 32 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 33 |
+
|
| 34 |
+
# Perform face detection
|
| 35 |
+
faces = face_cascade.detectMultiScale(gray)
|
| 36 |
+
|
| 37 |
+
emotions = []
|
| 38 |
+
|
| 39 |
+
# Process each detected face
|
| 40 |
+
for (x, y, w, h) in faces:
|
| 41 |
+
face_region = gray[y:y+h, x:x+w]
|
| 42 |
+
face_resized = cv2.resize(face_region, (48, 48))
|
| 43 |
+
img = np.expand_dims(face_resized, axis=0)
|
| 44 |
+
img = np.expand_dims(img, axis=-1)
|
| 45 |
+
predictions = model.predict(img)
|
| 46 |
+
emo = model.predict(img)[0]
|
| 47 |
+
emotions.append(emo)
|
| 48 |
+
predicted_class = np.argmax(predictions)
|
| 49 |
+
predicted_emotion = emotion_dict[predicted_class]
|
| 50 |
+
accuracy = predictions[0][predicted_class]
|
| 51 |
+
|
| 52 |
+
# Draw bounding box and emotion label on the frame
|
| 53 |
+
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
|
| 54 |
+
cv2.putText(frame, f"{predicted_emotion} ({accuracy:.2f})", (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 0), 2)
|
| 55 |
+
|
| 56 |
+
return frame, emotions
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
class VideoProcessor(VideoProcessorBase):
|
| 60 |
+
def recv(self, frame):
|
| 61 |
+
img = frame.to_ndarray(format="bgr24")
|
| 62 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 63 |
+
faces = face_cascade.detectMultiScale(gray)
|
| 64 |
+
|
| 65 |
+
for (x, y, w, h) in faces:
|
| 66 |
+
face_region = gray[y:y+h, x:x+w]
|
| 67 |
+
face_resized = cv2.resize(face_region, (48, 48))
|
| 68 |
+
img_array = np.expand_dims(face_resized, axis=0)
|
| 69 |
+
img_array = np.expand_dims(img_array, axis=-1)
|
| 70 |
+
predictions = model.predict(img_array)
|
| 71 |
+
predicted_class = np.argmax(predictions)
|
| 72 |
+
predicted_emotion = emotion_dict[predicted_class]
|
| 73 |
+
accuracy = predictions[0][predicted_class]
|
| 74 |
+
|
| 75 |
+
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
|
| 76 |
+
cv2.putText(img, f"{predicted_emotion} ({accuracy:.2f})", (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 0), 2)
|
| 77 |
+
|
| 78 |
+
return frame.from_ndarray(img, format="bgr24")
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
# Page Title and Description
|
| 85 |
+
st.set_page_config(page_title="Facial Emotion Recognition", layout="wide")
|
| 86 |
+
st.title("Facial Emotion Recognition")
|
| 87 |
+
|
| 88 |
+
# Sidebar
|
| 89 |
+
st.sidebar.title("Options")
|
| 90 |
+
option = st.sidebar.radio("Select Option", ("Drag a File","Process Video"))
|
| 91 |
+
|
| 92 |
+
# Main Content Area
|
| 93 |
+
if option == "Drag a File" :
|
| 94 |
+
st.subheader("Photo Processing")
|
| 95 |
+
|
| 96 |
+
# Process image or captured frame
|
| 97 |
+
if option == "Drag a File":
|
| 98 |
+
uploaded_file = st.file_uploader("Upload Photo", type=["jpg", "jpeg", "png"])
|
| 99 |
+
if uploaded_file is not None:
|
| 100 |
+
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 101 |
+
image = cv2.imdecode(file_bytes, 1)
|
| 102 |
+
|
| 103 |
+
if 'image' in locals():
|
| 104 |
+
processed_frame, emotions = process_frame(image)
|
| 105 |
+
# Display processed frame and emotions
|
| 106 |
+
st.subheader("Processed Frame")
|
| 107 |
+
st.image(processed_frame, channels="BGR", use_column_width=False)
|
| 108 |
+
if not emotions:
|
| 109 |
+
st.warning("No faces detected in the image.")
|
| 110 |
+
elif option == "Process Video":
|
| 111 |
+
webrtc_streamer(key="camera", video_processor_factory=VideoProcessor)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
emotion_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:df5da9f5739b0cae3008d1c12e902ab55074f20bc60be0e7847bd34378bbadca
|
| 3 |
+
size 48573144
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
opencv-python-headless
|
| 2 |
+
numpy
|
| 3 |
+
pillow
|
| 4 |
+
keras
|
| 5 |
+
streamlit
|
| 6 |
+
tensorflow
|
| 7 |
+
streamlit-webrtc
|