File size: 2,019 Bytes
3fce28b
 
 
5c8fbca
3fce28b
 
 
 
 
 
b83f529
5c8fbca
3fce28b
 
b83f529
4dc78b6
5c8fbca
 
 
 
 
 
 
 
 
 
 
3d2dcbc
5c8fbca
 
 
 
 
 
 
 
 
7d3edd2
5c8fbca
 
b83f529
5c8fbca
70daa10
5c8fbca
 
 
 
 
 
 
 
3d2dcbc
5c8fbca
3d2dcbc
3fce28b
 
d16f22f
3fce28b
 
 
 
4dc78b6
3fce28b
d16f22f
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
from depth import MidasDepth
import gradio as gr
import numpy as np
import tempfile


depth_estimator = MidasDepth()


def get_depth(rgb):
    print("Estimating depth...")
    rgb = rgb.convert("RGB")
    depth = depth_estimator.get_depth(rgb)

    print("Creating mesh...")
    w, h = rgb.size
    grid = np.mgrid[0:h, 0:w].transpose(1, 2, 0
                                        ).reshape(-1, 2)[..., ::-1]
    flat_grid = grid[:, 1] * w + grid[:, 0]

    positions = np.concatenate(((grid - np.array([[w, h]])
                                 / 2) / w * 2,
                                depth.flatten()[flat_grid][..., np.newaxis]),
                               axis=-1)
    positions[:, :-1] *= positions[:, -1:]
    positions[:, :2] *= -1

    pick_edges = depth < 0
    y, x = (t.flatten() for t in np.mgrid[0:h, 0:w])
    faces = np.concatenate((
        np.stack((y * w + x,
                  (y - 1) * w + x,
                  y * w + (x - 1)), axis=-1)
        [(~pick_edges.flatten()) * (x > 0) * (y > 0)],
        np.stack((y * w + x,
                  (y + 1) * w + x,
                  y * w + (x + 1)), axis=-1)
        [(~pick_edges.flatten()) * (x < w - 1) * (y < h - 1)]
    ))

    print("Writing...")
    tf = tempfile.NamedTemporaryFile(suffix=".obj").name
    save_obj(positions, np.asarray(rgb).reshape(-1, 3) / 255., faces, tf)

    return rgb, (depth.clip(0, 64) * 1024).astype("uint16"), tf


def save_obj(positions, rgb, faces, filename):
    with open(filename, "w") as f:
        for position, color in zip(positions, rgb):
            f.write(
                f"v {' '.join(map(str, position))} {' '.join(map(str, color))}\n")
        for face in faces:
            f.write(f"f {' '.join(map(str, face))}\n")


gr.Interface(fn=get_depth, inputs=[
    gr.components.Image(label="rgb", type="pil"),
], outputs=[
    gr.components.Image(type="pil", label="image"),
    gr.components.Image(type="numpy", label="depth"),
    gr.components.Model3D(label="3d model")

]).launch(share=True)