Spaces:
Running
Running
Commit
•
3a22773
0
Parent(s):
Duplicate from pytorch/MiDaS
Browse filesCo-authored-by: Julien Chaumond <julien-c@users.noreply.huggingface.co>
- .gitattributes +16 -0
- README.md +34 -0
- app.py +63 -0
- requirements.txt +6 -0
.gitattributes
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.bin.* filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.tar.gz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: MiDaS
|
3 |
+
emoji: 😻
|
4 |
+
colorFrom: pink
|
5 |
+
colorTo: yellow
|
6 |
+
sdk: gradio
|
7 |
+
app_file: app.py
|
8 |
+
pinned: false
|
9 |
+
duplicated_from: pytorch/MiDaS
|
10 |
+
---
|
11 |
+
|
12 |
+
# Configuration
|
13 |
+
|
14 |
+
`title`: _string_
|
15 |
+
Display title for the Space
|
16 |
+
|
17 |
+
`emoji`: _string_
|
18 |
+
Space emoji (emoji-only character allowed)
|
19 |
+
|
20 |
+
`colorFrom`: _string_
|
21 |
+
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
22 |
+
|
23 |
+
`colorTo`: _string_
|
24 |
+
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
25 |
+
|
26 |
+
`sdk`: _string_
|
27 |
+
Can be either `gradio` or `streamlit`
|
28 |
+
|
29 |
+
`app_file`: _string_
|
30 |
+
Path to your main application file (which contains either `gradio` or `streamlit` Python code).
|
31 |
+
Path is relative to the root of the repository.
|
32 |
+
|
33 |
+
`pinned`: _boolean_
|
34 |
+
Whether the Space stays on top of your list.
|
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import torch
|
3 |
+
import gradio as gr
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
torch.hub.download_url_to_file('https://images.unsplash.com/photo-1437622368342-7a3d73a34c8f', 'turtle.jpg')
|
8 |
+
torch.hub.download_url_to_file('https://images.unsplash.com/photo-1519066629447-267fffa62d4b', 'lions.jpg')
|
9 |
+
|
10 |
+
midas = torch.hub.load("intel-isl/MiDaS", "MiDaS")
|
11 |
+
|
12 |
+
use_large_model = True
|
13 |
+
|
14 |
+
if use_large_model:
|
15 |
+
midas = torch.hub.load("intel-isl/MiDaS", "MiDaS")
|
16 |
+
else:
|
17 |
+
midas = torch.hub.load("intel-isl/MiDaS", "MiDaS_small")
|
18 |
+
|
19 |
+
device = "cpu"
|
20 |
+
midas.to(device)
|
21 |
+
|
22 |
+
midas_transforms = torch.hub.load("intel-isl/MiDaS", "transforms")
|
23 |
+
|
24 |
+
if use_large_model:
|
25 |
+
transform = midas_transforms.default_transform
|
26 |
+
else:
|
27 |
+
transform = midas_transforms.small_transform
|
28 |
+
|
29 |
+
|
30 |
+
def depth(img):
|
31 |
+
cv_image = np.array(img)
|
32 |
+
img = cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB)
|
33 |
+
|
34 |
+
input_batch = transform(img).to(device)
|
35 |
+
with torch.no_grad():
|
36 |
+
prediction = midas(input_batch)
|
37 |
+
|
38 |
+
prediction = torch.nn.functional.interpolate(
|
39 |
+
prediction.unsqueeze(1),
|
40 |
+
size=img.shape[:2],
|
41 |
+
mode="bicubic",
|
42 |
+
align_corners=False,
|
43 |
+
).squeeze()
|
44 |
+
|
45 |
+
output = prediction.cpu().numpy()
|
46 |
+
formatted = (output * 255 / np.max(output)).astype('uint8')
|
47 |
+
img = Image.fromarray(formatted)
|
48 |
+
return img
|
49 |
+
|
50 |
+
|
51 |
+
inputs = gr.inputs.Image(type='pil', label="Original Image")
|
52 |
+
outputs = gr.outputs.Image(type="pil",label="Output Image")
|
53 |
+
|
54 |
+
title = "MiDaS"
|
55 |
+
description = "Gradio demo for MiDaS v2.1 which takes in a single image for computing relative depth. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
|
56 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1907.01341v3'>Towards Robust Monocular Depth Estimation: Mixing Datasets for Zero-shot Cross-dataset Transfer</a> | <a href='https://github.com/intel-isl/MiDaS'>Github Repo</a></p>"
|
57 |
+
|
58 |
+
examples = [
|
59 |
+
["turtle.jpg"],
|
60 |
+
["lions.jpg"]
|
61 |
+
]
|
62 |
+
|
63 |
+
gr.Interface(depth, inputs, outputs, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch(enable_queue=True,cache_examples=True)
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
timm
|
2 |
+
torch~=1.8
|
3 |
+
torchvision
|
4 |
+
opencv-python-headless
|
5 |
+
numpy
|
6 |
+
Pillow
|