Load via create_model_and_transforms so the focal estimator is included (fixes depth=None)
Browse files
app.py
CHANGED
|
@@ -15,16 +15,18 @@ try:
|
|
| 15 |
except ImportError:
|
| 16 |
spaces = None
|
| 17 |
|
| 18 |
-
from depth_pro_2 import
|
| 19 |
from depth_pro_2.model import to_normalized_tensor
|
| 20 |
|
| 21 |
-
MODEL_ID = "apple/DepthPro2-H"
|
| 22 |
MAX_VIDEO_FRAMES = 100
|
| 23 |
|
| 24 |
-
# The weights
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
|
|
@@ -76,7 +78,10 @@ def run_image(image: Image.Image) -> Image.Image:
|
|
| 76 |
image.save(path)
|
| 77 |
img, f_px = load_rgb(path) # f_px: the focal length recorded in the photo, or None
|
| 78 |
net, transform = _device_and_model()
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
| 80 |
return Image.fromarray(_colorize(depth.float().cpu().numpy()))
|
| 81 |
|
| 82 |
|
|
|
|
| 15 |
except ImportError:
|
| 16 |
spaces = None
|
| 17 |
|
| 18 |
+
from depth_pro_2 import create_model_and_transforms, load_rgb, upright
|
| 19 |
from depth_pro_2.model import to_normalized_tensor
|
| 20 |
|
|
|
|
| 21 |
MAX_VIDEO_FRAMES = 100
|
| 22 |
|
| 23 |
+
# The weights and the focal-length estimator load at startup via the library's canonical
|
| 24 |
+
# loader (from_pretrained on the per-size repo leaves the FOV estimator unloaded, which makes
|
| 25 |
+
# infer return depth=None for photos without an EXIF focal length). The model is created on CPU
|
| 26 |
+
# and moved to CUDA inside the GPU-decorated functions, since ZeroGPU exposes the GPU only
|
| 27 |
+
# during those calls. bfloat16 is how the models run on a GPU.
|
| 28 |
+
model, _ = create_model_and_transforms(
|
| 29 |
+
"depthpro2-h", device="cpu", precision=torch.bfloat16
|
| 30 |
)
|
| 31 |
|
| 32 |
|
|
|
|
| 78 |
image.save(path)
|
| 79 |
img, f_px = load_rgb(path) # f_px: the focal length recorded in the photo, or None
|
| 80 |
net, transform = _device_and_model()
|
| 81 |
+
prediction = net.infer(transform(img), f_px=f_px)
|
| 82 |
+
if prediction["depth"] is None:
|
| 83 |
+
raise gr.Error("No focal length recorded in the photo and none could be estimated.")
|
| 84 |
+
depth = prediction["depth"] # meters, (H, W)
|
| 85 |
return Image.fromarray(_colorize(depth.float().cpu().numpy()))
|
| 86 |
|
| 87 |
|