Views
No views yet
1pip install -U -q keras-hub
2pip install -U -q keras| Preset | Parameters | Description |
|---|---|---|
depth_anything_v2_small | ~24.8M | The most lightweight variant, based on ViT-Small. Ideal for real-time mobile and edge applications. |
depth_anything_v2_base | ~97.5M | A mid-sized model based on ViT-Base. Offers a strong balance between speed and precision. |
depth_anything_v2_large | ~335.3M | The most powerful variant based on ViT-Large. Delivers state-of-the-art accuracy and fine-grained depth detail. |
1import keras
2import numpy as np
3import requests
4from PIL import Image
5
6from keras_hub.src.models.depth_anything.depth_anything_depth_estimator import (
7 DepthAnythingDepthEstimator,
8)
9
10image = Image.open(requests.get("http://images.cocodataset.org/val2017/000000039769.jpg", stream=True).raw)
11image = image.resize((518, 518))
12depth_estimator = DepthAnythingDepthEstimator.from_preset(
13 "depth_anything_v2_base,
14 depth_estimation_type="relative",
15 max_depth=None,
16)
17images = np.expand_dims(np.array(image).astype("float32"), axis=0)
18outputs = depth_estimator.predict({"images": images})["depths"]
19depth = keras.ops.nn.relu(outputs[0, ..., 0])
20depth = (depth - keras.ops.min(depth)) / (
21 keras.ops.max(depth) - keras.ops.min(depth)
22)
23depth = keras.ops.convert_to_numpy(depth) * 255
24Image.fromarray(depth.astype("uint8")).save("depth_map.png")1import keras
2import numpy as np
3import requests
4from PIL import Image
5
6from keras_hub.src.models.depth_anything.depth_anything_depth_estimator import (
7 DepthAnythingDepthEstimator,
8)
9
10image = Image.open(requests.get("http://images.cocodataset.org/val2017/000000039769.jpg", stream=True).raw)
11image = image.resize((518, 518))
12depth_estimator = DepthAnythingDepthEstimator.from_preset(
13 "depth_anything_v2_base,
14 depth_estimation_type="relative",
15 max_depth=None,
16)
17images = np.expand_dims(np.array(image).astype("float32"), axis=0)
18outputs = depth_estimator.predict({"images": images})["depths"]
19depth = keras.ops.nn.relu(outputs[0, ..., 0])
20depth = (depth - keras.ops.min(depth)) / (
21 keras.ops.max(depth) - keras.ops.min(depth)
22)
23depth = keras.ops.convert_to_numpy(depth) * 255
24Image.fromarray(depth.astype("uint8")).save("depth_map.png")