-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemotion_recognition_webcam.py
77 lines (69 loc) · 2.67 KB
/
emotion_recognition_webcam.py
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 numpy as np
import cv2
from keras.models import load_model
from sklearn.preprocessing import LabelEncoder
import Exceptions
from mtcnn.mtcnn import MTCNN
from PIL import Image
# Funkcja ekstracji twarzy z obrazu
def extract_face(det, image, required_size=(160, 160), bounding_box=False):
pixels = image
results = det.detect_faces(pixels)
if results:
# Wyznaczenie parametrów opisujących bounding box twarzy
x1, y1, width, height = results[0]['box']
x1, y1 = abs(x1), abs(y1)
x2, y2 = x1 + width, y1 + height
# Ekstrakcja rejonu obrazu, gdzie znajduje się twarz
face = pixels[y1:y2, x1:x2]
# resize pixels to the model size
image = Image.fromarray(face)
# Przeskalowanie obrazu twarzy
image = image.resize(required_size)
face_array = np.asarray(image)
if bounding_box:
return face_array, x1, x2, y1, y2
else:
return face_array
else:
raise Exceptions.NoFaceFoundError()
# Wczytanie nauczonego modelu
model = load_model('FER2013_CK2_model_VGGFace_without_disgust_contempt_best.h5')
train_data = np.load('FER2013-faces-without-disgust.npz')
labels = train_data['arr_1']
# Zakodowanie klas do postaci liczb naturalnych
out_encoder = LabelEncoder()
out_encoder.fit(labels)
# Połączenie z kamerą internetową
capture = cv2.VideoCapture(0)
# Stworzenie instancji detektora twarzy
detector = MTCNN()
while True:
# Odczytanie klatki obrazu z kamery
ret, frame = capture.read()
try:
# Wyowałanie funkcji szukającej twarzy na obrazie
face, x1, x2, y1, y2 = extract_face(detector, frame, bounding_box=True)
face = face.astype('float32')
# Normalizacji obrazu twarzy (przeskalowanie pikseli)
face /= 255.0
prediction = model.predict(np.asarray([face]))[0]
class_index = np.argmax(prediction)
# Prawdopodobieństwo predykcji modelu
prob = prediction[class_index] * 100
# Predykcja modelu
prediction = out_encoder.inverse_transform([class_index])[0]
prediction_message = 'Predicted: %s (%.3f)' % (prediction, prob)
print(prediction_message)
# Narysowanie bounding box twarzy
cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2)
# Wyświetlenie informacji o wyniku predykcji
cv2.putText(frame, prediction_message, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, .7, (36, 255, 12), 1)
except Exceptions.NoFaceFoundError:
print('No face found')
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
cv2.imshow('frame', frame)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
capture.stop()
cv2.destroyAllWindows()