-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathopencv_haar.py
53 lines (38 loc) · 1.33 KB
/
opencv_haar.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
import cv2, time
# load model
detector = cv2.CascadeClassifier('models/haarcascade_frontalface_default.xml')
# initialize video source, default 0 (webcam)
video_path = 'videos/RollerCoasters.mp4'
cap = cv2.VideoCapture(video_path)
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
out = cv2.VideoWriter('%s_output_opencv_haar.mp4' % (video_path.split('.')[0]), fourcc, cap.get(cv2.CAP_PROP_FPS), (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))))
frame_count, tt = 0, 0
while cap.isOpened():
ret, img = cap.read()
if not ret:
break
frame_count += 1
start_time = time.time()
# prepare input
result_img = img.copy()
gray = cv2.cvtColor(result_img, cv2.COLOR_BGR2GRAY)
# inference, find faces
detections = detector.detectMultiScale(gray)
# postprocessing
for (x1, y1, w, h) in detections:
x2 = x1 + w
y2 = y1 + h
# draw rects
cv2.rectangle(result_img, (x1, y1), (x2, y2), (255, 255, 255), 2, cv2.LINE_AA)
# inference time
tt += time.time() - start_time
fps = frame_count / tt
cv2.putText(result_img, 'FPS(haar): %.2f' % (fps), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
# visualize
cv2.imshow('result', result_img)
if cv2.waitKey(1) == ord('q'):
break
out.write(result_img)
cap.release()
out.release()
cv2.destroyAllWindows()