File size: 1,851 Bytes
ab6fee2
 
 
201ca3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
---
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(
    "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")
```