Spaces:
Runtime error
Runtime error
HEMANTH
commited on
Commit
·
c7b74ee
1
Parent(s):
572a74b
added all files from git hub (html --> streamlit)
Browse files- CNN_Model_acc_75.h5 +3 -0
- app.py +97 -0
- requirements.txt +6 -0
CNN_Model_acc_75.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4f17414ec703aa77db97a22766b6f4454f766e367148285d16cbbc729e69725c
|
| 3 |
+
size 94225136
|
app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import time
|
| 5 |
+
# Larger title
|
| 6 |
+
st.markdown("<h1 style='text-align: center;'>Emotion Detection</h1>", unsafe_allow_html=True)
|
| 7 |
+
|
| 8 |
+
# Smaller subtitle
|
| 9 |
+
st.markdown("<h3 style='text-align: center;'>angry, fear, happy, neutral, sad, surprise</h3>", unsafe_allow_html=True)
|
| 10 |
+
start = time.time()
|
| 11 |
+
from keras.models import load_model
|
| 12 |
+
import tempfile
|
| 13 |
+
from PIL import Image
|
| 14 |
+
|
| 15 |
+
@st.cache_resource
|
| 16 |
+
def load_emotion_model():
|
| 17 |
+
model = load_model('CNN_Model_acc_75.h5')
|
| 18 |
+
return model
|
| 19 |
+
|
| 20 |
+
# Load the model
|
| 21 |
+
model = load_emotion_model()
|
| 22 |
+
print("time taken to load model : " , time.time() - start)
|
| 23 |
+
img_shape = 48
|
| 24 |
+
emotion_labels = ['angry', 'fear', 'happy', 'neutral', 'sad', 'surprise']
|
| 25 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def process_frame(frame):
|
| 29 |
+
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 30 |
+
faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
| 31 |
+
|
| 32 |
+
for (x, y, w, h) in faces:
|
| 33 |
+
roi_gray = gray_frame[y:y+h, x:x+w]
|
| 34 |
+
roi_color = frame[y:y+h, x:x+w]
|
| 35 |
+
|
| 36 |
+
face_roi = cv2.resize(roi_color, (img_shape, img_shape))
|
| 37 |
+
face_roi = np.expand_dims(face_roi, axis=0)
|
| 38 |
+
face_roi = face_roi / float(img_shape)
|
| 39 |
+
predictions = model.predict(face_roi)
|
| 40 |
+
emotion = emotion_labels[np.argmax(predictions[0])]
|
| 41 |
+
|
| 42 |
+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 43 |
+
cv2.putText(frame, emotion, (x, y+h), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
| 44 |
+
|
| 45 |
+
return frame
|
| 46 |
+
|
| 47 |
+
# def video_feed(video_source):
|
| 48 |
+
# # Read and process video frames
|
| 49 |
+
# while True:
|
| 50 |
+
# ret, frame = video_source.read()
|
| 51 |
+
# if not ret:
|
| 52 |
+
# break
|
| 53 |
+
# frame = process_frame(frame)
|
| 54 |
+
# st.image(frame, channels="BGR")
|
| 55 |
+
|
| 56 |
+
def video_feed(video_source):
|
| 57 |
+
# Create a placeholder to display the frames
|
| 58 |
+
frame_placeholder = st.empty() # This placeholder will be used to replace frames in-place
|
| 59 |
+
|
| 60 |
+
while True:
|
| 61 |
+
ret, frame = video_source.read()
|
| 62 |
+
if not ret:
|
| 63 |
+
break
|
| 64 |
+
|
| 65 |
+
frame = process_frame(frame)
|
| 66 |
+
|
| 67 |
+
# Display the frame in the placeholder
|
| 68 |
+
frame_placeholder.image(frame, channels="BGR", use_column_width=True)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# Sidebar for video or image upload
|
| 73 |
+
upload_choice = st.sidebar.radio("Choose input source", [ "Upload Video", "Upload Image" ,"Camera"])
|
| 74 |
+
|
| 75 |
+
if upload_choice == "Camera":
|
| 76 |
+
# Access camera
|
| 77 |
+
video_source = cv2.VideoCapture(0)
|
| 78 |
+
video_feed(video_source)
|
| 79 |
+
|
| 80 |
+
elif upload_choice == "Upload Video":
|
| 81 |
+
uploaded_video = st.file_uploader("Upload Video", type=["mp4", "mov", "avi", "mkv", "webm"])
|
| 82 |
+
if uploaded_video:
|
| 83 |
+
# Temporarily save the video to disk
|
| 84 |
+
with tempfile.NamedTemporaryFile(delete=False) as tfile:
|
| 85 |
+
tfile.write(uploaded_video.read())
|
| 86 |
+
video_source = cv2.VideoCapture(tfile.name)
|
| 87 |
+
video_feed(video_source)
|
| 88 |
+
|
| 89 |
+
elif upload_choice == "Upload Image":
|
| 90 |
+
uploaded_image = st.file_uploader("Upload Image", type=["png", "jpg", "jpeg", "gif"])
|
| 91 |
+
if uploaded_image:
|
| 92 |
+
image = Image.open(uploaded_image)
|
| 93 |
+
frame = np.array(image)
|
| 94 |
+
frame = process_frame(frame)
|
| 95 |
+
st.image(frame, caption='Processed Image', use_column_width=True)
|
| 96 |
+
|
| 97 |
+
st.sidebar.write("Emotion Labels: Angry, Fear, Happy, Neutral, Sad, Surprise")
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
pandas
|
| 3 |
+
flask
|
| 4 |
+
opencv-python
|
| 5 |
+
keras
|
| 6 |
+
tensorflow
|