Skip to content

Commit 9acf541

Browse files
committed
Clean up bounding boxes
1 parent 83f5700 commit 9acf541

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

examples/04_security_camera_example/security_camera_processor.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ def _detect_packages_sync(self, frame_rgb: np.ndarray) -> List[Dict[str, Any]]:
394394
return []
395395

396396
image = Image.fromarray(frame_rgb)
397+
height, width = frame_rgb.shape[:2]
397398
all_detections = []
398399

399400
for object_type in self.package_detect_objects:
@@ -408,12 +409,36 @@ def _detect_packages_sync(self, frame_rgb: np.ndarray) -> List[Dict[str, Any]]:
408409
obj, object_type, self.package_conf_threshold
409410
)
410411
if detection:
411-
# Convert bbox from [x_min, y_min, x_max, y_max] to (x, y, w, h)
412-
x_min, y_min, x_max, y_max = detection["bbox"]
412+
# Get bbox in [x_min, y_min, x_max, y_max] format
413+
bbox_raw = detection["bbox"]
414+
x_min, y_min, x_max, y_max = bbox_raw
415+
416+
# Check if normalized coordinates (between 0 and 1)
417+
if x_min <= 1.0 and y_min <= 1.0 and x_max <= 1.0 and y_max <= 1.0:
418+
# Convert normalized to pixel coordinates
419+
x_min = int(x_min * width)
420+
y_min = int(y_min * height)
421+
x_max = int(x_max * width)
422+
y_max = int(y_max * height)
423+
else:
424+
# Already pixel coordinates, convert to int
425+
x_min = int(x_min)
426+
y_min = int(y_min)
427+
x_max = int(x_max)
428+
y_max = int(y_max)
429+
430+
# Ensure coordinates are within frame bounds
431+
x_min = max(0, min(x_min, width - 1))
432+
y_min = max(0, min(y_min, height - 1))
433+
x_max = max(x_min + 1, min(x_max, width))
434+
y_max = max(y_min + 1, min(y_max, height))
435+
436+
# Convert to (x, y, w, h) format
413437
x = x_min
414438
y = y_min
415439
w = x_max - x_min
416440
h = y_max - y_min
441+
417442
detection["bbox"] = (x, y, w, h)
418443
all_detections.append(detection)
419444

@@ -454,6 +479,13 @@ async def _detect_and_store_packages(
454479
x, y, w, h = package_data["bbox"]
455480
confidence = package_data["confidence"]
456481

482+
# Ensure coordinates are integers and within frame bounds
483+
height, width = frame_bgr.shape[:2]
484+
x = int(max(0, min(x, width - 1)))
485+
y = int(max(0, min(y, height - 1)))
486+
w = int(max(1, min(w, width - x)))
487+
h = int(max(1, min(h, height - y)))
488+
457489
# Extract package thumbnail
458490
package_roi = frame_bgr[y : y + h, x : x + w]
459491
if package_roi.size == 0:
@@ -530,14 +562,21 @@ def _create_overlay(
530562
# Draw package bounding boxes on the frame
531563
for package in self._detected_packages.values():
532564
x, y, w, h = package.bbox
565+
# Ensure coordinates are integers
566+
x, y, w, h = int(x), int(y), int(w), int(h)
567+
# Ensure coordinates are within bounds
568+
x = max(0, min(x, width - 1))
569+
y = max(0, min(y, height - 1))
570+
x2 = min(x + w, width)
571+
y2 = min(y + h, height)
533572
# Draw blue rectangle for packages
534-
cv2.rectangle(frame_with_overlay, (x, y), (x + w, y + h), (255, 0, 0), 2)
573+
cv2.rectangle(frame_with_overlay, (x, y), (x2, y2), (255, 0, 0), 2)
535574
# Draw package label
536575
label_text = f"Package {package.confidence:.2f}"
537576
cv2.putText(
538577
frame_with_overlay,
539578
label_text,
540-
(x, y - 5),
579+
(x, max(10, y - 5)),
541580
cv2.FONT_HERSHEY_SIMPLEX,
542581
0.4,
543582
(255, 0, 0),

0 commit comments

Comments
 (0)