Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,94 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
import torch
|
4 |
import numpy as np
|
5 |
-
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
10 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
# prepare image for the model
|
14 |
encoding = feature_extractor(image, return_tensors="pt")
|
15 |
-
|
16 |
# forward pass
|
17 |
with torch.no_grad():
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
# interpolate to original size
|
22 |
prediction = torch.nn.functional.interpolate(
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
output = prediction.cpu().numpy()
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from doctest import Example
|
2 |
import gradio as gr
|
3 |
+
from transformers import DPTImageProcessor, DPTForDepthEstimation
|
4 |
import torch
|
5 |
import numpy as np
|
6 |
+
from PIL import Image, ImageOps
|
7 |
+
from pathlib import Path
|
8 |
+
import glob
|
9 |
+
from autostereogram.converter import StereogramConverter
|
10 |
+
from datetime import datetime
|
11 |
+
import time
|
12 |
+
import tempfile
|
13 |
|
14 |
+
feature_extractor = DPTImageProcessor.from_pretrained("Intel/dpt-large")
|
|
|
|
|
15 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
16 |
|
17 |
+
stereo_converter = StereogramConverter()
|
18 |
+
|
19 |
+
|
20 |
+
def process_image(image_path):
|
21 |
+
print("\n\n\n")
|
22 |
+
print("Processing image:", image_path)
|
23 |
+
last_time = time.time()
|
24 |
+
image_raw = Image.open(Path(image_path))
|
25 |
+
|
26 |
+
image = image_raw.resize(
|
27 |
+
(1280, int(1280 * image_raw.size[1] / image_raw.size[0])),
|
28 |
+
Image.Resampling.LANCZOS,
|
29 |
+
)
|
30 |
+
|
31 |
# prepare image for the model
|
32 |
encoding = feature_extractor(image, return_tensors="pt")
|
33 |
+
|
34 |
# forward pass
|
35 |
with torch.no_grad():
|
36 |
+
outputs = model(**encoding)
|
37 |
+
predicted_depth = outputs.predicted_depth
|
38 |
+
|
39 |
# interpolate to original size
|
40 |
prediction = torch.nn.functional.interpolate(
|
41 |
+
predicted_depth.unsqueeze(1),
|
42 |
+
size=image.size[::-1],
|
43 |
+
mode="bicubic",
|
44 |
+
align_corners=False,
|
45 |
+
).squeeze()
|
46 |
output = prediction.cpu().numpy()
|
47 |
+
depth_image = (output * 255 / np.max(output)).astype("uint8")
|
48 |
+
depth_image_padded = np.array(
|
49 |
+
ImageOps.pad(Image.fromarray(depth_image), (1280, 720))
|
50 |
+
)
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
return depth_image_padded
|
55 |
+
|
56 |
+
|
57 |
+
examples_images = [[f] for f in sorted(glob.glob("examples/*.jpg"))]
|
58 |
+
|
59 |
+
|
60 |
+
with gr.Blocks() as blocks:
|
61 |
+
gr.Markdown(
|
62 |
+
"""
|
63 |
+
## Depth Image to Autostereogram (Magic Eye)
|
64 |
+
This demo is a variation from the original [DPT Demo](https://huggingface.co/spaces/nielsr/dpt-depth-estimation).
|
65 |
+
Zero-shot depth estimation from an image, then it uses [pystereogram](https://github.com/yxiao1996/pystereogram)
|
66 |
+
to generate the autostereogram (Magic Eye)
|
67 |
+
<base target="_blank">
|
68 |
+
"""
|
69 |
+
)
|
70 |
+
with gr.Row():
|
71 |
+
with gr.Column():
|
72 |
+
input_image = gr.Image(type="filepath", label="Input Image")
|
73 |
+
button = gr.Button("Predict")
|
74 |
+
with gr.Column():
|
75 |
+
predicted_depth = gr.Image(label="Predicted Depth", type="pil")
|
76 |
+
with gr.Row():
|
77 |
+
autostereogram = gr.Image(label="Autostereogram", type="pil")
|
78 |
+
with gr.Row():
|
79 |
+
with gr.Column():
|
80 |
+
file_download = gr.File(label="Download Image")
|
81 |
+
with gr.Row():
|
82 |
+
gr.Examples(
|
83 |
+
examples=examples_images,
|
84 |
+
fn=process_image,
|
85 |
+
inputs=[input_image],
|
86 |
+
outputs=predicted_depth,
|
87 |
+
cache_examples=True,
|
88 |
+
)
|
89 |
+
button.click(
|
90 |
+
fn=process_image,
|
91 |
+
inputs=[input_image],
|
92 |
+
outputs=predicted_depth,
|
93 |
+
)
|
94 |
+
blocks.launch(debug=True)
|