akhaliq HF Staff commited on
Commit
e1950cc
·
verified ·
1 Parent(s): 853d299

Write outputs beside the uploaded input so Gradio serves them (fixes 404 on depth URL)

Browse files
Files changed (1) hide show
  1. app.py +5 -3
app.py CHANGED
@@ -1,6 +1,5 @@
1
  from __future__ import annotations
2
 
3
- import tempfile
4
  from functools import partial
5
  from pathlib import Path
6
 
@@ -102,7 +101,8 @@ def _infer_video(path: str) -> str:
102
  net, transform = _device_and_model()
103
  predictions = net.infer_video(transform(f) for f in frames())
104
 
105
- out_path = Path(tempfile.gettempdir()) / "depthpro2-output.mp4"
 
106
  writer = None
107
  written = 0
108
  with av.open(out_path, mode="w") as out:
@@ -138,7 +138,9 @@ def predict_depth(image: FileData) -> FileData:
138
  """Estimate metric depth for an uploaded photo; returns the colorized depth map."""
139
  img, f_px = load_rgb(image["path"]) # f_px: the focal length recorded in the photo, or None
140
  rgb = _infer_image(img, f_px)
141
- out_path = Path(tempfile.gettempdir()) / "depthpro2-depth.png"
 
 
142
  Image.fromarray(rgb).save(out_path)
143
  return FileData(path=str(out_path))
144
 
 
1
  from __future__ import annotations
2
 
 
3
  from functools import partial
4
  from pathlib import Path
5
 
 
101
  net, transform = _device_and_model()
102
  predictions = net.infer_video(transform(f) for f in frames())
103
 
104
+ # Beside the uploaded input, which is in Gradio's servable upload cache.
105
+ out_path = Path(path).with_suffix(".depth.mp4")
106
  writer = None
107
  written = 0
108
  with av.open(out_path, mode="w") as out:
 
138
  """Estimate metric depth for an uploaded photo; returns the colorized depth map."""
139
  img, f_px = load_rgb(image["path"]) # f_px: the focal length recorded in the photo, or None
140
  rgb = _infer_image(img, f_px)
141
+ # Write beside the uploaded input: it is in Gradio's upload cache, which is servable; a raw
142
+ # tempfile.gettempdir() path is not, and its URL 404s in the browser.
143
+ out_path = Path(image["path"]).with_suffix(".depth.png")
144
  Image.fromarray(rgb).save(out_path)
145
  return FileData(path=str(out_path))
146