Spaces:
Runtime error
Runtime error
add tqdm tracking
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import os
|
|
| 5 |
import sys
|
| 6 |
import random
|
| 7 |
import warnings
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
os.system("export BUILD_WITH_CUDA=True")
|
|
@@ -74,39 +75,44 @@ generator.to(device)
|
|
| 74 |
|
| 75 |
def get_frames(video_in):
|
| 76 |
frames = []
|
| 77 |
-
#resize the video
|
| 78 |
clip = VideoFileClip(video_in)
|
| 79 |
-
|
| 80 |
-
#check fps
|
| 81 |
if clip.fps > 30:
|
| 82 |
-
print("
|
| 83 |
clip_resized = clip.resize(height=512)
|
| 84 |
-
clip_resized.write_videofile("video_resized.mp4", fps=30)
|
| 85 |
else:
|
| 86 |
print("video rate is OK")
|
| 87 |
clip_resized = clip.resize(height=512)
|
| 88 |
-
clip_resized.write_videofile("video_resized.mp4", fps=clip.fps)
|
| 89 |
-
|
| 90 |
print("video resized to 512 height")
|
| 91 |
-
|
| 92 |
# Opens the Video file with CV2
|
| 93 |
-
cap= cv2.VideoCapture("video_resized.mp4")
|
| 94 |
-
|
| 95 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
cap.release()
|
| 107 |
cv2.destroyAllWindows()
|
| 108 |
print("broke the video into frames")
|
| 109 |
-
|
| 110 |
return frames, fps
|
| 111 |
|
| 112 |
|
|
@@ -291,7 +297,7 @@ def run_grounded_sam(input_image, text_prompt, task_type, background_prompt, bg_
|
|
| 291 |
#return [(com_img, 'composite with background'), (green_img, 'green screen'), (alpha_rgb, 'alpha matte')]
|
| 292 |
return com_img, green_img, alpha_rgb
|
| 293 |
|
| 294 |
-
def infer(video_in, trim_value, prompt, background_prompt):
|
| 295 |
print(prompt)
|
| 296 |
break_vid = get_frames(video_in)
|
| 297 |
|
|
|
|
| 5 |
import sys
|
| 6 |
import random
|
| 7 |
import warnings
|
| 8 |
+
from tqdm import tqdm
|
| 9 |
|
| 10 |
|
| 11 |
os.system("export BUILD_WITH_CUDA=True")
|
|
|
|
| 75 |
|
| 76 |
def get_frames(video_in):
|
| 77 |
frames = []
|
| 78 |
+
# resize the video
|
| 79 |
clip = VideoFileClip(video_in)
|
| 80 |
+
|
| 81 |
+
# check fps
|
| 82 |
if clip.fps > 30:
|
| 83 |
+
print("video rate is over 30, resetting to 30")
|
| 84 |
clip_resized = clip.resize(height=512)
|
| 85 |
+
clip_resized.write_videofile("video_resized.mp4", fps=30, verbose=False, logger=None)
|
| 86 |
else:
|
| 87 |
print("video rate is OK")
|
| 88 |
clip_resized = clip.resize(height=512)
|
| 89 |
+
clip_resized.write_videofile("video_resized.mp4", fps=clip.fps, verbose=False, logger=None)
|
| 90 |
+
|
| 91 |
print("video resized to 512 height")
|
| 92 |
+
|
| 93 |
# Opens the Video file with CV2
|
| 94 |
+
cap = cv2.VideoCapture("video_resized.mp4")
|
| 95 |
+
|
| 96 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 97 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) # total number of frames
|
| 98 |
+
print(f"video fps: {fps}, total frames: {total_frames}")
|
| 99 |
+
|
| 100 |
+
i = 0
|
| 101 |
+
with tqdm(total=total_frames, desc="Extracting frames", unit="frame") as pbar:
|
| 102 |
+
while cap.isOpened():
|
| 103 |
+
ret, frame = cap.read()
|
| 104 |
+
if not ret:
|
| 105 |
+
break
|
| 106 |
+
frame_name = f'kang{i}.jpg'
|
| 107 |
+
cv2.imwrite(frame_name, frame)
|
| 108 |
+
frames.append(frame_name)
|
| 109 |
+
i += 1
|
| 110 |
+
pbar.update(1)
|
| 111 |
+
|
| 112 |
cap.release()
|
| 113 |
cv2.destroyAllWindows()
|
| 114 |
print("broke the video into frames")
|
| 115 |
+
|
| 116 |
return frames, fps
|
| 117 |
|
| 118 |
|
|
|
|
| 297 |
#return [(com_img, 'composite with background'), (green_img, 'green screen'), (alpha_rgb, 'alpha matte')]
|
| 298 |
return com_img, green_img, alpha_rgb
|
| 299 |
|
| 300 |
+
def infer(video_in, trim_value, prompt, background_prompt, progress=gr.Progress(track_tqdm=True)):
|
| 301 |
print(prompt)
|
| 302 |
break_vid = get_frames(video_in)
|
| 303 |
|