Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,82 @@ import os.path as osp
|
|
8 |
import sys
|
9 |
CODE_SPACE=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
import sys
|
9 |
CODE_SPACE=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
10 |
|
11 |
+
try:
|
12 |
+
from mmcv.utils import Config, DictAction
|
13 |
+
except:
|
14 |
+
from mmengine import Config, DictAction
|
15 |
+
from mono.utils.logger import setup_logger
|
16 |
+
import glob
|
17 |
+
from mono.utils.comm import init_env
|
18 |
+
from mono.model.monodepth_model import get_configured_monodepth_model
|
19 |
+
from mono.utils.running import load_ckpt
|
20 |
+
from mono.utils.do_test import transform_test_data_scalecano, get_prediction
|
21 |
+
from mono.utils.custom_data import load_from_annos, load_data
|
22 |
|
23 |
+
from mono.utils.avg_meter import MetricAverageMeter
|
24 |
+
from mono.utils.visualization import save_val_imgs, create_html, save_raw_imgs, save_normal_val_imgs
|
25 |
+
import cv2
|
26 |
+
from tqdm import tqdm
|
27 |
+
import numpy as np
|
28 |
+
from PIL import Image
|
29 |
+
import matplotlib.pyplot as plt
|
30 |
|
31 |
+
from mono.utils.unproj_pcd import reconstruct_pcd, save_point_cloud
|
32 |
+
import gradio as gr
|
33 |
+
|
34 |
+
os.chdir(CODE_SPACE)
|
35 |
+
print(osp.file.exists('./mono/configs/_base_/models/encoder_decoder/dino_vit_small_reg.dpt_raft.py'))
|
36 |
+
cfg = Config.fromfile('./mono/configs/_base_/models/encoder_decoder/dino_vit_small_reg.dpt_raft.py')
|
37 |
+
|
38 |
+
torch.hub.download_url_to_file('https://images.unsplash.com/photo-1437622368342-7a3d73a34c8f', 'turtle.jpg')
|
39 |
+
torch.hub.download_url_to_file('https://images.unsplash.com/photo-1519066629447-267fffa62d4b', 'lions.jpg')
|
40 |
+
|
41 |
+
model = get_configured_monodepth_model(cfg, )
|
42 |
+
model, _, _, _ = load_ckpt('./weight/metric_depth_vit_small_800k.pth', model, strict_match=False)
|
43 |
+
model.eval()
|
44 |
+
|
45 |
+
device = "cpu"
|
46 |
+
model.to(device)
|
47 |
+
|
48 |
+
def depth_normal(img):
|
49 |
+
cv_image = np.array(img)
|
50 |
+
img = cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB)
|
51 |
+
intrinsic = [1000.0, 1000.0, img.shape[1]/2, img.shape[0]/2]
|
52 |
+
rgb_input, cam_models_stacks, pad, label_scale_factor = transform_test_data_scalecano(rgb_origin, intrinsic, cfg.data_basic)
|
53 |
+
|
54 |
+
with torch.no_grad():
|
55 |
+
pred_depth, pred_depth_scale, scale, output = get_prediction(
|
56 |
+
model = model,
|
57 |
+
input = rgb_input,
|
58 |
+
cam_model = cam_models_stacks,
|
59 |
+
pad_info = pad,
|
60 |
+
scale_info = label_scale_factor,
|
61 |
+
gt_depth = None,
|
62 |
+
normalize_scale = cfg.data_basic.depth_range[1],
|
63 |
+
ori_shape=[img.shape[0], img.shape[1]],
|
64 |
+
)
|
65 |
+
|
66 |
+
pred_normal = output['normal_out_list'][0][:, :3, :, :]
|
67 |
+
H, W = pred_normal.shape[2:]
|
68 |
+
pred_normal[:, :, pad[0]:H-pad[1], pad[2]:W-pad[3]]
|
69 |
+
|
70 |
+
output = pred_depth.cpu().numpy()
|
71 |
+
formatted = (output * 255 / np.max(output)).astype('uint8')
|
72 |
+
img = Image.fromarray(formatted)
|
73 |
+
return img
|
74 |
+
|
75 |
+
|
76 |
+
inputs = gr.inputs.Image(type='pil', label="Original Image")
|
77 |
+
depth = gr.outputs.Image(type="pil",label="Output Depth")
|
78 |
+
#normal = gr.outputs.Image(type="pil",label="Output Normal")
|
79 |
+
|
80 |
+
title = "Metric3DS"
|
81 |
+
description = "Gradio demo for Metric3DS (v2) which takes in a single image for computing metric depth and surface normal. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
|
82 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/pdf/2307.10984.pdf'>Metric3DS: Towards Zero-shot Metric 3D and Shape Prediction from A Single Image</a> | <a href='https://github.com/YvanYin/Metric3D'>Github Repo</a></p>"
|
83 |
+
|
84 |
+
examples = [
|
85 |
+
["turtle.jpg"],
|
86 |
+
["lions.jpg"]
|
87 |
+
]
|
88 |
+
|
89 |
+
gr.Interface(depth_normal, inputs, depth, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch(enable_queue=True,cache_examples=True)
|