Alessio Grancini commited on
Commit
f84b937
·
verified ·
1 Parent(s): 867cbce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -19
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 generate_image_url(image):
171
- """Generate a shareable URL for an OpenCV image."""
172
- success, encoded_buffer = cv2.imencode(".png", image)
173
- if not success:
174
- raise ValueError("Could not encode image to PNG buffer")
175
-
176
- b64_str = base64.b64encode(encoded_buffer).decode("utf-8")
177
- return "data:image/png;base64," + b64_str
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
- plot_html = fig.to_html(include_plotlyjs='cdn', full_html=False)
209
- b64_str = base64.b64encode(plot_html.encode("utf-8")).decode("utf-8")
210
- return "data:text/html;base64," + b64_str
 
 
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": generate_image_url(image_segmentation),
276
- "depth_url": generate_image_url(depth_colormap),
277
- "distance_url": generate_image_url(utils.draw_depth_info(image, depthmap, objects_data)),
278
- "point_cloud_url": generate_plot_url(utils.generate_obj_pcd(depthmap, objects_data)),
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
  }