-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacereco.py
52 lines (40 loc) · 1.58 KB
/
facereco.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
import os
import cv2
class EmotionClassifier:
def __init__(self):
self.emotions = ["neutral", "anger", "disgust", "happy", "surprise", "fear", "sadness"]
self.fishface = cv2.face.createFisherFaceRecognizer()
self.fishface.load("final.json")
face_cascade_path = "haarcascade/haarcascade_frontalface_alt.xml"
self.face_cascade = cv2.CascadeClassifier(os.path.expanduser(face_cascade_path))
def find_face_roi(self, image):
scale_factor = 1.1
min_neighbors = 3
min_size = (30, 30)
faces = self.face_cascade.detectMultiScale(image, scaleFactor=scale_factor, minNeighbors=min_neighbors,
minSize=min_size)
for (x, y, w, h) in faces:
cutedImg = image[y:y + h, x:x + w]
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 255, 0), 2)
return cutedImg, image
return [], image
def make_prediction(self, img):
cutedImg, image = self.find_face_roi(img)
img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
img = cv2.resize(img, (60, 60))
pred, conf = self.fishface.predict(img)
return self.emotions[pred], conf
def run():
emotion_classifier = EmotionClassifier()
cam = cv2.VideoCapture(0)
while True:
ret_val, img = cam.read()
emotion, certainty = emotion_classifier.make_prediction(img)
print (emotion, certainty)
if cv2.waitKey(1) == 27:
cam.release()
break
cam.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
run()