Spaces:
Running
on
Zero
Running
on
Zero
Charlie Amalet
commited on
Commit
·
b6b254b
1
Parent(s):
5212158
Upload depth.py
Browse files
depth.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from zoedepth.utils.misc import colorize, save_raw_16bit
|
2 |
+
import torch
|
3 |
+
import gradio as gr
|
4 |
+
from PIL import Image
|
5 |
+
import numpy as np
|
6 |
+
from functools import partial
|
7 |
+
|
8 |
+
def save_raw_16bit(depth, fpath="raw.png"):
|
9 |
+
if isinstance(depth, torch.Tensor):
|
10 |
+
depth = depth.squeeze().cpu().numpy()
|
11 |
+
|
12 |
+
assert isinstance(depth, np.ndarray), "Depth must be a torch tensor or numpy array"
|
13 |
+
assert depth.ndim == 2, "Depth must be 2D"
|
14 |
+
depth = depth * 256 # scale for 16-bit png
|
15 |
+
depth = depth.astype(np.uint16)
|
16 |
+
return depth
|
17 |
+
|
18 |
+
# Your image processing function
|
19 |
+
def process_image(model, image: Image.Image):
|
20 |
+
image = image.convert("RGB")
|
21 |
+
|
22 |
+
out = model.infer_pil(image)
|
23 |
+
|
24 |
+
processed_array = save_raw_16bit(colorize(out)[:, :, 0])
|
25 |
+
return Image.fromarray(processed_array)
|
26 |
+
|
27 |
+
def depth_interface(model):
|
28 |
+
with gr.Row():
|
29 |
+
inputs=gr.Image(label="Input Image", type='pil') # Input is an image
|
30 |
+
outputs=gr.Image(label="Depth Map", type='pil') # Output is also an image
|
31 |
+
generate_btn = gr.Button(value="Generate")
|
32 |
+
generate_btn.click(partial(process_image, model), inputs=inputs, outputs=outputs, api_name="generate_depth")
|