Spaces:
Paused
Paused
Update web-demos/hugging_face/app.py
Browse files- web-demos/hugging_face/app.py +49 -71
web-demos/hugging_face/app.py
CHANGED
@@ -2,7 +2,6 @@ import sys
|
|
2 |
sys.path.append("../../")
|
3 |
|
4 |
import os
|
5 |
-
import subprocess
|
6 |
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
|
7 |
import json
|
8 |
import time
|
@@ -16,7 +15,6 @@ import numpy as np
|
|
16 |
import gradio as gr
|
17 |
from gradio import Brush
|
18 |
import tempfile
|
19 |
-
import glob
|
20 |
import ffmpeg
|
21 |
from PIL import Image
|
22 |
from tools.painter import mask_painter
|
@@ -67,95 +65,75 @@ def get_prompt(click_state, click_input):
|
|
67 |
|
68 |
# extract frames from upload video
|
69 |
|
70 |
-
import os
|
71 |
-
import time
|
72 |
-
import subprocess
|
73 |
-
import glob
|
74 |
-
import numpy as np
|
75 |
-
from PIL import Image
|
76 |
-
import gradio as gr
|
77 |
-
|
78 |
def get_frames_from_video(video_input, video_state):
|
79 |
"""
|
80 |
-
|
|
|
|
|
|
|
|
|
81 |
"""
|
|
|
82 |
frames = []
|
83 |
user_name = time.time()
|
84 |
status_ok = True
|
85 |
operation_log = [("[Must Do]", "Click image"), (": Video uploaded! Try to click the image shown in step2 to add masks.\n", None)]
|
86 |
|
87 |
try:
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
# Извлечение кадров без сжатия (TIFF 16 бит, RGB)
|
93 |
-
command = [
|
94 |
-
"ffmpeg", "-i", video_input,
|
95 |
-
"-pix_fmt", "rgb48le",
|
96 |
-
os.path.join(temp_output_dir, "frame_%05d.tiff")
|
97 |
-
]
|
98 |
-
subprocess.run(command, check=True)
|
99 |
-
|
100 |
-
# Получаем пути к кадрам
|
101 |
-
frame_paths = sorted(glob.glob(os.path.join(temp_output_dir, "frame_*.tiff")))
|
102 |
-
if len(frame_paths) == 0:
|
103 |
-
raise RuntimeError("ffmpeg failed to extract frames or no frames found.")
|
104 |
-
|
105 |
-
for path in frame_paths:
|
106 |
-
image = Image.open(path)
|
107 |
-
frames.append(np.array(image))
|
108 |
-
|
109 |
-
# Получение FPS через ffprobe
|
110 |
-
fps_cmd = [
|
111 |
-
"ffprobe", "-v", "error", "-select_streams", "v:0",
|
112 |
-
"-show_entries", "stream=r_frame_rate",
|
113 |
-
"-of", "default=noprint_wrappers=1:nokey=1", video_input
|
114 |
-
]
|
115 |
-
fps_raw = subprocess.check_output(fps_cmd).decode().strip()
|
116 |
-
num, den = map(int, fps_raw.split("/")) if "/" in fps_raw else (int(fps_raw), 1)
|
117 |
-
fps = num / den
|
118 |
-
|
119 |
-
original_h, original_w = frames[0].shape[:2]
|
120 |
-
length = len(frames)
|
121 |
|
122 |
if length >= 600:
|
123 |
-
operation_log = [("You uploaded a video with more than 500 frames. Stop the video extraction. Kindly lower the video frame rate to a value below 500.", "Error")]
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
status_ok = False
|
129 |
-
|
|
|
|
|
|
|
|
|
130 |
|
131 |
-
# Собираем состояние
|
132 |
video_state = {
|
133 |
"user_name": user_name,
|
134 |
-
"video_name": os.path.
|
135 |
"origin_images": frames,
|
136 |
"painted_images": frames.copy(),
|
137 |
-
"masks": [np.zeros((original_h, original_w), np.uint8)]
|
138 |
-
"logits": [None]
|
139 |
"select_frame_number": 0,
|
140 |
"fps": fps
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
model.samcontroler.sam_controler.reset_image()
|
146 |
model.samcontroler.sam_controler.set_image(video_state["origin_images"][0])
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
gr.update(visible=status_ok), gr.update(visible=status_ok), \
|
156 |
-
gr.update(visible=status_ok), gr.update(visible=status_ok, choices=[], value=[]), \
|
157 |
-
gr.update(visible=True, value=operation_log), \
|
158 |
-
gr.update(visible=status_ok, value=operation_log)
|
159 |
|
160 |
def select_template(image_selection_slider, video_state, interactive_state, mask_dropdown):
|
161 |
|
|
|
2 |
sys.path.append("../../")
|
3 |
|
4 |
import os
|
|
|
5 |
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
|
6 |
import json
|
7 |
import time
|
|
|
15 |
import gradio as gr
|
16 |
from gradio import Brush
|
17 |
import tempfile
|
|
|
18 |
import ffmpeg
|
19 |
from PIL import Image
|
20 |
from tools.painter import mask_painter
|
|
|
65 |
|
66 |
# extract frames from upload video
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
def get_frames_from_video(video_input, video_state):
|
69 |
"""
|
70 |
+
Args:
|
71 |
+
video_path:str
|
72 |
+
timestamp:float64
|
73 |
+
Return
|
74 |
+
[[0:nearest_frame], [nearest_frame:], nearest_frame]
|
75 |
"""
|
76 |
+
video_path = video_input
|
77 |
frames = []
|
78 |
user_name = time.time()
|
79 |
status_ok = True
|
80 |
operation_log = [("[Must Do]", "Click image"), (": Video uploaded! Try to click the image shown in step2 to add masks.\n", None)]
|
81 |
|
82 |
try:
|
83 |
+
cap = cv2.VideoCapture(video_path)
|
84 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
85 |
+
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
if length >= 600:
|
88 |
+
operation_log = [("You uploaded a video with more than 500 frames. Stop the video extraction. Kindly lower the video frame rate to a value below 500. We highly recommend deploying the demo locally for long video processing.", "Error")]
|
89 |
+
ret, frame = cap.read()
|
90 |
+
if ret == True:
|
91 |
+
original_h, original_w = frame.shape[:2]
|
92 |
+
frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
93 |
+
status_ok = False
|
94 |
+
else:
|
95 |
+
while cap.isOpened():
|
96 |
+
ret, frame = cap.read()
|
97 |
+
if ret == True:
|
98 |
+
# resize input image
|
99 |
+
original_h, original_w = frame.shape[:2]
|
100 |
+
frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
101 |
+
else:
|
102 |
+
break
|
103 |
+
t = len(frames)
|
104 |
+
if t > 0:
|
105 |
+
print(f'Inp video shape: t_{t}, s_{original_h}x{original_w}')
|
106 |
+
else:
|
107 |
+
print(f'Inp video shape: t_{t}, no input video!!!')
|
108 |
+
except (OSError, TypeError, ValueError, KeyError, SyntaxError) as e:
|
109 |
status_ok = False
|
110 |
+
print("read_frame_source:{} error. {}\n".format(video_path, str(e)))
|
111 |
+
|
112 |
+
# initialize video_state
|
113 |
+
if frames[0].shape[0] > 720 or frames[0].shape[1] > 720:
|
114 |
+
operation_log = [(f"Video uploaded! Try to click the image shown in step2 to add masks. (You uploaded a video with a size of {original_w}x{original_h}, and the length of its longest edge exceeds 720 pixels. We may resize the input video during processing.)", "Normal")]
|
115 |
|
|
|
116 |
video_state = {
|
117 |
"user_name": user_name,
|
118 |
+
"video_name": os.path.split(video_path)[-1],
|
119 |
"origin_images": frames,
|
120 |
"painted_images": frames.copy(),
|
121 |
+
"masks": [np.zeros((original_h, original_w), np.uint8)]*len(frames),
|
122 |
+
"logits": [None]*len(frames),
|
123 |
"select_frame_number": 0,
|
124 |
"fps": fps
|
125 |
+
}
|
126 |
+
video_info = "Video Name: {},\nFPS: {},\nTotal Frames: {},\nImage Size:{}".format(video_state["video_name"], round(video_state["fps"], 0), length, (original_w, original_h))
|
127 |
+
model.samcontroler.sam_controler.reset_image()
|
|
|
|
|
128 |
model.samcontroler.sam_controler.set_image(video_state["origin_images"][0])
|
129 |
+
return video_state, video_info, video_state["origin_images"][0], gr.update(visible=status_ok, maximum=len(frames), value=1), gr.update(visible=status_ok, maximum=len(frames), value=len(frames)), \
|
130 |
+
gr.update(visible=status_ok), gr.update(visible=status_ok), \
|
131 |
+
gr.update(visible=status_ok), gr.update(visible=status_ok),\
|
132 |
+
gr.update(visible=status_ok), gr.update(visible=status_ok), \
|
133 |
+
gr.update(visible=status_ok), gr.update(visible=status_ok), \
|
134 |
+
gr.update(visible=status_ok), gr.update(visible=status_ok), \
|
135 |
+
gr.update(visible=status_ok), gr.update(visible=status_ok, choices=[], value=[]), \
|
136 |
+
gr.update(visible=True, value=operation_log), gr.update(visible=status_ok, value=operation_log)
|
|
|
|
|
|
|
|
|
137 |
|
138 |
def select_template(image_selection_slider, video_state, interactive_state, mask_dropdown):
|
139 |
|