-
Notifications
You must be signed in to change notification settings - Fork 80
/
deface.py
executable file
·363 lines (312 loc) · 12.1 KB
/
deface.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env python3
import argparse
import glob
import json
import mimetypes
import os
import sys
from typing import Dict, Tuple
import tqdm
import skimage.draw
import numpy as np
import imageio
import imageio.plugins.ffmpeg
import cv2
from deface import __version__
from deface.centerface import CenterFace
# TODO: Optionally preserve audio track?
def scale_bb(x1, y1, x2, y2, mask_scale=1.0):
s = mask_scale - 1.0
h, w = y2 - y1, x2 - x1
y1 -= h * s
y2 += h * s
x1 -= w * s
x2 += w * s
return np.round([x1, y1, x2, y2]).astype(int)
def draw_det(
frame, score, det_idx, x1, y1, x2, y2,
replacewith: str = 'blur',
ellipse: bool = True,
draw_scores: bool = False,
ovcolor: Tuple[int] = (0, 0, 0)
):
if replacewith == 'solid':
cv2.rectangle(frame, (x1, y1), (x2, y2), ovcolor, -1)
elif replacewith == 'blur':
bf = 2 # blur factor (number of pixels in each dimension that the face will be reduced to)
blurred_box = cv2.blur(
frame[y1:y2, x1:x2],
(abs(x2 - x1) // bf, abs(y2 - y1) // bf)
)
if ellipse:
roibox = frame[y1:y2, x1:x2]
# Get y and x coordinate lists of the "bounding ellipse"
ey, ex = skimage.draw.ellipse((y2 - y1) // 2, (x2 - x1) // 2, (y2 - y1) // 2, (x2 - x1) // 2)
roibox[ey, ex] = blurred_box[ey, ex]
frame[y1:y2, x1:x2] = roibox
else:
frame[y1:y2, x1:x2] = blurred_box
elif replacewith == 'none':
pass
if draw_scores:
cv2.putText(
frame, f'{score:.2f}', (x1 + 0, y1 - 20),
cv2.FONT_HERSHEY_DUPLEX, 0.5, (0, 255, 0)
)
def anonymize_frame(
dets, frame, mask_scale,
replacewith, ellipse, draw_scores
):
for i, det in enumerate(dets):
boxes, score = det[:4], det[4]
x1, y1, x2, y2 = boxes.astype(int)
x1, y1, x2, y2 = scale_bb(x1, y1, x2, y2, mask_scale)
# Clip bb coordinates to valid frame region
y1, y2 = max(0, y1), min(frame.shape[0] - 1, y2)
x1, x2 = max(0, x1), min(frame.shape[1] - 1, x2)
draw_det(
frame, score, i, x1, y1, x2, y2,
replacewith=replacewith,
ellipse=ellipse,
draw_scores=draw_scores,
)
def cam_read_iter(reader):
while True:
yield reader.get_next_data()
def video_detect(
ipath: str,
opath: str,
centerface: str,
threshold: float,
enable_preview: bool,
cam: bool,
nested: bool,
replacewith: str,
mask_scale: float,
ellipse: bool,
draw_scores: bool,
ffmpeg_config: Dict[str, str]
):
try:
reader: imageio.plugins.ffmpeg.FfmpegFormat.Reader = imageio.get_reader(ipath)
meta = reader.get_meta_data()
_ = meta['size']
except:
if cam:
print(f'Could not find video device {ipath}. Please set a valid input.')
else:
print(f'Could not open file {ipath} as a video file with imageio. Skipping file...')
return
if cam:
nframes = None
read_iter = cam_read_iter(reader)
else:
read_iter = reader.iter_data()
nframes = reader.count_frames()
if nested:
bar = tqdm.tqdm(dynamic_ncols=True, total=nframes, position=1, leave=True)
else:
bar = tqdm.tqdm(dynamic_ncols=True, total=nframes)
if opath is not None:
writer: imageio.plugins.ffmpeg.FfmpegFormat.Writer = imageio.get_writer(
opath, format='FFMPEG', mode='I', fps=meta['fps'], **ffmpeg_config
)
for frame in read_iter:
# Perform network inference, get bb dets but discard landmark predictions
dets, _ = centerface(frame, threshold=threshold)
anonymize_frame(
dets, frame, mask_scale=mask_scale,
replacewith=replacewith, ellipse=ellipse, draw_scores=draw_scores
)
if opath is not None:
writer.append_data(frame)
if enable_preview:
cv2.imshow('Preview of anonymization results (quit by pressing Q or Escape)', frame[:, :, ::-1]) # RGB -> RGB
if cv2.waitKey(1) & 0xFF in [ord('q'), 27]: # 27 is the escape key code
cv2.destroyAllWindows()
break
bar.update()
reader.close()
if opath is not None:
writer.close()
bar.close()
def image_detect(
ipath: str,
opath: str,
centerface: str,
threshold: float,
replacewith: str,
mask_scale: float,
ellipse: bool,
draw_scores: bool,
enable_preview: bool
):
frame = imageio.imread(ipath)
# Perform network inference, get bb dets but discard landmark predictions
dets, _ = centerface(frame, threshold=threshold)
anonymize_frame(
dets, frame, mask_scale=mask_scale,
replacewith=replacewith, ellipse=ellipse, draw_scores=draw_scores
)
if enable_preview:
cv2.imshow('Preview of anonymization results (quit by pressing Q or Escape)', frame[:, :, ::-1]) # RGB -> RGB
if cv2.waitKey(0) & 0xFF in [ord('q'), 27]: # 27 is the escape key code
cv2.destroyAllWindows()
imageio.imsave(opath, frame)
# print(f'Output saved to {opath}')
def get_file_type(path):
if path.startswith('<video'):
return 'cam'
if not os.path.isfile(path):
return 'notfound'
mime = mimetypes.guess_type(path)[0]
if mime is None:
return None
if mime.startswith('video'):
return 'video'
if mime.startswith('image'):
return 'image'
return mime
def get_anonymized_image(frame,
threshold: float,
replacewith: str,
mask_scale: float,
ellipse: bool,
draw_scores: bool,
):
"""
Method for getting an anonymized image without CLI
returns frame
"""
centerface = CenterFace(in_shape=None, backend='auto')
dets, _ = centerface(frame, threshold=threshold)
anonymize_frame(
dets, frame, mask_scale=mask_scale,
replacewith=replacewith, ellipse=ellipse, draw_scores=draw_scores
)
return frame
def parse_cli_args():
parser = argparse.ArgumentParser(description='Video anonymization by face detection', add_help=False)
parser.add_argument(
'input', nargs='*',
help=f'File path(s) or camera device name. It is possible to pass multiple paths by separating them by spaces or by using shell expansion (e.g. `$ deface vids/*.mp4`). If a camera is installed, a live webcam demo can be started by running `$ deface cam` (which is a shortcut for `$ deface -p \'<video0>\'`.')
parser.add_argument(
'--output', '-o', default=None, metavar='O',
help='Output file name. Defaults to input path + postfix "_anonymized".')
parser.add_argument(
'--thresh', '-t', default=0.2, type=float, metavar='T',
help='Detection threshold (tune this to trade off between false positive and false negative rate). Default: 0.2.')
parser.add_argument(
'--scale', '-s', default=None, metavar='WxH',
help='Downscale images for network inference to this size (format: WxH, example: --scale 640x360).')
parser.add_argument(
'--preview', '-p', default=False, action='store_true',
help='Enable live preview GUI (can decrease performance).')
parser.add_argument(
'--boxes', default=False, action='store_true',
help='Use boxes instead of ellipse masks.')
parser.add_argument(
'--draw-scores', default=False, action='store_true',
help='Draw detection scores onto outputs.')
parser.add_argument(
'--mask-scale', default=1.3, type=float, metavar='M',
help='Scale factor for face masks, to make sure that masks cover the complete face. Default: 1.3.')
parser.add_argument(
'--replacewith', default='blur', choices=['blur', 'solid', 'none'],
help='Anonymization filter mode for face regions. "blur" applies a strong gaussian blurring, "solid" draws a solid black box and "none" does leaves the input unchanged. Default: "blur".')
parser.add_argument(
'--ffmpeg-config', default={"codec": "libx264"}, type=json.loads,
help='FFMPEG config arguments for encoding output videos. This argument is expected in JSON notation. For a list of possible options, refer to the ffmpeg-imageio docs. Default: \'{"codec": "libx264"}\'.'
) # See https://imageio.readthedocs.io/en/stable/format_ffmpeg.html#parameters-for-saving
parser.add_argument(
'--backend', default='auto', choices=['auto', 'onnxrt', 'opencv'],
help='Backend for ONNX model execution. Default: "auto" (prefer onnxrt if available).')
parser.add_argument(
'--version', action='version', version=__version__,
help='Print version number and exit.')
parser.add_argument('--help', '-h', action='help', help='Show this help message and exit.')
args = parser.parse_args()
if len(args.input) == 0:
parser.print_help()
print('\nPlease supply at least one input path.')
exit(1)
if args.input == ['cam']: # Shortcut for webcam demo with live preview
args.input = ['<video0>']
args.preview = True
return args
def main():
args = parse_cli_args()
ipaths = []
# add files in folders
for path in args.input:
if os.path.isdir(path):
for file in os.listdir(path):
ipaths.append(os.path.join(path,file))
elif os.path.isfile(path):
ipaths.append(path)
base_opath = args.output
replacewith = args.replacewith
enable_preview = args.preview
draw_scores = args.draw_scores
threshold = args.thresh
ellipse = not args.boxes
mask_scale = args.mask_scale
ffmpeg_config = args.ffmpeg_config
backend = args.backend
in_shape = args.scale
if in_shape is not None:
w, h = in_shape.split('x')
in_shape = int(w), int(h)
# TODO: scalar downscaling setting (-> in_shape), preserving aspect ratio
centerface = CenterFace(in_shape=in_shape, backend=backend)
multi_file = len(ipaths) > 1
if multi_file:
ipaths = tqdm.tqdm(ipaths, position=0, dynamic_ncols=True, desc='Batch progress')
for ipath in ipaths:
opath = base_opath
if ipath == 'cam':
ipath = '<video0>'
enable_preview = True
filetype = get_file_type(ipath)
is_cam = filetype == 'cam'
if opath is None and not is_cam:
root, ext = os.path.splitext(ipath)
opath = f'{root}_anonymized{ext}'
print(f'Input: {ipath}\nOutput: {opath}')
if opath is None and not enable_preview:
print('No output file is specified and the preview GUI is disabled. No output will be produced.')
if filetype == 'video' or is_cam:
video_detect(
ipath=ipath,
opath=opath,
centerface=centerface,
threshold=threshold,
cam=is_cam,
replacewith=replacewith,
mask_scale=mask_scale,
ellipse=ellipse,
draw_scores=draw_scores,
enable_preview=enable_preview,
nested=multi_file,
ffmpeg_config=ffmpeg_config
)
elif filetype == 'image':
image_detect(
ipath=ipath,
opath=opath,
centerface=centerface,
threshold=threshold,
replacewith=replacewith,
mask_scale=mask_scale,
ellipse=ellipse,
draw_scores=draw_scores,
enable_preview=enable_preview
)
elif filetype is None:
print(f'Can\'t determine file type of file {ipath}. Skipping...')
elif filetype == 'notfound':
print(f'File {ipath} not found. Skipping...')
else:
print(f'File {ipath} has an unknown type {filetype}. Skipping...')
if __name__ == '__main__':
main()