Update app.py
Browse files
app.py
CHANGED
@@ -114,7 +114,7 @@ def create_video(frames, fps, type):
|
|
114 |
return f"video_{type}_result.mp4"
|
115 |
|
116 |
|
117 |
-
def run_grounded_sam(input_image, text_prompt, task_type, background_prompt):
|
118 |
background_type = "generated_by_text"
|
119 |
box_threshold = 0.25
|
120 |
text_threshold = 0.25
|
@@ -262,6 +262,7 @@ def run_grounded_sam(input_image, text_prompt, task_type, background_prompt):
|
|
262 |
### alpha matte
|
263 |
alpha_rgb = cv2.cvtColor(np.uint8(alpha_pred*255), cv2.COLOR_GRAY2RGB)
|
264 |
### com img with background
|
|
|
265 |
if background_type == 'real_world_sample':
|
266 |
background_img_file = os.path.join('assets/backgrounds', random.choice(background_list))
|
267 |
background_img = cv2.imread(background_img_file)
|
@@ -273,7 +274,9 @@ def run_grounded_sam(input_image, text_prompt, task_type, background_prompt):
|
|
273 |
if background_prompt is None:
|
274 |
print('Please input non-empty background prompt')
|
275 |
else:
|
276 |
-
|
|
|
|
|
277 |
background_img = np.array(background_img)
|
278 |
background_img = cv2.resize(background_img, (image_ori.shape[1], image_ori.shape[0]))
|
279 |
com_img = alpha_pred[..., None] * image_ori + (1 - alpha_pred[..., None]) * np.uint8(background_img)
|
@@ -301,15 +304,15 @@ def infer(video_in, trim_value, prompt, background_prompt):
|
|
301 |
with_matte_result_frames = []
|
302 |
|
303 |
print("set stop frames to: " + str(n_frame))
|
304 |
-
|
305 |
for i in frames_list[0:int(n_frame)]:
|
306 |
to_numpy_i = Image.open(i).convert("RGB")
|
307 |
#need to convert to numpy
|
308 |
# Convert the image to a NumPy array
|
309 |
image_array = np.array(to_numpy_i)
|
310 |
|
311 |
-
results = run_grounded_sam(image_array, prompt, "text", background_prompt)
|
312 |
-
|
313 |
bg_img = Image.fromarray(results[0])
|
314 |
green_img = Image.fromarray(results[1])
|
315 |
matte_img = Image.fromarray(results[2])
|
@@ -327,6 +330,8 @@ def infer(video_in, trim_value, prompt, background_prompt):
|
|
327 |
vid_bg = create_video(with_bg_result_frames, fps, "bg")
|
328 |
vid_green = create_video(with_green_result_frames, fps, "greenscreen")
|
329 |
vid_matte = create_video(with_matte_result_frames, fps, "matte")
|
|
|
|
|
330 |
print("finished !")
|
331 |
|
332 |
return vid_bg, vid_green, vid_matte
|
|
|
114 |
return f"video_{type}_result.mp4"
|
115 |
|
116 |
|
117 |
+
def run_grounded_sam(input_image, text_prompt, task_type, background_prompt, bg_already):
|
118 |
background_type = "generated_by_text"
|
119 |
box_threshold = 0.25
|
120 |
text_threshold = 0.25
|
|
|
262 |
### alpha matte
|
263 |
alpha_rgb = cv2.cvtColor(np.uint8(alpha_pred*255), cv2.COLOR_GRAY2RGB)
|
264 |
### com img with background
|
265 |
+
global background_img
|
266 |
if background_type == 'real_world_sample':
|
267 |
background_img_file = os.path.join('assets/backgrounds', random.choice(background_list))
|
268 |
background_img = cv2.imread(background_img_file)
|
|
|
274 |
if background_prompt is None:
|
275 |
print('Please input non-empty background prompt')
|
276 |
else:
|
277 |
+
if bg_already is False:
|
278 |
+
background_img = generator(background_prompt).images[0]
|
279 |
+
else:
|
280 |
background_img = np.array(background_img)
|
281 |
background_img = cv2.resize(background_img, (image_ori.shape[1], image_ori.shape[0]))
|
282 |
com_img = alpha_pred[..., None] * image_ori + (1 - alpha_pred[..., None]) * np.uint8(background_img)
|
|
|
304 |
with_matte_result_frames = []
|
305 |
|
306 |
print("set stop frames to: " + str(n_frame))
|
307 |
+
bg_already = False
|
308 |
for i in frames_list[0:int(n_frame)]:
|
309 |
to_numpy_i = Image.open(i).convert("RGB")
|
310 |
#need to convert to numpy
|
311 |
# Convert the image to a NumPy array
|
312 |
image_array = np.array(to_numpy_i)
|
313 |
|
314 |
+
results = run_grounded_sam(image_array, prompt, "text", background_prompt, bg_already)
|
315 |
+
bg_already = True
|
316 |
bg_img = Image.fromarray(results[0])
|
317 |
green_img = Image.fromarray(results[1])
|
318 |
matte_img = Image.fromarray(results[2])
|
|
|
330 |
vid_bg = create_video(with_bg_result_frames, fps, "bg")
|
331 |
vid_green = create_video(with_green_result_frames, fps, "greenscreen")
|
332 |
vid_matte = create_video(with_matte_result_frames, fps, "matte")
|
333 |
+
|
334 |
+
bg_already = False
|
335 |
print("finished !")
|
336 |
|
337 |
return vid_bg, vid_green, vid_matte
|