Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -53,8 +53,6 @@ set_seed(666)
|
|
| 53 |
|
| 54 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 55 |
|
| 56 |
-
# DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 57 |
-
# DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 58 |
default_steps = 20
|
| 59 |
model = PixelPerfectDepth(sampling_steps=default_steps)
|
| 60 |
ckpt_path = hf_hub_download(
|
|
@@ -82,19 +80,13 @@ def main(share=True):
|
|
| 82 |
|
| 83 |
@(spaces.GPU if HUGGINFACE_SPACES_INSTALLED else (lambda x: x))
|
| 84 |
def predict_depth(image, denoise_steps):
|
| 85 |
-
# DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 86 |
-
# global model
|
| 87 |
-
# model = model.to(DEVICE)
|
| 88 |
depth, resize_image = model.infer_image(image, sampling_steps=denoise_steps)
|
| 89 |
return depth, resize_image
|
| 90 |
|
| 91 |
@(spaces.GPU if HUGGINFACE_SPACES_INSTALLED else (lambda x: x))
|
| 92 |
def predict_moge_depth(image):
|
| 93 |
-
# DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 94 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 95 |
image = torch.tensor(image / 255, dtype=torch.float32, device=DEVICE).permute(2, 0, 1)
|
| 96 |
-
# global moge_model
|
| 97 |
-
# moge_model = moge_model.to(DEVICE)
|
| 98 |
metric_depth, mask, intrinsics = moge_model.infer(image)
|
| 99 |
metric_depth[~mask] = metric_depth[mask].max()
|
| 100 |
return metric_depth, mask, intrinsics
|
|
@@ -102,9 +94,7 @@ def main(share=True):
|
|
| 102 |
def on_submit(image, denoise_steps, apply_filter, request: gr.Request = None):
|
| 103 |
|
| 104 |
H, W = image.shape[:2]
|
| 105 |
-
|
| 106 |
ppd_depth, resize_image = predict_depth(image[:, :, ::-1], denoise_steps)
|
| 107 |
-
|
| 108 |
resize_H, resize_W = resize_image.shape[:2]
|
| 109 |
|
| 110 |
# moge provide metric depth and intrinsics
|
|
@@ -132,12 +122,15 @@ def main(share=True):
|
|
| 132 |
ply_path = os.path.join(output_path, 'pointcloud.ply')
|
| 133 |
|
| 134 |
# save pcd to temporary .ply
|
|
|
|
|
|
|
|
|
|
| 135 |
o3d.io.write_point_cloud(ply_path, pcd)
|
| 136 |
-
|
| 137 |
vertices = np.asarray(pcd.points)
|
| 138 |
vertex_colors = (np.asarray(pcd.colors) * 255).astype(np.uint8)
|
| 139 |
-
vertices
|
| 140 |
-
vertices[:,
|
|
|
|
| 141 |
mesh = trimesh.PointCloud(vertices=vertices, colors=vertex_colors)
|
| 142 |
glb_path = os.path.join(output_path, 'pointcloud.glb')
|
| 143 |
mesh.export(glb_path)
|
|
@@ -172,7 +165,7 @@ def main(share=True):
|
|
| 172 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 173 |
gr.Markdown(title)
|
| 174 |
gr.Markdown(description)
|
| 175 |
-
gr.Markdown("### Depth Prediction demo")
|
| 176 |
|
| 177 |
with gr.Row():
|
| 178 |
# Left: input image + settings
|
|
|
|
| 53 |
|
| 54 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 55 |
|
|
|
|
|
|
|
| 56 |
default_steps = 20
|
| 57 |
model = PixelPerfectDepth(sampling_steps=default_steps)
|
| 58 |
ckpt_path = hf_hub_download(
|
|
|
|
| 80 |
|
| 81 |
@(spaces.GPU if HUGGINFACE_SPACES_INSTALLED else (lambda x: x))
|
| 82 |
def predict_depth(image, denoise_steps):
|
|
|
|
|
|
|
|
|
|
| 83 |
depth, resize_image = model.infer_image(image, sampling_steps=denoise_steps)
|
| 84 |
return depth, resize_image
|
| 85 |
|
| 86 |
@(spaces.GPU if HUGGINFACE_SPACES_INSTALLED else (lambda x: x))
|
| 87 |
def predict_moge_depth(image):
|
|
|
|
| 88 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 89 |
image = torch.tensor(image / 255, dtype=torch.float32, device=DEVICE).permute(2, 0, 1)
|
|
|
|
|
|
|
| 90 |
metric_depth, mask, intrinsics = moge_model.infer(image)
|
| 91 |
metric_depth[~mask] = metric_depth[mask].max()
|
| 92 |
return metric_depth, mask, intrinsics
|
|
|
|
| 94 |
def on_submit(image, denoise_steps, apply_filter, request: gr.Request = None):
|
| 95 |
|
| 96 |
H, W = image.shape[:2]
|
|
|
|
| 97 |
ppd_depth, resize_image = predict_depth(image[:, :, ::-1], denoise_steps)
|
|
|
|
| 98 |
resize_H, resize_W = resize_image.shape[:2]
|
| 99 |
|
| 100 |
# moge provide metric depth and intrinsics
|
|
|
|
| 122 |
ply_path = os.path.join(output_path, 'pointcloud.ply')
|
| 123 |
|
| 124 |
# save pcd to temporary .ply
|
| 125 |
+
pcd.points = o3d.utility.Vector3dVector(
|
| 126 |
+
np.asarray(pcd.points) * np.array([1, -1, -1], dtype=np.float32)
|
| 127 |
+
)
|
| 128 |
o3d.io.write_point_cloud(ply_path, pcd)
|
|
|
|
| 129 |
vertices = np.asarray(pcd.points)
|
| 130 |
vertex_colors = (np.asarray(pcd.colors) * 255).astype(np.uint8)
|
| 131 |
+
# vertices = vertices * np.array([1, -1, -1], dtype=np.float32)
|
| 132 |
+
# vertices[:, 2] = -vertices[:, 2]
|
| 133 |
+
# vertices[:, 1] = -vertices[:, 1]
|
| 134 |
mesh = trimesh.PointCloud(vertices=vertices, colors=vertex_colors)
|
| 135 |
glb_path = os.path.join(output_path, 'pointcloud.glb')
|
| 136 |
mesh.export(glb_path)
|
|
|
|
| 165 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 166 |
gr.Markdown(title)
|
| 167 |
gr.Markdown(description)
|
| 168 |
+
gr.Markdown("### Point Cloud & Depth Prediction demo")
|
| 169 |
|
| 170 |
with gr.Row():
|
| 171 |
# Left: input image + settings
|