Spaces:
Runtime error
Runtime error
neverix
commited on
Commit
·
2ddc005
1
Parent(s):
e2dc8d7
First prototype?
Browse files- app.py +90 -1
- packages.txt +1 -0
app.py
CHANGED
@@ -1,7 +1,13 @@
|
|
|
|
1 |
from PIL import Image
|
2 |
import gradio as gr
|
3 |
import numpy as np
|
|
|
|
|
|
|
4 |
import torch
|
|
|
|
|
5 |
|
6 |
|
7 |
class MidasDepth(object):
|
@@ -27,9 +33,91 @@ class MidasDepth(object):
|
|
27 |
return prediction.detach().cpu().numpy()
|
28 |
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
def main():
|
|
|
|
|
31 |
midas = MidasDepth()
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
gr.inputs.Image(label="src", type="numpy"),
|
34 |
gr.inputs.Number(label="tx", default=0.0),
|
35 |
gr.inputs.Number(label="ty", default=0.0),
|
@@ -38,6 +126,7 @@ def main():
|
|
38 |
gr.inputs.Number(label="ry", default=0.0),
|
39 |
gr.inputs.Number(label="rz", default=0.0)
|
40 |
], outputs=[
|
|
|
41 |
gr.outputs.Image(type="numpy"),
|
42 |
gr.outputs.Video()
|
43 |
], title="DALL·E 6D", description="Lift DALL·E 2 (or any other model) into 3D!")
|
|
|
1 |
+
from tqdm.auto import trange
|
2 |
from PIL import Image
|
3 |
import gradio as gr
|
4 |
import numpy as np
|
5 |
+
import pyrender
|
6 |
+
import trimesh
|
7 |
+
import scipy
|
8 |
import torch
|
9 |
+
import cv2
|
10 |
+
import os
|
11 |
|
12 |
|
13 |
class MidasDepth(object):
|
|
|
33 |
return prediction.detach().cpu().numpy()
|
34 |
|
35 |
|
36 |
+
def process_depth(dep):
|
37 |
+
depth = dep.copy()
|
38 |
+
depth -= depth.min()
|
39 |
+
depth /= depth.max()
|
40 |
+
depth = 1 / np.clip(depth, 0.2, 1)
|
41 |
+
blurred = cv2.medianBlur(depth, 5) # 9 not available because it requires 8-bit
|
42 |
+
maxd = cv2.dilate(blurred, np.ones((3, 3)))
|
43 |
+
mind = cv2.erode(blurred, np.ones((3, 3)))
|
44 |
+
edges = maxd - mind
|
45 |
+
threshold = .05 # Better to have false positives
|
46 |
+
pick_edges = edges > threshold
|
47 |
+
return depth, pick_edges
|
48 |
+
|
49 |
+
|
50 |
+
def make_mesh(pic, depth, pick_edges):
|
51 |
+
faces = []
|
52 |
+
im = np.asarray(pic)
|
53 |
+
grid = np.mgrid[0:im.shape[0], 0:im.shape[1]].transpose(1, 2, 0
|
54 |
+
).reshape(-1, 2)[..., ::-1]
|
55 |
+
flat_grid = grid[:, 1] * im.shape[1] + grid[:, 0]
|
56 |
+
positions = np.concatenate(((grid - np.array(im.shape[:-1])[np.newaxis, :]
|
57 |
+
/ 2) / im.shape[1] * 2,
|
58 |
+
depth.flatten()[flat_grid][..., np.newaxis]),
|
59 |
+
axis=-1)
|
60 |
+
positions[:, :-1] *= positions[:, -1:]
|
61 |
+
positions[:, 1] *= -1
|
62 |
+
colors = im.reshape(-1, 3)[flat_grid]
|
63 |
+
|
64 |
+
c = lambda x, y: y * im.shape[1] + x
|
65 |
+
for y in trange(im.shape[0]):
|
66 |
+
for x in range(im.shape[1]):
|
67 |
+
if pick_edges[y, x]:
|
68 |
+
continue
|
69 |
+
if x > 0 and y > 0:
|
70 |
+
faces.append([c(x, y), c(x, y - 1), c(x - 1, y)])
|
71 |
+
if x < im.shape[1] - 1 and y < im.shape[0] - 1:
|
72 |
+
faces.append([c(x, y), c(x, y + 1), c(x + 1, y)])
|
73 |
+
face_colors = np.asarray([colors[i[0]] for i in faces])
|
74 |
+
|
75 |
+
tri_mesh = trimesh.Trimesh(vertices=positions * np.array([1.0, 1.0, -1.0]),
|
76 |
+
faces=faces,
|
77 |
+
face_colors=np.concatenate((face_colors,
|
78 |
+
face_colors[..., -1:]
|
79 |
+
* 0 + 255),
|
80 |
+
axis=-1).reshape(-1, 4),
|
81 |
+
smooth=False,
|
82 |
+
)
|
83 |
+
|
84 |
+
return tri_mesh
|
85 |
+
|
86 |
+
|
87 |
+
def args_to_mat(tx, ty, tz, rx, ry, rz):
|
88 |
+
mat = np.eye(4)
|
89 |
+
mat[:3, :3] = scipy.spatial.Rotation.from_euler("XYZ", (rx, ry, rz)).as_matrix()
|
90 |
+
mat[:3, 3] = tx, ty, tz
|
91 |
+
return mat
|
92 |
+
|
93 |
+
|
94 |
+
def render(mesh, mat):
|
95 |
+
scene = pyrender.Scene(ambient_light=np.array([1.0, 1.0, 1.0]))
|
96 |
+
camera = pyrender.PerspectiveCamera(yfov=np.pi / 2, aspectRatio=1.0)
|
97 |
+
scene.add(camera, pose=mat)
|
98 |
+
scene.add(mesh)
|
99 |
+
r = pyrender.OffscreenRenderer(1024, 1024)
|
100 |
+
rgb, d = r.render(scene, pyrender.constants.RenderFlags.FLAT)
|
101 |
+
mask = d == 0
|
102 |
+
rgb = rgb.copy()
|
103 |
+
rgb[mask] = 0
|
104 |
+
res = Image.fromarray(np.concatenate((rgb,
|
105 |
+
((mask[..., np.newaxis]) == 0)
|
106 |
+
.astype(np.uint8) * 255), axis=-1))
|
107 |
+
return res
|
108 |
+
|
109 |
+
|
110 |
def main():
|
111 |
+
os.environ["PYOPENGL_PLATFORM"] = "osmesa"
|
112 |
+
|
113 |
midas = MidasDepth()
|
114 |
+
def fn(pic, *args):
|
115 |
+
depth, pick_edges = process_depth(midas.get_depth(pic))
|
116 |
+
mesh = make_mesh(pic, depth, pick_edges)
|
117 |
+
frame = render(mesh, args_to_mat(*args))
|
118 |
+
return np.asarray(frame), (255 / np.asarray(depth)).astype(np.uint8), None
|
119 |
+
|
120 |
+
interface = gr.Interface(fn=fn, inputs=[
|
121 |
gr.inputs.Image(label="src", type="numpy"),
|
122 |
gr.inputs.Number(label="tx", default=0.0),
|
123 |
gr.inputs.Number(label="ty", default=0.0),
|
|
|
126 |
gr.inputs.Number(label="ry", default=0.0),
|
127 |
gr.inputs.Number(label="rz", default=0.0)
|
128 |
], outputs=[
|
129 |
+
gr.outputs.Image(type="numpy"),
|
130 |
gr.outputs.Image(type="numpy"),
|
131 |
gr.outputs.Video()
|
132 |
], title="DALL·E 6D", description="Lift DALL·E 2 (or any other model) into 3D!")
|
packages.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
mesa-utils
|