@camera.ui/camera-ui-opencv 0.0.17 → 0.0.19
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 +1 -1
- package/requirements.txt +1 -1
- package/src/main.py +4 -2
- package/src/opencv_utils.py +50 -0
package/package.json
CHANGED
package/requirements.txt
CHANGED
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
|
-
|
|
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))
|
package/src/opencv_utils.py
CHANGED
|
@@ -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],
|