added blur filtering
Browse files- app.py +16 -21
- utils/blur_filter.py +53 -0
app.py
CHANGED
@@ -26,6 +26,8 @@ import numpy as np
|
|
26 |
from PIL import Image
|
27 |
import boto3
|
28 |
|
|
|
|
|
29 |
import os
|
30 |
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
|
31 |
os.environ['OMP_NUM_THREADS'] = '4'
|
@@ -217,29 +219,20 @@ def process_video(vid_path, dsdg_thresh):
|
|
217 |
cap = cv.VideoCapture(vid_path)
|
218 |
input_width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))
|
219 |
input_height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
|
220 |
-
fourcc = cv.VideoWriter_fourcc(*'mp4v')
|
221 |
-
output_vid_path = 'output_dsdg.mp4'
|
222 |
|
223 |
-
|
224 |
-
|
225 |
inference_images = []
|
226 |
inference_bboxes = []
|
227 |
inference_depths = []
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
if bbox and (depth_img is not None):
|
237 |
-
inference_images.append(img)
|
238 |
-
inference_bboxes.append(bbox)
|
239 |
-
inference_depths.append(depth_img)
|
240 |
-
all_frames.append(img)
|
241 |
-
frame_counter += 1
|
242 |
-
cap.release()
|
243 |
if not inference_images:
|
244 |
return vid_path, {'Not supported right now': 0}, -1, vid_path, 'Faces too small or not found', -1
|
245 |
|
@@ -255,9 +248,11 @@ def process_video(vid_path, dsdg_thresh):
|
|
255 |
text = f'{cls_dsdg} {w}*{h}'
|
256 |
cv.rectangle(img, (x, y), (x2, y2), color_dsdg, 2)
|
257 |
cv.putText(img, text, (x, y2 + 30), cv.FONT_HERSHEY_COMPLEX, 1, color_dsdg)
|
258 |
-
|
|
|
|
|
259 |
out_dsdg = cv.VideoWriter(output_vid_path, fourcc, 6.0, (input_width, input_height))
|
260 |
-
for img in
|
261 |
# Write the DSDG frame to the output video
|
262 |
out_dsdg.write(img)
|
263 |
out_dsdg.release()
|
|
|
26 |
from PIL import Image
|
27 |
import boto3
|
28 |
|
29 |
+
from utils.blur_filter import filter_frames
|
30 |
+
|
31 |
import os
|
32 |
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
|
33 |
os.environ['OMP_NUM_THREADS'] = '4'
|
|
|
219 |
cap = cv.VideoCapture(vid_path)
|
220 |
input_width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))
|
221 |
input_height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
|
|
|
|
|
222 |
|
223 |
+
most_focused = filter_frames(cap)
|
224 |
+
|
225 |
inference_images = []
|
226 |
inference_bboxes = []
|
227 |
inference_depths = []
|
228 |
+
for frame in most_focused:
|
229 |
+
# Run inference on the current frame
|
230 |
+
img, bbox, depth_img = analyze_face(frame)
|
231 |
+
if bbox and (depth_img is not None):
|
232 |
+
inference_images.append(img)
|
233 |
+
inference_bboxes.append(bbox)
|
234 |
+
inference_depths.append(depth_img)
|
235 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
236 |
if not inference_images:
|
237 |
return vid_path, {'Not supported right now': 0}, -1, vid_path, 'Faces too small or not found', -1
|
238 |
|
|
|
248 |
text = f'{cls_dsdg} {w}*{h}'
|
249 |
cv.rectangle(img, (x, y), (x2, y2), color_dsdg, 2)
|
250 |
cv.putText(img, text, (x, y2 + 30), cv.FONT_HERSHEY_COMPLEX, 1, color_dsdg)
|
251 |
+
|
252 |
+
fourcc = cv.VideoWriter_fourcc(*'mp4v')
|
253 |
+
output_vid_path = 'output_dsdg.mp4'
|
254 |
out_dsdg = cv.VideoWriter(output_vid_path, fourcc, 6.0, (input_width, input_height))
|
255 |
+
for img in most_focused:
|
256 |
# Write the DSDG frame to the output video
|
257 |
out_dsdg.write(img)
|
258 |
out_dsdg.release()
|
utils/blur_filter.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from collections import deque
|
2 |
+
import cv2 as cv
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
|
6 |
+
def estimate_focus_measure(frame):
|
7 |
+
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
|
8 |
+
laplacian = cv.Laplacian(gray, cv.CV_64F)
|
9 |
+
return laplacian.var()
|
10 |
+
|
11 |
+
|
12 |
+
def update_threshold(blur_history):
|
13 |
+
return np.percentile(blur_history, 50)
|
14 |
+
|
15 |
+
|
16 |
+
def select_most_focused(frame_history, threshold, select_n=5):
|
17 |
+
sorted_frames = sorted(frame_history, key=lambda x: x[1], reverse=True)
|
18 |
+
# Select least blurry frames from the last incomplete second
|
19 |
+
selected = [frame for frame, fm in sorted_frames[:select_n] if fm > threshold]
|
20 |
+
return selected
|
21 |
+
|
22 |
+
|
23 |
+
def filter_frames(cap):
|
24 |
+
fps = int(cap.get(cv.CAP_PROP_FPS))
|
25 |
+
frame_history = deque(maxlen=fps)
|
26 |
+
blur_history = []
|
27 |
+
most_focused_frames = []
|
28 |
+
|
29 |
+
if cap.isOpened():
|
30 |
+
ret, frame = cap.read()
|
31 |
+
counter = 1
|
32 |
+
second = 0
|
33 |
+
|
34 |
+
while ret:
|
35 |
+
fm = estimate_focus_measure(frame)
|
36 |
+
frame_history.append([frame, fm])
|
37 |
+
blur_history.append(fm)
|
38 |
+
|
39 |
+
if counter >= fps:
|
40 |
+
second += 1
|
41 |
+
threshold = update_threshold(blur_history)
|
42 |
+
if counter % fps == 0:
|
43 |
+
most_focused_frames += select_most_focused(frame_history, threshold)
|
44 |
+
frame_history.clear()
|
45 |
+
ret, frame = cap.read()
|
46 |
+
counter += 1
|
47 |
+
|
48 |
+
if frame_history:
|
49 |
+
threshold = update_threshold(blur_history)
|
50 |
+
most_focused_frames += select_most_focused(frame_history, threshold)
|
51 |
+
|
52 |
+
cap.release()
|
53 |
+
return most_focused_frames
|