Spaces:
Build error
Build error
add examples
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import numpy as np
|
|
| 5 |
from PIL import Image
|
| 6 |
import open3d as o3d
|
| 7 |
from pathlib import Path
|
|
|
|
| 8 |
|
| 9 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
| 10 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
|
@@ -12,8 +13,11 @@ model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
|
| 12 |
|
| 13 |
def process_image(image_path):
|
| 14 |
image_path = Path(image_path)
|
| 15 |
-
|
| 16 |
-
image =
|
|
|
|
|
|
|
|
|
|
| 17 |
# prepare image for the model
|
| 18 |
encoding = feature_extractor(image, return_tensors="pt")
|
| 19 |
|
|
@@ -57,17 +61,20 @@ def create_3d_obj(rgb_image, depth_image, image_path):
|
|
| 57 |
pcd.estimate_normals(
|
| 58 |
search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.01, max_nn=30))
|
| 59 |
pcd.transform([[1, 0, 0, 0],
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
print('run Poisson surface reconstruction')
|
| 66 |
with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug) as cm:
|
| 67 |
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 68 |
pcd, depth=10, width=0, scale=1.1, linear_fit=True)
|
| 69 |
|
| 70 |
-
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) /
|
| 71 |
print(f'voxel_size = {voxel_size:e}')
|
| 72 |
mesh = mesh_raw.simplify_vertex_clustering(
|
| 73 |
voxel_size=voxel_size,
|
|
@@ -86,8 +93,7 @@ def create_3d_obj(rgb_image, depth_image, image_path):
|
|
| 86 |
|
| 87 |
title = "Demo: zero-shot depth estimation with DPT + 3D Point Cloud"
|
| 88 |
description = "This demo is a variation from the original <a href='https://huggingface.co/spaces/nielsr/dpt-depth-estimation' target='_blank'>DPT Demo</a>. It uses the DPT model to predict the depth of an image and then uses 3D Point Cloud to create a 3D object."
|
| 89 |
-
examples = [[
|
| 90 |
-
['./examples/amber-kipp-75715CVEJhI-unsplash.jpeg']]
|
| 91 |
|
| 92 |
iface = gr.Interface(fn=process_image,
|
| 93 |
inputs=[gr.inputs.Image(
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import open3d as o3d
|
| 7 |
from pathlib import Path
|
| 8 |
+
import os
|
| 9 |
|
| 10 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
| 11 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
|
|
|
| 13 |
|
| 14 |
def process_image(image_path):
|
| 15 |
image_path = Path(image_path)
|
| 16 |
+
image_raw = Image.open(image_path)
|
| 17 |
+
image = image_raw.resize(
|
| 18 |
+
(800, int(800 * image_raw.size[1] / image_raw.size[0])),
|
| 19 |
+
Image.Resampling.LANCZOS)
|
| 20 |
+
|
| 21 |
# prepare image for the model
|
| 22 |
encoding = feature_extractor(image, return_tensors="pt")
|
| 23 |
|
|
|
|
| 61 |
pcd.estimate_normals(
|
| 62 |
search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.01, max_nn=30))
|
| 63 |
pcd.transform([[1, 0, 0, 0],
|
| 64 |
+
[0, -1, 0, 0],
|
| 65 |
+
[0, 0, -1, 0],
|
| 66 |
+
[0, 0, 0, 1]])
|
| 67 |
+
pcd.transform([[-1, 0, 0, 0],
|
| 68 |
+
[0, 1, 0, 0],
|
| 69 |
+
[0, 0, 1, 0],
|
| 70 |
+
[0, 0, 0, 1]])
|
| 71 |
|
| 72 |
print('run Poisson surface reconstruction')
|
| 73 |
with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug) as cm:
|
| 74 |
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 75 |
pcd, depth=10, width=0, scale=1.1, linear_fit=True)
|
| 76 |
|
| 77 |
+
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 256
|
| 78 |
print(f'voxel_size = {voxel_size:e}')
|
| 79 |
mesh = mesh_raw.simplify_vertex_clustering(
|
| 80 |
voxel_size=voxel_size,
|
|
|
|
| 93 |
|
| 94 |
title = "Demo: zero-shot depth estimation with DPT + 3D Point Cloud"
|
| 95 |
description = "This demo is a variation from the original <a href='https://huggingface.co/spaces/nielsr/dpt-depth-estimation' target='_blank'>DPT Demo</a>. It uses the DPT model to predict the depth of an image and then uses 3D Point Cloud to create a 3D object."
|
| 96 |
+
examples = [["examples/" + img] for img in os.listdir("examples/")]
|
|
|
|
| 97 |
|
| 98 |
iface = gr.Interface(fn=process_image,
|
| 99 |
inputs=[gr.inputs.Image(
|
examples/1-jonathan-borba-CgWTqYxHEkg-unsplash.jpg
ADDED
|
examples/2-ronan-furuta-cvM7AC22dSI-unsplash.jpg
ADDED
|
examples/3-artem-beliaikin-vyxOD0NuJbs-unsplash.jpg
ADDED
|
examples/4-thomas-le-pRJhn4MbsMM-unsplash.jpg
ADDED
|
examples/5-amber-kipp-75715CVEJhI-unsplash.jpg
ADDED
|