-
Notifications
You must be signed in to change notification settings - Fork 0
/
dl_video_tools.py
131 lines (102 loc) · 5.08 KB
/
dl_video_tools.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
from inpainting import GenerativeInpainting
from segmentation import DeepLabSegmentation
from registration import ImageRegistration
import neuralgym.neuralgym as ng
import cv2
import labels
# Just inpainting, no registration
def test1():
# TODO: Pass as parameter
config_file = "config/config.yml"
config = ng.Config(config_file)
segmentation = DeepLabSegmentation(config)
inpainting = GenerativeInpainting(config)
video_in = cv2.VideoCapture(f'data/people_walking.mp4')
frame_width = int(video_in.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(video_in.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(video_in.get(cv2.CAP_PROP_FPS))
video_out = None
count = 0
while (video_in.isOpened()):
print(f"==== Processing image {count} ====")
count += 1
ret, frame = video_in.read()
if not ret:
break
resized_image, seg_map = segmentation.run(frame)
mask = labels.mask_from_labels(seg_map, config.classes_to_remove)
if config.dilate_mask.apply:
if config.dilate_mask.type == "ellipse":
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,
(config.dilate_mask.kernel_size, config.dilate_mask.kernel_size))
mask = cv2.dilate(mask, kernel, iterations=1)
elif config.dilate_mask.type == "rect":
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,
(config.dilate_mask.kernel_size, config.dilate_mask.kernel_size))
mask = cv2.dilate(mask, kernel, iterations=1)
elif config.dilate_mask.type == "cross":
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,
(config.dilate_mask.kernel_size, config.dilate_mask.kernel_size))
mask = cv2.dilate(mask, kernel, iterations=1)
else:
raise Exception("Unsupported dilation type. Exiting...")
new_frame = inpainting.inpaint_image(resized_image, mask)
resized_new_frame = cv2.resize(new_frame, (frame_width, frame_height), interpolation=cv2.INTER_CUBIC)
if not video_out:
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
video_out = cv2.VideoWriter(f'data/wo_people_walking_no_registration.avi', fourcc, fps, (frame_width, frame_height))
video_out.write(resized_new_frame)
video_in.release()
video_out.release()
# Inpainting + registration
def test2():
# TODO: Pass as parameter
config_file = "config/config.yml"
config = ng.Config(config_file)
segmentation = DeepLabSegmentation(config)
registration = ImageRegistration(config)
inpainting = GenerativeInpainting(config)
video_in = cv2.VideoCapture(f'data/people_walking.mp4')
frame_width = int(video_in.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(video_in.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(video_in.get(cv2.CAP_PROP_FPS))
video_out = None
count = 0
while (video_in.isOpened()):
print(f"==== Processing image {count} ====")
count += 1
ret, frame = video_in.read()
if not ret:
break
resized_image, seg_map = segmentation.run(frame)
mask = labels.mask_from_labels(seg_map, config.classes_to_remove)
bg_mask = labels.mask_from_labels(seg_map, config.background_classes)
if config.dilate_mask.apply:
if config.dilate_mask.type == "ellipse":
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,
(config.dilate_mask.kernel_size, config.dilate_mask.kernel_size))
mask = cv2.dilate(mask, kernel, iterations=1)
elif config.dilate_mask.type == "rect":
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,
(config.dilate_mask.kernel_size, config.dilate_mask.kernel_size))
mask = cv2.dilate(mask, kernel, iterations=1)
elif config.dilate_mask.type == "cross":
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,
(config.dilate_mask.kernel_size, config.dilate_mask.kernel_size))
mask = cv2.dilate(mask, kernel, iterations=1)
else:
raise Exception("Unsupported dilation type. Exiting...")
new_frame = registration.updateBuffer(resized_image, mask, bg_mask)
resized_new_frame = cv2.resize(new_frame, (frame_width, frame_height), interpolation=cv2.INTER_CUBIC)
if not video_out:
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
video_out = cv2.VideoWriter(f'data/wo_people_walking_registration.avi', fourcc, fps,
(frame_width, frame_height))
video_out.write(resized_new_frame)
video_in.release()
video_out.release()
if __name__ == "__main__":
# test1()
test2()