-
Notifications
You must be signed in to change notification settings - Fork 9
/
noone_video.py
178 lines (140 loc) · 6.49 KB
/
noone_video.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from deep_face_detector import DeepFaceDetector
import cv2
import argparse
import time
# haarcascade file from https://github.com/opencv/opencv/tree/master/data/haarcascades
default_haar_file = 'model/haarcascade_frontalface_default.xml'
# dnn model from https://github.com/opencv/opencv_extra/tree/master/testdata/dnn
default_proto_file = 'model/opencv_face_detector.prototxt'
default_model_file = 'model/res10_300x300_ssd_iter_140000.caffemodel'
parser = argparse.ArgumentParser(description="Deep face detection!")
parser.add_argument('cam_number', type=int, nargs='?', default=0,
help='Index number of the camera')
parser.add_argument('--file', type=str,
help="Video file path")
parser.add_argument('--out', type=str,
help="Output video path")
parser.add_argument('--proto_file', type=str, default=default_proto_file,
help="DNN proto file path")
parser.add_argument('--model_file', type=str, default=default_model_file,
help="DNN model file path")
parser.add_argument('--haar_file', type=str, default=default_haar_file,
help="Haar cascade xml file path")
parser.add_argument('--vertical', type=int, default=0,
help='0 : horizontal video(default), 1 : vertical video')
parser.add_argument('--show', type=int, default=1,
help="0 : Hide figure, 1 : show figure")
parser.add_argument('--debug_rect', type=int, default=0,
help='Show rectangular around the detected faces. Default : 0')
parser.add_argument('--debug_fill_color', type=int, default=0,
help='Fill colors on the candidate area of the faces, Default : 0')
parser.add_argument('--debug_text', type=int, default=0,
help='Show text information of the detectors, Default : 0')
parser.add_argument('--detector_dnn_nocrop', type=int, default=0,
help='Use DNN without crop. Default : 0')
parser.add_argument('--detector_dnn_crop', type=int, default=0,
help='Use DNN with crop. Default : 0')
parser.add_argument('--detector_dnn_scan', type=int, default=0,
help='Use DNN while scanning the frame. Default : 0')
parser.add_argument('--detector_dnn_crop_scan', type=int, default=1,
help='Use DNN while scanning the cropped frame. Default : 1')
parser.add_argument('--detector_haar', type=int, default=0,
help='Use Haar detector. Default : 0')
parser.add_argument('--detector_combined', type=int, default=0,
help='Use combined detection result. Default : 0')
parser.add_argument('--blur_faces', type=int, default=1,
help='Blur detected faces')
parser.add_argument('--reduce_scale', type=float, default=2,
help='Reduce scale ratio. ex) 2 = half size of the input. Default : 2')
parser.add_argument('--verbose', type=int, default=0,
help='Show current progress and remaining time')
parser.add_argument('--blur_level', type=int, default=100,
help='Blurriness of the detected face')
args = parser.parse_args()
if args.file is None:
cap = cv2.VideoCapture(args.cam_number)
else:
cap = cv2.VideoCapture(args.file)
frame_size = (int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)), int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)))
detector = DeepFaceDetector(args.proto_file, args.model_file, frame_size=frame_size, haar_file=args.haar_file, is_vertical=args.vertical == 1)
detector_param = [
args.detector_dnn_nocrop == 1,
args.detector_dnn_crop == 1,
args.detector_dnn_scan == 1,
args.detector_dnn_crop_scan == 1,
args.detector_haar == 1,
args.detector_combined == 1,
]
detector.detector_param = detector_param
if args.out is not None:
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
if args.vertical == 1:
out_size = (int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) // args.reduce_scale), int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) // args.reduce_scale))
else:
out_size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) // args.reduce_scale), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) // args.reduce_scale))
out = cv2.VideoWriter(args.out,
fourcc,
cap.get(cv2.CAP_PROP_FPS),
out_size
)
is_draw_rect = args.debug_rect == 1
is_fill_color = args.debug_fill_color == 1
is_text_on = args.debug_text == 1
is_blur_faces = args.blur_faces == 1
total_frame = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
f_count = 0
e_time = None
s_time = None
while cap.isOpened():
if args.verbose > 0 and e_time is not None:
ittime = (e_time - s_time) * (total_frame-f_count)
hour = int(ittime / 60.0 / 60.0)
minute = int((ittime / 60.0) - (hour*60))
second = int(ittime % 60.0)
print("Progress %d/%d(%.2f%%), Estimated time : %02d:%02d:%02d" %
(f_count, total_frame, (f_count/total_frame)*100, hour, minute, second))
s_time = time.time()
ret, frame = cap.read()
if frame is None:
break
if args.vertical == 1:
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
faces, face_maps, face_points = detector.find_face(frame)
if is_draw_rect:
detector.draw_square_on_faces(frame)
if is_blur_faces:
detector.blur_faces(frame, blur_level=args.blur_level)
if args.reduce_scale == 1:
resized_img = frame
else:
resized_img = cv2.resize(frame, (int(frame.shape[1] // args.reduce_scale), int(frame.shape[0] // args.reduce_scale)))
if is_fill_color:
detector.fill_color_on_faces(resized_img)
if is_text_on:
detector.draw_classifier_list_text(resized_img)
if args.out is not None:
out.write(resized_img)
if args.show == 1:
cv2.imshow('frame', resized_img)
key = cv2.waitKey(1)
if key > 0:
if key == ord('q'):
break
for i in range(6):
if key == ord(str(i+1)):
detector.detector_param[i] = not detector.detector_param[i]
if key == ord('r'):
is_draw_rect = not is_draw_rect
elif key == ord('f'):
is_fill_color = not is_fill_color
elif key == ord('t'):
is_text_on = not is_text_on
elif key == ord('b'):
is_blur_faces = not is_blur_faces
e_time = time.time()
f_count += 1
cap.release()
if args.out is not None:
out.release()
if args.show == 1:
cv2.destroyAllWindows()