Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
|
| 11 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
| 12 |
|
| 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(
|
|
@@ -19,23 +19,25 @@ def process_image(image_path):
|
|
| 19 |
Image.Resampling.LANCZOS,
|
| 20 |
)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
try:
|
| 40 |
gltf_path = create_3d_obj(np.array(image), depth_image, image_path)
|
| 41 |
img = Image.fromarray(depth_image)
|
|
@@ -89,8 +91,6 @@ def create_3d_obj(rgb_image, depth_image, image_path, depth=10):
|
|
| 89 |
contraction=o3d.geometry.SimplificationContraction.Average,
|
| 90 |
)
|
| 91 |
|
| 92 |
-
# vertices_to_remove = densities < np.quantile(densities, 0.001)
|
| 93 |
-
# mesh.remove_vertices_by_mask(vertices_to_remove)
|
| 94 |
bbox = pcd.get_axis_aligned_bounding_box()
|
| 95 |
mesh_crop = mesh.crop(bbox)
|
| 96 |
gltf_path = f"./{image_path.stem}.gltf"
|
|
@@ -98,17 +98,20 @@ def create_3d_obj(rgb_image, depth_image, image_path, depth=10):
|
|
| 98 |
return gltf_path
|
| 99 |
|
| 100 |
|
| 101 |
-
title = "Demo:
|
| 102 |
-
description = "This demo
|
| 103 |
examples = [["examples/" + img] for img in os.listdir("examples/")]
|
| 104 |
|
| 105 |
iface = gr.Interface(
|
| 106 |
fn=process_image,
|
| 107 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
| 108 |
outputs=[
|
| 109 |
-
gr.Image(label="
|
| 110 |
-
gr.Model3D(label="
|
| 111 |
-
gr.File(label="
|
| 112 |
],
|
| 113 |
title=title,
|
| 114 |
description=description,
|
|
|
|
| 11 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
| 12 |
|
| 13 |
|
| 14 |
+
def process_image(image_path, depth_map_path=None):
|
| 15 |
image_path = Path(image_path)
|
| 16 |
image_raw = Image.open(image_path)
|
| 17 |
image = image_raw.resize(
|
|
|
|
| 19 |
Image.Resampling.LANCZOS,
|
| 20 |
)
|
| 21 |
|
| 22 |
+
if depth_map_path:
|
| 23 |
+
# Load the user-provided depth map
|
| 24 |
+
depth_image = np.array(Image.open(depth_map_path))
|
| 25 |
+
else:
|
| 26 |
+
# Automatically generate the depth map using the model
|
| 27 |
+
encoding = feature_extractor(image, return_tensors="pt")
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
outputs = model(**encoding)
|
| 30 |
+
predicted_depth = outputs.predicted_depth
|
| 31 |
+
|
| 32 |
+
# Interpolate to original size
|
| 33 |
+
prediction = torch.nn.functional.interpolate(
|
| 34 |
+
predicted_depth.unsqueeze(1),
|
| 35 |
+
size=image.size[::-1],
|
| 36 |
+
mode="bicubic",
|
| 37 |
+
align_corners=False,
|
| 38 |
+
).squeeze()
|
| 39 |
+
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 40 |
+
|
| 41 |
try:
|
| 42 |
gltf_path = create_3d_obj(np.array(image), depth_image, image_path)
|
| 43 |
img = Image.fromarray(depth_image)
|
|
|
|
| 91 |
contraction=o3d.geometry.SimplificationContraction.Average,
|
| 92 |
)
|
| 93 |
|
|
|
|
|
|
|
| 94 |
bbox = pcd.get_axis_aligned_bounding_box()
|
| 95 |
mesh_crop = mesh.crop(bbox)
|
| 96 |
gltf_path = f"./{image_path.stem}.gltf"
|
|
|
|
| 98 |
return gltf_path
|
| 99 |
|
| 100 |
|
| 101 |
+
title = "Demo: Depth Estimation & 3D Reconstruction with DPT + Point Cloud"
|
| 102 |
+
description = "This demo allows users to provide an RGB image and optionally a depth map to create a 3D object. If no depth map is provided, the DPT model will generate it."
|
| 103 |
examples = [["examples/" + img] for img in os.listdir("examples/")]
|
| 104 |
|
| 105 |
iface = gr.Interface(
|
| 106 |
fn=process_image,
|
| 107 |
+
inputs=[
|
| 108 |
+
gr.Image(type="filepath", label="Input Image"),
|
| 109 |
+
gr.Image(type="filepath", label="Input Depth Map (optional)", optional=True),
|
| 110 |
+
],
|
| 111 |
outputs=[
|
| 112 |
+
gr.Image(label="Predicted Depth", type="pil"),
|
| 113 |
+
gr.Model3D(label="3D Mesh Reconstruction", clear_color=[1.0, 1.0, 1.0, 1.0]),
|
| 114 |
+
gr.File(label="3D gLTF File"),
|
| 115 |
],
|
| 116 |
title=title,
|
| 117 |
description=description,
|