pynode-flow 0.2.0__py3-none-any.whl
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.
- pynode/__init__.py +37 -0
- pynode/__main__.py +9 -0
- pynode/_perf.py +65 -0
- pynode/_version.py +24 -0
- pynode/api/__init__.py +15 -0
- pynode/api/helpers.py +79 -0
- pynode/api/nodes.py +423 -0
- pynode/api/services.py +124 -0
- pynode/api/sse.py +58 -0
- pynode/api/uploads.py +59 -0
- pynode/api/workflows.py +466 -0
- pynode/config.py +93 -0
- pynode/install_nodes.py +143 -0
- pynode/main.py +118 -0
- pynode/node_registry.py +156 -0
- pynode/nodes/AutoSyncNode/auto_sync_node.py +151 -0
- pynode/nodes/BBoxMetricsNode/__init__.py +3 -0
- pynode/nodes/BBoxMetricsNode/bbox_metrics_node.py +250 -0
- pynode/nodes/BBoxMetricsNode/requirements.txt +1 -0
- pynode/nodes/BatchNode/__init__.py +3 -0
- pynode/nodes/BatchNode/batch_node.py +86 -0
- pynode/nodes/BatchNode/requirements.txt +1 -0
- pynode/nodes/CameraNode/__init__.py +4 -0
- pynode/nodes/CameraNode/camera_node.py +204 -0
- pynode/nodes/CameraNode/requirements.txt +2 -0
- pynode/nodes/ChangeNode/__init__.py +1 -0
- pynode/nodes/ChangeNode/change_node.py +255 -0
- pynode/nodes/ChangeNode/requirements.txt +1 -0
- pynode/nodes/ConfidenceFilterNode/__init__.py +4 -0
- pynode/nodes/ConfidenceFilterNode/confidence_filter_node.py +208 -0
- pynode/nodes/ConfidenceFilterNode/requirements.txt +1 -0
- pynode/nodes/CounterNode/__init__.py +3 -0
- pynode/nodes/CounterNode/counter_node.py +191 -0
- pynode/nodes/CropNode/__init__.py +4 -0
- pynode/nodes/CropNode/crop_node.py +317 -0
- pynode/nodes/CropNode/requirements.txt +2 -0
- pynode/nodes/DebugNode/__init__.py +4 -0
- pynode/nodes/DebugNode/debug_node.py +169 -0
- pynode/nodes/DebugNode/requirements.txt +1 -0
- pynode/nodes/DelayNode/delay_node.py +218 -0
- pynode/nodes/DelayNode/requirements.txt +1 -0
- pynode/nodes/DrawPredictionsNode/__init__.py +3 -0
- pynode/nodes/DrawPredictionsNode/draw_predictions_node.py +365 -0
- pynode/nodes/DrawPredictionsNode/requirements.txt +2 -0
- pynode/nodes/ErrorNode/error_node.py +94 -0
- pynode/nodes/ErrorNode/requirements.txt +1 -0
- pynode/nodes/FilterNode/__init__.py +3 -0
- pynode/nodes/FilterNode/filter_node.py +102 -0
- pynode/nodes/FilterNode/requirements.txt +1 -0
- pynode/nodes/FrameSourceNode/__init__.py +4 -0
- pynode/nodes/FrameSourceNode/frame_source_node.py +319 -0
- pynode/nodes/FrameSourceNode/requirements.txt +1 -0
- pynode/nodes/FunctionNode/__init__.py +3 -0
- pynode/nodes/FunctionNode/function_node.py +168 -0
- pynode/nodes/FunctionNode/requirements.txt +1 -0
- pynode/nodes/GateNode/gate_node.py +60 -0
- pynode/nodes/GateNode/requirements.txt +1 -0
- pynode/nodes/GetiUploadNode/__init__.py +3 -0
- pynode/nodes/GetiUploadNode/geti_upload_node.py +278 -0
- pynode/nodes/GetiUploadNode/requirements.txt +1 -0
- pynode/nodes/ImageFormatNode/__init__.py +4 -0
- pynode/nodes/ImageFormatNode/image_format_node.py +310 -0
- pynode/nodes/ImageFormatNode/requirements.txt +2 -0
- pynode/nodes/ImageUploadNode/image_upload_node.py +96 -0
- pynode/nodes/ImageViewerNode/__init__.py +4 -0
- pynode/nodes/ImageViewerNode/image_viewer_node.py +162 -0
- pynode/nodes/ImageViewerNode/requirements.txt +2 -0
- pynode/nodes/ImageWriterNode/__init__.py +3 -0
- pynode/nodes/ImageWriterNode/imagewriter_node.py +352 -0
- pynode/nodes/ImageWriterNode/requirements.txt +1 -0
- pynode/nodes/InferenceNode/InferenceEngine/__init__.py +15 -0
- pynode/nodes/InferenceNode/InferenceEngine/device_detection.py +149 -0
- pynode/nodes/InferenceNode/InferenceEngine/engines/base_engine.py +155 -0
- pynode/nodes/InferenceNode/InferenceEngine/engines/example_engine_template.py +272 -0
- pynode/nodes/InferenceNode/InferenceEngine/engines/geti_engine.py +309 -0
- pynode/nodes/InferenceNode/InferenceEngine/engines/onnx_engine.py +346 -0
- pynode/nodes/InferenceNode/InferenceEngine/engines/pass_engine.py +86 -0
- pynode/nodes/InferenceNode/InferenceEngine/engines/ultralytics_engine.py +863 -0
- pynode/nodes/InferenceNode/InferenceEngine/inference_engine_factory.py +533 -0
- pynode/nodes/InferenceNode/InferenceEngine/readme.md +107 -0
- pynode/nodes/InferenceNode/InferenceEngine/result_converters.py +121 -0
- pynode/nodes/InferenceNode/inference_node.py +451 -0
- pynode/nodes/InferenceNode/requirements.txt +13 -0
- pynode/nodes/InjectNode/inject_node.py +245 -0
- pynode/nodes/InjectNode/requirements.txt +1 -0
- pynode/nodes/JoinNode/__init__.py +3 -0
- pynode/nodes/JoinNode/join_node.py +156 -0
- pynode/nodes/JoinNode/requirements.txt +1 -0
- pynode/nodes/LabelFilterNode/__init__.py +3 -0
- pynode/nodes/LabelFilterNode/label_filter_node.py +188 -0
- pynode/nodes/LabelFilterNode/requirements.txt +1 -0
- pynode/nodes/MQTTNode/__init__.py +8 -0
- pynode/nodes/MQTTNode/mqtt_in_node.py +148 -0
- pynode/nodes/MQTTNode/mqtt_out_node.py +157 -0
- pynode/nodes/MQTTNode/mqtt_service.py +367 -0
- pynode/nodes/MQTTNode/requirements.txt +1 -0
- pynode/nodes/MessageWriterNode/__init__.py +7 -0
- pynode/nodes/MessageWriterNode/messagereader_node.py +568 -0
- pynode/nodes/MessageWriterNode/messagewriter_node.py +505 -0
- pynode/nodes/OmronCameraNode/__init__.py +3 -0
- pynode/nodes/OmronCameraNode/omron_camera_node.py +652 -0
- pynode/nodes/OmronCameraNode/requirements.txt +4 -0
- pynode/nodes/OpenCV/__init__.py +1 -0
- pynode/nodes/OpenCV/arithmetic_node.py +376 -0
- pynode/nodes/OpenCV/bitwise_node.py +127 -0
- pynode/nodes/OpenCV/blend_node.py +127 -0
- pynode/nodes/OpenCV/blob_detector_node.py +233 -0
- pynode/nodes/OpenCV/blur_node.py +141 -0
- pynode/nodes/OpenCV/color_space_node.py +139 -0
- pynode/nodes/OpenCV/colormap_node.py +262 -0
- pynode/nodes/OpenCV/contours_node.py +258 -0
- pynode/nodes/OpenCV/denormalize_coords_node.py +365 -0
- pynode/nodes/OpenCV/draw_node.py +312 -0
- pynode/nodes/OpenCV/edge_detector_node.py +177 -0
- pynode/nodes/OpenCV/equirectangular_node.py +447 -0
- pynode/nodes/OpenCV/fft_node.py +102 -0
- pynode/nodes/OpenCV/focus_estimation_node.py +189 -0
- pynode/nodes/OpenCV/frequency_filter_node.py +241 -0
- pynode/nodes/OpenCV/histogram_node.py +160 -0
- pynode/nodes/OpenCV/hough_circles_node.py +224 -0
- pynode/nodes/OpenCV/hough_lines_node.py +243 -0
- pynode/nodes/OpenCV/in_range_node.py +186 -0
- pynode/nodes/OpenCV/morphology_node.py +136 -0
- pynode/nodes/OpenCV/normalize_coords_node.py +344 -0
- pynode/nodes/OpenCV/paste_node.py +244 -0
- pynode/nodes/OpenCV/perspective_node.py +174 -0
- pynode/nodes/OpenCV/realsense_depth_node.py +253 -0
- pynode/nodes/OpenCV/requirements.txt +2 -0
- pynode/nodes/OpenCV/resize_node.py +175 -0
- pynode/nodes/OpenCV/rotate_node.py +159 -0
- pynode/nodes/OpenCV/template_match_node.py +227 -0
- pynode/nodes/OpenCV/threshold_node.py +167 -0
- pynode/nodes/PointInShapeNode/__init__.py +3 -0
- pynode/nodes/PointInShapeNode/point_in_shape_node.py +313 -0
- pynode/nodes/PointInShapeNode/requirements.txt +2 -0
- pynode/nodes/PolygonMetricsNode/__init__.py +3 -0
- pynode/nodes/PolygonMetricsNode/polygon_metrics_node.py +331 -0
- pynode/nodes/PolygonMetricsNode/requirements.txt +2 -0
- pynode/nodes/QueueLengthProbeNode/__init__.py +3 -0
- pynode/nodes/QueueLengthProbeNode/queue_length_probe_node.py +116 -0
- pynode/nodes/Qwen3VLMNode/__init__.py +3 -0
- pynode/nodes/Qwen3VLMNode/qwen3_vlm_node.py +519 -0
- pynode/nodes/Qwen3VLMNode/requirements.txt +5 -0
- pynode/nodes/RESTEndpointNode/__init__.py +3 -0
- pynode/nodes/RESTEndpointNode/rest_endpoint_node.py +491 -0
- pynode/nodes/RangeNode/__init__.py +3 -0
- pynode/nodes/RangeNode/range_node.py +115 -0
- pynode/nodes/RangeNode/requirements.txt +1 -0
- pynode/nodes/RateProbeNode/__init__.py +3 -0
- pynode/nodes/RateProbeNode/rate_probe_node.py +183 -0
- pynode/nodes/RateProbeNode/requirements.txt +1 -0
- pynode/nodes/RoboflowUploadNode/__init__.py +3 -0
- pynode/nodes/RoboflowUploadNode/requirements.txt +1 -0
- pynode/nodes/RoboflowUploadNode/roboflow_upload_node.py +229 -0
- pynode/nodes/SliceNode/README.md +149 -0
- pynode/nodes/SliceNode/__init__.py +5 -0
- pynode/nodes/SliceNode/merge_predictions_node.py +444 -0
- pynode/nodes/SliceNode/requirements.txt +3 -0
- pynode/nodes/SliceNode/slice_collector_node.py +370 -0
- pynode/nodes/SliceNode/slice_image_node.py +380 -0
- pynode/nodes/SplitNode/__init__.py +3 -0
- pynode/nodes/SplitNode/requirements.txt +1 -0
- pynode/nodes/SplitNode/split_node.py +155 -0
- pynode/nodes/SwitchNode/requirements.txt +1 -0
- pynode/nodes/SwitchNode/switch_node.py +259 -0
- pynode/nodes/SyncNode/__init__.py +3 -0
- pynode/nodes/SyncNode/sync_node.py +287 -0
- pynode/nodes/TemplateNode/requirements.txt +1 -0
- pynode/nodes/TemplateNode/template_node.py +86 -0
- pynode/nodes/TrackerNode/__init__.py +2 -0
- pynode/nodes/TrackerNode/supervision_tracker_node.py +248 -0
- pynode/nodes/TrackerNode/tracker_node.py +745 -0
- pynode/nodes/UltralyticsNode/__init__.py +4 -0
- pynode/nodes/UltralyticsNode/requirements.txt +2 -0
- pynode/nodes/UltralyticsNode/ultralytics_node.py +364 -0
- pynode/nodes/UnknownNode/__init__.py +93 -0
- pynode/nodes/VLLMNode/__init__.py +3 -0
- pynode/nodes/VLLMNode/vllm_node.py +345 -0
- pynode/nodes/VideoReaderNode/__init__.py +3 -0
- pynode/nodes/VideoReaderNode/video_reader_node.py +425 -0
- pynode/nodes/VideoWriterNode/__init__.py +3 -0
- pynode/nodes/VideoWriterNode/video_writer_node.py +441 -0
- pynode/nodes/WebhookNode/__init__.py +3 -0
- pynode/nodes/WebhookNode/webhook_node.py +591 -0
- pynode/nodes/__init__.py +109 -0
- pynode/nodes/base_node.py +562 -0
- pynode/nodes/image_utils.py +291 -0
- pynode/nodes/info.py +109 -0
- pynode/nodes/mDNSNode/__init__.py +13 -0
- pynode/nodes/mDNSNode/mdns_broadcast_node.py +214 -0
- pynode/nodes/mDNSNode/mdns_discovery_node.py +362 -0
- pynode/nodes/mDNSNode/mdns_manager.py +399 -0
- pynode/nodes/mDNSNode/requirements.txt +1 -0
- pynode/nodes/messages.py +81 -0
- pynode/server.py +187 -0
- pynode/static/images/favicon4.png +0 -0
- pynode/static/index.html +200 -0
- pynode/static/js/README.md +139 -0
- pynode/static/js/auth.js +89 -0
- pynode/static/js/clipboard.js +152 -0
- pynode/static/js/config.js +14 -0
- pynode/static/js/connections.js +844 -0
- pynode/static/js/debug.js +524 -0
- pynode/static/js/events.js +775 -0
- pynode/static/js/history.js +199 -0
- pynode/static/js/main.js +55 -0
- pynode/static/js/minimap.js +242 -0
- pynode/static/js/nodes.js +995 -0
- pynode/static/js/palette.js +255 -0
- pynode/static/js/properties.js +1270 -0
- pynode/static/js/selection.js +241 -0
- pynode/static/js/state.js +211 -0
- pynode/static/js/ui-utils.js +28 -0
- pynode/static/js/viewport.js +204 -0
- pynode/static/js/workflow.js +469 -0
- pynode/static/js/workflows.js +344 -0
- pynode/static/style.css +2751 -0
- pynode/workflow_engine.py +483 -0
- pynode/workflow_manager.py +440 -0
- pynode_flow-0.2.0.dist-info/METADATA +520 -0
- pynode_flow-0.2.0.dist-info/RECORD +227 -0
- pynode_flow-0.2.0.dist-info/WHEEL +5 -0
- pynode_flow-0.2.0.dist-info/entry_points.txt +3 -0
- pynode_flow-0.2.0.dist-info/licenses/LICENSE +21 -0
- pynode_flow-0.2.0.dist-info/scm_file_list.json +270 -0
- pynode_flow-0.2.0.dist-info/scm_version.json +8 -0
- pynode_flow-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Merge Slice Predictions Node - combines predictions from image slices.
|
|
3
|
+
Based on SAHI (Slicing Aided Hyper Inference) methodology for prediction merging.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
from typing import Any, Dict, List, Tuple, Optional
|
|
8
|
+
from pynode.nodes.base_node import BaseNode, Info, MessageKeys
|
|
9
|
+
|
|
10
|
+
_info = Info()
|
|
11
|
+
_info.add_text("Combines detection predictions from multiple image slices back into a unified result. Based on SAHI methodology for prediction merging.")
|
|
12
|
+
_info.add_header("Inputs")
|
|
13
|
+
_info.add_bullets(("Input 0:", "Array message containing slice predictions with offset metadata"))
|
|
14
|
+
_info.add_header("Outputs")
|
|
15
|
+
_info.add_bullets(
|
|
16
|
+
("Output 0:", "Merged predictions with coordinates transformed to original image space"),
|
|
17
|
+
)
|
|
18
|
+
_info.add_header("Configuration")
|
|
19
|
+
_info.add_bullets(
|
|
20
|
+
("NMS IoU Threshold:", "Threshold for Non-Maximum Suppression to remove duplicates"),
|
|
21
|
+
("Match Metric:", "IoU (Intersection over Union) or IoS (Intersection over Smaller)"),
|
|
22
|
+
("Match Threshold:", "Threshold for considering detections as duplicates"),
|
|
23
|
+
("Class Agnostic NMS:", "Apply NMS across all classes vs per-class"),
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class MergeSlicePredictionsNode(BaseNode):
|
|
28
|
+
"""
|
|
29
|
+
Merge Slice Predictions Node - combines detection predictions from multiple
|
|
30
|
+
image slices back into a single unified result.
|
|
31
|
+
|
|
32
|
+
Handles coordinate transformation from slice-local to original image coordinates,
|
|
33
|
+
and applies Non-Maximum Suppression (NMS) to remove duplicate detections
|
|
34
|
+
from overlapping regions.
|
|
35
|
+
"""
|
|
36
|
+
display_name = 'Merge Predictions'
|
|
37
|
+
info = str(_info)
|
|
38
|
+
icon = '🔗'
|
|
39
|
+
category = 'vision'
|
|
40
|
+
color = '#FF6B6B'
|
|
41
|
+
border_color = '#EE5A5A'
|
|
42
|
+
text_color = '#000000'
|
|
43
|
+
input_count = 1
|
|
44
|
+
output_count = 1
|
|
45
|
+
|
|
46
|
+
DEFAULT_CONFIG = {
|
|
47
|
+
'nms_threshold': 0.5,
|
|
48
|
+
'match_metric': 'iou',
|
|
49
|
+
'match_threshold': 0.5,
|
|
50
|
+
'class_agnostic': False,
|
|
51
|
+
'drop_messages': False
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
properties = [
|
|
55
|
+
{
|
|
56
|
+
'name': 'nms_threshold',
|
|
57
|
+
'label': 'NMS IoU Threshold',
|
|
58
|
+
'type': 'number',
|
|
59
|
+
'default': 0.5
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
'name': 'match_metric',
|
|
63
|
+
'label': 'Match Metric',
|
|
64
|
+
'type': 'select',
|
|
65
|
+
'options': [
|
|
66
|
+
{'value': 'iou', 'label': 'IoU (Intersection over Union)'},
|
|
67
|
+
{'value': 'ios', 'label': 'IoS (Intersection over Smaller)'}
|
|
68
|
+
],
|
|
69
|
+
'default': 'iou'
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
'name': 'match_threshold',
|
|
73
|
+
'label': 'Match Threshold',
|
|
74
|
+
'type': 'number',
|
|
75
|
+
'default': 0.5
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
'name': 'class_agnostic',
|
|
79
|
+
'label': 'Class Agnostic NMS',
|
|
80
|
+
'type': 'checkbox',
|
|
81
|
+
'default': False
|
|
82
|
+
}
|
|
83
|
+
]
|
|
84
|
+
|
|
85
|
+
def __init__(self, node_id=None, name="merge_predictions"):
|
|
86
|
+
super().__init__(node_id, name)
|
|
87
|
+
self._pending_slices: Dict[str, Dict] = {} # msg_id -> collected slice data
|
|
88
|
+
|
|
89
|
+
def _calculate_iou(self, box1: List[float], box2: List[float]) -> float:
|
|
90
|
+
"""
|
|
91
|
+
Calculate Intersection over Union (IoU) between two bounding boxes.
|
|
92
|
+
Boxes are in [x1, y1, x2, y2] format.
|
|
93
|
+
"""
|
|
94
|
+
x1_1, y1_1, x2_1, y2_1 = box1
|
|
95
|
+
x1_2, y1_2, x2_2, y2_2 = box2
|
|
96
|
+
|
|
97
|
+
# Calculate intersection
|
|
98
|
+
x1_i = max(x1_1, x1_2)
|
|
99
|
+
y1_i = max(y1_1, y1_2)
|
|
100
|
+
x2_i = min(x2_1, x2_2)
|
|
101
|
+
y2_i = min(y2_1, y2_2)
|
|
102
|
+
|
|
103
|
+
if x2_i <= x1_i or y2_i <= y1_i:
|
|
104
|
+
return 0.0
|
|
105
|
+
|
|
106
|
+
intersection = (x2_i - x1_i) * (y2_i - y1_i)
|
|
107
|
+
|
|
108
|
+
# Calculate areas
|
|
109
|
+
area1 = (x2_1 - x1_1) * (y2_1 - y1_1)
|
|
110
|
+
area2 = (x2_2 - x1_2) * (y2_2 - y1_2)
|
|
111
|
+
|
|
112
|
+
# Calculate union
|
|
113
|
+
union = area1 + area2 - intersection
|
|
114
|
+
|
|
115
|
+
if union <= 0:
|
|
116
|
+
return 0.0
|
|
117
|
+
|
|
118
|
+
return intersection / union
|
|
119
|
+
|
|
120
|
+
def _calculate_ios(self, box1: List[float], box2: List[float]) -> float:
|
|
121
|
+
"""
|
|
122
|
+
Calculate Intersection over Smaller area (IoS) between two bounding boxes.
|
|
123
|
+
Useful for detecting when a smaller box is inside a larger one.
|
|
124
|
+
"""
|
|
125
|
+
x1_1, y1_1, x2_1, y2_1 = box1
|
|
126
|
+
x1_2, y1_2, x2_2, y2_2 = box2
|
|
127
|
+
|
|
128
|
+
# Calculate intersection
|
|
129
|
+
x1_i = max(x1_1, x1_2)
|
|
130
|
+
y1_i = max(y1_1, y1_2)
|
|
131
|
+
x2_i = min(x2_1, x2_2)
|
|
132
|
+
y2_i = min(y2_1, y2_2)
|
|
133
|
+
|
|
134
|
+
if x2_i <= x1_i or y2_i <= y1_i:
|
|
135
|
+
return 0.0
|
|
136
|
+
|
|
137
|
+
intersection = (x2_i - x1_i) * (y2_i - y1_i)
|
|
138
|
+
|
|
139
|
+
# Calculate areas
|
|
140
|
+
area1 = (x2_1 - x1_1) * (y2_1 - y1_1)
|
|
141
|
+
area2 = (x2_2 - x1_2) * (y2_2 - y1_2)
|
|
142
|
+
|
|
143
|
+
# Smaller area
|
|
144
|
+
smaller = min(area1, area2)
|
|
145
|
+
|
|
146
|
+
if smaller <= 0:
|
|
147
|
+
return 0.0
|
|
148
|
+
|
|
149
|
+
return intersection / smaller
|
|
150
|
+
|
|
151
|
+
def _transform_detections(
|
|
152
|
+
self,
|
|
153
|
+
detections: List[Dict],
|
|
154
|
+
offset: List[int]
|
|
155
|
+
) -> List[Dict]:
|
|
156
|
+
"""
|
|
157
|
+
Transform detection coordinates from slice-local to original image coordinates.
|
|
158
|
+
"""
|
|
159
|
+
transformed = []
|
|
160
|
+
offset_x, offset_y = offset
|
|
161
|
+
|
|
162
|
+
for det in detections:
|
|
163
|
+
new_det = det.copy()
|
|
164
|
+
bbox = det.get('bbox', [])
|
|
165
|
+
|
|
166
|
+
if len(bbox) >= 4:
|
|
167
|
+
# Transform bounding box coordinates
|
|
168
|
+
x1, y1, x2, y2 = bbox[:4]
|
|
169
|
+
new_det['bbox'] = [
|
|
170
|
+
x1 + offset_x,
|
|
171
|
+
y1 + offset_y,
|
|
172
|
+
x2 + offset_x,
|
|
173
|
+
y2 + offset_y
|
|
174
|
+
]
|
|
175
|
+
# Store original slice-local bbox for reference
|
|
176
|
+
new_det['slice_bbox'] = bbox[:4]
|
|
177
|
+
new_det['slice_offset'] = offset
|
|
178
|
+
|
|
179
|
+
transformed.append(new_det)
|
|
180
|
+
|
|
181
|
+
return transformed
|
|
182
|
+
|
|
183
|
+
def _nms(
|
|
184
|
+
self,
|
|
185
|
+
detections: List[Dict],
|
|
186
|
+
nms_threshold: float,
|
|
187
|
+
match_metric: str = 'iou',
|
|
188
|
+
class_agnostic: bool = False
|
|
189
|
+
) -> List[Dict]:
|
|
190
|
+
"""
|
|
191
|
+
Apply Non-Maximum Suppression to remove duplicate detections.
|
|
192
|
+
|
|
193
|
+
Args:
|
|
194
|
+
detections: List of detection dicts with 'bbox' and 'confidence'
|
|
195
|
+
nms_threshold: IoU/IoS threshold for suppression
|
|
196
|
+
match_metric: 'iou' or 'ios'
|
|
197
|
+
class_agnostic: If True, NMS across all classes; if False, per-class NMS
|
|
198
|
+
|
|
199
|
+
Returns:
|
|
200
|
+
Filtered list of detections
|
|
201
|
+
"""
|
|
202
|
+
if not detections:
|
|
203
|
+
return []
|
|
204
|
+
|
|
205
|
+
# Sort by confidence (highest first)
|
|
206
|
+
sorted_dets = sorted(detections, key=lambda x: x.get('confidence', 0), reverse=True)
|
|
207
|
+
|
|
208
|
+
# Select metric function
|
|
209
|
+
metric_fn = self._calculate_iou if match_metric == 'iou' else self._calculate_ios
|
|
210
|
+
|
|
211
|
+
# Group by class if not class-agnostic
|
|
212
|
+
if class_agnostic:
|
|
213
|
+
groups = {'all': sorted_dets}
|
|
214
|
+
else:
|
|
215
|
+
groups: Dict[Any, List[Dict]] = {}
|
|
216
|
+
for det in sorted_dets:
|
|
217
|
+
class_id = det.get('class_id', det.get('class_name', 'unknown'))
|
|
218
|
+
if class_id not in groups:
|
|
219
|
+
groups[class_id] = []
|
|
220
|
+
groups[class_id].append(det)
|
|
221
|
+
|
|
222
|
+
# Apply NMS per group
|
|
223
|
+
kept = []
|
|
224
|
+
for class_id, group in groups.items():
|
|
225
|
+
keep_indices = []
|
|
226
|
+
|
|
227
|
+
for i, det in enumerate(group):
|
|
228
|
+
should_keep = True
|
|
229
|
+
bbox_i = det.get('bbox', [])
|
|
230
|
+
|
|
231
|
+
if len(bbox_i) < 4:
|
|
232
|
+
continue
|
|
233
|
+
|
|
234
|
+
for j in keep_indices:
|
|
235
|
+
bbox_j = group[j].get('bbox', [])
|
|
236
|
+
if len(bbox_j) < 4:
|
|
237
|
+
continue
|
|
238
|
+
|
|
239
|
+
overlap = metric_fn(bbox_i, bbox_j)
|
|
240
|
+
if overlap > nms_threshold:
|
|
241
|
+
should_keep = False
|
|
242
|
+
break
|
|
243
|
+
|
|
244
|
+
if should_keep:
|
|
245
|
+
keep_indices.append(i)
|
|
246
|
+
|
|
247
|
+
kept.extend([group[i] for i in keep_indices])
|
|
248
|
+
|
|
249
|
+
return kept
|
|
250
|
+
|
|
251
|
+
def _greedy_nmm(
|
|
252
|
+
self,
|
|
253
|
+
detections: List[Dict],
|
|
254
|
+
match_threshold: float,
|
|
255
|
+
match_metric: str = 'iou'
|
|
256
|
+
) -> List[Dict]:
|
|
257
|
+
"""
|
|
258
|
+
Greedy Non-Maximum Merging - merges overlapping detections of same class.
|
|
259
|
+
|
|
260
|
+
Instead of suppressing, this merges overlapping boxes by averaging
|
|
261
|
+
their coordinates, weighted by confidence.
|
|
262
|
+
"""
|
|
263
|
+
if not detections:
|
|
264
|
+
return []
|
|
265
|
+
|
|
266
|
+
# Sort by confidence (highest first)
|
|
267
|
+
sorted_dets = sorted(detections, key=lambda x: x.get('confidence', 0), reverse=True)
|
|
268
|
+
|
|
269
|
+
metric_fn = self._calculate_iou if match_metric == 'iou' else self._calculate_ios
|
|
270
|
+
|
|
271
|
+
merged = []
|
|
272
|
+
used = set()
|
|
273
|
+
|
|
274
|
+
for i, det_i in enumerate(sorted_dets):
|
|
275
|
+
if i in used:
|
|
276
|
+
continue
|
|
277
|
+
|
|
278
|
+
bbox_i = det_i.get('bbox', [])
|
|
279
|
+
if len(bbox_i) < 4:
|
|
280
|
+
continue
|
|
281
|
+
|
|
282
|
+
class_i = det_i.get('class_id', det_i.get('class_name', 'unknown'))
|
|
283
|
+
conf_i = det_i.get('confidence', 0)
|
|
284
|
+
|
|
285
|
+
# Find all matching detections
|
|
286
|
+
matches = [(i, det_i, conf_i)]
|
|
287
|
+
|
|
288
|
+
for j, det_j in enumerate(sorted_dets[i+1:], start=i+1):
|
|
289
|
+
if j in used:
|
|
290
|
+
continue
|
|
291
|
+
|
|
292
|
+
class_j = det_j.get('class_id', det_j.get('class_name', 'unknown'))
|
|
293
|
+
if class_i != class_j:
|
|
294
|
+
continue
|
|
295
|
+
|
|
296
|
+
bbox_j = det_j.get('bbox', [])
|
|
297
|
+
if len(bbox_j) < 4:
|
|
298
|
+
continue
|
|
299
|
+
|
|
300
|
+
overlap = metric_fn(bbox_i, bbox_j)
|
|
301
|
+
if overlap > match_threshold:
|
|
302
|
+
matches.append((j, det_j, det_j.get('confidence', 0)))
|
|
303
|
+
used.add(j)
|
|
304
|
+
|
|
305
|
+
# Merge matched detections
|
|
306
|
+
if len(matches) == 1:
|
|
307
|
+
merged.append(det_i)
|
|
308
|
+
else:
|
|
309
|
+
# Weighted average of bounding boxes
|
|
310
|
+
total_conf = sum(m[2] for m in matches)
|
|
311
|
+
if total_conf > 0:
|
|
312
|
+
merged_bbox = [0, 0, 0, 0]
|
|
313
|
+
for idx, det, conf in matches:
|
|
314
|
+
bbox = det.get('bbox', [0, 0, 0, 0])
|
|
315
|
+
weight = conf / total_conf
|
|
316
|
+
for k in range(4):
|
|
317
|
+
merged_bbox[k] += bbox[k] * weight
|
|
318
|
+
|
|
319
|
+
merged_det = det_i.copy()
|
|
320
|
+
merged_det['bbox'] = merged_bbox
|
|
321
|
+
merged_det['confidence'] = max(m[2] for m in matches)
|
|
322
|
+
merged_det['merged_count'] = len(matches)
|
|
323
|
+
merged.append(merged_det)
|
|
324
|
+
else:
|
|
325
|
+
merged.append(det_i)
|
|
326
|
+
|
|
327
|
+
used.add(i)
|
|
328
|
+
|
|
329
|
+
return merged
|
|
330
|
+
|
|
331
|
+
def on_input(self, msg: Dict[str, Any], input_index: int = 0):
|
|
332
|
+
"""
|
|
333
|
+
Process incoming slice predictions and merge them.
|
|
334
|
+
|
|
335
|
+
Expected input format (from SliceImageNode + detection):
|
|
336
|
+
{
|
|
337
|
+
'payload': {
|
|
338
|
+
'slice_predictions': [
|
|
339
|
+
{
|
|
340
|
+
'slice_index': 0,
|
|
341
|
+
'offset': [x, y],
|
|
342
|
+
'detections': [...],
|
|
343
|
+
'is_full_image': bool
|
|
344
|
+
},
|
|
345
|
+
...
|
|
346
|
+
],
|
|
347
|
+
'original_width': int,
|
|
348
|
+
'original_height': int
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
"""
|
|
352
|
+
payload = msg.get(MessageKeys.PAYLOAD, {})
|
|
353
|
+
|
|
354
|
+
if not isinstance(payload, dict):
|
|
355
|
+
self.report_error("Payload must be a dictionary")
|
|
356
|
+
return
|
|
357
|
+
|
|
358
|
+
slice_predictions = payload.get('slice_predictions', [])
|
|
359
|
+
|
|
360
|
+
if not slice_predictions:
|
|
361
|
+
# Try alternative format - direct detections array with offsets
|
|
362
|
+
slices = payload.get('slices', [])
|
|
363
|
+
if slices:
|
|
364
|
+
slice_predictions = []
|
|
365
|
+
for s in slices:
|
|
366
|
+
if 'detections' in s:
|
|
367
|
+
slice_predictions.append(s)
|
|
368
|
+
|
|
369
|
+
if not slice_predictions:
|
|
370
|
+
self.report_error("No slice predictions found in payload")
|
|
371
|
+
return
|
|
372
|
+
|
|
373
|
+
# Get configuration
|
|
374
|
+
nms_threshold = self.get_config_float('nms_threshold', 0.5)
|
|
375
|
+
match_metric = self.config.get('match_metric', 'iou')
|
|
376
|
+
match_threshold = self.get_config_float('match_threshold', 0.5)
|
|
377
|
+
class_agnostic = self.get_config_bool('class_agnostic', False)
|
|
378
|
+
|
|
379
|
+
# Collect all detections, transforming coordinates
|
|
380
|
+
all_detections = []
|
|
381
|
+
full_image_detections = []
|
|
382
|
+
|
|
383
|
+
for slice_pred in slice_predictions:
|
|
384
|
+
offset = slice_pred.get('offset', [0, 0])
|
|
385
|
+
detections = slice_pred.get('detections', [])
|
|
386
|
+
is_full_image = slice_pred.get('is_full_image', False)
|
|
387
|
+
|
|
388
|
+
if is_full_image:
|
|
389
|
+
# Store full image detections separately
|
|
390
|
+
full_image_detections.extend(detections)
|
|
391
|
+
else:
|
|
392
|
+
# Transform slice-local coordinates to original image coordinates
|
|
393
|
+
transformed = self._transform_detections(detections, offset)
|
|
394
|
+
all_detections.extend(transformed)
|
|
395
|
+
|
|
396
|
+
# Apply NMS to slice detections
|
|
397
|
+
merged_detections = self._nms(
|
|
398
|
+
all_detections,
|
|
399
|
+
nms_threshold,
|
|
400
|
+
match_metric,
|
|
401
|
+
class_agnostic
|
|
402
|
+
)
|
|
403
|
+
|
|
404
|
+
# Optionally combine with full image detections
|
|
405
|
+
if full_image_detections:
|
|
406
|
+
# Add full image detections and run NMS again
|
|
407
|
+
combined = merged_detections + full_image_detections
|
|
408
|
+
final_detections = self._nms(
|
|
409
|
+
combined,
|
|
410
|
+
nms_threshold,
|
|
411
|
+
match_metric,
|
|
412
|
+
class_agnostic
|
|
413
|
+
)
|
|
414
|
+
else:
|
|
415
|
+
final_detections = merged_detections
|
|
416
|
+
|
|
417
|
+
# Sort by confidence
|
|
418
|
+
final_detections = sorted(
|
|
419
|
+
final_detections,
|
|
420
|
+
key=lambda x: x.get('confidence', 0),
|
|
421
|
+
reverse=True
|
|
422
|
+
)
|
|
423
|
+
|
|
424
|
+
# Prepare output
|
|
425
|
+
original_width = payload.get('original_width', 0)
|
|
426
|
+
original_height = payload.get('original_height', 0)
|
|
427
|
+
|
|
428
|
+
out_msg = msg.copy()
|
|
429
|
+
out_msg[MessageKeys.PAYLOAD] = {
|
|
430
|
+
'detections': final_detections,
|
|
431
|
+
'detection_count': len(final_detections),
|
|
432
|
+
'original_width': original_width,
|
|
433
|
+
'original_height': original_height,
|
|
434
|
+
'slice_count': len(slice_predictions),
|
|
435
|
+
'bbox_format': 'xyxy'
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
# Preserve image if present
|
|
439
|
+
if MessageKeys.IMAGE.PATH in payload:
|
|
440
|
+
out_msg[MessageKeys.PAYLOAD][MessageKeys.IMAGE.PATH] = payload[MessageKeys.IMAGE.PATH]
|
|
441
|
+
|
|
442
|
+
out_msg[MessageKeys.TOPIC] = msg.get(MessageKeys.TOPIC, 'merged_predictions')
|
|
443
|
+
|
|
444
|
+
self.send(out_msg)
|