| | --- |
| | library_name: keras-hub |
| | --- |
| | ### Model Overview |
| |
|
| |
|
| | ## Example Usage |
| | ```python |
| | import keras |
| | import numpy as np |
| | import requests |
| | from PIL import Image |
| | |
| | from keras_hub.src.models.depth_anything.depth_anything_depth_estimator import ( |
| | DepthAnythingDepthEstimator, |
| | ) |
| | |
| | image = Image.open(requests.get("http://images.cocodataset.org/val2017/000000039769.jpg", stream=True).raw) |
| | image = image.resize((518, 518)) |
| | depth_estimator = DepthAnythingDepthEstimator.from_preset( |
| | "depth_anything_v2_base, |
| | depth_estimation_type="relative", |
| | max_depth=None, |
| | ) |
| | images = np.expand_dims(np.array(image).astype("float32"), axis=0) |
| | outputs = depth_estimator.predict({"images": images})["depths"] |
| | depth = keras.ops.nn.relu(outputs[0, ..., 0]) |
| | depth = (depth - keras.ops.min(depth)) / ( |
| | keras.ops.max(depth) - keras.ops.min(depth) |
| | ) |
| | depth = keras.ops.convert_to_numpy(depth) * 255 |
| | Image.fromarray(depth.astype("uint8")).save("depth_map.png") |
| | ``` |
| |
|
| | ## Example Usage with Hugging Face URI |
| |
|
| | ```python |
| | import keras |
| | import numpy as np |
| | import requests |
| | from PIL import Image |
| | |
| | from keras_hub.src.models.depth_anything.depth_anything_depth_estimator import ( |
| | DepthAnythingDepthEstimator, |
| | ) |
| | |
| | image = Image.open(requests.get("http://images.cocodataset.org/val2017/000000039769.jpg", stream=True).raw) |
| | image = image.resize((518, 518)) |
| | depth_estimator = DepthAnythingDepthEstimator.from_preset( |
| | "hf://keras/depth_anything_v2_base, |
| | depth_estimation_type="relative", |
| | max_depth=None, |
| | ) |
| | images = np.expand_dims(np.array(image).astype("float32"), axis=0) |
| | outputs = depth_estimator.predict({"images": images})["depths"] |
| | depth = keras.ops.nn.relu(outputs[0, ..., 0]) |
| | depth = (depth - keras.ops.min(depth)) / ( |
| | keras.ops.max(depth) - keras.ops.min(depth) |
| | ) |
| | depth = keras.ops.convert_to_numpy(depth) * 255 |
| | Image.fromarray(depth.astype("uint8")).save("depth_map.png") |
| | ``` |
| |
|