-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfaceDetect.py
More file actions
48 lines (38 loc) · 1.52 KB
/
faceDetect.py
File metadata and controls
48 lines (38 loc) · 1.52 KB
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
# -*- coding: utf-8 -*-
__author__ = "Jasper Xu"
import cv2
import time
def face_detect(cascade_dir):
# load cascade file for face detection
faceCascade = cv2.CascadeClassifier(cascade_dir)
font = cv2.FONT_HERSHEY_SIMPLEX
# get video stream
cam = cv2.VideoCapture(0)
cam.set(3,640)
cam.set(4,480)
# show fps
start_time = time.time()
fps = 0
while True:
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale (gray, 1.3, 5, minSize = (30,30))
i = 1
for (x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 3)
cv2.putText(img, 'Face No.'+str(i), (x,y-5), font, 1, (255,255,255), 2)
i += 1
fps += 1
d_time = time.time() - start_time
cv2.putText(img, 'FPS:' + str(fps//d_time), (20,460), font, 1, (255,255,255), 2)
cv2.imshow('camera',img)
k = cv2.waitKey(10) & 0xff
if k == ord(' '): # press space to break
break
cam.release()
cv2.destroyAllWindows()
#------------------------------main-------------------------------------------------#
if __name__ == '__main__':
haar_dir = 'D:/Anaconda/pkgs/opencv-3.3.1-py36h20b85fd_1/Library/etc/haarcascades/haarcascade_frontalface_alt2.xml'
lbp_dir = 'D:/Anaconda/pkgs/opencv-3.3.1-py36h20b85fd_1/Library/etc/lbpcascades/lbpcascade_frontalface_improved.xml'
face_detect(haar_dir)