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,309 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import os
|
|
3
|
+
import tempfile
|
|
4
|
+
import zipfile
|
|
5
|
+
from flask import json
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
# Add the project root to the path for imports
|
|
9
|
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
10
|
+
engines_dir = current_dir
|
|
11
|
+
inference_engine_dir = os.path.dirname(engines_dir)
|
|
12
|
+
project_root = os.path.dirname(inference_engine_dir)
|
|
13
|
+
|
|
14
|
+
# Add both the project root and the parent directory to sys.path
|
|
15
|
+
if project_root not in sys.path:
|
|
16
|
+
sys.path.insert(0, project_root)
|
|
17
|
+
if inference_engine_dir not in sys.path:
|
|
18
|
+
sys.path.insert(0, inference_engine_dir)
|
|
19
|
+
|
|
20
|
+
import cv2
|
|
21
|
+
import numpy as np
|
|
22
|
+
from typing import Any, Dict
|
|
23
|
+
import shutil
|
|
24
|
+
|
|
25
|
+
# Handle both standalone and module imports
|
|
26
|
+
try:
|
|
27
|
+
from .base_engine import BaseInferenceEngine
|
|
28
|
+
except ImportError:
|
|
29
|
+
# If running as standalone script, try absolute import
|
|
30
|
+
try:
|
|
31
|
+
from InferenceEngine.engines.base_engine import BaseInferenceEngine
|
|
32
|
+
except ImportError:
|
|
33
|
+
# Last resort - direct import from the same directory
|
|
34
|
+
from base_engine import BaseInferenceEngine
|
|
35
|
+
|
|
36
|
+
# Try to import geti_sdk dependencies - fail gracefully if not available
|
|
37
|
+
try:
|
|
38
|
+
from geti_sdk.deployment import Deployment
|
|
39
|
+
from geti_sdk.utils import show_image_with_annotation_scene
|
|
40
|
+
from geti_sdk.data_models.predictions import Prediction
|
|
41
|
+
GETI_SDK_AVAILABLE = True
|
|
42
|
+
except ImportError:
|
|
43
|
+
# If geti_sdk is not installed, create placeholder to allow module to load
|
|
44
|
+
# The engine will raise a clear error when actually instantiated
|
|
45
|
+
GETI_SDK_AVAILABLE = False
|
|
46
|
+
Deployment = None
|
|
47
|
+
show_image_with_annotation_scene = None
|
|
48
|
+
Prediction = None
|
|
49
|
+
|
|
50
|
+
class GetiEngine(BaseInferenceEngine):
|
|
51
|
+
"""Inference engine for Geti models"""
|
|
52
|
+
|
|
53
|
+
# Define the user-friendly display name
|
|
54
|
+
display_name = "Geti"
|
|
55
|
+
|
|
56
|
+
def __init__(self, **kwargs):
|
|
57
|
+
# Check if geti_sdk is available before doing anything else
|
|
58
|
+
if not GETI_SDK_AVAILABLE:
|
|
59
|
+
raise ImportError(
|
|
60
|
+
"geti_sdk is not installed. GetiEngine requires the geti-sdk package. "
|
|
61
|
+
"Install it with: pip install geti-sdk"
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
super().__init__(**kwargs)
|
|
65
|
+
self.model_path = kwargs.get('model_path', None)
|
|
66
|
+
self.device = kwargs.get('device', "CPU") # Default device
|
|
67
|
+
self.output_format = kwargs.get('output_format', "geti") # Default output format
|
|
68
|
+
self.deployment = None
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _load_with_geti_sdk(self, model_path: str) -> bool:
|
|
72
|
+
"""Try to load with actual Geti SDK"""
|
|
73
|
+
try:
|
|
74
|
+
|
|
75
|
+
# Extract deployment to temporary directory
|
|
76
|
+
if model_path.endswith(".zip"):
|
|
77
|
+
temp_dir = tempfile.mkdtemp()
|
|
78
|
+
with zipfile.ZipFile(model_path, 'r') as zip_ref:
|
|
79
|
+
zip_ref.extractall(temp_dir)
|
|
80
|
+
|
|
81
|
+
model_path = temp_dir
|
|
82
|
+
|
|
83
|
+
# Load deployment
|
|
84
|
+
self.deployment = Deployment.from_folder(model_path)
|
|
85
|
+
self.deployment.load_inference_models(device=self.device)
|
|
86
|
+
self.model_path = model_path
|
|
87
|
+
|
|
88
|
+
self.logger.info("Geti SDK deployment loaded successfully")
|
|
89
|
+
return True
|
|
90
|
+
|
|
91
|
+
except ImportError:
|
|
92
|
+
self.logger.warning("Geti SDK not installed. Install with: pip install geti-sdk")
|
|
93
|
+
return False
|
|
94
|
+
except Exception as e:
|
|
95
|
+
self.logger.error(f"Error loading with Geti SDK: {e}")
|
|
96
|
+
return False
|
|
97
|
+
|
|
98
|
+
def _load_model(self, model_file: str, device: str = "CPU") -> bool:
|
|
99
|
+
|
|
100
|
+
if device is None:
|
|
101
|
+
device = self.device.upper()
|
|
102
|
+
else:
|
|
103
|
+
# Update the instance device with the passed parameter
|
|
104
|
+
self.device = device.upper()
|
|
105
|
+
|
|
106
|
+
if model_file is None:
|
|
107
|
+
model_file = self.model_path
|
|
108
|
+
|
|
109
|
+
if not self.check_valid_model(model_file):
|
|
110
|
+
return False
|
|
111
|
+
|
|
112
|
+
# If it's a deployment package
|
|
113
|
+
if not self._load_with_geti_sdk(model_file):
|
|
114
|
+
raise RuntimeError("Failed to load Geti deployment package")
|
|
115
|
+
|
|
116
|
+
# self.model_path = model_file
|
|
117
|
+
self.is_loaded = True
|
|
118
|
+
return True
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _preprocess(self, image: np.ndarray) -> np.ndarray:
|
|
122
|
+
"""Preprocess image for Geti inference"""
|
|
123
|
+
if not isinstance(image, np.ndarray):
|
|
124
|
+
raise TypeError("Input image must be a numpy array")
|
|
125
|
+
return image
|
|
126
|
+
|
|
127
|
+
def _infer(self, preprocessed_input: np.ndarray) -> Any:
|
|
128
|
+
"""Run Geti inference"""
|
|
129
|
+
if self.deployment is not None:
|
|
130
|
+
# Use actual Geti SDK
|
|
131
|
+
try:
|
|
132
|
+
prediction = self.deployment.infer(image=preprocessed_input)
|
|
133
|
+
return prediction
|
|
134
|
+
except Exception as e:
|
|
135
|
+
self.logger.error(f"Geti inference error: {e}")
|
|
136
|
+
return None
|
|
137
|
+
else:
|
|
138
|
+
return None
|
|
139
|
+
|
|
140
|
+
def _postprocess(self, raw_output: Any) -> Any:
|
|
141
|
+
"""Postprocess Geti results"""
|
|
142
|
+
return raw_output
|
|
143
|
+
|
|
144
|
+
def cleanup(self):
|
|
145
|
+
"""Clean up temporary files"""
|
|
146
|
+
if self.model_path and os.path.exists(self.model_path):
|
|
147
|
+
try:
|
|
148
|
+
shutil.rmtree(self.model_path)
|
|
149
|
+
self.logger.info("Cleaned up temporary model files")
|
|
150
|
+
except Exception as e:
|
|
151
|
+
self.logger.warning(f"Failed to cleanup model files: {e}")
|
|
152
|
+
|
|
153
|
+
def check_valid_model(self, model_file: str) -> bool:
|
|
154
|
+
# Check if the model file is valid
|
|
155
|
+
if not os.path.exists(model_file):
|
|
156
|
+
self.logger.warning(f"Model file not found: {model_file}")
|
|
157
|
+
return False
|
|
158
|
+
|
|
159
|
+
# If it's a directory, check for Geti deployment structure
|
|
160
|
+
if os.path.isdir(model_file):
|
|
161
|
+
required_items = ['deployment', 'example_code', 'LICENSE', 'README.md', 'sample_image.jpg']
|
|
162
|
+
missing_items = []
|
|
163
|
+
|
|
164
|
+
for item in required_items:
|
|
165
|
+
item_path = os.path.join(model_file, item)
|
|
166
|
+
if not os.path.exists(item_path):
|
|
167
|
+
missing_items.append(item)
|
|
168
|
+
|
|
169
|
+
if missing_items:
|
|
170
|
+
self.logger.warning(f"Directory missing required Geti deployment items: {missing_items}")
|
|
171
|
+
return False
|
|
172
|
+
|
|
173
|
+
# Additional check: ensure 'deployment' is actually a directory
|
|
174
|
+
deployment_dir = os.path.join(model_file, 'deployment')
|
|
175
|
+
if not os.path.isdir(deployment_dir):
|
|
176
|
+
self.logger.warning("'deployment' should be a directory")
|
|
177
|
+
return False
|
|
178
|
+
|
|
179
|
+
return True
|
|
180
|
+
|
|
181
|
+
# Check if the model file has a .zip extension - TODO add more checks that it's a deployment package from Geti
|
|
182
|
+
if model_file.endswith(".zip"):
|
|
183
|
+
return True
|
|
184
|
+
|
|
185
|
+
return False
|
|
186
|
+
|
|
187
|
+
def draw(self, image: np.ndarray, results: Any) -> np.ndarray:
|
|
188
|
+
image = show_image_with_annotation_scene(image, results, show_results=False, channel_order='bgr')
|
|
189
|
+
return image
|
|
190
|
+
|
|
191
|
+
def result_to_json(self, results: Any, output_format: str = "dict") -> Any:
|
|
192
|
+
#convert the results to a simple json format
|
|
193
|
+
|
|
194
|
+
# Convert Prediction object to dictionary
|
|
195
|
+
predictions_dict = None
|
|
196
|
+
if results is not None:
|
|
197
|
+
try:
|
|
198
|
+
# Try to use the model_dump method if available (Pydantic models)
|
|
199
|
+
if hasattr(results, 'model_dump'):
|
|
200
|
+
predictions_dict = results.model_dump()
|
|
201
|
+
elif hasattr(results, 'dict'):
|
|
202
|
+
predictions_dict = results.dict()
|
|
203
|
+
elif hasattr(results, 'to_dict'):
|
|
204
|
+
predictions_dict = results.to_dict()
|
|
205
|
+
elif hasattr(results, '__dict__'):
|
|
206
|
+
predictions_dict = results.__dict__
|
|
207
|
+
else:
|
|
208
|
+
predictions_dict = str(results)
|
|
209
|
+
|
|
210
|
+
# Always run through our serialization converter to handle numpy types
|
|
211
|
+
if isinstance(predictions_dict, dict):
|
|
212
|
+
predictions_dict = self._convert_to_serializable(predictions_dict)
|
|
213
|
+
|
|
214
|
+
except Exception as e:
|
|
215
|
+
self.logger.warning(f"Failed to convert prediction to dict: {e}")
|
|
216
|
+
predictions_dict = str(results)
|
|
217
|
+
|
|
218
|
+
json_results = {
|
|
219
|
+
"predictions": predictions_dict,
|
|
220
|
+
"original_image": None
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
if output_format == "dict":
|
|
225
|
+
return json_results
|
|
226
|
+
|
|
227
|
+
return json.dumps(json_results)
|
|
228
|
+
|
|
229
|
+
def _convert_to_serializable(self, obj):
|
|
230
|
+
"""Recursively convert objects to JSON serializable format"""
|
|
231
|
+
if isinstance(obj, dict):
|
|
232
|
+
return {key: self._convert_to_serializable(value) for key, value in obj.items()}
|
|
233
|
+
elif isinstance(obj, (list, tuple)):
|
|
234
|
+
return [self._convert_to_serializable(item) for item in obj]
|
|
235
|
+
elif isinstance(obj, np.ndarray):
|
|
236
|
+
return obj.tolist() # Convert numpy arrays to lists
|
|
237
|
+
elif isinstance(obj, (np.integer, np.floating)):
|
|
238
|
+
# Convert numpy scalars to Python scalars
|
|
239
|
+
if isinstance(obj, np.floating):
|
|
240
|
+
# For coordinates, convert to int; for probabilities, keep as float
|
|
241
|
+
if hasattr(obj, 'item'):
|
|
242
|
+
value = obj.item()
|
|
243
|
+
else:
|
|
244
|
+
value = float(obj)
|
|
245
|
+
# If it's a whole number, convert to int (for coordinates)
|
|
246
|
+
if isinstance(value, float) and value.is_integer():
|
|
247
|
+
return int(value)
|
|
248
|
+
else:
|
|
249
|
+
return float(value) # Ensure it's a Python float, not numpy float
|
|
250
|
+
else:
|
|
251
|
+
return obj.item() if hasattr(obj, 'item') else int(obj)
|
|
252
|
+
elif isinstance(obj, np.bool_):
|
|
253
|
+
return bool(obj) # Convert numpy booleans to Python booleans
|
|
254
|
+
elif hasattr(obj, 'model_dump'):
|
|
255
|
+
return self._convert_to_serializable(obj.model_dump())
|
|
256
|
+
elif hasattr(obj, 'dict'):
|
|
257
|
+
return self._convert_to_serializable(obj.dict())
|
|
258
|
+
elif hasattr(obj, '__dict__'):
|
|
259
|
+
return self._convert_to_serializable(obj.__dict__)
|
|
260
|
+
elif isinstance(obj, (str, int, float, bool, type(None))):
|
|
261
|
+
return obj
|
|
262
|
+
else:
|
|
263
|
+
return str(obj)
|
|
264
|
+
|
|
265
|
+
if __name__ == "__main__":
|
|
266
|
+
# Example usage - updated to work with base engine interface
|
|
267
|
+
import logging
|
|
268
|
+
logging.basicConfig(level=logging.INFO)
|
|
269
|
+
|
|
270
|
+
engine = GetiEngine()
|
|
271
|
+
|
|
272
|
+
# Try to load from deployment using the load_from_deployment method
|
|
273
|
+
deployment_path = "C:/Users/olive/OneDrive/Projects/InferNode/InferenceEngine/models/geti_sdk-deployment-yolox"
|
|
274
|
+
if os.path.exists(deployment_path):
|
|
275
|
+
if engine.load(deployment_path):
|
|
276
|
+
print("✓ Geti deployment loaded successfully")
|
|
277
|
+
|
|
278
|
+
# Test with a test image
|
|
279
|
+
test_image_path = "C:/Users/olive/OneDrive/Projects/InferNode/InferenceEngine/test_image/test.jpg"
|
|
280
|
+
if os.path.exists(test_image_path):
|
|
281
|
+
test_image = cv2.imread(test_image_path)
|
|
282
|
+
if test_image is not None:
|
|
283
|
+
print(f"✓ Test image loaded: {test_image.shape}")
|
|
284
|
+
results = engine.infer(test_image)
|
|
285
|
+
output = engine.draw(test_image, results)
|
|
286
|
+
json_results = engine.result_to_json(results)
|
|
287
|
+
cv2.imshow("Inference Output", output)
|
|
288
|
+
print("✓ Inference results (JSON):")
|
|
289
|
+
print(json_results)
|
|
290
|
+
|
|
291
|
+
print("✓ Inference results:")
|
|
292
|
+
print(results)
|
|
293
|
+
cv2.waitKey(0)
|
|
294
|
+
cv2.destroyAllWindows()
|
|
295
|
+
|
|
296
|
+
else:
|
|
297
|
+
print("✗ Failed to load test image")
|
|
298
|
+
else:
|
|
299
|
+
print(f"✗ Test image not found at {test_image_path}")
|
|
300
|
+
print("Creating dummy test image for demonstration...")
|
|
301
|
+
# Create a dummy image for testing
|
|
302
|
+
dummy_image = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8)
|
|
303
|
+
results = engine.infer(dummy_image)
|
|
304
|
+
print("✓ Inference results with dummy image:")
|
|
305
|
+
print(results)
|
|
306
|
+
else:
|
|
307
|
+
print("✗ Failed to load Geti deployment")
|
|
308
|
+
else:
|
|
309
|
+
print(f"✗ Deployment file not found at {deployment_path}")
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import numpy as np
|
|
3
|
+
import logging
|
|
4
|
+
from typing import Any, Dict, Optional, Tuple
|
|
5
|
+
|
|
6
|
+
try:
|
|
7
|
+
import onnxruntime as ort
|
|
8
|
+
except Exception:
|
|
9
|
+
ort = None
|
|
10
|
+
|
|
11
|
+
try:
|
|
12
|
+
from .base_engine import BaseInferenceEngine
|
|
13
|
+
except ImportError:
|
|
14
|
+
# Fallback for different import contexts
|
|
15
|
+
from base_engine import BaseInferenceEngine
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class OnnxEngine(BaseInferenceEngine):
|
|
19
|
+
"""Inference engine for generic ONNX models.
|
|
20
|
+
|
|
21
|
+
Expects the model to output a single tensor with shape:
|
|
22
|
+
(1, 4 + num_classes, num_detections)
|
|
23
|
+
|
|
24
|
+
The 4 are: x_center, y_center, width, height (in pixels or normalized depending on model).
|
|
25
|
+
The remaining num_classes channels are the per-class confidence scores.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
display_name = "ONNX Runtime"
|
|
29
|
+
|
|
30
|
+
def __init__(self, **kwargs):
|
|
31
|
+
super().__init__(**kwargs)
|
|
32
|
+
self.model_path = kwargs.get('model_path', self.model_path)
|
|
33
|
+
self.device = kwargs.get('device', self.device)
|
|
34
|
+
self.session: Optional[ort.InferenceSession] = None
|
|
35
|
+
self.input_name: Optional[str] = None
|
|
36
|
+
self.input_shape: Optional[Tuple[int, ...]] = None
|
|
37
|
+
self.output_name: Optional[str] = None
|
|
38
|
+
self.num_classes: Optional[int] = None
|
|
39
|
+
# Confidence threshold for filtering predictions
|
|
40
|
+
self.confidence_threshold: float = float(kwargs.get('confidence_threshold', 0.5))
|
|
41
|
+
self.cat_map: Optional[Dict[int, str]] = kwargs.get('cat_map', None)
|
|
42
|
+
# Track sizes to map model coordinates back to original image
|
|
43
|
+
# types: Optional[Tuple[int,int]]
|
|
44
|
+
self._original_image_size = None # (height, width)
|
|
45
|
+
self._model_input_size = None # (width, height)
|
|
46
|
+
self.logger = logging.getLogger(self.__class__.__name__)
|
|
47
|
+
|
|
48
|
+
def _load_model(self, model_file: str, device: str = "CPU") -> bool:
|
|
49
|
+
if ort is None:
|
|
50
|
+
self.logger.error("onnxruntime is not installed. Install with: pip install onnxruntime")
|
|
51
|
+
return False
|
|
52
|
+
|
|
53
|
+
if model_file is None or not os.path.exists(model_file):
|
|
54
|
+
self.logger.error(f"ONNX model file not found: {model_file}")
|
|
55
|
+
return False
|
|
56
|
+
|
|
57
|
+
# Create provider list based on device
|
|
58
|
+
providers = None
|
|
59
|
+
device_lower = (device or "cpu").lower()
|
|
60
|
+
if 'cuda' in device_lower:
|
|
61
|
+
# Try CUDA provider first
|
|
62
|
+
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider']
|
|
63
|
+
provider_options = None
|
|
64
|
+
elif 'gpu' in device_lower:
|
|
65
|
+
providers = ['OpenVINOExecutionProvider', 'CUDAExecutionProvider', 'CPUExecutionProvider']
|
|
66
|
+
provider_options = [{'device_type': 'GPU'}, {'device_type': 'GPU'}, {'device_type': 'CPU'}]
|
|
67
|
+
else:
|
|
68
|
+
providers = ['OpenVINOExecutionProvider', 'CPUExecutionProvider']
|
|
69
|
+
provider_options = [{'device_type': 'CPU'}, {'device_type': 'CPU'}]
|
|
70
|
+
|
|
71
|
+
try:
|
|
72
|
+
self.session = ort.InferenceSession(model_file, providers=providers, provider_options=provider_options)
|
|
73
|
+
|
|
74
|
+
# Inspect inputs/outputs
|
|
75
|
+
inputs = self.session.get_inputs()
|
|
76
|
+
outputs = self.session.get_outputs()
|
|
77
|
+
|
|
78
|
+
if len(inputs) == 0:
|
|
79
|
+
raise RuntimeError("ONNX model has no inputs")
|
|
80
|
+
|
|
81
|
+
# Use the first input
|
|
82
|
+
inp = inputs[0]
|
|
83
|
+
self.input_name = inp.name
|
|
84
|
+
self.input_shape = tuple(dim if isinstance(dim, int) else -1 for dim in inp.shape)
|
|
85
|
+
|
|
86
|
+
if len(outputs) == 0:
|
|
87
|
+
raise RuntimeError("ONNX model has no outputs")
|
|
88
|
+
|
|
89
|
+
# Expect single output with channels = 4 + num_classes
|
|
90
|
+
out = outputs[0]
|
|
91
|
+
self.output_name = out.name
|
|
92
|
+
out_shape = out.shape # may contain None or symbolic dims
|
|
93
|
+
|
|
94
|
+
# Try to infer num_classes if possible
|
|
95
|
+
# Expected shape: (1, 4+num_classes, num_detections)
|
|
96
|
+
if len(out_shape) >= 3:
|
|
97
|
+
# Replace non-int dims with -1
|
|
98
|
+
normalized = [d if isinstance(d, int) else -1 for d in out_shape]
|
|
99
|
+
if normalized[0] in (1, -1):
|
|
100
|
+
channels = normalized[1]
|
|
101
|
+
if isinstance(channels, int) and channels > 4:
|
|
102
|
+
self.num_classes = channels - 4
|
|
103
|
+
|
|
104
|
+
self.model_path = model_file
|
|
105
|
+
self.device = device
|
|
106
|
+
self.is_loaded = True
|
|
107
|
+
self.logger.info(f"Loaded ONNX model: {model_file} (input={self.input_shape}, output={out_shape})")
|
|
108
|
+
return True
|
|
109
|
+
except Exception as e:
|
|
110
|
+
self.logger.error(f"Failed to load ONNX model: {e}")
|
|
111
|
+
return False
|
|
112
|
+
|
|
113
|
+
def check_valid_model(self, model_file: str) -> bool:
|
|
114
|
+
if model_file is None:
|
|
115
|
+
return False
|
|
116
|
+
return model_file.lower().endswith('.onnx') and os.path.exists(model_file)
|
|
117
|
+
|
|
118
|
+
def _preprocess(self, image: np.ndarray) -> np.ndarray:
|
|
119
|
+
"""Preprocess input image for ONNX model.
|
|
120
|
+
|
|
121
|
+
Behavior / assumptions:
|
|
122
|
+
- Input is a HxWxC BGR uint8 image (as OpenCV typically provides).
|
|
123
|
+
- Converts to RGB, scales to [0,1], transposes to CHW and adds batch dim.
|
|
124
|
+
- If the model input height/width are fixed and known, will resize to that.
|
|
125
|
+
"""
|
|
126
|
+
if not isinstance(image, np.ndarray):
|
|
127
|
+
raise TypeError("Input image must be a numpy array")
|
|
128
|
+
|
|
129
|
+
img = image.copy()
|
|
130
|
+
# Record original image size (height, width)
|
|
131
|
+
if img.ndim >= 2:
|
|
132
|
+
self._original_image_size = (int(img.shape[0]), int(img.shape[1]))
|
|
133
|
+
else:
|
|
134
|
+
self._original_image_size = None
|
|
135
|
+
# Convert BGR -> RGB when we detect 3 channels
|
|
136
|
+
if img.ndim == 3 and img.shape[2] == 3:
|
|
137
|
+
img = img[:, :, ::-1]
|
|
138
|
+
|
|
139
|
+
# If the model has a fixed spatial input, resize
|
|
140
|
+
# Reset model input size each preprocess; we'll set it if we resize
|
|
141
|
+
self._model_input_size = None
|
|
142
|
+
if self.input_shape is not None and len(self.input_shape) >= 4:
|
|
143
|
+
# input shape often like (batch, channels, height, width) or (batch, height, width, channels)
|
|
144
|
+
shp = self.input_shape
|
|
145
|
+
# try common patterns
|
|
146
|
+
if shp[1] > 0 and shp[2] > 0 and shp[3] > 0:
|
|
147
|
+
# assume NCHW
|
|
148
|
+
expected_h = shp[2]
|
|
149
|
+
expected_w = shp[3]
|
|
150
|
+
# Store model input size as (width, height) for scaling later
|
|
151
|
+
try:
|
|
152
|
+
import cv2
|
|
153
|
+
img = cv2.resize(img, (expected_w, expected_h))
|
|
154
|
+
except Exception:
|
|
155
|
+
# If cv2 import/resize fails, fall back to numpy-based nearest neighbor resize
|
|
156
|
+
try:
|
|
157
|
+
from PIL import Image
|
|
158
|
+
img = np.array(Image.fromarray(img).resize((expected_w, expected_h)))
|
|
159
|
+
except Exception:
|
|
160
|
+
# As a last resort, leave the image unchanged
|
|
161
|
+
pass
|
|
162
|
+
self._model_input_size = (int(expected_w), int(expected_h))
|
|
163
|
+
elif shp[2] > 0 and shp[3] > 0 and shp[1] == -1:
|
|
164
|
+
# fallback heuristics ignored
|
|
165
|
+
pass
|
|
166
|
+
|
|
167
|
+
# Normalize to 0..1
|
|
168
|
+
img = img.astype(np.float32) / 255.0
|
|
169
|
+
|
|
170
|
+
# Convert HWC -> CHW
|
|
171
|
+
if img.ndim == 3:
|
|
172
|
+
img = np.transpose(img, (2, 0, 1))
|
|
173
|
+
else:
|
|
174
|
+
# grayscale -> add channel dim
|
|
175
|
+
img = np.expand_dims(img, 0)
|
|
176
|
+
|
|
177
|
+
# Add batch dim
|
|
178
|
+
img = np.expand_dims(img, 0)
|
|
179
|
+
|
|
180
|
+
return img
|
|
181
|
+
|
|
182
|
+
def _infer(self, preprocessed_input: np.ndarray) -> np.ndarray:
|
|
183
|
+
if not self.is_loaded or self.session is None:
|
|
184
|
+
raise RuntimeError("Model not loaded")
|
|
185
|
+
|
|
186
|
+
# Prepare feed dict
|
|
187
|
+
feed = {self.input_name: preprocessed_input.astype(np.float32)}
|
|
188
|
+
outputs = self.session.run([self.output_name], feed)
|
|
189
|
+
|
|
190
|
+
# Expect outputs[0] to be numpy array
|
|
191
|
+
return outputs[0]
|
|
192
|
+
|
|
193
|
+
def _postprocess(self, raw_output: np.ndarray) -> Dict[str, Any]:
|
|
194
|
+
"""Convert raw ONNX output to engine-standard dict.
|
|
195
|
+
|
|
196
|
+
raw_output expected shape: (1, 4+classes, num_detections)
|
|
197
|
+
We'll parse into list of detections where each detection includes bbox in x_center,y_center,w,h
|
|
198
|
+
and a list of class confidences.
|
|
199
|
+
"""
|
|
200
|
+
result: Dict[str, Any] = {
|
|
201
|
+
"success": True,
|
|
202
|
+
"device": self.device,
|
|
203
|
+
"model_name": os.path.basename(self.model_path) if self.model_path else None,
|
|
204
|
+
"predictions": [],
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
try:
|
|
208
|
+
out = np.asarray(raw_output)
|
|
209
|
+
|
|
210
|
+
# Ensure expected dims
|
|
211
|
+
if out.ndim != 3:
|
|
212
|
+
# Try to squeeze batch dim if present
|
|
213
|
+
if out.ndim == 4 and out.shape[0] == 1:
|
|
214
|
+
out = out[0]
|
|
215
|
+
else:
|
|
216
|
+
raise ValueError(f"Unexpected model output shape: {out.shape}")
|
|
217
|
+
|
|
218
|
+
# If first dim is batch, remove it
|
|
219
|
+
if out.shape[0] == 1:
|
|
220
|
+
out = out[0]
|
|
221
|
+
|
|
222
|
+
# Now out should be (channels, num_detections)
|
|
223
|
+
if out.ndim != 2:
|
|
224
|
+
raise ValueError(f"Unexpected post-squeezed output shape: {out.shape}")
|
|
225
|
+
|
|
226
|
+
channels, num_dets = out.shape
|
|
227
|
+
|
|
228
|
+
if channels < 5:
|
|
229
|
+
raise ValueError(f"Output channels too small: {channels}. Expect at least 5 (4+1 class)")
|
|
230
|
+
|
|
231
|
+
num_classes = channels - 4
|
|
232
|
+
# For each detection, first 4 channels are bbox, remaining channels are class confidences
|
|
233
|
+
for i in range(num_dets):
|
|
234
|
+
bbox = out[0:4, i].tolist() # [x_center, y_center, w, h]
|
|
235
|
+
class_confidences = out[4:, i].tolist() if num_classes > 0 else []
|
|
236
|
+
|
|
237
|
+
# Determine top class and score
|
|
238
|
+
if class_confidences:
|
|
239
|
+
top_idx = int(np.argmax(class_confidences))
|
|
240
|
+
top_score = float(class_confidences[top_idx])
|
|
241
|
+
else:
|
|
242
|
+
top_idx = -1
|
|
243
|
+
top_score = 0.0
|
|
244
|
+
|
|
245
|
+
# Convert bbox from model/input coords to original image coords if possible
|
|
246
|
+
# bbox is x_center, y_center, width, height in either absolute units (model input pixels)
|
|
247
|
+
# or normalized [0,1]. We'll map to original image size when _original_image_size is known.
|
|
248
|
+
x_c, y_c, bw, bh = bbox
|
|
249
|
+
|
|
250
|
+
if self._original_image_size is not None:
|
|
251
|
+
orig_h, orig_w = self._original_image_size
|
|
252
|
+
|
|
253
|
+
if self._model_input_size is not None:
|
|
254
|
+
model_w, model_h = self._model_input_size
|
|
255
|
+
# If bbox values appear to be in model pixel space (greater than 1), scale by ratio
|
|
256
|
+
if any(v > 1.0 for v in [x_c, y_c, bw, bh]):
|
|
257
|
+
scale_x = orig_w / model_w if model_w > 0 else 1.0
|
|
258
|
+
scale_y = orig_h / model_h if model_h > 0 else 1.0
|
|
259
|
+
x_c = x_c * scale_x
|
|
260
|
+
bw = bw * scale_x
|
|
261
|
+
y_c = y_c * scale_y
|
|
262
|
+
bh = bh * scale_y
|
|
263
|
+
else:
|
|
264
|
+
# bbox appears normalized -> scale by original image size
|
|
265
|
+
x_c = x_c * orig_w
|
|
266
|
+
bw = bw * orig_w
|
|
267
|
+
y_c = y_c * orig_h
|
|
268
|
+
bh = bh * orig_h
|
|
269
|
+
else:
|
|
270
|
+
# No explicit model input size. Use normalization heuristic
|
|
271
|
+
if any(v > 1.0 for v in [x_c, y_c, bw, bh]):
|
|
272
|
+
# Assume already in original image coords
|
|
273
|
+
pass
|
|
274
|
+
else:
|
|
275
|
+
x_c = x_c * orig_w
|
|
276
|
+
bw = bw * orig_w
|
|
277
|
+
y_c = y_c * orig_h
|
|
278
|
+
bh = bh * orig_h
|
|
279
|
+
|
|
280
|
+
# Update bbox to scaled values
|
|
281
|
+
bbox = [x_c, y_c, bw, bh]
|
|
282
|
+
|
|
283
|
+
# Filter by confidence threshold
|
|
284
|
+
if top_score < self.confidence_threshold:
|
|
285
|
+
# Skip low-confidence detection
|
|
286
|
+
continue
|
|
287
|
+
|
|
288
|
+
if self.cat_map:
|
|
289
|
+
# Map to external category name if provided
|
|
290
|
+
top_idx = self.cat_map.get(top_idx, "Other")
|
|
291
|
+
|
|
292
|
+
det = {
|
|
293
|
+
"bbox": bbox,
|
|
294
|
+
"bbox_format": "xywh_center",
|
|
295
|
+
"class_confidences": class_confidences,
|
|
296
|
+
"top_class": top_idx,
|
|
297
|
+
"top_score": top_score,
|
|
298
|
+
"detection_index": i
|
|
299
|
+
}
|
|
300
|
+
result["predictions"].append(det)
|
|
301
|
+
|
|
302
|
+
result["num_detections"] = len(result["predictions"])
|
|
303
|
+
# Echo back the confidence threshold used
|
|
304
|
+
result["confidence_threshold"] = float(self.confidence_threshold)
|
|
305
|
+
return result
|
|
306
|
+
|
|
307
|
+
except Exception as e:
|
|
308
|
+
self.logger.error(f"ONNX postprocessing failed: {e}")
|
|
309
|
+
return {
|
|
310
|
+
"success": False,
|
|
311
|
+
"error": str(e),
|
|
312
|
+
"device": self.device
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
def draw(self, image: np.ndarray, results: Dict[str, Any]) -> np.ndarray:
|
|
316
|
+
# Draw simple boxes using xywh center format
|
|
317
|
+
import cv2
|
|
318
|
+
out_img = image.copy()
|
|
319
|
+
h, w = out_img.shape[:2]
|
|
320
|
+
|
|
321
|
+
for pred in results.get('predictions', []):
|
|
322
|
+
x_c, y_c, bw, bh = pred.get('bbox', [0, 0, 0, 0])
|
|
323
|
+
|
|
324
|
+
# If bbox seems normalized (<=1), scale to image size
|
|
325
|
+
if 0 < x_c <= 1 and 0 < y_c <= 1 and 0 < bw <= 1 and 0 < bh <= 1:
|
|
326
|
+
x_c *= w
|
|
327
|
+
y_c *= h
|
|
328
|
+
bw *= w
|
|
329
|
+
bh *= h
|
|
330
|
+
|
|
331
|
+
x1 = int(x_c - bw / 2)
|
|
332
|
+
y1 = int(y_c - bh / 2)
|
|
333
|
+
x2 = int(x_c + bw / 2)
|
|
334
|
+
y2 = int(y_c + bh / 2)
|
|
335
|
+
|
|
336
|
+
cv2.rectangle(out_img, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
|
337
|
+
label = f"{pred.get('top_class', -1)}:{pred.get('top_score', 0):.2f}"
|
|
338
|
+
cv2.putText(out_img, label, (max(x1,0), max(y1-6,0)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 1)
|
|
339
|
+
|
|
340
|
+
return out_img
|
|
341
|
+
|
|
342
|
+
def result_to_json(self, results: Dict[str, Any], output_format: str = "dict") -> Any:
|
|
343
|
+
import json
|
|
344
|
+
if output_format == 'dict':
|
|
345
|
+
return results
|
|
346
|
+
return json.dumps(results, indent=2)
|