Alessio Grancini commited on
Commit
5a818ae
·
verified ·
1 Parent(s): 0b582a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -51
app.py CHANGED
@@ -264,79 +264,34 @@ def get_detection_data(image_data):
264
  cls_id, cls_name, center, mask, color_bgr, confidence = obj
265
  x1, y1, x2, y2 = get_bbox_from_mask(mask)
266
 
267
- #depth_value = depth_at_center(depthmap, [x1, y1, x2, y2])
268
-
269
  # Debug: Log original center and vertices (1536x1024)
270
  print(f"Debug - Object {idx}: Original Center = {center}, Original Vertices = {get_box_vertices([x1, y1, x2, y2])}")
271
 
272
- # Handle invalid or NaN depth values
273
- #if depth_value is None or np.isnan(depth_value) or not isinstance(depth_value, (int, float)) or depth_value <= 0:
274
- #print(f"Warning: Invalid depth value ({depth_value}) for Object {idx}. Using default depth of 1.0...")
275
- #depth_value = 1.0 # Default to 1.0 if depth is invalid or NaN
276
-
277
  # Use get_masked_depth to get mean depth directly from depthmap and mask
278
  masked_depth_map, mean_depth = utils.get_masked_depth(depthmap, mask)
279
  print(f"Debug - Object {idx}: Masked depth min/max: {masked_depth_map.min()}, {masked_depth_map.max()}, Mean depth: {mean_depth}")
280
 
281
  # Handle invalid or NaN mean_depth
282
- if (np.isnan(mean_depth) or not isinstance(mean_depth, (int, float)) or mean_depth <= 0):
283
  print(f"Warning: Invalid mean depth ({mean_depth}) for Object {idx}. Using default depth of 1.0...")
284
  mean_depth = 1.0 # Fallback to 1.0 meter
285
 
286
-
287
- # Scale from 1536x1024 to 1512x1008, ensuring values fit within bounds
288
- scale_x = 1512 / 1536 # Width scaling factor
289
- scale_y = 1008 / 1024 # Height scaling factor
290
-
291
- # Scale and clamp center and vertices to 1512x1008
292
- scaled_x = center[0] * scale_x
293
- scaled_y = center[1] * scale_y
294
- scaled_center = (
295
- int(min(max(scaled_x, 0), 1511)), # Clamp to [0, 1511]
296
- int(min(max(scaled_y, 0), 1007)) # Clamp to [0, 1007]
297
- )
298
- scaled_vertices = [
299
- [int(min(max(x * scale_x, 0), 1511)), int(min(max(y * scale_y, 0), 1007))]
300
- for x, y in get_box_vertices([x1, y1, x2, y2])
301
- ]
302
- # Debug: Log scaled (before and after clamping) and clamped center and vertices
303
- print(f"Debug - Object {idx}: Pre-Clamp Scaled Center = ({scaled_x}, {scaled_y}), Clamped Center = {scaled_center}, Clamped Vertices = {scaled_vertices}")
304
  # Convert BGR to RGB
305
  color_rgb = (int(color_bgr[2]), int(color_bgr[1]), int(color_bgr[0]))
306
 
307
- #detections.append({
308
- #"class_id": cls_id,
309
- #"class_name": cls_name,
310
- #"bounding_box": {
311
- # "vertices": get_box_vertices([x1, y1, x2, y2])
312
- #},
313
- #"position_3d": get_3d_position(center, depth_value, get_camera_matrix(depth_estimator)),
314
- #"distance": depth_value,
315
- #"color": color_rgb,
316
- #"confidence": float(confidence)
317
-
318
  detections.append({
319
  "class_id": cls_id,
320
  "class_name": cls_name,
321
  "bounding_box": {
322
  "vertices": get_box_vertices([x1, y1, x2, y2])
323
  },
324
- "center_2d": center, # Add 2D center in screen space
325
- "distance": float(mean_depth), # Depth in meters
326
- #"distance": depth_value, # Depth in meters
327
  "color": color_rgb,
328
  "confidence": float(confidence)
329
-
330
- #detections.append({
331
- #"class_id": cls_id,
332
- #"class_name": cls_name,
333
- #"bounding_box": {
334
- # "vertices": scaled_vertices
335
- #},
336
- #"center_2d": scaled_center, # Use scaled center
337
- #"distance": float(depth_value), # Ensure depth is a float, not NaN
338
- #"color": color_rgb,
339
- #"confidence": float(confidence)
340
 
341
  })
342
 
 
264
  cls_id, cls_name, center, mask, color_bgr, confidence = obj
265
  x1, y1, x2, y2 = get_bbox_from_mask(mask)
266
 
 
 
267
  # Debug: Log original center and vertices (1536x1024)
268
  print(f"Debug - Object {idx}: Original Center = {center}, Original Vertices = {get_box_vertices([x1, y1, x2, y2])}")
269
 
 
 
 
 
 
270
  # Use get_masked_depth to get mean depth directly from depthmap and mask
271
  masked_depth_map, mean_depth = utils.get_masked_depth(depthmap, mask)
272
  print(f"Debug - Object {idx}: Masked depth min/max: {masked_depth_map.min()}, {masked_depth_map.max()}, Mean depth: {mean_depth}")
273
 
274
  # Handle invalid or NaN mean_depth
275
+ if np.isnan(mean_depth) or not isinstance(mean_depth, (int, float)) or mean_depth <= 0:
276
  print(f"Warning: Invalid mean depth ({mean_depth}) for Object {idx}. Using default depth of 1.0...")
277
  mean_depth = 1.0 # Fallback to 1.0 meter
278
 
279
+ # Calculate real-world distance as done in draw_depth_info
280
+ real_distance = mean_depth * 10 # Scale by 10 to match draw_depth_info
281
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
282
  # Convert BGR to RGB
283
  color_rgb = (int(color_bgr[2]), int(color_bgr[1]), int(color_bgr[0]))
284
 
 
 
 
 
 
 
 
 
 
 
 
285
  detections.append({
286
  "class_id": cls_id,
287
  "class_name": cls_name,
288
  "bounding_box": {
289
  "vertices": get_box_vertices([x1, y1, x2, y2])
290
  },
291
+ "center_2d": center,
292
+ "distance": float(real_distance),
 
293
  "color": color_rgb,
294
  "confidence": float(confidence)
 
 
 
 
 
 
 
 
 
 
 
295
 
296
  })
297