-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathemotion_detection.py
58 lines (49 loc) · 2.01 KB
/
emotion_detection.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
"""The Viola-Jones algorithm, emotion detection """
# Importing the libraries
import cv2
#--------------------------------------------
# Loading the cascades, detecting only face and eyes
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
smile_cascade = cv2.CascadeClassifier('haarcascade_smile.xml')
def detect(gray, frame):
"""
Parameters
----------
gray : TYPE
Cascade work on black and white image
frame : TYPE
original image
Returns
-------
frame : TYPE
same image with deteced face and eyes
"""
# detectMultiScale(image, scaleFactor, minNeighbors)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces: # cordinate of upper left corner
cv2.rectangle(frame,(x, y), (x+w, y+h), (255, 0, 0), 3) # thr color of bounding box
cv2.putText(frame, "FACE", (x,y),cv2.FONT_HERSHEY_COMPLEX,0.5,(250,250,250),1)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 22)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color,(ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
cv2.putText(frame, "Eye", (ex,ey),cv2.FONT_HERSHEY_COMPLEX,0.5,(250,250,250),1)
# detect smile
smiles = smile_cascade.detectMultiScale(roi_gray, 1.7, 25)
for (sx, sy, sw, sh) in smiles:
cv2.rectangle(roi_color, (sx, sy), (sx+sw, sy+sh), (0, 0, 255), 2)
cv2.putText(frame, "smile", (sx,sy),cv2.FONT_HERSHEY_COMPLEX,0.5,(250,250,250),1)
return frame
# Doing some Face Recognition with the webcam
video_capture = cv2.VideoCapture(0)
while True:
_, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
canvas = detect(gray, frame)
cv2.imshow('Video', canvas)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()