@camera.ui/camera-ui-opencv 0.0.18 → 0.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "OpenCV",
3
3
  "name": "@camera.ui/camera-ui-opencv",
4
- "version": "0.0.18",
4
+ "version": "0.0.20",
5
5
  "description": "camera.ui opencv plugin",
6
6
  "author": "seydx (https://github.com/seydx/camera.ui)",
7
7
  "main": "./src/main.py",
@@ -21,7 +21,7 @@
21
21
  "url": "https://github.com/seydx/camera.ui/issues"
22
22
  },
23
23
  "engines": {
24
- "camera.ui": ">=0.0.31-alpha.1",
24
+ "camera.ui": ">=0.0.34-alpha.1",
25
25
  "node": ">=20.17.0"
26
26
  },
27
27
  "homepage": "https://github.com/seydx/camera.ui#readme",
package/requirements.txt CHANGED
@@ -1,3 +1,3 @@
1
1
  numpy==1.26.4
2
2
  opencv-python-headless==4.10.0.84
3
- camera-ui-python-types==0.1.90
3
+ camera-ui-python-types==0.1.93
package/src/main.py CHANGED
@@ -27,7 +27,7 @@ from detector_defaults import (
27
27
  default_threshold,
28
28
  default_threshold_bs,
29
29
  )
30
- from opencv_utils import get_detections, get_detections_bs, get_detections_fd
30
+ from opencv_utils import get_detections, get_detections_bs, get_detections_fd, merge_overlapping_boxes
31
31
 
32
32
 
33
33
  class OpenCV(MotionDetectionPlugin):
@@ -315,7 +315,9 @@ class OpenCV(MotionDetectionPlugin):
315
315
 
316
316
  previous_frame = gray
317
317
 
318
- for det in dets:
318
+ merged_boxes = merge_overlapping_boxes(dets)
319
+
320
+ for det in merged_boxes:
319
321
  (x1, y1, x2, y2) = det
320
322
 
321
323
  pt1: cv2.typing.Point = (int(x1), int(y1))
@@ -9,6 +9,56 @@ DEFAULT_MASK_KERNEL = np.array((9, 9), dtype=np.uint8)
9
9
  DEFAULT_LEARNING_RATE = 0.08
10
10
 
11
11
 
12
+ def merge_overlapping_boxes(
13
+ boxes: list[tuple[float, float, float, float]], overlap_threshold: float = 0.1
14
+ ) -> list[tuple[float, float, float, float]]:
15
+ if not boxes:
16
+ return []
17
+
18
+ boxes_array = np.array(boxes)
19
+ merged_boxes: list[tuple[float, float, float, float]] = []
20
+
21
+ while len(boxes_array) > 0:
22
+ box = boxes_array[0]
23
+ boxes_array: np.ndarray[Any, Any] = boxes_array[1:]
24
+
25
+ x1, y1, w1, h1 = box
26
+ x2 = x1 + w1
27
+ y2 = y1 + h1
28
+
29
+ indices_to_remove: list[int] = []
30
+ for i, (x1o, y1o, w1o, h1o) in enumerate(boxes_array):
31
+ x2o = x1o + w1o
32
+ y2o = y1o + h1o
33
+
34
+ xx1 = max(x1, x1o)
35
+ yy1 = max(y1, y1o)
36
+ xx2 = min(x2, x2o)
37
+ yy2 = min(y2, y2o)
38
+
39
+ w_overlap = max(0, xx2 - xx1)
40
+ h_overlap = max(0, yy2 - yy1)
41
+ overlap_area = w_overlap * h_overlap
42
+ area1 = w1 * h1
43
+ area2 = w1o * h1o
44
+
45
+ if area1 == 0 or area2 == 0:
46
+ continue
47
+ overlap_ratio = overlap_area / min(area1, area2)
48
+
49
+ if overlap_ratio > overlap_threshold:
50
+ x1 = min(x1, x1o)
51
+ y1 = min(y1, y1o)
52
+ x2 = max(x2, x2o)
53
+ y2 = max(y2, y2o)
54
+ indices_to_remove.append(i)
55
+
56
+ boxes_array = np.delete(boxes_array, indices_to_remove, axis=0)
57
+ merged_boxes.append((int(x1), int(y1), int(x2 - x1), int(y2 - y1)))
58
+
59
+ return merged_boxes
60
+
61
+
12
62
  def get_mask(
13
63
  frame1: np.ndarray[Any, Any],
14
64
  frame2: np.ndarray[Any, Any],