css:
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
background-color: #f0f0f0;
}
h1 {
margin-top: 20px;
}
#videoContainer {
margin-top: 20px;
border: 2px solid #000;
width: 640px; /* Adjust width as needed */
}
img {
width: 100%; /* Make the video frame responsive */
}
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='css/[Link]') }}">
<title>Real-time Emotion Detection</title>
</head>
<body>
<h1>Real-time Emotion Detection</h1>
<div id="videoContainer">
<img id="videoFeed" src="" alt="Video Feed" style="display:none;">
</div>
<button id="detectEmotionBtn">Detect Emotion</button>
<script>
[Link]("detectEmotionBtn").onclick = function() {
startDetection();
};
function startDetection() {
const videoFeed = [Link]("videoFeed");
[Link] = "block"; // Show the video feed
[Link] = "{{ url_for('video_feed') }}"; // Start the video feed
[Link]("Emotion detection started!");
}
</script>
</body>
</html>
[Link]:
import cv2
from flask import Flask, render_template, Response
from deepface import DeepFace
app = Flask(__name__)
# Load face cascade classifier
face_cascade = [Link]([Link] +
'haarcascade_frontalface_default.xml')
# Initialize video capture
cap = [Link](0)
def generate_frames():
while True:
success, frame = [Link]()
if not success:
break
# Convert frame to grayscale
gray_frame = [Link](frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the frame
faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1,
minNeighbors=5, minSize=(30, 30))
for (x, y, w, h) in faces:
# Extract the face ROI (Region of Interest)
face_roi = frame[y:y + h, x:x + w]
try:
# Perform emotion analysis on the face ROI
result = [Link](face_roi, actions=['emotion'],
enforce_detection=True)
emotion = result[0]['dominant_emotion']
# Draw rectangle around face and label with predicted emotion
[Link](frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
[Link](frame, emotion, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
0.9, (0, 0, 255), 2)
except Exception as e:
print(f"Error in emotion detection: {e}")
# Encode the frame in JPEG format
ret, buffer = [Link]('.jpg', frame)
frame = [Link]()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@[Link]('/')
def index():
return render_template('[Link]')
@[Link]('/video_feed')
def video_feed():
return Response(generate_frames(), mimetype='multipart/x-mixed-replace;
boundary=frame')
if __name__ == '__main__':
[Link](debug=True, threaded=True)
# Note: Cap release should be handled appropriately (e.g., when the server shuts
down)
[Link]:
import cv2
from deepface import DeepFace
import time
# Load face cascade classifier
face_cascade = [Link]([Link] +
'haarcascade_frontalface_default.xml')
# Start capturing video
cap = [Link](0)
# Set time buffer (e.g., 5 seconds)
time_buffer = 5
# Set threshold (e.g., 70%)
threshold = 0.7
# Initialize emotion votes
emotion_votes = {}
# Initialize start time
start_time = [Link]()
# Check if the video capture is opened successfully
if not [Link]():
print("Error: Could not open video capture.")
exit()
while True:
# Capture frame-by-frame
ret, frame = [Link]()
if not ret:
print("Error: Could not read frame.")
break
# Convert frame to grayscale
gray_frame = [Link](frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the frame
faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1,
minNeighbors=5, minSize=(30, 30))
if not faces:
continue
# Extract the face ROI (Region of Interest)
x, y, w, h = faces[0]
face_roi = frame[y:y + h, x:x + w] # Use original frame for emotion analysis
try:
# Perform emotion analysis on the face ROI
result = [Link](face_roi, actions=['emotion'],
enforce_detection=True)
# Determine the dominant emotion
emotion = result['dominant_emotion']
# Update emotion votes
if emotion in emotion_votes:
emotion_votes[emotion] += 1
else:
emotion_votes[emotion] = 1
# Draw rectangle around face and label with predicted emotion
[Link](frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
[Link](frame, emotion, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0,
0, 255), 2)
except Exception as e:
print(f"Error in emotion detection: {e}")
# Display the resulting frame
[Link]('Real-time Emotion Detection', frame)
# Check if time buffer has exceeded
if [Link]() - start_time >= time_buffer:
# Calculate final emotion
final_emotion = max(emotion_votes, key=emotion_votes.get)
# Check if final emotion exceeds threshold
if emotion_votes[final_emotion] / sum(emotion_votes.values()) >= threshold:
print(f"Final emotion detected: {final_emotion}")
# Perform Quranic work for the final emotion detected
else:
print("Insufficient confidence in emotion detection.")
# Reset emotion votes and start time
emotion_votes = {}
start_time = [Link]()
# Press 'q' to exit
if [Link](1) & 0xFF == ord('q'):
break
# Release the capture and close all windows
[Link]()
[Link]()