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.
Files changed (227) hide show
  1. pynode/__init__.py +37 -0
  2. pynode/__main__.py +9 -0
  3. pynode/_perf.py +65 -0
  4. pynode/_version.py +24 -0
  5. pynode/api/__init__.py +15 -0
  6. pynode/api/helpers.py +79 -0
  7. pynode/api/nodes.py +423 -0
  8. pynode/api/services.py +124 -0
  9. pynode/api/sse.py +58 -0
  10. pynode/api/uploads.py +59 -0
  11. pynode/api/workflows.py +466 -0
  12. pynode/config.py +93 -0
  13. pynode/install_nodes.py +143 -0
  14. pynode/main.py +118 -0
  15. pynode/node_registry.py +156 -0
  16. pynode/nodes/AutoSyncNode/auto_sync_node.py +151 -0
  17. pynode/nodes/BBoxMetricsNode/__init__.py +3 -0
  18. pynode/nodes/BBoxMetricsNode/bbox_metrics_node.py +250 -0
  19. pynode/nodes/BBoxMetricsNode/requirements.txt +1 -0
  20. pynode/nodes/BatchNode/__init__.py +3 -0
  21. pynode/nodes/BatchNode/batch_node.py +86 -0
  22. pynode/nodes/BatchNode/requirements.txt +1 -0
  23. pynode/nodes/CameraNode/__init__.py +4 -0
  24. pynode/nodes/CameraNode/camera_node.py +204 -0
  25. pynode/nodes/CameraNode/requirements.txt +2 -0
  26. pynode/nodes/ChangeNode/__init__.py +1 -0
  27. pynode/nodes/ChangeNode/change_node.py +255 -0
  28. pynode/nodes/ChangeNode/requirements.txt +1 -0
  29. pynode/nodes/ConfidenceFilterNode/__init__.py +4 -0
  30. pynode/nodes/ConfidenceFilterNode/confidence_filter_node.py +208 -0
  31. pynode/nodes/ConfidenceFilterNode/requirements.txt +1 -0
  32. pynode/nodes/CounterNode/__init__.py +3 -0
  33. pynode/nodes/CounterNode/counter_node.py +191 -0
  34. pynode/nodes/CropNode/__init__.py +4 -0
  35. pynode/nodes/CropNode/crop_node.py +317 -0
  36. pynode/nodes/CropNode/requirements.txt +2 -0
  37. pynode/nodes/DebugNode/__init__.py +4 -0
  38. pynode/nodes/DebugNode/debug_node.py +169 -0
  39. pynode/nodes/DebugNode/requirements.txt +1 -0
  40. pynode/nodes/DelayNode/delay_node.py +218 -0
  41. pynode/nodes/DelayNode/requirements.txt +1 -0
  42. pynode/nodes/DrawPredictionsNode/__init__.py +3 -0
  43. pynode/nodes/DrawPredictionsNode/draw_predictions_node.py +365 -0
  44. pynode/nodes/DrawPredictionsNode/requirements.txt +2 -0
  45. pynode/nodes/ErrorNode/error_node.py +94 -0
  46. pynode/nodes/ErrorNode/requirements.txt +1 -0
  47. pynode/nodes/FilterNode/__init__.py +3 -0
  48. pynode/nodes/FilterNode/filter_node.py +102 -0
  49. pynode/nodes/FilterNode/requirements.txt +1 -0
  50. pynode/nodes/FrameSourceNode/__init__.py +4 -0
  51. pynode/nodes/FrameSourceNode/frame_source_node.py +319 -0
  52. pynode/nodes/FrameSourceNode/requirements.txt +1 -0
  53. pynode/nodes/FunctionNode/__init__.py +3 -0
  54. pynode/nodes/FunctionNode/function_node.py +168 -0
  55. pynode/nodes/FunctionNode/requirements.txt +1 -0
  56. pynode/nodes/GateNode/gate_node.py +60 -0
  57. pynode/nodes/GateNode/requirements.txt +1 -0
  58. pynode/nodes/GetiUploadNode/__init__.py +3 -0
  59. pynode/nodes/GetiUploadNode/geti_upload_node.py +278 -0
  60. pynode/nodes/GetiUploadNode/requirements.txt +1 -0
  61. pynode/nodes/ImageFormatNode/__init__.py +4 -0
  62. pynode/nodes/ImageFormatNode/image_format_node.py +310 -0
  63. pynode/nodes/ImageFormatNode/requirements.txt +2 -0
  64. pynode/nodes/ImageUploadNode/image_upload_node.py +96 -0
  65. pynode/nodes/ImageViewerNode/__init__.py +4 -0
  66. pynode/nodes/ImageViewerNode/image_viewer_node.py +162 -0
  67. pynode/nodes/ImageViewerNode/requirements.txt +2 -0
  68. pynode/nodes/ImageWriterNode/__init__.py +3 -0
  69. pynode/nodes/ImageWriterNode/imagewriter_node.py +352 -0
  70. pynode/nodes/ImageWriterNode/requirements.txt +1 -0
  71. pynode/nodes/InferenceNode/InferenceEngine/__init__.py +15 -0
  72. pynode/nodes/InferenceNode/InferenceEngine/device_detection.py +149 -0
  73. pynode/nodes/InferenceNode/InferenceEngine/engines/base_engine.py +155 -0
  74. pynode/nodes/InferenceNode/InferenceEngine/engines/example_engine_template.py +272 -0
  75. pynode/nodes/InferenceNode/InferenceEngine/engines/geti_engine.py +309 -0
  76. pynode/nodes/InferenceNode/InferenceEngine/engines/onnx_engine.py +346 -0
  77. pynode/nodes/InferenceNode/InferenceEngine/engines/pass_engine.py +86 -0
  78. pynode/nodes/InferenceNode/InferenceEngine/engines/ultralytics_engine.py +863 -0
  79. pynode/nodes/InferenceNode/InferenceEngine/inference_engine_factory.py +533 -0
  80. pynode/nodes/InferenceNode/InferenceEngine/readme.md +107 -0
  81. pynode/nodes/InferenceNode/InferenceEngine/result_converters.py +121 -0
  82. pynode/nodes/InferenceNode/inference_node.py +451 -0
  83. pynode/nodes/InferenceNode/requirements.txt +13 -0
  84. pynode/nodes/InjectNode/inject_node.py +245 -0
  85. pynode/nodes/InjectNode/requirements.txt +1 -0
  86. pynode/nodes/JoinNode/__init__.py +3 -0
  87. pynode/nodes/JoinNode/join_node.py +156 -0
  88. pynode/nodes/JoinNode/requirements.txt +1 -0
  89. pynode/nodes/LabelFilterNode/__init__.py +3 -0
  90. pynode/nodes/LabelFilterNode/label_filter_node.py +188 -0
  91. pynode/nodes/LabelFilterNode/requirements.txt +1 -0
  92. pynode/nodes/MQTTNode/__init__.py +8 -0
  93. pynode/nodes/MQTTNode/mqtt_in_node.py +148 -0
  94. pynode/nodes/MQTTNode/mqtt_out_node.py +157 -0
  95. pynode/nodes/MQTTNode/mqtt_service.py +367 -0
  96. pynode/nodes/MQTTNode/requirements.txt +1 -0
  97. pynode/nodes/MessageWriterNode/__init__.py +7 -0
  98. pynode/nodes/MessageWriterNode/messagereader_node.py +568 -0
  99. pynode/nodes/MessageWriterNode/messagewriter_node.py +505 -0
  100. pynode/nodes/OmronCameraNode/__init__.py +3 -0
  101. pynode/nodes/OmronCameraNode/omron_camera_node.py +652 -0
  102. pynode/nodes/OmronCameraNode/requirements.txt +4 -0
  103. pynode/nodes/OpenCV/__init__.py +1 -0
  104. pynode/nodes/OpenCV/arithmetic_node.py +376 -0
  105. pynode/nodes/OpenCV/bitwise_node.py +127 -0
  106. pynode/nodes/OpenCV/blend_node.py +127 -0
  107. pynode/nodes/OpenCV/blob_detector_node.py +233 -0
  108. pynode/nodes/OpenCV/blur_node.py +141 -0
  109. pynode/nodes/OpenCV/color_space_node.py +139 -0
  110. pynode/nodes/OpenCV/colormap_node.py +262 -0
  111. pynode/nodes/OpenCV/contours_node.py +258 -0
  112. pynode/nodes/OpenCV/denormalize_coords_node.py +365 -0
  113. pynode/nodes/OpenCV/draw_node.py +312 -0
  114. pynode/nodes/OpenCV/edge_detector_node.py +177 -0
  115. pynode/nodes/OpenCV/equirectangular_node.py +447 -0
  116. pynode/nodes/OpenCV/fft_node.py +102 -0
  117. pynode/nodes/OpenCV/focus_estimation_node.py +189 -0
  118. pynode/nodes/OpenCV/frequency_filter_node.py +241 -0
  119. pynode/nodes/OpenCV/histogram_node.py +160 -0
  120. pynode/nodes/OpenCV/hough_circles_node.py +224 -0
  121. pynode/nodes/OpenCV/hough_lines_node.py +243 -0
  122. pynode/nodes/OpenCV/in_range_node.py +186 -0
  123. pynode/nodes/OpenCV/morphology_node.py +136 -0
  124. pynode/nodes/OpenCV/normalize_coords_node.py +344 -0
  125. pynode/nodes/OpenCV/paste_node.py +244 -0
  126. pynode/nodes/OpenCV/perspective_node.py +174 -0
  127. pynode/nodes/OpenCV/realsense_depth_node.py +253 -0
  128. pynode/nodes/OpenCV/requirements.txt +2 -0
  129. pynode/nodes/OpenCV/resize_node.py +175 -0
  130. pynode/nodes/OpenCV/rotate_node.py +159 -0
  131. pynode/nodes/OpenCV/template_match_node.py +227 -0
  132. pynode/nodes/OpenCV/threshold_node.py +167 -0
  133. pynode/nodes/PointInShapeNode/__init__.py +3 -0
  134. pynode/nodes/PointInShapeNode/point_in_shape_node.py +313 -0
  135. pynode/nodes/PointInShapeNode/requirements.txt +2 -0
  136. pynode/nodes/PolygonMetricsNode/__init__.py +3 -0
  137. pynode/nodes/PolygonMetricsNode/polygon_metrics_node.py +331 -0
  138. pynode/nodes/PolygonMetricsNode/requirements.txt +2 -0
  139. pynode/nodes/QueueLengthProbeNode/__init__.py +3 -0
  140. pynode/nodes/QueueLengthProbeNode/queue_length_probe_node.py +116 -0
  141. pynode/nodes/Qwen3VLMNode/__init__.py +3 -0
  142. pynode/nodes/Qwen3VLMNode/qwen3_vlm_node.py +519 -0
  143. pynode/nodes/Qwen3VLMNode/requirements.txt +5 -0
  144. pynode/nodes/RESTEndpointNode/__init__.py +3 -0
  145. pynode/nodes/RESTEndpointNode/rest_endpoint_node.py +491 -0
  146. pynode/nodes/RangeNode/__init__.py +3 -0
  147. pynode/nodes/RangeNode/range_node.py +115 -0
  148. pynode/nodes/RangeNode/requirements.txt +1 -0
  149. pynode/nodes/RateProbeNode/__init__.py +3 -0
  150. pynode/nodes/RateProbeNode/rate_probe_node.py +183 -0
  151. pynode/nodes/RateProbeNode/requirements.txt +1 -0
  152. pynode/nodes/RoboflowUploadNode/__init__.py +3 -0
  153. pynode/nodes/RoboflowUploadNode/requirements.txt +1 -0
  154. pynode/nodes/RoboflowUploadNode/roboflow_upload_node.py +229 -0
  155. pynode/nodes/SliceNode/README.md +149 -0
  156. pynode/nodes/SliceNode/__init__.py +5 -0
  157. pynode/nodes/SliceNode/merge_predictions_node.py +444 -0
  158. pynode/nodes/SliceNode/requirements.txt +3 -0
  159. pynode/nodes/SliceNode/slice_collector_node.py +370 -0
  160. pynode/nodes/SliceNode/slice_image_node.py +380 -0
  161. pynode/nodes/SplitNode/__init__.py +3 -0
  162. pynode/nodes/SplitNode/requirements.txt +1 -0
  163. pynode/nodes/SplitNode/split_node.py +155 -0
  164. pynode/nodes/SwitchNode/requirements.txt +1 -0
  165. pynode/nodes/SwitchNode/switch_node.py +259 -0
  166. pynode/nodes/SyncNode/__init__.py +3 -0
  167. pynode/nodes/SyncNode/sync_node.py +287 -0
  168. pynode/nodes/TemplateNode/requirements.txt +1 -0
  169. pynode/nodes/TemplateNode/template_node.py +86 -0
  170. pynode/nodes/TrackerNode/__init__.py +2 -0
  171. pynode/nodes/TrackerNode/supervision_tracker_node.py +248 -0
  172. pynode/nodes/TrackerNode/tracker_node.py +745 -0
  173. pynode/nodes/UltralyticsNode/__init__.py +4 -0
  174. pynode/nodes/UltralyticsNode/requirements.txt +2 -0
  175. pynode/nodes/UltralyticsNode/ultralytics_node.py +364 -0
  176. pynode/nodes/UnknownNode/__init__.py +93 -0
  177. pynode/nodes/VLLMNode/__init__.py +3 -0
  178. pynode/nodes/VLLMNode/vllm_node.py +345 -0
  179. pynode/nodes/VideoReaderNode/__init__.py +3 -0
  180. pynode/nodes/VideoReaderNode/video_reader_node.py +425 -0
  181. pynode/nodes/VideoWriterNode/__init__.py +3 -0
  182. pynode/nodes/VideoWriterNode/video_writer_node.py +441 -0
  183. pynode/nodes/WebhookNode/__init__.py +3 -0
  184. pynode/nodes/WebhookNode/webhook_node.py +591 -0
  185. pynode/nodes/__init__.py +109 -0
  186. pynode/nodes/base_node.py +562 -0
  187. pynode/nodes/image_utils.py +291 -0
  188. pynode/nodes/info.py +109 -0
  189. pynode/nodes/mDNSNode/__init__.py +13 -0
  190. pynode/nodes/mDNSNode/mdns_broadcast_node.py +214 -0
  191. pynode/nodes/mDNSNode/mdns_discovery_node.py +362 -0
  192. pynode/nodes/mDNSNode/mdns_manager.py +399 -0
  193. pynode/nodes/mDNSNode/requirements.txt +1 -0
  194. pynode/nodes/messages.py +81 -0
  195. pynode/server.py +187 -0
  196. pynode/static/images/favicon4.png +0 -0
  197. pynode/static/index.html +200 -0
  198. pynode/static/js/README.md +139 -0
  199. pynode/static/js/auth.js +89 -0
  200. pynode/static/js/clipboard.js +152 -0
  201. pynode/static/js/config.js +14 -0
  202. pynode/static/js/connections.js +844 -0
  203. pynode/static/js/debug.js +524 -0
  204. pynode/static/js/events.js +775 -0
  205. pynode/static/js/history.js +199 -0
  206. pynode/static/js/main.js +55 -0
  207. pynode/static/js/minimap.js +242 -0
  208. pynode/static/js/nodes.js +995 -0
  209. pynode/static/js/palette.js +255 -0
  210. pynode/static/js/properties.js +1270 -0
  211. pynode/static/js/selection.js +241 -0
  212. pynode/static/js/state.js +211 -0
  213. pynode/static/js/ui-utils.js +28 -0
  214. pynode/static/js/viewport.js +204 -0
  215. pynode/static/js/workflow.js +469 -0
  216. pynode/static/js/workflows.js +344 -0
  217. pynode/static/style.css +2751 -0
  218. pynode/workflow_engine.py +483 -0
  219. pynode/workflow_manager.py +440 -0
  220. pynode_flow-0.2.0.dist-info/METADATA +520 -0
  221. pynode_flow-0.2.0.dist-info/RECORD +227 -0
  222. pynode_flow-0.2.0.dist-info/WHEEL +5 -0
  223. pynode_flow-0.2.0.dist-info/entry_points.txt +3 -0
  224. pynode_flow-0.2.0.dist-info/licenses/LICENSE +21 -0
  225. pynode_flow-0.2.0.dist-info/scm_file_list.json +270 -0
  226. pynode_flow-0.2.0.dist-info/scm_version.json +8 -0
  227. pynode_flow-0.2.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,745 @@
1
+ import numpy as np
2
+ from pynode.nodes.base_node import BaseNode, Info, MessageKeys
3
+ import time
4
+ import logging
5
+ from typing import List, Dict, Any, Tuple
6
+
7
+ logger = logging.getLogger(__name__)
8
+
9
+ _info = Info()
10
+ _info.add_text("Implements ByteTrack algorithm to track detected objects across video frames. Uses Kalman filtering for motion prediction and IoU-based association to maintain consistent track IDs.")
11
+ _info.add_header("Inputs")
12
+ _info.add_bullets(
13
+ ("Input 0:", "Message with payload containing 'detections' array (from object detector) and optional 'image' for visualization")
14
+ )
15
+ _info.add_header("Outputs")
16
+ _info.add_bullets(
17
+ ("Output 0:", "Message with 'tracks' array added to payload, each track containing track_id, class_id, confidence, bbox [x1,y1,x2,y2], and bbox_wh [x,y,w,h]")
18
+ )
19
+ _info.add_header("Properties")
20
+ _info.add_bullets(
21
+ ("Tracking Threshold:", "Minimum confidence score for high-priority detections (default: 0.5)"),
22
+ ("Track Buffer:", "Number of frames to keep lost tracks before removal (default: 30)"),
23
+ ("Match Threshold:", "IoU threshold for associating detections with tracks (default: 0.8)"),
24
+ ("Draw Tracks:", "Annotate image with bounding boxes and track IDs")
25
+ )
26
+
27
+ class KalmanFilter:
28
+ """
29
+ A simple Kalman filter for tracking bounding boxes in image space.
30
+ The 8-dimensional state space is [x, y, a, h, vx, vy, va, vh], where the x, y is the center of the box,
31
+ a is the aspect ratio, and h is the height.
32
+ """
33
+ def __init__(self):
34
+ ndim, dt = 4, 1.
35
+
36
+ # Create Kalman filter model matrices
37
+ self._motion_mat = np.eye(2 * ndim, 2 * ndim)
38
+ for i in range(ndim):
39
+ self._motion_mat[i, ndim + i] = dt
40
+
41
+ self._update_mat = np.eye(ndim, 2 * ndim)
42
+
43
+ self._std_weight_position = 1. / 20
44
+ self._std_weight_velocity = 1. / 160
45
+
46
+ def initiate(self, measurement):
47
+ """Create track from unassociated measurement.
48
+
49
+ Parameters
50
+ ----------
51
+ measurement : ndarray
52
+ Bounding box coordinates (x, y, a, h) with center position (x, y),
53
+ aspect ratio a, and height h.
54
+
55
+ Returns
56
+ -------
57
+ (ndarray, ndarray)
58
+ Returns the mean vector (8 dimensional) and covariance matrix (8x8 dimensional) of the new track.
59
+ """
60
+ mean_pos = measurement
61
+ mean_vel = np.zeros_like(mean_pos)
62
+ mean = np.r_[mean_pos, mean_vel]
63
+
64
+ std = [
65
+ 2 * self._std_weight_position * measurement[3],
66
+ 2 * self._std_weight_position * measurement[3],
67
+ 1e-2,
68
+ 2 * self._std_weight_position * measurement[3],
69
+ 10 * self._std_weight_velocity * measurement[3],
70
+ 10 * self._std_weight_velocity * measurement[3],
71
+ 1e-5,
72
+ 10 * self._std_weight_velocity * measurement[3]
73
+ ]
74
+ covariance = np.diag(np.square(std))
75
+ return mean, covariance
76
+
77
+ def predict(self, mean, covariance):
78
+ """Run Kalman filter prediction step.
79
+
80
+ Parameters
81
+ ----------
82
+ mean : ndarray
83
+ The 8 dimensional mean vector of the object state at the previous time step.
84
+ covariance : ndarray
85
+ The 8x8 dimensional covariance matrix of the object state at the previous time step.
86
+
87
+ Returns
88
+ -------
89
+ (ndarray, ndarray)
90
+ Returns the mean vector and covariance matrix of the predicted state.
91
+ """
92
+ std_pos = [
93
+ self._std_weight_position * mean[3],
94
+ self._std_weight_position * mean[3],
95
+ 1e-2,
96
+ self._std_weight_position * mean[3]
97
+ ]
98
+ std_vel = [
99
+ self._std_weight_velocity * mean[3],
100
+ self._std_weight_velocity * mean[3],
101
+ 1e-5,
102
+ self._std_weight_velocity * mean[3]
103
+ ]
104
+ motion_cov = np.diag(np.square(np.r_[std_pos, std_vel]))
105
+
106
+ mean = np.dot(self._motion_mat, mean)
107
+ covariance = np.linalg.multi_dot((
108
+ self._motion_mat, covariance, self._motion_mat.T
109
+ )) + motion_cov
110
+
111
+ return mean, covariance
112
+
113
+ def project(self, mean, covariance):
114
+ """Project state distribution to measurement space.
115
+
116
+ Parameters
117
+ ----------
118
+ mean : ndarray
119
+ The state's mean vector (8 dimensional).
120
+ covariance : ndarray
121
+ The state's covariance matrix (8x8 dimensional).
122
+
123
+ Returns
124
+ -------
125
+ (ndarray, ndarray)
126
+ Returns the projected mean and covariance matrix of the given state estimate.
127
+ """
128
+ std = [
129
+ self._std_weight_position * mean[3],
130
+ self._std_weight_position * mean[3],
131
+ 1e-1,
132
+ self._std_weight_position * mean[3]
133
+ ]
134
+ innovation_cov = np.diag(np.square(std))
135
+
136
+ mean = np.dot(self._update_mat, mean)
137
+ covariance = np.linalg.multi_dot((
138
+ self._update_mat, covariance, self._update_mat.T
139
+ )) + innovation_cov
140
+
141
+ return mean, covariance
142
+
143
+ def update(self, mean, covariance, measurement):
144
+ """Run Kalman filter correction step.
145
+
146
+ Parameters
147
+ ----------
148
+ mean : ndarray
149
+ The predicted state's mean vector (8 dimensional).
150
+ covariance : ndarray
151
+ The state's covariance matrix (8x8 dimensional).
152
+ measurement : ndarray
153
+ The 4 dimensional measurement vector (x, y, a, h), where (x, y) is the center position,
154
+ a the aspect ratio, and h the height of the bounding box.
155
+
156
+ Returns
157
+ -------
158
+ (ndarray, ndarray)
159
+ Returns the measurement-corrected state distribution.
160
+ """
161
+ projected_mean, projected_cov = self.project(mean, covariance)
162
+
163
+ try:
164
+ # Solve S * K^T = H * P for K^T
165
+ # S = projected_cov
166
+ # B = (H * P)^T = P * H^T
167
+ B = np.dot(covariance, self._update_mat.T).T
168
+ kalman_gain = np.linalg.solve(projected_cov, B).T
169
+ except np.linalg.LinAlgError:
170
+ return mean, covariance
171
+
172
+ innovation = measurement - projected_mean
173
+
174
+ new_mean = mean + np.dot(innovation, kalman_gain.T)
175
+
176
+ # P_new = P - K * S * K^T
177
+ new_covariance = covariance - np.linalg.multi_dot((
178
+ kalman_gain, projected_cov, kalman_gain.T
179
+ ))
180
+ return new_mean, new_covariance
181
+
182
+
183
+ class STrack:
184
+ """
185
+ Single Track class for ByteTrack.
186
+ """
187
+ shared_kalman = KalmanFilter()
188
+
189
+ def __init__(self, tlwh, score, class_id):
190
+ # wait activate
191
+ self._tlwh = np.asarray(tlwh, dtype=np.float32)
192
+ self.kalman_filter = None
193
+ self.mean, self.covariance = None, None
194
+ self.is_activated = False
195
+
196
+ self.score = score
197
+ self.class_id = class_id
198
+ self.tracklet_len = 0
199
+ self.track_id = 0
200
+ self.state = 1 # TrackState.New
201
+ self.frame_id = 0
202
+ self.start_frame = 0
203
+
204
+ @property
205
+ def end_frame(self):
206
+ return self.frame_id
207
+
208
+ @property
209
+ def tlbr(self):
210
+ """Convert bounding box to format `(min x, min y, max x, max y)`, i.e.,
211
+ `(top left, bottom right)`.
212
+ """
213
+ ret = self.tlwh.copy()
214
+ ret[2:] += ret[:2]
215
+ return ret
216
+
217
+ @property
218
+ def tlwh(self):
219
+ """Get current position in bounding box format `(top left x, top left y,
220
+ width, height)`.
221
+ """
222
+ if self.mean is None:
223
+ return self._tlwh.copy()
224
+ ret = self.mean[:4].copy()
225
+ ret[2] *= ret[3]
226
+ ret[:2] -= ret[2:] / 2
227
+ return ret
228
+
229
+ def predict(self):
230
+ mean_state = self.mean.copy()
231
+ if self.state != 1: # TrackState.New
232
+ self.mean, self.covariance = self.shared_kalman.predict(mean_state, self.covariance)
233
+
234
+ def activate(self, kalman_filter, frame_id):
235
+ """Start a new tracklet"""
236
+ self.kalman_filter = kalman_filter
237
+ self.track_id = self.next_id()
238
+ self.mean, self.covariance = self.kalman_filter.initiate(self.tlwh_to_xyah(self._tlwh))
239
+
240
+ self.tracklet_len = 0
241
+ self.state = 2 # TrackState.Tracked
242
+ if frame_id == 1:
243
+ self.is_activated = True
244
+ self.frame_id = frame_id
245
+ self.start_frame = frame_id
246
+
247
+ def re_activate(self, new_track, frame_id, new_id=False):
248
+ self.mean, self.covariance = self.kalman_filter.update(
249
+ self.mean, self.covariance, self.tlwh_to_xyah(new_track.tlwh)
250
+ )
251
+ self.tracklet_len = 0
252
+ self.state = 2 # TrackState.Tracked
253
+ self.is_activated = True
254
+ self.frame_id = frame_id
255
+ if new_id:
256
+ self.track_id = self.next_id()
257
+ self.score = new_track.score
258
+ self.class_id = new_track.class_id
259
+
260
+ def update(self, new_track, frame_id):
261
+ """
262
+ Update a matched track
263
+ :type new_track: STrack
264
+ :type frame_id: int
265
+ :type update_feature: bool
266
+ :return:
267
+ """
268
+ self.frame_id = frame_id
269
+ self.tracklet_len += 1
270
+
271
+ new_tlwh = new_track.tlwh
272
+ self.mean, self.covariance = self.kalman_filter.update(
273
+ self.mean, self.covariance, self.tlwh_to_xyah(new_tlwh)
274
+ )
275
+ self.state = 2 # TrackState.Tracked
276
+ self.is_activated = True
277
+
278
+ self.score = new_track.score
279
+ self.class_id = new_track.class_id
280
+
281
+ @staticmethod
282
+ def tlwh_to_xyah(tlwh):
283
+ """Convert bounding box to format `(center x, center y, aspect ratio,
284
+ height)`, where the aspect ratio is `width / height`.
285
+ """
286
+ ret = np.asarray(tlwh).copy()
287
+ ret[:2] += ret[2:] / 2
288
+ ret[2] /= ret[3]
289
+ return ret
290
+
291
+ @staticmethod
292
+ def next_id():
293
+ if not hasattr(STrack, '_count'):
294
+ STrack._count = 0
295
+ STrack._count += 1
296
+ return STrack._count
297
+
298
+
299
+ class ByteTracker:
300
+ def __init__(self, track_thresh=0.5, track_buffer=30, match_thresh=0.8, frame_rate=30):
301
+ self.track_thresh = track_thresh
302
+ self.track_buffer = track_buffer
303
+ self.match_thresh = match_thresh
304
+ self.frame_id = 0
305
+ self.det_thresh = track_thresh + 0.1
306
+ self.buffer_size = int(frame_rate / 30.0 * track_buffer)
307
+ self.max_time_lost = self.buffer_size
308
+ self.kalman_filter = KalmanFilter()
309
+
310
+ self.tracked_stracks = []
311
+ self.lost_stracks = []
312
+ self.removed_stracks = []
313
+
314
+ def update(self, output_results):
315
+ self.frame_id += 1
316
+ activated_stracks = []
317
+ refind_stracks = []
318
+ lost_stracks = []
319
+ removed_stracks = []
320
+
321
+ # Parse detections
322
+ # output_results is list of [x1, y1, x2, y2, score, class_id]
323
+ if len(output_results) == 0:
324
+ scores = np.array([], dtype=np.float32)
325
+ bboxes = np.array([], dtype=np.float32)
326
+ classes = np.array([], dtype=np.float32)
327
+ else:
328
+ output_results = np.array(output_results)
329
+ scores = output_results[:, 4]
330
+ bboxes = output_results[:, :4] # x1y1x2y2
331
+ classes = output_results[:, 5]
332
+
333
+ remain_inds = scores > self.track_thresh
334
+ inds_low = scores > 0.1
335
+ inds_high = scores < self.track_thresh
336
+
337
+ inds_second = np.logical_and(inds_low, inds_high)
338
+
339
+ dets_second = []
340
+ dets = []
341
+
342
+ if len(bboxes) > 0:
343
+ # High confidence detections
344
+ dets = [STrack(ByteTracker.tlbr_to_tlwh(tlbr), score, cls_id)
345
+ for tlbr, score, cls_id in zip(bboxes[remain_inds], scores[remain_inds], classes[remain_inds])]
346
+
347
+ # Low confidence detections
348
+ dets_second = [STrack(ByteTracker.tlbr_to_tlwh(tlbr), score, cls_id)
349
+ for tlbr, score, cls_id in zip(bboxes[inds_second], scores[inds_second], classes[inds_second])]
350
+
351
+ # Add unconfirmed tracks to tracked_stracks list for matching
352
+ unconfirmed = []
353
+ tracked_stracks = [] # type: list[STrack]
354
+ for track in self.tracked_stracks:
355
+ if not track.is_activated:
356
+ unconfirmed.append(track)
357
+ else:
358
+ tracked_stracks.append(track)
359
+
360
+ # Step 2: First association, with high score detection boxes
361
+ strack_pool = join_stracks(tracked_stracks, self.lost_stracks)
362
+
363
+ # Predict the current location with KF
364
+ for strack in strack_pool:
365
+ strack.predict()
366
+
367
+ dists = iou_distance(strack_pool, dets)
368
+
369
+ # Use greedy matching instead of Hungarian algorithm to avoid scipy dependency
370
+ matches, u_track, u_detection = greedy_match(dists, thresh=self.match_thresh)
371
+
372
+ for itracked, idet in matches:
373
+ track = strack_pool[itracked]
374
+ det = dets[idet]
375
+ if track.state == 2: # TrackState.Tracked
376
+ track.update(det, self.frame_id)
377
+ activated_stracks.append(track)
378
+ else:
379
+ track.re_activate(det, self.frame_id, new_id=False)
380
+ refind_stracks.append(track)
381
+
382
+ # Step 3: Second association, with low score detection boxes
383
+ r_tracked_stracks = [strack_pool[i] for i in u_track if strack_pool[i].state == 2]
384
+ dists = iou_distance(r_tracked_stracks, dets_second)
385
+
386
+ matches, u_track, u_detection_second = greedy_match(dists, thresh=0.5)
387
+
388
+ for itracked, idet in matches:
389
+ track = r_tracked_stracks[itracked]
390
+ det = dets_second[idet]
391
+ if track.state == 2:
392
+ track.update(det, self.frame_id)
393
+ activated_stracks.append(track)
394
+ else:
395
+ track.re_activate(det, self.frame_id, new_id=False)
396
+ refind_stracks.append(track)
397
+
398
+ for it in u_track:
399
+ track = r_tracked_stracks[it]
400
+ if not track.state == 3: # TrackState.Lost
401
+ track.state = 3 # TrackState.Lost
402
+ lost_stracks.append(track)
403
+
404
+ # Deal with unconfirmed tracks, usually tracks with only one beginning frame
405
+ detections = [dets[i] for i in u_detection]
406
+ dists = iou_distance(unconfirmed, detections)
407
+
408
+ matches, u_unconfirmed, u_detection = greedy_match(dists, thresh=0.7)
409
+
410
+ for itracked, idet in matches:
411
+ unconfirmed[itracked].update(detections[idet], self.frame_id)
412
+ activated_stracks.append(unconfirmed[itracked])
413
+
414
+ for it in u_unconfirmed:
415
+ track = unconfirmed[it]
416
+ track.state = 4 # TrackState.Removed
417
+ removed_stracks.append(track)
418
+
419
+ # Step 4: Init new stracks
420
+ for inew in u_detection:
421
+ track = detections[inew]
422
+ if track.score < self.det_thresh:
423
+ continue
424
+ track.activate(self.kalman_filter, self.frame_id)
425
+ activated_stracks.append(track)
426
+
427
+ # Step 5: Update state
428
+ for track in self.lost_stracks:
429
+ if self.frame_id - track.end_frame > self.max_time_lost:
430
+ track.state = 4 # TrackState.Removed
431
+ removed_stracks.append(track)
432
+
433
+ self.tracked_stracks = [t for t in self.tracked_stracks if t.state == 2]
434
+ self.tracked_stracks = join_stracks(self.tracked_stracks, activated_stracks)
435
+ self.tracked_stracks = join_stracks(self.tracked_stracks, refind_stracks)
436
+ self.lost_stracks = sub_stracks(self.lost_stracks, self.tracked_stracks)
437
+ self.lost_stracks.extend(lost_stracks)
438
+ self.lost_stracks = sub_stracks(self.lost_stracks, self.removed_stracks)
439
+ self.removed_stracks.extend(removed_stracks)
440
+ self.tracked_stracks, self.lost_stracks = remove_duplicate_stracks(self.tracked_stracks, self.lost_stracks)
441
+
442
+ # Return output tracks
443
+ output_stracks = [track for track in self.tracked_stracks if track.is_activated]
444
+ return output_stracks
445
+
446
+ @staticmethod
447
+ def tlbr_to_tlwh(tlbr):
448
+ ret = np.asarray(tlbr).copy()
449
+ ret[2:] -= ret[:2]
450
+ return ret
451
+
452
+
453
+ def join_stracks(tlista, tlistb):
454
+ exists = {}
455
+ res = []
456
+ for t in tlista:
457
+ exists[t.track_id] = 1
458
+ res.append(t)
459
+ for t in tlistb:
460
+ tid = t.track_id
461
+ if not exists.get(tid, 0):
462
+ exists[tid] = 1
463
+ res.append(t)
464
+ return res
465
+
466
+
467
+ def sub_stracks(tlista, tlistb):
468
+ stracks = {}
469
+ for t in tlista:
470
+ stracks[t.track_id] = t
471
+ for t in tlistb:
472
+ tid = t.track_id
473
+ if stracks.get(tid, 0):
474
+ del stracks[tid]
475
+ return list(stracks.values())
476
+
477
+
478
+ def remove_duplicate_stracks(stracksa, stracksb):
479
+ pdist = iou_distance(stracksa, stracksb)
480
+ pairs = np.where(pdist < 0.15)
481
+ dupa, dupb = pairs
482
+ for a, b in zip(dupa, dupb):
483
+ timea = stracksa[a].frame_id - stracksa[a].start_frame
484
+ timeb = stracksb[b].frame_id - stracksb[b].start_frame
485
+ if timea > timeb:
486
+ dupb = list(dupb)
487
+ dupb.remove(b)
488
+ dupb = tuple(dupb)
489
+ else:
490
+ dupa = list(dupa)
491
+ dupa.remove(a)
492
+ dupa = tuple(dupa)
493
+ res_a = [t for i, t in enumerate(stracksa) if not i in dupa]
494
+ res_b = [t for i, t in enumerate(stracksb) if not i in dupb]
495
+ return res_a, res_b
496
+
497
+
498
+ def iou_distance(atracks, btracks):
499
+ """
500
+ Compute cost based on IoU
501
+ :type atracks: list[STrack]
502
+ :type btracks: list[STrack]
503
+ :rtype cost_matrix np.ndarray
504
+ """
505
+ if (len(atracks) == 0 and len(btracks) == 0):
506
+ return np.empty((0, 0))
507
+
508
+ atlbrs = [track.tlbr for track in atracks]
509
+ btlbrs = [track.tlbr for track in btracks]
510
+
511
+ ious = np.zeros((len(atlbrs), len(btlbrs)), dtype=np.float32)
512
+ if len(atlbrs) * len(btlbrs) == 0:
513
+ return ious
514
+
515
+ ious = bbox_ious(np.ascontiguousarray(atlbrs, dtype=np.float32),
516
+ np.ascontiguousarray(btlbrs, dtype=np.float32))
517
+ cost_matrix = 1 - ious
518
+ return cost_matrix
519
+
520
+
521
+ def bbox_ious(boxes1, boxes2):
522
+ """
523
+ Compute IOU between two sets of boxes
524
+ """
525
+ b1_x1, b1_y1, b1_x2, b1_y2 = boxes1[:, 0], boxes1[:, 1], boxes1[:, 2], boxes1[:, 3]
526
+ b2_x1, b2_y1, b2_x2, b2_y2 = boxes2[:, 0], boxes2[:, 1], boxes2[:, 2], boxes2[:, 3]
527
+
528
+ inter_rect_x1 = np.maximum(b1_x1[:, None], b2_x1)
529
+ inter_rect_y1 = np.maximum(b1_y1[:, None], b2_y1)
530
+ inter_rect_x2 = np.minimum(b1_x2[:, None], b2_x2)
531
+ inter_rect_y2 = np.minimum(b1_y2[:, None], b2_y2)
532
+
533
+ inter_area = np.maximum(inter_rect_x2 - inter_rect_x1, 0) * \
534
+ np.maximum(inter_rect_y2 - inter_rect_y1, 0)
535
+
536
+ b1_area = (b1_x2 - b1_x1) * (b1_y2 - b1_y1)
537
+ b2_area = (b2_x2 - b2_x1) * (b2_y2 - b2_y1)
538
+
539
+ iou = inter_area / (b1_area[:, None] + b2_area - inter_area + 1e-16)
540
+ return iou
541
+
542
+
543
+ def greedy_match(distance_matrix, thresh):
544
+ """
545
+ Greedy matching to replace linear_assignment (Hungarian algorithm)
546
+ when scipy is not available.
547
+ """
548
+ if distance_matrix.size == 0:
549
+ return [], list(range(distance_matrix.shape[0])), list(range(distance_matrix.shape[1]))
550
+
551
+ matched_indices = []
552
+
553
+ # Work on a copy
554
+ dist = distance_matrix.copy()
555
+ rows, cols = dist.shape
556
+
557
+ # Mask for available rows and cols
558
+ row_available = np.ones(rows, dtype=bool)
559
+ col_available = np.ones(cols, dtype=bool)
560
+
561
+ # Flatten and sort indices by distance (ascending)
562
+ # We want smallest distance (highest IoU)
563
+ flat_indices = np.argsort(dist.ravel())
564
+
565
+ for idx in flat_indices:
566
+ r, c = divmod(idx, cols)
567
+
568
+ if row_available[r] and col_available[c]:
569
+ if dist[r, c] > thresh:
570
+ # Since it's sorted, all subsequent will be > thresh
571
+ break
572
+
573
+ matched_indices.append((r, c))
574
+ row_available[r] = False
575
+ col_available[c] = False
576
+
577
+ unmatched_rows = np.where(row_available)[0].tolist()
578
+ unmatched_cols = np.where(col_available)[0].tolist()
579
+
580
+ return matched_indices, unmatched_rows, unmatched_cols
581
+
582
+
583
+ class TrackerNode(BaseNode):
584
+ """
585
+ Tracker Node that implements ByteTrack algorithm to track objects across frames.
586
+ Takes detections as input and outputs tracks with IDs.
587
+ """
588
+ info = str(_info)
589
+ display_name = 'Tracker'
590
+ icon = '👣'
591
+ category = 'vision'
592
+ color = '#2C3E50'
593
+ border_color = '#1A252F'
594
+ text_color = '#ECF0F1'
595
+
596
+ DEFAULT_CONFIG = {
597
+ 'track_thresh': '0.5',
598
+ 'track_buffer': '30',
599
+ 'match_thresh': '0.8',
600
+ 'draw_tracks': 'true'
601
+ }
602
+
603
+ properties = [
604
+ {
605
+ 'name': 'track_thresh',
606
+ 'label': 'Tracking Threshold',
607
+ 'type': 'text',
608
+ 'placeholder': '0.5'
609
+ },
610
+ {
611
+ 'name': 'track_buffer',
612
+ 'label': 'Track Buffer (frames)',
613
+ 'type': 'text',
614
+ 'placeholder': '30'
615
+ },
616
+ {
617
+ 'name': 'match_thresh',
618
+ 'label': 'Match Threshold (IoU)',
619
+ 'type': 'text',
620
+ 'placeholder': '0.8'
621
+ },
622
+ {
623
+ 'name': 'draw_tracks',
624
+ 'label': 'Draw Tracks',
625
+ 'type': 'select',
626
+ 'options': [
627
+ {'value': 'true', 'label': 'Yes'},
628
+ {'value': 'false', 'label': 'No'}
629
+ ]
630
+ }
631
+ ]
632
+
633
+ def __init__(self, node_id=None, name="tracker"):
634
+ self.tracker = None
635
+ self.last_config = {}
636
+ super().__init__(node_id, name)
637
+
638
+ def _init_tracker(self):
639
+ track_thresh = self.get_config_float('track_thresh', 0.5)
640
+ track_buffer = self.get_config_int('track_buffer', 30)
641
+ match_thresh = self.get_config_float('match_thresh', 0.8)
642
+
643
+ self.tracker = ByteTracker(
644
+ track_thresh=track_thresh,
645
+ track_buffer=track_buffer,
646
+ match_thresh=match_thresh
647
+ )
648
+
649
+ self.last_config = {
650
+ 'track_thresh': track_thresh,
651
+ 'track_buffer': track_buffer,
652
+ 'match_thresh': match_thresh
653
+ }
654
+
655
+ def configure(self, config):
656
+ super().configure(config)
657
+
658
+ # Check if tracker params changed
659
+ track_thresh = self.get_config_float('track_thresh', 0.5)
660
+ track_buffer = self.get_config_int('track_buffer', 30)
661
+ match_thresh = self.get_config_float('match_thresh', 0.8)
662
+
663
+ if (track_thresh != self.last_config.get('track_thresh') or
664
+ track_buffer != self.last_config.get('track_buffer') or
665
+ match_thresh != self.last_config.get('match_thresh')):
666
+ self._init_tracker()
667
+
668
+ def on_input(self, msg, input_index=0):
669
+ try:
670
+ payload = msg.get(MessageKeys.PAYLOAD, {})
671
+ if not isinstance(payload, dict):
672
+ self.send(msg)
673
+ return
674
+
675
+ detections = payload.get('detections', [])
676
+ if detections is None:
677
+ detections = []
678
+
679
+ # Prepare detections for tracker: [x1, y1, x2, y2, score, class_id]
680
+ tracker_inputs = []
681
+ for det in detections:
682
+ bbox = det.get('bbox', [])
683
+ if len(bbox) == 4:
684
+ score = det.get('confidence', 0.0)
685
+ class_id = det.get('class_id', 0)
686
+ tracker_inputs.append(bbox + [score, class_id])
687
+
688
+ # Run tracker
689
+ if self.tracker is None:
690
+ self._init_tracker()
691
+
692
+ online_targets = self.tracker.update(tracker_inputs)
693
+
694
+ # Format output tracks
695
+ tracks = []
696
+ for t in online_targets:
697
+ tlwh = t.tlwh
698
+ tid = t.track_id
699
+ vertical = tlwh[2] / tlwh[3] > 1.6
700
+ if tlwh[2] * tlwh[3] > 10 and not vertical:
701
+ tracks.append({
702
+ 'track_id': int(tid),
703
+ 'class_id': int(t.class_id),
704
+ 'confidence': float(t.score),
705
+ 'bbox': [float(tlwh[0]), float(tlwh[1]), float(tlwh[0] + tlwh[2]), float(tlwh[1] + tlwh[3])], # x1, y1, x2, y2
706
+ 'bbox_wh': [float(tlwh[0]), float(tlwh[1]), float(tlwh[2]), float(tlwh[3])] # x, y, w, h
707
+ })
708
+
709
+ # Update payload
710
+ payload['tracks'] = tracks
711
+ payload['track_count'] = len(tracks)
712
+
713
+ # Draw tracks if requested
714
+ if self.get_config_bool('draw_tracks', True) and MessageKeys.IMAGE.PATH in payload:
715
+ try:
716
+ import cv2
717
+ image, fmt = self.decode_image(payload[MessageKeys.IMAGE.PATH])
718
+ if image is not None and fmt is not None:
719
+ for t in tracks:
720
+ bbox = [int(x) for x in t['bbox']]
721
+ tid = t['track_id']
722
+ cls_id = t['class_id']
723
+
724
+ # Draw box
725
+ cv2.rectangle(image, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 0), 2)
726
+
727
+ # Draw label
728
+ label = f"ID:{tid} C:{cls_id}"
729
+ cv2.putText(image, label, (bbox[0], bbox[1] - 10),
730
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
731
+
732
+ # Encode back
733
+ encoded = self.encode_image(image, fmt)
734
+ if encoded:
735
+ payload[MessageKeys.IMAGE.PATH] = encoded
736
+ except Exception as e:
737
+ self.report_error(f"Error drawing tracks: {e}")
738
+
739
+ self.send(msg)
740
+
741
+ except Exception as e:
742
+ logger.error(f"Error in TrackerNode: {e}", exc_info=True)
743
+ self.report_error(f"Tracker error: {e}")
744
+ # Pass through message even on error
745
+ self.send(msg)