forked from halfzm/ProPainter-Webui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
413 lines (321 loc) · 16.2 KB
/
app.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import os
import subprocess
import sys
import cv2
import torch
import numpy as np
import gradio as gr
from util import resolve_relative_path
from SegTracker import SegTracker
from model_args import segtracker_args, sam_args, aot_args
from seg_track_anything import aot_model2ckpt, tracking_objects_in_video
def clean():
return None, None, None, None, None, None, [[], []]
def get_click_prompt(click_stack, point):
click_stack[0].append(point["coord"])
click_stack[1].append(point["mode"])
prompt = {
"points_coord": click_stack[0],
"points_mode": click_stack[1],
"multimask": "True",
}
return prompt
def get_meta_from_video(input_video):
if input_video is None:
return None, None, None, ""
print("Get the meta information of the input video")
cap = cv2.VideoCapture(input_video)
_, first_frame = cap.read()
cap.release()
first_frame = cv2.cvtColor(first_frame, cv2.COLOR_BGR2RGB)
required_vram, current_gpu_memory = check_vram_requirements(input_video)
if required_vram > current_gpu_memory:
print(f"WARNING: Insufficient VRAM. the video is either too long or has too high a resolution. Try using a video with a smaller size.")
return first_frame, first_frame, first_frame, ""
def check_vram_requirements(input_video):
cap = cv2.VideoCapture(input_video)
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
cap.release()
current_free_vram = round(torch.cuda.mem_get_info()[0] / (1024 ** 3), 1)
pixels = np.array([1280*720, 720*480, 640*480, 320*240, 1280*720, 720*480, 640*480, 320*240, 640*368, 512*512, 1280*720])
frames = np.array([50, 50, 50, 50, 80, 80, 80, 80, 750, 120, 750])
vram_used = np.array([19, 7, 6, 2, 25, 8, 7, 3, 30, 7, 25])
X = np.stack((pixels, frames), axis=-1)
X_transformed = np.hstack((X, X**2, X**3)) # polynomial transformation
coefficients, _, _, _ = np.linalg.lstsq(X_transformed, vram_used, rcond=None)
x = np.array([frame_width * frame_height, frame_count])
x_transformed = np.hstack((x, x**2, x**3))
required_vram = round(np.dot(coefficients, x_transformed), 1)
print(f"VRAM Est. {required_vram}/{current_free_vram} GB | Px: {frame_width}*{frame_height} - Frames: {frame_count}")
return required_vram, current_free_vram
def SegTracker_add_first_frame(Seg_Tracker, origin_frame, predicted_mask):
with torch.cuda.amp.autocast():
# Reset the first frame's mask
frame_idx = 0
Seg_Tracker.restart_tracker()
Seg_Tracker.add_reference(origin_frame, predicted_mask, frame_idx)
Seg_Tracker.first_frame_mask = predicted_mask
return Seg_Tracker
def init_SegTracker(aot_model, long_term_mem, max_len_long_term, sam_gap, max_obj_num, points_per_side, origin_frame):
if origin_frame is None:
return None, origin_frame, [[], []], ""
# reset aot args
aot_args["model"] = aot_model
aot_args["model_path"] = aot_model2ckpt[aot_model]
aot_args["long_term_mem_gap"] = long_term_mem
aot_args["max_len_long_term"] = max_len_long_term
# reset sam args
segtracker_args["sam_gap"] = sam_gap
segtracker_args["max_obj_num"] = max_obj_num
sam_args["generator_args"]["points_per_side"] = points_per_side
Seg_Tracker = SegTracker(segtracker_args, sam_args, aot_args)
Seg_Tracker.restart_tracker()
return Seg_Tracker, origin_frame, [[], []], ""
def init_SegTracker_Stroke(aot_model, long_term_mem, max_len_long_term, sam_gap, max_obj_num, points_per_side,
origin_frame):
if origin_frame is None:
return None, origin_frame, [[], []], origin_frame
# reset aot args
aot_args["model"] = aot_model
aot_args["model_path"] = aot_model2ckpt[aot_model]
aot_args["long_term_mem_gap"] = long_term_mem
aot_args["max_len_long_term"] = max_len_long_term
# reset sam args
segtracker_args["sam_gap"] = sam_gap
segtracker_args["max_obj_num"] = max_obj_num
sam_args["generator_args"]["points_per_side"] = points_per_side
Seg_Tracker = SegTracker(segtracker_args, sam_args, aot_args)
Seg_Tracker.restart_tracker()
return Seg_Tracker, origin_frame, [[], []], origin_frame
def undo_click_stack_and_refine_seg(Seg_Tracker, origin_frame, click_stack, aot_model, long_term_mem, max_len_long_term,
sam_gap, max_obj_num, points_per_side):
if Seg_Tracker is None:
return Seg_Tracker, origin_frame, [[], []]
print("Quash!")
if len(click_stack[0]) > 0:
click_stack[0] = click_stack[0][:-1]
click_stack[1] = click_stack[1][:-1]
if len(click_stack[0]) > 0:
prompt = {
"points_coord": click_stack[0],
"points_mode": click_stack[1],
"multimask": "True",
}
masked_frame = seg_acc_click(Seg_Tracker, prompt, origin_frame)
return Seg_Tracker, masked_frame, click_stack
else:
return Seg_Tracker, origin_frame, [[], []]
def seg_acc_click(Seg_Tracker, prompt, origin_frame):
# seg acc to click
predicted_mask, masked_frame = Seg_Tracker.seg_acc_click(
origin_frame=origin_frame,
coords=np.array(prompt["points_coord"]),
modes=np.array(prompt["points_mode"]),
multimask=prompt["multimask"],
)
Seg_Tracker = SegTracker_add_first_frame(Seg_Tracker, origin_frame, predicted_mask)
return masked_frame
def sam_click(Seg_Tracker, origin_frame, point_mode, click_stack, aot_model, long_term_mem, max_len_long_term, sam_gap,
max_obj_num, points_per_side, evt: gr.SelectData):
"""
Args:
origin_frame: nd.array
click_stack: [[coordinate], [point_mode]]
"""
print("Click")
if point_mode == "Positive":
point = {"coord": [evt.index[0], evt.index[1]], "mode": 1}
else:
point = {"coord": [evt.index[0], evt.index[1]], "mode": 0}
if Seg_Tracker is None:
Seg_Tracker, _, _, _ = init_SegTracker(aot_model, long_term_mem, max_len_long_term, sam_gap, max_obj_num,
points_per_side, origin_frame)
# get click prompts for sam to predict mask
click_prompt = get_click_prompt(click_stack, point)
# Refine acc to prompt
masked_frame = seg_acc_click(Seg_Tracker, click_prompt, origin_frame)
return Seg_Tracker, masked_frame, click_stack
def add_new_object(Seg_Tracker):
prev_mask = Seg_Tracker.first_frame_mask
Seg_Tracker.update_origin_merged_mask(prev_mask)
Seg_Tracker.curr_idx += 1
print("Start preparing to add new objects!")
return Seg_Tracker, [[], []]
def tracking_objects(Seg_Tracker, input_video, input_img_seq=None, frame_num=0):
print("Start tracking")
return tracking_objects_in_video(Seg_Tracker, input_video, input_img_seq, frame_num)
def remove_watermark(input_video):
print("Start removing the watermark")
print('cwd', os.getcwd())
os.chdir('./ProPainter')
print('cwd', os.getcwd())
inference = resolve_relative_path('./ProPainter/inference_propainter.py')
video_name = os.path.basename(input_video).rsplit('.', 1)[0].rsplit('_', 1)[0]
output_base_path = resolve_relative_path('./output/')
output_path = f'{output_base_path}/{video_name}/'
mask = f'{output_path}/{video_name}_masks/'
command = f'python "{inference}" --video "{input_video}" --mask "{mask}" --output "{output_path}" --fp16 --subvideo_length 50'
print(command)
while len(os.listdir(mask)) > int(cv2.VideoCapture(input_video).get(cv2.CAP_PROP_FRAME_COUNT)): os.remove(os.path.join(mask, sorted(os.listdir(mask))[-1]))
result = subprocess.run(command, shell=True)
if result.returncode != 0:
error_message = result.stderr.decode('utf-8', 'ignore')
print(f"Error {error_message}")
else:
print("Success")
file_name = input_video.split('\\')[-1].split('.')[0]
print(file_name)
os.chdir(resolve_relative_path('./'))
print('cwd', os.getcwd())
return output_path + '/' + file_name + '/' + 'inpaint_out' + '.mp4'
# return input_video
def seg_track_app():
##########################################################
###################### Front-end ########################
##########################################################
app = gr.Blocks()
with app:
gr.Markdown('''
<div style="text-align:center;">
<span style="font-size:3em; font-weight:bold;">ProPainter Webui</span>
</div>
''')
gr.Markdown('## Step 1: Generate a mask')
click_stack = gr.State([[], []]) # Storage clicks status
origin_frame = gr.State(None)
Seg_Tracker = gr.State(None)
aot_model = gr.State(None)
sam_gap = gr.State(None)
points_per_side = gr.State(None)
max_obj_num = gr.State(None)
with gr.Row():
# video input
input_video = gr.Video(label='Pending video', height=400)
input_first_frame = gr.Image(label='Select the mask object', interactive=True, height=400)
with gr.Row():
with gr.Column():
tab_click = gr.Tab(label="Click")
with tab_click:
with gr.Row():
point_mode = gr.Radio(choices=["Positive", "Negative"],
value="Positive",
label="Point Prompt",
interactive=True)
# args for modify and tracking
click_undo_but = gr.Button(value="Cancel", interactive=True)
with gr.Column():
with gr.Tab(label="SegTracker Args", visible=False):
# args for tracking in video do segment-everthing
points_per_side = gr.Slider(label="points_per_side",
minimum=1,
step=1,
maximum=100,
value=16,
interactive=True,
visible=False)
sam_gap = gr.Slider(label='sam_gap',
minimum=1,
step=1,
maximum=9999,
value=9999,
interactive=True,
visible=False)
max_obj_num = gr.Slider(label='max_obj_num',
minimum=50,
step=1,
maximum=300,
value=255,
interactive=True,
visible=False)
with gr.Accordion("aot advanced options", open=False, visible=False):
aot_model = gr.Dropdown(label="aot_model",
choices=["deaotb", "deaotl", "r50_deaotl"],
value="r50_deaotl",
interactive=True,
visible=False)
long_term_mem = gr.Slider(label="long term memory gap",
minimum=1,
maximum=9999,
value=9999,
step=1,
visible=False)
max_len_long_term = gr.Slider(label="max len of long term memory",
minimum=1,
maximum=9999,
value=9999,
step=1,
visible=False)
with gr.Row():
new_object_button = gr.Button(value="Add new object", interactive=True)
reset_button = gr.Button(
value="Reset",
interactive=True,
)
track_for_video = gr.Button(
value="Start tracking",
interactive=True,
)
output_video = gr.Video(label='Output video', height=400)
gr.Markdown('## Step 2: Remove the masked object')
start_remove_watermark = gr.Button(value="Remove the object", interactive=True)
final_video = gr.Video(label='After removing the video', height=400)
##########################################################
###################### back-end #########################
##########################################################
# listen to the input_video to get the first frame of video
input_video.change(fn=get_meta_from_video, inputs=[input_video], outputs=[input_first_frame, origin_frame])
# ------------------- Interactive component -----------------
# Interactively modify the mask acc click
input_first_frame.select(fn=sam_click,
inputs=[
Seg_Tracker,
origin_frame,
point_mode,
click_stack,
aot_model,
long_term_mem,
max_len_long_term,
sam_gap,
max_obj_num,
points_per_side,
],
outputs=[Seg_Tracker, input_first_frame, click_stack])
# Add new object
new_object_button.click(fn=add_new_object, inputs=[Seg_Tracker], outputs=[Seg_Tracker, click_stack])
# Track object in video
track_for_video.click(fn=tracking_objects, inputs=[
Seg_Tracker,
input_video,
], outputs=[output_video])
# Remove watermark in video
start_remove_watermark.click(fn=remove_watermark, inputs=[output_video], outputs=[final_video])
# ----------------- Reset and Undo ---------------------------
# Reset
reset_button.click(
fn=init_SegTracker,
inputs=[aot_model, long_term_mem, max_len_long_term, sam_gap, max_obj_num, points_per_side, origin_frame],
outputs=[Seg_Tracker, input_first_frame, click_stack],
queue=False,
show_progress=False)
# Undo click
click_undo_but.click(fn=undo_click_stack_and_refine_seg,
inputs=[
Seg_Tracker,
origin_frame,
click_stack,
aot_model,
long_term_mem,
max_len_long_term,
sam_gap,
max_obj_num,
points_per_side,
],
outputs=[Seg_Tracker, input_first_frame, click_stack])
app.queue(concurrency_count=1)
app.launch(debug=True, share=True)
if __name__ == "__main__":
ffmpeg_exe = resolve_relative_path('env/Library/bin/')
os.environ['PATH'] = ffmpeg_exe + ';' + os.environ['PATH']
seg_track_app()