Spaces:
Sleeping
Sleeping
File size: 5,925 Bytes
f771ff2 5971329 082b842 5971329 b125ffc 5971329 b125ffc 5971329 136ac77 5971329 b125ffc 5971329 082b842 cdadc4f f771ff2 2b8f874 5971329 5cfcc26 cdadc4f 5cfcc26 c81b58e 5cfcc26 cdadc4f 5cfcc26 e3c30ee 082b842 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# import gradio as gr
# import os
# import cv2
# import numpy as np
# import imutils
# from keras.preprocessing.image import img_to_array
# from keras.models import load_model
# # Load the pre-trained models and define parameters
# detection_model_path = 'haarcascade_files/haarcascade_frontalface_default.xml'
# emotion_model_path = 'model4_0.83/model4_entire_model.h5'
# face_detection = cv2.CascadeClassifier(detection_model_path)
# emotion_classifier = load_model(emotion_model_path, compile=False)
# EMOTIONS = ['neutral', 'happiness', 'surprise', 'sadness', 'anger', 'disgust', 'fear', 'contempt', 'unknown']
# # face_detector_mtcnn = MTCNN()
# classifier = load_model(emotion_model_path)
# def predict_emotion(frame):
# frame = imutils.resize(frame, width=300)
# gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
# faces = face_detection.detectMultiScale(gray, scaleFactor=1.1,
# minNeighbors=5, minSize=(30, 30),
# flags=cv2.CASCADE_SCALE_IMAGE)
# for (fX, fY, fW, fH) in faces:
# # Extract the ROI of the face from the grayscale image, resize it to a fixed 28x28 pixels, and then prepare
# # the ROI for classification via the CNN
# roi = gray[fY:fY + fH, fX:fX + fW]
# roi = cv2.resize(roi, (48, 48))
# roi = roi.astype("float") / 255.0
# roi = img_to_array(roi)
# roi = np.expand_dims(roi, axis=0)
# preds = emotion_classifier.predict(roi)[0]
# label = EMOTIONS[preds.argmax()]
# # Overlay a box over the detected face
# cv2.putText(frame, label, (fX, fY - 10),
# cv2.FONT_HERSHEY_DUPLEX, 0.5, (238, 164, 64), 1, cv2.LINE_AA)
# cv2.rectangle(frame, (fX, fY), (fX + fW, fY + fH),
# (238, 164, 64), 2)
# return frame
# demo = gr.Interface(
# fn = predict_emotion,
# inputs = gr.Image(type="numpy"),
# outputs = gr.Image(),
# # gr.components.Image(label="Predicted Emotion"),
# # gr.components.Label(num_top_classes=2, label="Top 2 Probabilities")
# #flagging_options=["blurry", "incorrect", "other"],
# examples = [
# os.path.join(os.path.dirname(__file__), "images/chandler.jpeg"),
# os.path.join(os.path.dirname(__file__), "images/janice.jpeg"),
# os.path.join(os.path.dirname(__file__), "images/joey.jpeg"),
# os.path.join(os.path.dirname(__file__), "images/phoebe.jpeg"),
# os.path.join(os.path.dirname(__file__), "images/rachel_monica.jpeg"),
# os.path.join(os.path.dirname(__file__), "images/ross.jpeg"),
# os.path.join(os.path.dirname(__file__), "images/gunther.jpeg")
# ],
# title = "How are you feeling?",
# theme = "shivi/calm_seafoam"
# )
# if __name__ == "__main__":
# demo.launch()
######################################################################################################################################################
import gradio as gr
import os
import cv2
import numpy as np
import imutils
from keras.preprocessing.image import img_to_array
from keras.models import load_model
# Load the pre-trained models and define parameters
detection_model_path = 'haarcascade_files/haarcascade_frontalface_default.xml'
emotion_model_path = 'model4_0.83/model4_entire_model.h5'
face_detection = cv2.CascadeClassifier(detection_model_path)
emotion_classifier = load_model(emotion_model_path, compile=False)
EMOTIONS = ['neutral', 'happiness', 'surprise', 'sadness', 'anger', 'disgust', 'fear', 'contempt', 'unknown']
# Define a function to process each frame for emotion prediction
def predict_emotion(frame):
frame = imutils.resize(frame, width=300)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_detection.detectMultiScale(gray, scaleFactor=1.1,
minNeighbors=5, minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE)
for (fX, fY, fW, fH) in faces:
roi = gray[fY:fY + fH, fX:fX + fW]
roi = cv2.resize(roi, (48, 48))
roi = roi.astype("float") / 255.0
roi = img_to_array(roi)
roi = np.expand_dims(roi, axis=0)
preds = emotion_classifier.predict(roi)[0]
label = EMOTIONS[preds.argmax()]
cv2.putText(frame, label, (fX, fY - 10),
cv2.FONT_HERSHEY_DUPLEX, 0.5, (238, 164, 64), 1, cv2.LINE_AA)
cv2.rectangle(frame, (fX, fY), (fX + fW, fY + fH),
(238, 164, 64), 2)
return frame
# Define a function to process video input and output
def process_video(input_video_path, output_video_path):
# Open the video capture
cap = cv2.VideoCapture(input_video_path)
# Get video properties (dimensions, frame rate)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
# Define video writer for output
out = cv2.VideoWriter(output_video_path, cv2.VideoWriter_fourcc(*'XVID'), fps, (width, height))
# Process each frame in the video
while True:
ret, frame = cap.read()
if not ret:
break
frame_with_emotion = predict_emotion(frame)
out.write(frame_with_emotion)
# Release video capture and writer
cap.release()
out.release()
# Define the Gradio interface
demo = gr.Interface(
fn=process_video,
inputs=["video", "file"], # Allow video input from webcam or file
outputs="video", # Output video with emotion overlay
capture_session=True, # Maintain capture session for video input
title="Emotion Detection in Video",
description="Upload a video file or use your webcam to detect emotions in real-time.",
theme="huggingface",
)
# Launch the Gradio interface
if __name__ == "__main__":
demo.launch()
|