Spaces:
Runtime error
Runtime error
jens
commited on
Commit
·
2a1828c
1
Parent(s):
c93bd34
depth mapping and n_samples
Browse files- app.py +8 -2
- inference.py +7 -4
- utils.py +25 -0
app.py
CHANGED
|
@@ -62,6 +62,8 @@ with block:
|
|
| 62 |
with gr.Column():
|
| 63 |
pcl_figure = gr.Model3D(label="3-D Reconstruction", clear_color=[1.0, 1.0, 1.0, 1.0])
|
| 64 |
with gr.Row():
|
|
|
|
|
|
|
| 65 |
n_samples = gr.Slider(minimum=1e3, maximum=1e6, step=1e3, default=1e3, label='Number of Samples')
|
| 66 |
cube_size = gr.Slider(minimum=0.00001, maximum=0.001, step=0.000001, default=0.00001, label='Cube size')
|
| 67 |
depth_reconstruction_btn = gr.Button('Depth Reconstruction', variant = 'primary')
|
|
@@ -153,8 +155,12 @@ with block:
|
|
| 153 |
|
| 154 |
def on_depth_reconstruction_btn_click(inputs):
|
| 155 |
print("depth reconstruction")
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
return {pcl_figure: path}
|
| 159 |
depth_reconstruction_btn.click(on_depth_reconstruction_btn_click, components, [pcl_figure], queue=False)
|
| 160 |
|
|
|
|
| 62 |
with gr.Column():
|
| 63 |
pcl_figure = gr.Model3D(label="3-D Reconstruction", clear_color=[1.0, 1.0, 1.0, 1.0])
|
| 64 |
with gr.Row():
|
| 65 |
+
max_depth = gr.Slider(minimum=0, maximum=10, step=0.01, default=1, label='Number of Samples')
|
| 66 |
+
min_depth = gr.Slider(minimum=0, maximum=10, step=0.01, default=0.1, label='Number of Samples')
|
| 67 |
n_samples = gr.Slider(minimum=1e3, maximum=1e6, step=1e3, default=1e3, label='Number of Samples')
|
| 68 |
cube_size = gr.Slider(minimum=0.00001, maximum=0.001, step=0.000001, default=0.00001, label='Cube size')
|
| 69 |
depth_reconstruction_btn = gr.Button('Depth Reconstruction', variant = 'primary')
|
|
|
|
| 155 |
|
| 156 |
def on_depth_reconstruction_btn_click(inputs):
|
| 157 |
print("depth reconstruction")
|
| 158 |
+
path = dpt.generate_obj_masks2(image=inputs[input_image],
|
| 159 |
+
cube_size=inputs[cube_size],
|
| 160 |
+
n_samples=inputs[n_samples],
|
| 161 |
+
masks=inputs[masks],
|
| 162 |
+
min_depth=inputs[min_depth],
|
| 163 |
+
max_depth=inputs[max_depth]) #
|
| 164 |
return {pcl_figure: path}
|
| 165 |
depth_reconstruction_btn.click(on_depth_reconstruction_btn_click, components, [pcl_figure], queue=False)
|
| 166 |
|
inference.py
CHANGED
|
@@ -10,6 +10,7 @@ import open3d as o3d
|
|
| 10 |
import pandas as pd
|
| 11 |
import plotly.express as px
|
| 12 |
import matplotlib.pyplot as plt
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
|
|
@@ -60,9 +61,8 @@ class DepthPredictor:
|
|
| 60 |
align_corners=False,
|
| 61 |
).squeeze()
|
| 62 |
|
| 63 |
-
output = prediction.cpu().numpy()
|
| 64 |
-
formatted = 255 - (output * 255 / np.max(output)).astype('uint8')
|
| 65 |
-
|
| 66 |
#img = Image.fromarray(formatted)
|
| 67 |
return output
|
| 68 |
|
|
@@ -144,15 +144,18 @@ class DepthPredictor:
|
|
| 144 |
o3d.io.write_triangle_mesh(output_file, mesh)
|
| 145 |
return output_file
|
| 146 |
|
| 147 |
-
def generate_obj_masks2(self, image, masks, cube_size):
|
| 148 |
# Generate a point cloud
|
| 149 |
depth = self.predict(image)
|
|
|
|
| 150 |
image = np.array(image)
|
| 151 |
mesh = o3d.geometry.TriangleMesh()
|
| 152 |
# Create cubes and add them to the mesh
|
| 153 |
cs = [(255,0,0),(0,255,0),(0,0,255)]
|
| 154 |
for c,(mask, _) in zip(cs, masks):
|
| 155 |
points, _ = PCL(mask, depth)
|
|
|
|
|
|
|
| 156 |
for point in points:
|
| 157 |
cube = o3d.geometry.TriangleMesh.create_box(width=cube_size, height=cube_size, depth=cube_size)
|
| 158 |
cube.translate(-point)
|
|
|
|
| 10 |
import pandas as pd
|
| 11 |
import plotly.express as px
|
| 12 |
import matplotlib.pyplot as plt
|
| 13 |
+
from utils import map_image_range
|
| 14 |
|
| 15 |
|
| 16 |
|
|
|
|
| 61 |
align_corners=False,
|
| 62 |
).squeeze()
|
| 63 |
|
| 64 |
+
output = 1 - (prediction.cpu().numpy() / np.max(output))
|
| 65 |
+
#formatted = 255 - (output * 255 / np.max(output)).astype('uint8')
|
|
|
|
| 66 |
#img = Image.fromarray(formatted)
|
| 67 |
return output
|
| 68 |
|
|
|
|
| 144 |
o3d.io.write_triangle_mesh(output_file, mesh)
|
| 145 |
return output_file
|
| 146 |
|
| 147 |
+
def generate_obj_masks2(self, image, masks, cube_size, n_samples, min_depth, max_depth):
|
| 148 |
# Generate a point cloud
|
| 149 |
depth = self.predict(image)
|
| 150 |
+
depth = map_image_range(depth, min_depth, max_depth)
|
| 151 |
image = np.array(image)
|
| 152 |
mesh = o3d.geometry.TriangleMesh()
|
| 153 |
# Create cubes and add them to the mesh
|
| 154 |
cs = [(255,0,0),(0,255,0),(0,0,255)]
|
| 155 |
for c,(mask, _) in zip(cs, masks):
|
| 156 |
points, _ = PCL(mask, depth)
|
| 157 |
+
idxs = np.random.choice(len(points), int(n_samples))
|
| 158 |
+
points = points[idxs]
|
| 159 |
for point in points:
|
| 160 |
cube = o3d.geometry.TriangleMesh.create_box(width=cube_size, height=cube_size, depth=cube_size)
|
| 161 |
cube.translate(-point)
|
utils.py
CHANGED
|
@@ -195,3 +195,28 @@ def PCL3(image):
|
|
| 195 |
|
| 196 |
return fig
|
| 197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
return fig
|
| 197 |
|
| 198 |
+
import numpy as np
|
| 199 |
+
|
| 200 |
+
def map_image_range(image, min_value, max_value):
|
| 201 |
+
"""
|
| 202 |
+
Maps the values of a numpy image array to a specified range.
|
| 203 |
+
|
| 204 |
+
Args:
|
| 205 |
+
image (numpy.ndarray): Input image array with values ranging from 0 to 1.
|
| 206 |
+
min_value (float): Minimum value of the new range.
|
| 207 |
+
max_value (float): Maximum value of the new range.
|
| 208 |
+
|
| 209 |
+
Returns:
|
| 210 |
+
numpy.ndarray: Image array with values mapped to the specified range.
|
| 211 |
+
"""
|
| 212 |
+
# Ensure the input image is a numpy array
|
| 213 |
+
if not isinstance(image, np.ndarray):
|
| 214 |
+
raise ValueError("Input image must be a numpy array.")
|
| 215 |
+
|
| 216 |
+
# Ensure the image values are within the valid range (0 to 1)
|
| 217 |
+
if not (0 <= np.min(image) <= 1) or not (0 <= np.max(image) <= 1):
|
| 218 |
+
raise ValueError("Input image values must be in the range [0, 1].")
|
| 219 |
+
|
| 220 |
+
# Map the values to the specified range
|
| 221 |
+
mapped_image = (image - 0) * (max_value - min_value) / (1 - 0) + min_value
|
| 222 |
+
return mapped_image
|