Repository navigation
Expand file tree
/
Copy pathwebcam_implementation.py
More file actions
77 lines (57 loc) · 2.45 KB
/
Copy pathwebcam_implementation.py
File metadata and controls
77 lines (57 loc) · 2.45 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
import cv2
from keras.models import model_from_json
import numpy as np
# Load the pre-trained model architecture from JSON file
json_file = open("emotiondetector.json", "r")
model_json = json_file.read()
json_file.close()
model = model_from_json(model_json)
# Load the pre-trained model weights
model.load_weights("emotiondetector.h5")
# Load the Haar cascade classifier for face detection
haar_file = cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
face_cascade = cv2.CascadeClassifier(haar_file)
# Define a function to extract features from an image
def extract_features(image):
feature = np.array(image)
feature = feature.reshape(1, 48, 48, 1)
return feature / 255.0
# Open the webcam (camera)
webcam = cv2.VideoCapture(0)
# Define labels for emotion classes
labels = {0: 'angry', 1: 'disgust', 2: 'fear', 3: 'happy', 4: 'neutral', 5: 'sad', 6: 'surprise'}
while True:
# Read a frame from the webcam
i, im = webcam.read()
# Convert the frame to grayscale
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
# Detect faces in the grayscale frame
faces = face_cascade.detectMultiScale(im, 1.3, 5)
try:
# For each detected face, perform facial emotion recognition
for (p, q, r, s) in faces:
# Extract the region of interest (ROI) which contains the face
image = gray[q:q + s, p:p + r]
# Draw a rectangle around the detected face
cv2.rectangle(im, (p, q), (p + r, q + s), (255, 0, 0), 2)
# Resize the face image to the required input size (48x48)
image = cv2.resize(image, (48, 48))
# Extract features from the resized face image
img = extract_features(image)
# Make a prediction using the trained model
pred = model.predict(img)
# Get the predicted label for emotion
prediction_label = labels[pred.argmax()]
# Display the predicted emotion label near the detected face
cv2.putText(im, f'Emotion: {prediction_label}', (p - 10, q - 10),
cv2.FONT_HERSHEY_COMPLEX_SMALL, 2, (0, 0, 255))
# Display the frame with annotations in real-time
cv2.imshow("Real-time Facial Emotion Recognition", im)
# Break the loop if the 'Esc' key is pressed
if cv2.waitKey(1) == 27:
break
except cv2.error:
pass
# Release the webcam and close all OpenCV windows
webcam.release()
cv2.destroyAllWindows()