-
Notifications
You must be signed in to change notification settings - Fork 20
/
camera.py
48 lines (37 loc) · 1.26 KB
/
camera.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
import cv2
from model import FacialExpressionModel
import numpy as np
rgb = cv2.VideoCapture(0)
facec = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
font = cv2.FONT_HERSHEY_SIMPLEX
def __get_data__():
"""
__get_data__: Gets data from the VideoCapture object and classifies them
to a face or no face.
returns: tuple (faces in image, frame read, grayscale frame)
"""
_, fr = rgb.read()
gray = cv2.cvtColor(fr, cv2.COLOR_BGR2GRAY)
faces = facec.detectMultiScale(gray, 1.3, 5)
return faces, fr, gray
def start_app(cnn):
skip_frame = 10
data = []
flag = False
ix = 0
while True:
ix += 1
faces, fr, gray_fr = __get_data__()
for (x, y, w, h) in faces:
fc = gray_fr[y:y+h, x:x+w]
roi = cv2.resize(fc, (48, 48))
pred = cnn.predict_emotion(roi[np.newaxis, :, :, np.newaxis])
cv2.putText(fr, pred, (x, y), font, 1, (255, 255, 0), 2)
cv2.rectangle(fr,(x,y),(x+w,y+h),(255,0,0),2)
if cv2.waitKey(1) == 27:
break
cv2.imshow('Filter', fr)
cv2.destroyAllWindows()
if __name__ == '__main__':
model = FacialExpressionModel("face_model.json", "face_model.h5")
start_app(model)