-
Notifications
You must be signed in to change notification settings - Fork 9
/
webcamfaces.py
72 lines (56 loc) · 2.08 KB
/
webcamfaces.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
64
65
66
67
68
69
70
71
72
import cv2
video_capture = cv2.VideoCapture(0)
if not video_capture.isOpened():
print('No video camera found')
exit()
# Uncomment the "..FULLSCREEN" calls and comment out "..AUTOSIZE" to display full screen
# Mouse click on the screen and hit q to exit
# cv2.namedWindow("Video", cv2.WND_PROP_FULLSCREEN)
# cv2.setWindowProperty("Video", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.namedWindow("Video", cv2.WND_PROP_AUTOSIZE)
cv2.setWindowProperty("Video", cv2.WND_PROP_AUTOSIZE, cv2.WINDOW_AUTOSIZE)
print("OpenCV version : {0}".format(cv2.__version__))
t = cv2.getTickCount()
t_count = 0.0
t_sum = 0.0
faceCascade = cv2.CascadeClassifier('/opt/opencv-3.1.0/data/haarcascades_cuda/haarcascade_frontalface_default.xml')
def get_faces(frm):
# Gray scale image for the face detector, shrink it to make it faster
gray = cv2.cvtColor(frm, cv2.COLOR_BGR2GRAY)
image_scale = 3
scl = 1.0 / image_scale
smallgray = cv2.resize(gray, (0, 0), fx=scl, fy=scl)
faces = faceCascade.detectMultiScale(
smallgray,
scaleFactor=1.1,
minNeighbors=3,
minSize=(10, 10),
flags=cv2.CASCADE_SCALE_IMAGE
)
# Draw andy face rectangles on the full size image
for (x, y, w, h) in faces:
x1 = int(x * image_scale)
x2 = int((x + w) * image_scale)
y1 = int(y * image_scale)
y2 = int((y + h) * image_scale)
cv2.rectangle(frm, (x1, y1), (x2, y2), (0, 0, 255), 2)
return frm
while True:
t = cv2.getTickCount()
t_count += 1.0
# Capture frame-by-frame
ret, frame = video_capture.read()
cv2.imshow('Video', get_faces(frame))
# Calculate and print the frame rate every 10 frames.
t_sum += cv2.getTickFrequency() / (cv2.getTickCount() - t)
if t_count > 9:
print(t_sum / t_count)
t_count = 0.0
t_sum = 0.0
# OpenCV won't display anything until it hits the waitkey() function.
# Press q to exit the program
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When finished release the capture and window
video_capture.release()
cv2.destroyAllWindows()