jmartinezot
commited on
Commit
•
fb4ab4b
1
Parent(s):
f0027f2
Update app.py
Browse files
app.py
CHANGED
@@ -22,20 +22,19 @@ def point_cloud_to_image(point_cloud):
|
|
22 |
|
23 |
return pil_img
|
24 |
|
25 |
-
def create_file_mesh_from_pcd(pcd):
|
26 |
|
27 |
temp_file = tempfile.NamedTemporaryFile(suffix=".obj", delete=False)
|
28 |
file_path = temp_file.name
|
29 |
temp_file.close()
|
30 |
|
31 |
-
alpha = 2
|
32 |
# print(f"alpha={alpha:.3f}")
|
33 |
mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_alpha_shape(pcd, alpha)
|
34 |
mesh.compute_vertex_normals()
|
35 |
o3d.io.write_triangle_mesh(file_path, mesh, write_triangle_uvs=True)
|
36 |
return file_path
|
37 |
|
38 |
-
def plane_detection(pointcloud_path, voxel_size=0.05, distance_threshold=0.01, num_iterations=1000):
|
39 |
# Load point cloud from file
|
40 |
pcd = o3d.io.read_point_cloud(pointcloud_path.name)
|
41 |
|
@@ -54,9 +53,9 @@ def plane_detection(pointcloud_path, voxel_size=0.05, distance_threshold=0.01, n
|
|
54 |
a, b, c, d = plane_model
|
55 |
plane_model = np.array([[a], [b], [c], [d]])
|
56 |
df = pd.DataFrame(plane_model.reshape(1, 4), columns=["a", "b", "c", "d"])
|
57 |
-
input_path = create_file_mesh_from_pcd(pcd)
|
58 |
-
inlier_path = create_file_mesh_from_pcd(inlier_cloud)
|
59 |
-
outlier_path = create_file_mesh_from_pcd(outlier_cloud)
|
60 |
# Return inlier point cloud, outlier point cloud, and plane model
|
61 |
# return point_cloud_to_image(inlier_cloud), point_cloud_to_image(outlier_cloud), df
|
62 |
return input_path, inlier_path, outlier_path, df
|
@@ -76,16 +75,25 @@ outputs = [
|
|
76 |
iface = gr.Interface(plane_detection,
|
77 |
inputs=[
|
78 |
File(label="Point cloud file (.ply or .pcd format)"),
|
79 |
-
Slider(label="Voxel size", minimum=
|
80 |
-
Slider(label="Distance threshold", minimum=0.
|
81 |
-
Slider(label="Number of iterations", minimum=1, maximum=10000, step=1, value=100)
|
|
|
82 |
],
|
83 |
outputs=outputs,
|
84 |
title="Plane Detection using RANSAC",
|
85 |
description="This app takes as input a point cloud file (.ply or .pcd format), voxel size, distance threshold, and number of iterations, finds a plane using RANSAC algorithm, displays the inlier and outlier point clouds, and returns the inlier point cloud, outlier point cloud, and the plane model.",
|
86 |
-
allow_flagging="never"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
)
|
88 |
|
89 |
|
90 |
# Launch the interface
|
91 |
-
iface.launch()
|
|
|
22 |
|
23 |
return pil_img
|
24 |
|
25 |
+
def create_file_mesh_from_pcd(pcd, alpha):
|
26 |
|
27 |
temp_file = tempfile.NamedTemporaryFile(suffix=".obj", delete=False)
|
28 |
file_path = temp_file.name
|
29 |
temp_file.close()
|
30 |
|
|
|
31 |
# print(f"alpha={alpha:.3f}")
|
32 |
mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_alpha_shape(pcd, alpha)
|
33 |
mesh.compute_vertex_normals()
|
34 |
o3d.io.write_triangle_mesh(file_path, mesh, write_triangle_uvs=True)
|
35 |
return file_path
|
36 |
|
37 |
+
def plane_detection(pointcloud_path, voxel_size=0.05, distance_threshold=0.01, num_iterations=1000, alpha=2):
|
38 |
# Load point cloud from file
|
39 |
pcd = o3d.io.read_point_cloud(pointcloud_path.name)
|
40 |
|
|
|
53 |
a, b, c, d = plane_model
|
54 |
plane_model = np.array([[a], [b], [c], [d]])
|
55 |
df = pd.DataFrame(plane_model.reshape(1, 4), columns=["a", "b", "c", "d"])
|
56 |
+
input_path = create_file_mesh_from_pcd(pcd, alpha)
|
57 |
+
inlier_path = create_file_mesh_from_pcd(inlier_cloud, alpha)
|
58 |
+
outlier_path = create_file_mesh_from_pcd(outlier_cloud, alpha)
|
59 |
# Return inlier point cloud, outlier point cloud, and plane model
|
60 |
# return point_cloud_to_image(inlier_cloud), point_cloud_to_image(outlier_cloud), df
|
61 |
return input_path, inlier_path, outlier_path, df
|
|
|
75 |
iface = gr.Interface(plane_detection,
|
76 |
inputs=[
|
77 |
File(label="Point cloud file (.ply or .pcd format)"),
|
78 |
+
Slider(label="Voxel size", minimum=0.001, maximum=50, step=1, value=2),
|
79 |
+
Slider(label="Distance threshold", minimum=0.001, maximum=50, step=0.01, value=5),
|
80 |
+
Slider(label="Number of iterations", minimum=1, maximum=10000, step=1, value=100),
|
81 |
+
Slider(label="Alpha for surface reconstruction", minimum=0.02, maximum=100, step=0.01, value=2),
|
82 |
],
|
83 |
outputs=outputs,
|
84 |
title="Plane Detection using RANSAC",
|
85 |
description="This app takes as input a point cloud file (.ply or .pcd format), voxel size, distance threshold, and number of iterations, finds a plane using RANSAC algorithm, displays the inlier and outlier point clouds, and returns the inlier point cloud, outlier point cloud, and the plane model.",
|
86 |
+
allow_flagging="never",
|
87 |
+
examples=[
|
88 |
+
# ["Pointclouds/p1.ply", 2, 5, 100, 2]
|
89 |
+
["Pointclouds/cloud_bin_0.ply", 0.001, 0.03, 100, 0.05],
|
90 |
+
["Pointclouds/cloud_bin_20.ply", 0.001, 0.03, 100, 0.05],
|
91 |
+
["Pointclouds/cloud_bin_30.ply", 0.001, 0.03, 100, 0.05],
|
92 |
+
["Pointclouds/cloud_bin_40.ply", 0.001, 0.03, 100, 0.05],
|
93 |
+
["Pointclouds/cloud_bin_50.ply", 0.001, 0.03, 100, 0.05],
|
94 |
+
],
|
95 |
)
|
96 |
|
97 |
|
98 |
# Launch the interface
|
99 |
+
iface.launch()
|