JUGGHM commited on
Commit
7a2c9ac
1 Parent(s): 9e5d7d5

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +88 -0
  2. requirements.txt +16 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ import torch.nn.functional as F
4
+ import logging
5
+ import os
6
+ import os.path as osp
7
+
8
+ import sys
9
+ CODE_SPACE=os.path.dirname(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
+ cfg = Config.fromfile('https://huggingface.co/JUGGHM/Metric3D/blob/main/mono/configs/_base_/models/encoder_decoder/dino_vit_small_reg.dpt_raft.py')
36
+
37
+ torch.hub.download_url_to_file('https://images.unsplash.com/photo-1437622368342-7a3d73a34c8f', 'turtle.jpg')
38
+ torch.hub.download_url_to_file('https://images.unsplash.com/photo-1519066629447-267fffa62d4b', 'lions.jpg')
39
+
40
+ model = get_configured_monodepth_model(cfg, )
41
+ model, _, _, _ = load_ckpt(cfg.load_from, model, strict_match=False)
42
+ model.eval()
43
+
44
+ device = "cpu"
45
+ model.to(device)
46
+
47
+ def depth_normal(img):
48
+ cv_image = np.array(img)
49
+ img = cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB)
50
+ intrinsic = [1000.0, 1000.0, img.shape[1]/2, img.shape[0]/2]
51
+ rgb_input, cam_models_stacks, pad, label_scale_factor = transform_test_data_scalecano(rgb_origin, intrinsic, cfg.data_basic)
52
+
53
+ with torch.no_grad():
54
+ pred_depth, pred_depth_scale, scale, output = get_prediction(
55
+ model = model,
56
+ input = rgb_input,
57
+ cam_model = cam_models_stacks,
58
+ pad_info = pad,
59
+ scale_info = label_scale_factor,
60
+ gt_depth = None,
61
+ normalize_scale = cfg.data_basic.depth_range[1],
62
+ ori_shape=[img.shape[0], img.shape[1]],
63
+ )
64
+
65
+ pred_normal = output['normal_out_list'][0][:, :3, :, :]
66
+ H, W = pred_normal.shape[2:]
67
+ pred_normal[:, :, pad[0]:H-pad[1], pad[2]:W-pad[3]]
68
+
69
+ output = pred_depth.cpu().numpy()
70
+ formatted = (output * 255 / np.max(output)).astype('uint8')
71
+ img = Image.fromarray(formatted)
72
+ return img
73
+
74
+
75
+ inputs = gr.inputs.Image(type='pil', label="Original Image")
76
+ depth = gr.outputs.Image(type="pil",label="Output Depth")
77
+ #normal = gr.outputs.Image(type="pil",label="Output Normal")
78
+
79
+ title = "Metric3DS"
80
+ 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."
81
+ 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>"
82
+
83
+ examples = [
84
+ ["turtle.jpg"],
85
+ ["lions.jpg"]
86
+ ]
87
+
88
+ gr.Interface(depth_normal, inputs, depth, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch(enable_queue=True,cache_examples=True)
requirements.txt ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ torch == 2.0.1
2
+ torchvision == 0.15.2
3
+ opencv-python
4
+ numpy == 1.23.1
5
+ xformers == 0.0.21.dev564
6
+ Pillow
7
+ DateTime
8
+ matplotlib
9
+ plyfile
10
+ HTML4Vision
11
+ timm
12
+ tensorboardX
13
+ imgaug
14
+ iopath
15
+ imagecorruptions
16
+ mmcv