Alessio Grancini
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -167,18 +167,14 @@ def encode_base64_image(image_array):
|
|
| 167 |
# Return a data URL
|
| 168 |
return "data:image/png;base64," + b64_str
|
| 169 |
|
| 170 |
-
def
|
| 171 |
-
"""
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
return "
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
def generate_plot_url(objs_pcd):
|
| 181 |
-
"""Generate a shareable URL for a Plotly 3D scatter plot of point clouds."""
|
| 182 |
fig = go.Figure()
|
| 183 |
|
| 184 |
for data, clr in objs_pcd:
|
|
@@ -205,9 +201,11 @@ def generate_plot_url(objs_pcd):
|
|
| 205 |
)
|
| 206 |
)
|
| 207 |
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
| 211 |
|
| 212 |
def get_3d_position(center, depth, camera_matrix):
|
| 213 |
"""Project 2D center into 3D space using depth and camera matrix."""
|
|
@@ -272,10 +270,10 @@ def get_detection_data(image_data):
|
|
| 272 |
|
| 273 |
response = {
|
| 274 |
"detections": detections,
|
| 275 |
-
"segmentation_url":
|
| 276 |
-
"depth_url":
|
| 277 |
-
"distance_url":
|
| 278 |
-
"point_cloud_url":
|
| 279 |
"camera_matrix": get_camera_matrix(depth_estimator),
|
| 280 |
"camera_position": [0, 0, 0] # Assumed at origin based on camera intrinsics
|
| 281 |
}
|
|
|
|
| 167 |
# Return a data URL
|
| 168 |
return "data:image/png;base64," + b64_str
|
| 169 |
|
| 170 |
+
def save_image_to_url(image):
|
| 171 |
+
"""Save an OpenCV image to a temporary file and return its URL."""
|
| 172 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
|
| 173 |
+
cv2.imwrite(temp_file.name, image)
|
| 174 |
+
return "/".join(temp_file.name.split("/")[-2:]) # Return relative path for URL
|
| 175 |
+
|
| 176 |
+
def save_plot_to_url(objs_pcd):
|
| 177 |
+
"""Save a Plotly 3D scatter plot to a temporary file and return its URL."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
fig = go.Figure()
|
| 179 |
|
| 180 |
for data, clr in objs_pcd:
|
|
|
|
| 201 |
)
|
| 202 |
)
|
| 203 |
|
| 204 |
+
with tempfile.NamedTemporaryFile(suffix=".html", delete=False) as temp_file:
|
| 205 |
+
fig.write_html(temp_file.name)
|
| 206 |
+
return "/".join(temp_file.name.split("/")[-2:]) # Return relative path for URL
|
| 207 |
+
|
| 208 |
+
|
| 209 |
|
| 210 |
def get_3d_position(center, depth, camera_matrix):
|
| 211 |
"""Project 2D center into 3D space using depth and camera matrix."""
|
|
|
|
| 270 |
|
| 271 |
response = {
|
| 272 |
"detections": detections,
|
| 273 |
+
"segmentation_url": save_image_to_url(image_segmentation),
|
| 274 |
+
"depth_url": save_image_to_url(depth_colormap),
|
| 275 |
+
"distance_url": save_image_to_url(utils.draw_depth_info(image, depthmap, objects_data)),
|
| 276 |
+
"point_cloud_url": save_plot_to_url(utils.generate_obj_pcd(depthmap, objects_data)),
|
| 277 |
"camera_matrix": get_camera_matrix(depth_estimator),
|
| 278 |
"camera_position": [0, 0, 0] # Assumed at origin based on camera intrinsics
|
| 279 |
}
|