-
Notifications
You must be signed in to change notification settings - Fork 27
/
blurrer.py
231 lines (205 loc) · 8.72 KB
/
blurrer.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
import os
import subprocess
from shutil import which
from tqdm import tqdm
import cv2
import imageio
import numpy as np
import torch
from src.box import Box
class VideoBlurrer:
def __init__(self, weights_name, parameters=None):
"""
Constructor
:param weights_name: file name of the weights to be used
:param parameters: all relevant paremeters for the blurring process
"""
self.parameters = parameters
self.detections = []
weights_path = os.path.join(
os.path.dirname(os.path.dirname(__file__)),
"weights",
f"{weights_name}.pt".replace(".pt.pt", ".pt"),
)
self.detector = setup_detector(weights_path)
print("Worker created")
def apply_blur(self, frame: np.array, new_detections: list):
"""
Apply Gaussian blur to regions of interests
:param frame: input image
:param new_detections: list of newly detected faces and plates
:return: processed image
"""
# gather inputs from self.parameters
blur_size = self.parameters["blur_size"]
blur_memory = self.parameters["blur_memory"]
roi_multi = self.parameters["roi_multi"]
no_faces = self.parameters["no_faces"]
# gather and process all currently relevant detections
self.detections = [
[x[0], x[1] + 1] for x in self.detections if x[1] <= blur_memory
] # throw out outdated detections, increase age by 1
for detection in new_detections:
if no_faces and detection.kind == "face":
continue
scaled_detection = detection.scale(frame.shape, roi_multi)
self.detections.append([scaled_detection, 0])
# prepare copy and mask
temp = frame.copy()
mask = np.full((frame.shape[0], frame.shape[1], 1), 0, dtype=np.uint8)
for detection in [x[0] for x in self.detections]:
# two-fold blurring: softer blur on the edge of the box to look smoother and less abrupt
outer_box = detection
inner_box = detection.scale(frame.shape, 0.8)
if detection.kind == "plate":
# blur in-place on frame
frame[outer_box.coords_as_slices()] = cv2.blur(
frame[outer_box.coords_as_slices()], (blur_size, blur_size)
)
frame[inner_box.coords_as_slices()] = cv2.blur(
frame[inner_box.coords_as_slices()], (blur_size * 2 + 1, blur_size * 2 + 1)
)
elif detection.kind == "face":
center, axes = detection.ellipse_coordinates()
# blur rectangle around face
temp[outer_box.coords_as_slices()] = cv2.blur(
temp[outer_box.coords_as_slices()], (blur_size * 2 + 1, blur_size * 2 + 1)
)
# add ellipse to mask
cv2.ellipse(mask, center, axes, 0, 0, 360, (255, 255, 255), -1)
else:
raise ValueError(f"Detection kind not supported: {detection.kind}")
# apply mask to blur faces too
mask_inverted = cv2.bitwise_not(mask)
background = cv2.bitwise_and(frame, frame, mask=mask_inverted)
blurred = cv2.bitwise_and(temp, temp, mask=mask)
return cv2.add(background, blurred)
def detect_identifiable_information(self, images: list):
"""
Run plate and face detection on an input image
:param images: input images
:return: detected faces and plates
"""
scale = self.parameters["inference_size"]
results_list = self.detector(images, size=scale).xyxy
return [
[
Box(
det[0],
det[1],
det[2],
det[3],
det[4],
"plate" if det[5].item() == 0 else "face",
)
for det in tensor
]
for tensor in results_list
]
def blur_video(self):
"""
Write a copy of the input video stripped of identifiable information, i.e. faces and license plates
"""
# gather inputs from self.parameters
input_path = self.parameters["input_path"]
temp_output = f"{os.path.splitext(self.parameters['output_path'])[0]}_copy{os.path.splitext(self.parameters['output_path'])[1]}"
output_path = self.parameters["output_path"]
threshold = self.parameters["threshold"]
quality = self.parameters["quality"]
batch_size = self.parameters["batch_size"]
# customize detector
self.detector.conf = threshold
# open video file
with imageio.get_reader(input_path) as reader:
# get the height and width of each frame for future debug outputs on frame
meta = reader.get_meta_data()
fps = meta["fps"]
duration = meta["duration"]
length = int(duration * fps)
audio_present = "audio_codec" in meta
# save the video to a file
with imageio.get_writer(
temp_output, codec="libx264", fps=fps, quality=quality
) as writer:
with tqdm(
total=length, desc="Processing video", unit="frames", dynamic_ncols=True
) as progress_bar:
buffer = []
for frame_read in reader:
rgb_frame = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
if len(buffer) < batch_size:
buffer.append(rgb_frame)
else:
# buffer is full - detect information for all images in buffer
new_detections = self.detect_identifiable_information(buffer)
for frame, detections in zip(buffer, new_detections):
frame = self.apply_blur(frame, detections)
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
writer.append_data(frame_rgb)
progress_bar.update(len(buffer))
buffer = [rgb_frame]
# Detect information for the rest of the buffer
new_detections = self.detect_identifiable_information(buffer)
for frame, detections in zip(buffer, new_detections):
frame = self.apply_blur(frame, detections)
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
writer.append_data(frame_rgb)
progress_bar.update(len(buffer))
# copy over audio stream from original video to edited video
if is_installed("ffmpeg"):
ffmpeg_exe = "ffmpeg"
else:
ffmpeg_exe = os.getenv("FFMPEG_BINARY")
if not ffmpeg_exe:
print(
"FFMPEG could not be found! Please make sure the ffmpeg.exe is available under the envirnment variable 'FFMPEG_BINARY'."
)
return
if audio_present:
subprocess.run(
[
ffmpeg_exe,
"-y",
"-i",
temp_output,
"-i",
input_path,
"-c",
"copy",
"-map",
"0:0",
"-map",
"1:1",
"-shortest",
output_path,
],
stdout=subprocess.DEVNULL,
)
# delete temporary output that had no audio track
try:
os.remove(temp_output)
except Exception as e:
self.alert.emit(
f"Could not delete temporary, muted video. Maybe another process (like a cloud storage service or antivirus) is using it already.\n{str(e)}"
)
else:
os.rename(temp_output, output_path)
def setup_detector(weights_path: str):
"""
Load YOLOv5 detector from torch hub and update the detector with this repo's weights
:param weights_path: path to .pt file with this repo's weights
:return: initialized yolov5 detector
"""
model = torch.hub.load("ultralytics/yolov5", "custom", weights_path, _verbose=False)
if torch.cuda.is_available():
print(f"Using {torch.cuda.get_device_name(torch.cuda.current_device())}.")
model.cuda()
torch.backends.cudnn.benchmark = True
else:
print("Using CPU.")
return model
def is_installed(name):
"""
Check whether an executable is available
"""
return which(name) is not None