-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathface_detection.py
More file actions
101 lines (78 loc) · 3.62 KB
/
face_detection.py
File metadata and controls
101 lines (78 loc) · 3.62 KB
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
import cv2
from fer import FER
from flask import Flask, render_template, Response, jsonify
app = Flask(__name__)
# Initialize the camera
camera = cv2.VideoCapture(0) # 0 is usually the default webcam
# Initialize the FER detector
detector = FER()
current_frame = None
current_emotion = None
current_suggestion = None
# Define the suggestion dictionary
emotion_suggestions = {
"happy": "Go for a walk or call a friend!",
"sad": "Take a break, listen to music.",
"angry": "Take deep breaths, relax.",
"surprise": "Take a moment to collect yourself.",
"neutral": "Everything is okay, continue as you are."
}
# Function to generate video stream with emotion detection
def generate_frames():
global current_frame, current_emotion, current_suggestion
while True:
success, frame = camera.read() # Capture frame from webcam
if not success:
break
# Resize the frame to make it bigger (adjust size as needed)
frame = cv2.resize(frame, (800, 600)) # Adjust width and height as needed
# Detect emotion
emotion, score = detector.top_emotion(frame)
# Get suggestion based on the detected emotion
suggestion = emotion_suggestions.get(emotion, "Relax and take a break.")
# Update global variables with current frame and suggestion
current_frame = frame
current_emotion = emotion
current_suggestion = suggestion
# Add text to frame (Emotion and Suggestion)
cv2.putText(frame, f'Emotion: {emotion}', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
cv2.putText(frame, f'Suggestion: {suggestion}', (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
# Convert frame to bytes to send it to the webpage
ret, buffer = cv2.imencode('.jpg', frame)
if not ret:
continue
frame = buffer.tobytes()
# Send the frame to the frontend
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route('/get_suggestion')
def get_suggestion():
# Return the current suggestion as JSON data
return jsonify({'suggestion': current_suggestion})
def generate_frames():
global current_frame, current_emotion, current_suggestion
while True:
success, frame = camera.read() # Capture frame from webcam
if not success:
break
# Resize the frame to make it bigger (adjust size as needed)
frame = cv2.resize(frame, (800, 600)) # Adjust width and height as needed
# Detect emotion
emotion, score = detector.top_emotion(frame)
# Get suggestion based on the detected emotion
suggestion = emotion_suggestions.get(emotion, "Relax and take a break.")
# Update global variables with current frame and suggestion
current_frame = frame
current_emotion = emotion
current_suggestion = suggestion
# Add text to frame (Emotion and Suggestion)
cv2.putText(frame, f'Emotion: {emotion}', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
cv2.putText(frame, f'Suggestion: {suggestion}', (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
# Convert frame to bytes to send it to the webpage
ret, buffer = cv2.imencode('.jpg', frame)
if not ret:
continue
frame = buffer.tobytes()
# Send the frame to the frontend
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')