Krokodilpirat commited on
Commit
b297681
Β·
verified Β·
1 Parent(s): 529dcd7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -11
app.py CHANGED
@@ -215,57 +215,134 @@ def infer_video_depth_from_source(upload_video, video_url, filename, use_blip, *
215
  # --- UI event handlers ---
216
  def on_video_url_change(url, use_blip):
217
  """Handle URL input change with safe error handling"""
 
 
218
  if not url or url.strip() == "":
 
 
 
 
219
  return None, "", "Enter a valid video URL"
220
 
221
  try:
222
  print(f"Downloading video from URL: {url}")
223
  video_path = download_video_from_url(url)
224
- name = ""
225
 
 
 
 
 
 
 
 
226
  if use_blip and video_path:
227
  try:
228
  frames, _ = read_video_frames(video_path, 999, -1, 480)
229
  if len(frames) > 0:
230
  frame = frames[len(frames) // 2]
231
- name = generate_blip_name(frame)
232
  except Exception as e:
233
  print(f"BLIP naming failed: {e}")
234
- name = "downloaded_video"
 
 
 
 
235
 
236
- return video_path, name, "Video downloaded successfully!"
237
  except Exception as e:
238
  error_msg = f"Download failed: {str(e)}"
239
  print(error_msg)
240
  return None, "", error_msg
241
 
 
 
 
 
 
 
242
  def on_video_upload_change(video_file, use_blip):
243
- """Handle video upload with BLIP naming"""
 
 
244
  if not video_file:
 
 
 
245
  return "", "Upload a video file"
246
 
247
  try:
248
- name = ""
 
 
 
 
 
 
 
 
 
 
 
 
249
  if use_blip:
250
  frames, _ = read_video_frames(video_file, 999, -1, 480)
251
  if len(frames) > 0:
252
  frame = frames[len(frames) // 2]
253
- name = generate_blip_name(frame)
 
 
 
 
254
 
255
- return name or "uploaded_video", "Video uploaded successfully!"
256
  except Exception as e:
257
  error_msg = f"Upload processing failed: {str(e)}"
258
  print(error_msg)
259
  return "uploaded_video", error_msg
260
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
  # --- Gradio Interface ---
262
  with gr.Blocks(analytics_enabled=False, title="Video Depth Anything") as demo:
263
  gr.Markdown("""
264
  # πŸŽ₯ Video Depth Anything + RGBD Output
265
 
266
- Transform your videos into depth maps! Upload a video or paste a URL to generate:
267
  - **Depth visualization** (colorized depth map)
268
- - **RGBD side-by-side** (original + depth combined)
269
 
270
  [πŸ”— Project Page](https://videodepthanything.github.io/) | [πŸ“– Paper](https://arxiv.org/abs/2401.01884)
271
  """)
@@ -299,7 +376,7 @@ with gr.Blocks(analytics_enabled=False, title="Video Depth Anything") as demo:
299
 
300
  with gr.Row():
301
  video_url = gr.Textbox(
302
- label="πŸ”— Video URL",
303
  placeholder="Paste video URL here...",
304
  scale=3
305
  )
@@ -328,6 +405,13 @@ with gr.Blocks(analytics_enabled=False, title="Video Depth Anything") as demo:
328
  outputs=[filename, status_display]
329
  )
330
 
 
 
 
 
 
 
 
331
  with gr.Accordion("βš™οΈ Advanced Settings", open=False):
332
  with gr.Row():
333
  max_len = gr.Slider(
 
215
  # --- UI event handlers ---
216
  def on_video_url_change(url, use_blip):
217
  """Handle URL input change with safe error handling"""
218
+ global current_video_file, current_video_url, blip_generated_name, original_filename
219
+
220
  if not url or url.strip() == "":
221
+ current_video_file = None
222
+ current_video_url = None
223
+ blip_generated_name = ""
224
+ original_filename = ""
225
  return None, "", "Enter a valid video URL"
226
 
227
  try:
228
  print(f"Downloading video from URL: {url}")
229
  video_path = download_video_from_url(url)
 
230
 
231
+ # Store the current video info
232
+ current_video_file = None # Clear file when using URL
233
+ current_video_url = video_path
234
+ original_filename = "downloaded_video"
235
+ blip_generated_name = ""
236
+
237
+ # Generate BLIP name if requested
238
  if use_blip and video_path:
239
  try:
240
  frames, _ = read_video_frames(video_path, 999, -1, 480)
241
  if len(frames) > 0:
242
  frame = frames[len(frames) // 2]
243
+ blip_generated_name = generate_blip_name(frame)
244
  except Exception as e:
245
  print(f"BLIP naming failed: {e}")
246
+ blip_generated_name = ""
247
+
248
+ # Return appropriate name
249
+ final_name = blip_generated_name if (use_blip and blip_generated_name) else original_filename
250
+ return video_path, final_name, "Video downloaded successfully!"
251
 
 
252
  except Exception as e:
253
  error_msg = f"Download failed: {str(e)}"
254
  print(error_msg)
255
  return None, "", error_msg
256
 
257
+ # Global variables to store video info for toggling
258
+ current_video_file = None
259
+ current_video_url = None
260
+ blip_generated_name = ""
261
+ original_filename = ""
262
+
263
  def on_video_upload_change(video_file, use_blip):
264
+ """Handle video upload and store video info for toggling"""
265
+ global current_video_file, blip_generated_name, original_filename, current_video_url
266
+
267
  if not video_file:
268
+ current_video_file = None
269
+ blip_generated_name = ""
270
+ original_filename = ""
271
  return "", "Upload a video file"
272
 
273
  try:
274
+ # Store the current video
275
+ current_video_file = video_file
276
+ current_video_url = None # Clear URL when uploading file
277
+
278
+ # Generate original filename
279
+ if hasattr(video_file, 'name') and video_file.name:
280
+ original_name = os.path.splitext(os.path.basename(video_file.name))[0]
281
+ original_filename = "".join(c for c in original_name if c.isalnum() or c in "_-")[:30]
282
+ else:
283
+ original_filename = "uploaded_video"
284
+
285
+ # Generate BLIP name
286
+ blip_generated_name = ""
287
  if use_blip:
288
  frames, _ = read_video_frames(video_file, 999, -1, 480)
289
  if len(frames) > 0:
290
  frame = frames[len(frames) // 2]
291
+ blip_generated_name = generate_blip_name(frame)
292
+
293
+ # Return appropriate name based on BLIP setting
294
+ final_name = blip_generated_name if (use_blip and blip_generated_name) else original_filename
295
+ return final_name, "Video uploaded successfully!"
296
 
 
297
  except Exception as e:
298
  error_msg = f"Upload processing failed: {str(e)}"
299
  print(error_msg)
300
  return "uploaded_video", error_msg
301
 
302
+ def on_blip_toggle(use_blip):
303
+ """Handle BLIP checkbox toggle - switch between BLIP and original name"""
304
+ global current_video_file, current_video_url, blip_generated_name, original_filename
305
+
306
+ # Only react if we have a video loaded
307
+ if current_video_file is None and current_video_url is None:
308
+ return "", "No video loaded"
309
+
310
+ try:
311
+ # If toggling BLIP on and we don't have a BLIP name yet, generate it
312
+ if use_blip and not blip_generated_name:
313
+ if current_video_file:
314
+ frames, _ = read_video_frames(current_video_file, 999, -1, 480)
315
+ if len(frames) > 0:
316
+ frame = frames[len(frames) // 2]
317
+ blip_generated_name = generate_blip_name(frame)
318
+ elif current_video_url:
319
+ # For URL videos, we might need to re-read frames
320
+ frames, _ = read_video_frames(current_video_url, 999, -1, 480)
321
+ if len(frames) > 0:
322
+ frame = frames[len(frames) // 2]
323
+ blip_generated_name = generate_blip_name(frame)
324
+
325
+ # Return appropriate name based on toggle
326
+ if use_blip and blip_generated_name:
327
+ return blip_generated_name, "Using BLIP generated name"
328
+ else:
329
+ name = original_filename if original_filename else "video"
330
+ return name, "Using original filename"
331
+
332
+ except Exception as e:
333
+ error_msg = f"Name generation failed: {str(e)}"
334
+ print(error_msg)
335
+ fallback = original_filename if original_filename else "video"
336
+ return fallback, error_msg
337
+
338
  # --- Gradio Interface ---
339
  with gr.Blocks(analytics_enabled=False, title="Video Depth Anything") as demo:
340
  gr.Markdown("""
341
  # πŸŽ₯ Video Depth Anything + RGBD Output
342
 
343
+ Transform your videos into depth maps! Upload a video or paste a URL (Midjourney video for now) to generate:
344
  - **Depth visualization** (colorized depth map)
345
+ - **RGBD side-by-side** (original + depth combined) for viewing on holographic displays like Looking Glass Go.
346
 
347
  [πŸ”— Project Page](https://videodepthanything.github.io/) | [πŸ“– Paper](https://arxiv.org/abs/2401.01884)
348
  """)
 
376
 
377
  with gr.Row():
378
  video_url = gr.Textbox(
379
+ label="πŸ”— Midjourney Video URL (Experimental)",
380
  placeholder="Paste video URL here...",
381
  scale=3
382
  )
 
405
  outputs=[filename, status_display]
406
  )
407
 
408
+ # New: Toggle BLIP checkbox to switch between names
409
+ use_blip.change(
410
+ fn=on_blip_toggle,
411
+ inputs=[use_blip],
412
+ outputs=[filename, status_display]
413
+ )
414
+
415
  with gr.Accordion("βš™οΈ Advanced Settings", open=False):
416
  with gr.Row():
417
  max_len = gr.Slider(