Spaces:
Runtime error
Runtime error
jens
commited on
Commit
·
a979122
1
Parent(s):
f465c1d
scatter in plotly
Browse files- app.py +4 -5
- requirements.txt +4 -1
- utils.py +22 -0
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
from segment_anything import SamAutomaticMaskGenerator, sam_model_registry
|
| 3 |
import supervision as sv
|
| 4 |
from inference import DepthPredictor, SegmentPredictor
|
| 5 |
-
from utils import create_3d_obj, create_3d_pc
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
|
|
@@ -13,9 +13,9 @@ def snap(image, video):
|
|
| 13 |
|
| 14 |
segment_predictor = SegmentPredictor()
|
| 15 |
sam_result = segment_predictor.predict(image)
|
| 16 |
-
|
| 17 |
|
| 18 |
-
return [image, depth_result, sam_result, rgb_gltf_path,
|
| 19 |
|
| 20 |
|
| 21 |
demo = gr.Interface(
|
|
@@ -27,8 +27,7 @@ demo = gr.Interface(
|
|
| 27 |
gr.Image(label="predicted segmentation"),
|
| 28 |
gr.Model3D(label="3D mesh reconstruction - RGB",
|
| 29 |
clear_color=[1.0, 1.0, 1.0, 1.0]),
|
| 30 |
-
gr.
|
| 31 |
-
clear_color=[1.0, 1.0, 1.0, 1.0])]
|
| 32 |
)
|
| 33 |
|
| 34 |
if __name__ == "__main__":
|
|
|
|
| 2 |
from segment_anything import SamAutomaticMaskGenerator, sam_model_registry
|
| 3 |
import supervision as sv
|
| 4 |
from inference import DepthPredictor, SegmentPredictor
|
| 5 |
+
from utils import create_3d_obj, create_3d_pc, point_cloud
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
|
|
|
|
| 13 |
|
| 14 |
segment_predictor = SegmentPredictor()
|
| 15 |
sam_result = segment_predictor.predict(image)
|
| 16 |
+
fig = point_cloud(np.array(sam_result), depth_result)
|
| 17 |
|
| 18 |
+
return [image, depth_result, sam_result, rgb_gltf_path, fig]#[depth_result, gltf_path, gltf_path]
|
| 19 |
|
| 20 |
|
| 21 |
demo = gr.Interface(
|
|
|
|
| 27 |
gr.Image(label="predicted segmentation"),
|
| 28 |
gr.Model3D(label="3D mesh reconstruction - RGB",
|
| 29 |
clear_color=[1.0, 1.0, 1.0, 1.0]),
|
| 30 |
+
gr.Plot()]
|
|
|
|
| 31 |
)
|
| 32 |
|
| 33 |
if __name__ == "__main__":
|
requirements.txt
CHANGED
|
@@ -6,4 +6,7 @@ torch
|
|
| 6 |
torchvision
|
| 7 |
opencv-python
|
| 8 |
transformers
|
| 9 |
-
open3d
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
torchvision
|
| 7 |
opencv-python
|
| 8 |
transformers
|
| 9 |
+
open3d
|
| 10 |
+
plotly
|
| 11 |
+
pandas
|
| 12 |
+
numpy
|
utils.py
CHANGED
|
@@ -1,5 +1,9 @@
|
|
| 1 |
import numpy as np
|
| 2 |
import open3d as o3d
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
def create_3d_obj(rgb_image, depth_image, depth=10, path='./image.gltf'):
|
|
@@ -85,3 +89,21 @@ def create_3d_pc(rgb_image, depth_image, depth=10):
|
|
| 85 |
o3d.io.write_point_cloud(filename, pcd)
|
| 86 |
|
| 87 |
return filename # Return the file path where the PLY file is saved
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import open3d as o3d
|
| 3 |
+
import open3d as o3d
|
| 4 |
+
import plotly.express as px
|
| 5 |
+
import numpy as np
|
| 6 |
+
import pandas as pd
|
| 7 |
|
| 8 |
|
| 9 |
def create_3d_obj(rgb_image, depth_image, depth=10, path='./image.gltf'):
|
|
|
|
| 89 |
o3d.io.write_point_cloud(filename, pcd)
|
| 90 |
|
| 91 |
return filename # Return the file path where the PLY file is saved
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def point_cloud(rgb_image, depth_image):
|
| 95 |
+
# Step 2: Create an RGBD image from the RGB and depth images
|
| 96 |
+
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(rgb_image, depth_image, convert_rgb_to_intensity=False)
|
| 97 |
+
# Step 3: Create a PointCloud from the RGBD image
|
| 98 |
+
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, o3d.camera.PinholeCameraIntrinsic(o3d.camera.PinholeCameraIntrinsicParameters.PrimeSenseDefault))
|
| 99 |
+
# Step 4: Convert PointCloud data to a NumPy array
|
| 100 |
+
points = np.asarray(pcd.points)
|
| 101 |
+
colors = np.asarray(pcd.colors)
|
| 102 |
+
# Step 5: Create a DataFrame from the NumPy arrays
|
| 103 |
+
data = {'x': points[:, 0], 'y': points[:, 1], 'z': points[:, 2],
|
| 104 |
+
'red': colors[:, 0], 'green': colors[:, 1], 'blue': colors[:, 2]}
|
| 105 |
+
df = pd.DataFrame(data)
|
| 106 |
+
# Step 6: Create a 3D scatter plot using Plotly Express
|
| 107 |
+
fig = px.scatter_3d(df, x='x', y='y', z='z', color='red', size_max=0.1)
|
| 108 |
+
|
| 109 |
+
return fig
|