-
Notifications
You must be signed in to change notification settings - Fork 0
/
faceDetect.py
63 lines (50 loc) · 2.05 KB
/
faceDetect.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
import numpy as np
import cv2
def facedetection():
cam = cv2.VideoCapture(0)
faceCascade = cv2.CascadeClassifier('Cascades/haarcascade_frontalface_default.xml')
eyeCascade = cv2.CascadeClassifier('Cascades/haarcascade_eye.xml')
smileCascade = cv2.CascadeClassifier('Cascades/haarcascade_smile.xml')
minW = 0.1*cam.get(3)
minH = 0.1*cam.get(4)
while True:
rect, pic = cam.read()
gray = cv2.cvtColor(pic, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray, #input grayscale image
scaleFactor = 1.3, # how much the image is reduced in size by
minNeighbors = 5, # higher number means lower false +ves
minSize = (30, 30) # minimum rectangle size for a face
)
img_g = None
img_c = None
for (x, y, w, h) in faces: # (x,y) -> top left corner coordinate, w -> width, h -> height
cv2.rectangle(pic, (x, y), (x+w, y+h), (255, 0, 0), 2)
img_g = gray[y:y+h, x:x+w]
img_c = pic[y:y+h, x:x+w]
eyes = eyeCascade.detectMultiScale(
img_g,
scaleFactor = 1.5,
minNeighbors = 5,
minSize = (5, 5),
)
for (x_e, y_e, w_e, h_e) in eyes:
cv2.rectangle(img_c, (x_e, y_e), (x_e + w_e, y_e + h_e), (0, 255, 0), 2)
smile = smileCascade.detectMultiScale(
img_g,
scaleFactor = 1.5,
minNeighbors = 15,
minSize = (25, 25),
)
for (x_s, y_s, w_s, h_s) in smile:
cv2.rectangle(img_c, (x_s, y_s), (x_s + w_s, y_s + h_s), (0, 255, 0), 2)
pic = cv2.flip(pic, 1)
ret, buffer = cv2.imencode('.jpg', pic)
pic = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + pic + b'\r\n') # concat frame one by one and show result
k = cv2.waitKey(20)
if k == 27: # 'ESC' to quit
break
cam.release()
cv2.destroyAllWindows()