File size: 3,706 Bytes
2acb2ce
 
 
 
 
 
 
 
 
05e4845
2acb2ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05e4845
 
 
 
2acb2ce
 
05e4845
2acb2ce
05e4845
2acb2ce
05e4845
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2acb2ce
 
05e4845
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from doctest import Example
import gradio as gr
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
import torch
import numpy as np
from PIL import Image, ImageOps
from pathlib import Path
import glob
from autostereogram.sirds_converter import SirdsConverter
from datetime import datetime

feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")

stereo_converter = SirdsConverter()


def process_image(image_path):
    image_raw = Image.open(Path(image_path))

    image = image_raw.resize(
        (1280, int(1280 * image_raw.size[1] / image_raw.size[0])),
        Image.Resampling.LANCZOS)

    # prepare image for the model
    encoding = feature_extractor(image, return_tensors="pt")

    # forward pass
    with torch.no_grad():
        outputs = model(**encoding)
        predicted_depth = outputs.predicted_depth

    # interpolate to original size
    prediction = torch.nn.functional.interpolate(
        predicted_depth.unsqueeze(1),
        size=image.size[::-1],
        mode="bicubic",
        align_corners=False,
    ).squeeze()
    output = prediction.cpu().numpy()
    depth_image = (output * 255 / np.max(output)).astype('uint8')
    depth_image_padded = np.array(ImageOps.pad(
        Image.fromarray(depth_image), (1280, 720)))

    stereo_image = stereo_converter.convert_depth_to_stereogram_with_sird(
        depth_image_padded, False, 0.5).astype(np.uint8)

    stereo_image_pil = Image.fromarray(stereo_image).convert('RGB')
    image_name = f'stereo_image_{datetime.now().strftime("%Y%m%d_%H%M%S")}.jpg'
    stereo_image_pil.save(image_name)
    return [depth_image_padded, stereo_image, image_name]


examples_images = [[f] for f in sorted(glob.glob('examples/*.jpg'))]

blocks = gr.Blocks()

input_image = gr.Image(type="filepath", label="Input Image")
predicted_depth = gr.Image(label="Predicted Depth", type="pil")
autostereogram = gr.Image(label="Autostereogram", type="pil")
file_download = gr.File(label="Download Image")


def load_example(example_id):
    processed_examples = [
        component.preprocess_example(sample)
        for component, sample in zip(
            [input_image], examples_images[example_id]
        )
    ]
    if len(processed_examples) == 1:
        return processed_examples[0]
    else:
        return processed_examples


with blocks:
    gr.Markdown('''
## Depth Image to Autostereogram (Magic Eye)
This demo is a variation from the original [DPT Demo](https://huggingface.co/spaces/nielsr/dpt-depth-estimation).
Zero-shot depth estimation from an image, then it uses [pystereogram](https://github.com/yxiao1996/pystereogram)
to generate the autostereogram (Magic Eye)
<base target="_blank">

''')

    with gr.Row():
        examples_c = gr.components.Dataset(
            components=[input_image],
            samples=examples_images,
            type="index",
        )

        examples_c._click_no_postprocess(
            load_example,
            inputs=[examples_c],
            outputs=[input_image])

    with gr.Row():
        with gr.Column():
            button = gr.Button("Predict")
            button.click(fn=process_image, inputs=[input_image],
                         outputs=[predicted_depth,
                                  autostereogram, file_download],
                         )

    with gr.Row():
        with gr.Column():
            input_image.render()
        with gr.Column():
            predicted_depth.render()
    with gr.Row():
        autostereogram.render()
    with gr.Row():
        with gr.Column():
            file_download.render()

if __name__ == "__main__":
    blocks.launch(debug=True)