-
Notifications
You must be signed in to change notification settings - Fork 0
/
testcv.py
36 lines (26 loc) · 916 Bytes
/
testcv.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
from cv2 import *
HAAR_CASCADE_PATH = "haarcascade_frontalface_default.xml"
CAMERA_INDEX = 0
def detect_faces(image):
faces = []
detected = cv.HaarDetectObjects(image, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, (100,100))
if detected:
for (x,y,w,h),n in detected:
faces.append((x,y,w,h))
return faces
if __name__ == "__main__":
cv.NamedWindow("Video", cv.CV_WINDOW_AUTOSIZE)
capture = cv.CaptureFromCAM(CAMERA_INDEX)
storage = cv.CreateMemStorage()
cascade = cv.Load(HAAR_CASCADE_PATH)
faces = []
i = 0
while True:
image = cv.QueryFrame(capture)
# Only run the Detection algorithm every 5 frames to improve performance
if i%5==0:
faces = detect_faces(image)
for (x,y,w,h) in faces:
cv.Rectangle(image, (x,y), (x+w,y+h), 255)
cv.ShowImage("w1", image)
i += 1