Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -120,7 +120,7 @@ def get_first_frame(video_path):
|
|
| 120 |
def compute_duration_text(video_path, text_prompt, max_frames, timeout_seconds):
|
| 121 |
return timeout_seconds
|
| 122 |
|
| 123 |
-
def compute_duration_tracker(video_path,
|
| 124 |
return timeout_seconds
|
| 125 |
|
| 126 |
# --- LOGIQUE AVEC DÉCORATEURS ZEROGPU ---
|
|
@@ -144,10 +144,11 @@ def process_image_text(image, text_prompt, threshold, mask_threshold):
|
|
| 144 |
except Exception as e:
|
| 145 |
return image, f"Error: {str(e)}"
|
| 146 |
|
|
|
|
| 147 |
@spaces.GPU
|
| 148 |
-
def
|
| 149 |
if image is None: return image, [], []
|
| 150 |
-
x
|
| 151 |
if points_state is None: points_state = []; labels_state = []
|
| 152 |
points_state.append([x, y])
|
| 153 |
labels_state.append(1)
|
|
@@ -175,6 +176,14 @@ def process_image_tracker(image, evt: gr.SelectData, points_state, labels_state,
|
|
| 175 |
print(f"Tracker Error: {e}")
|
| 176 |
return image, points_state, labels_state
|
| 177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
@spaces.GPU(duration=compute_duration_text)
|
| 179 |
def process_video_text(video_path, text_prompt, max_frames, timeout_seconds):
|
| 180 |
if not video_path or not text_prompt: return None, "Missing video or prompt."
|
|
@@ -211,11 +220,9 @@ def process_video_text(video_path, text_prompt, max_frames, timeout_seconds):
|
|
| 211 |
return output_path, "Done!"
|
| 212 |
except Exception as e: return None, f"Error: {str(e)}"
|
| 213 |
|
|
|
|
| 214 |
@spaces.GPU(duration=compute_duration_tracker)
|
| 215 |
-
def
|
| 216 |
-
if not video_path or not first_frame_click: return None, "Please click on the first frame."
|
| 217 |
-
if hasattr(first_frame_click, 'index'): x, y = first_frame_click.index
|
| 218 |
-
else: return None, "Click error."
|
| 219 |
try:
|
| 220 |
model, processor = get_model("sam3_video_tracker")
|
| 221 |
cap = cv2.VideoCapture(video_path)
|
|
@@ -250,6 +257,18 @@ def process_video_tracker(video_path, first_frame_click, max_frames, timeout_sec
|
|
| 250 |
print(f"Video Tracker Error: {e}")
|
| 251 |
return None, f"Fatal Error: {str(e)}"
|
| 252 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
# --- INTERFACE GRADIO ---
|
| 254 |
|
| 255 |
with gr.Blocks(title="SAM3 Ultimate Suite") as demo:
|
|
@@ -287,7 +306,8 @@ with gr.Blocks(title="SAM3 Ultimate Suite") as demo:
|
|
| 287 |
with gr.Column():
|
| 288 |
i2_output = gr.Image(type="pil", label="Interactive Result")
|
| 289 |
|
| 290 |
-
|
|
|
|
| 291 |
i2_clear.click(lambda: (None, [], []), outputs=[i2_output, points_state, labels_state])
|
| 292 |
|
| 293 |
# TAB 3 : VIDEO + TEXTE
|
|
@@ -326,8 +346,9 @@ with gr.Blocks(title="SAM3 Ultimate Suite") as demo:
|
|
| 326 |
click_state = gr.State()
|
| 327 |
def save_click(evt: gr.SelectData): return evt
|
| 328 |
v4_frame0.select(save_click, None, click_state)
|
| 329 |
-
|
| 330 |
-
|
|
|
|
| 331 |
|
| 332 |
if __name__ == "__main__":
|
| 333 |
demo.launch(share=False, debug=True, theme=gr.themes.Soft())
|
|
|
|
| 120 |
def compute_duration_text(video_path, text_prompt, max_frames, timeout_seconds):
|
| 121 |
return timeout_seconds
|
| 122 |
|
| 123 |
+
def compute_duration_tracker(video_path, x, y, max_frames, timeout_seconds):
|
| 124 |
return timeout_seconds
|
| 125 |
|
| 126 |
# --- LOGIQUE AVEC DÉCORATEURS ZEROGPU ---
|
|
|
|
| 144 |
except Exception as e:
|
| 145 |
return image, f"Error: {str(e)}"
|
| 146 |
|
| 147 |
+
# MODIFICATION IMPORTANTE : Cette fonction GPU ne prend plus 'evt', mais 'x' et 'y' directement
|
| 148 |
@spaces.GPU
|
| 149 |
+
def process_image_tracker_gpu(image, x, y, points_state, labels_state, multimask):
|
| 150 |
if image is None: return image, [], []
|
| 151 |
+
# x et y sont maintenant des entiers simples
|
| 152 |
if points_state is None: points_state = []; labels_state = []
|
| 153 |
points_state.append([x, y])
|
| 154 |
labels_state.append(1)
|
|
|
|
| 176 |
print(f"Tracker Error: {e}")
|
| 177 |
return image, points_state, labels_state
|
| 178 |
|
| 179 |
+
# WRAPPER CPU POUR IMAGE TRACKER : Extrait les données avant d'appeler le GPU
|
| 180 |
+
def process_image_tracker_wrapper(image, evt: gr.SelectData, points_state, labels_state, multimask):
|
| 181 |
+
if evt is None: return image, points_state, labels_state
|
| 182 |
+
x, y = evt.index
|
| 183 |
+
# Appel de la fonction GPU avec des types simples
|
| 184 |
+
return process_image_tracker_gpu(image, x, y, points_state, labels_state, multimask)
|
| 185 |
+
|
| 186 |
+
|
| 187 |
@spaces.GPU(duration=compute_duration_text)
|
| 188 |
def process_video_text(video_path, text_prompt, max_frames, timeout_seconds):
|
| 189 |
if not video_path or not text_prompt: return None, "Missing video or prompt."
|
|
|
|
| 220 |
return output_path, "Done!"
|
| 221 |
except Exception as e: return None, f"Error: {str(e)}"
|
| 222 |
|
| 223 |
+
# MODIFICATION IMPORTANTE : Cette fonction GPU ne prend plus 'first_frame_click' (objet complexe) mais x, y
|
| 224 |
@spaces.GPU(duration=compute_duration_tracker)
|
| 225 |
+
def process_video_tracker_gpu(video_path, x, y, max_frames, timeout_seconds):
|
|
|
|
|
|
|
|
|
|
| 226 |
try:
|
| 227 |
model, processor = get_model("sam3_video_tracker")
|
| 228 |
cap = cv2.VideoCapture(video_path)
|
|
|
|
| 257 |
print(f"Video Tracker Error: {e}")
|
| 258 |
return None, f"Fatal Error: {str(e)}"
|
| 259 |
|
| 260 |
+
# WRAPPER CPU POUR VIDEO TRACKER
|
| 261 |
+
def process_video_tracker_wrapper(video_path, first_frame_click, max_frames, timeout_seconds):
|
| 262 |
+
if not video_path or not first_frame_click: return None, "Please click on the first frame."
|
| 263 |
+
# Extraction des données simples ici, sur le CPU
|
| 264 |
+
if hasattr(first_frame_click, 'index'):
|
| 265 |
+
x, y = first_frame_click.index
|
| 266 |
+
else:
|
| 267 |
+
return None, "Click error."
|
| 268 |
+
|
| 269 |
+
# Appel de la fonction GPU avec des entiers
|
| 270 |
+
return process_video_tracker_gpu(video_path, x, y, max_frames, timeout_seconds)
|
| 271 |
+
|
| 272 |
# --- INTERFACE GRADIO ---
|
| 273 |
|
| 274 |
with gr.Blocks(title="SAM3 Ultimate Suite") as demo:
|
|
|
|
| 306 |
with gr.Column():
|
| 307 |
i2_output = gr.Image(type="pil", label="Interactive Result")
|
| 308 |
|
| 309 |
+
# APPEL DU WRAPPER CPU, PAS DE LA FONCTION GPU DIRECTEMENT
|
| 310 |
+
i2_input.select(process_image_tracker_wrapper, [i2_input, points_state, labels_state, i2_multimask], [i2_output, points_state, labels_state])
|
| 311 |
i2_clear.click(lambda: (None, [], []), outputs=[i2_output, points_state, labels_state])
|
| 312 |
|
| 313 |
# TAB 3 : VIDEO + TEXTE
|
|
|
|
| 346 |
click_state = gr.State()
|
| 347 |
def save_click(evt: gr.SelectData): return evt
|
| 348 |
v4_frame0.select(save_click, None, click_state)
|
| 349 |
+
|
| 350 |
+
# APPEL DU WRAPPER CPU, PAS DE LA FONCTION GPU DIRECTEMENT
|
| 351 |
+
v4_btn.click(process_video_tracker_wrapper, [v4_input, click_state, v4_max_frames, v4_duration], [v4_output, v4_status])
|
| 352 |
|
| 353 |
if __name__ == "__main__":
|
| 354 |
demo.launch(share=False, debug=True, theme=gr.themes.Soft())
|