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,149 @@
|
|
|
1
|
+
"""OpenVINO device detection and device-string translation helpers.
|
|
2
|
+
|
|
3
|
+
This module must stay importable when openvino (and ultralytics/torch) are
|
|
4
|
+
NOT installed: enumeration is attempted lazily on first use and falls back to
|
|
5
|
+
a static device list when openvino is unavailable or enumeration fails.
|
|
6
|
+
|
|
7
|
+
Device naming background:
|
|
8
|
+
|
|
9
|
+
- ``openvino.Core().available_devices`` returns names like
|
|
10
|
+
``['CPU', 'GPU', 'NPU']`` (single GPU) or ``['CPU', 'GPU.0', 'GPU.1']``
|
|
11
|
+
(multiple GPUs).
|
|
12
|
+
- PyNode/Ultralytics config values use the ``intel:<device>`` form
|
|
13
|
+
(``intel:gpu``, ``intel:gpu.1`` ...). Ultralytics strips the ``intel:``
|
|
14
|
+
prefix and passes the uppercased remainder to OpenVINO, so on a multi-GPU
|
|
15
|
+
system a plain ``intel:gpu`` becomes ``GPU`` which is NOT in
|
|
16
|
+
``available_devices`` and Ultralytics silently falls back to ``AUTO``.
|
|
17
|
+
:func:`resolve_intel_device` fixes that by resolving ``intel:gpu`` to the
|
|
18
|
+
first detected GPU (e.g. ``intel:gpu.0``).
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
import logging
|
|
22
|
+
from typing import Dict, List, Optional, Tuple
|
|
23
|
+
|
|
24
|
+
logger = logging.getLogger(__name__)
|
|
25
|
+
|
|
26
|
+
# Static fallback used when openvino is not importable or enumeration fails.
|
|
27
|
+
# It mirrors the historical hardcoded dropdown (CPU/GPU/NPU) so behavior is
|
|
28
|
+
# unchanged on systems without openvino.
|
|
29
|
+
FALLBACK_DEVICES: List[str] = ['CPU', 'GPU', 'NPU']
|
|
30
|
+
|
|
31
|
+
# Cache: (devices, detected). ``detected`` is False when the fallback list is
|
|
32
|
+
# in use. Enumeration can be slow, so it runs once (lazily) per process.
|
|
33
|
+
_device_cache: Optional[Tuple[List[str], bool]] = None
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _enumerate_openvino_devices() -> Optional[List[str]]:
|
|
37
|
+
"""Return the real OpenVINO device list, or None if unavailable."""
|
|
38
|
+
try:
|
|
39
|
+
from openvino import Core # type: ignore
|
|
40
|
+
except ImportError:
|
|
41
|
+
try:
|
|
42
|
+
# Older openvino releases expose Core under openvino.runtime
|
|
43
|
+
from openvino.runtime import Core # type: ignore
|
|
44
|
+
except ImportError:
|
|
45
|
+
return None
|
|
46
|
+
try:
|
|
47
|
+
return list(Core().available_devices)
|
|
48
|
+
except Exception as e:
|
|
49
|
+
logger.warning(f"OpenVINO device enumeration failed: {e}")
|
|
50
|
+
return None
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def get_openvino_devices(refresh: bool = False) -> Tuple[List[str], bool]:
|
|
54
|
+
"""Return ``(devices, detected)``.
|
|
55
|
+
|
|
56
|
+
``devices`` is e.g. ``['CPU', 'GPU.0', 'GPU.1']`` when openvino is
|
|
57
|
+
available, otherwise :data:`FALLBACK_DEVICES`. ``detected`` says whether
|
|
58
|
+
real enumeration succeeded. The result is cached after the first call.
|
|
59
|
+
"""
|
|
60
|
+
global _device_cache
|
|
61
|
+
if refresh or _device_cache is None:
|
|
62
|
+
devices = _enumerate_openvino_devices()
|
|
63
|
+
if devices:
|
|
64
|
+
_device_cache = (devices, True)
|
|
65
|
+
else:
|
|
66
|
+
_device_cache = (list(FALLBACK_DEVICES), False)
|
|
67
|
+
devices, detected = _device_cache
|
|
68
|
+
return list(devices), detected
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _gpu_devices(devices: List[str]) -> List[str]:
|
|
72
|
+
"""Filter GPU entries ('GPU', 'GPU.0', ...) preserving enumeration order."""
|
|
73
|
+
return [d for d in devices if d == 'GPU' or d.startswith('GPU.')]
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def get_intel_device_options() -> List[Dict[str, str]]:
|
|
77
|
+
"""Build the Intel OpenVINO entries for the hardware dropdown.
|
|
78
|
+
|
|
79
|
+
- Intel CPU is always offered.
|
|
80
|
+
- One entry per detected GPU; if there is exactly one GPU the label has
|
|
81
|
+
no index. Values keep the device suffix (``intel:gpu.0`` etc.).
|
|
82
|
+
- NPU is offered only when present in the device list (the static
|
|
83
|
+
fallback list includes it, preserving the old dropdown when openvino
|
|
84
|
+
is not installed).
|
|
85
|
+
"""
|
|
86
|
+
devices, _detected = get_openvino_devices()
|
|
87
|
+
|
|
88
|
+
options: List[Dict[str, str]] = [
|
|
89
|
+
{'value': 'intel:cpu', 'label': 'Intel CPU (OpenVINO)'},
|
|
90
|
+
]
|
|
91
|
+
|
|
92
|
+
gpus = _gpu_devices(devices)
|
|
93
|
+
if len(gpus) == 1:
|
|
94
|
+
options.append({
|
|
95
|
+
'value': f'intel:{gpus[0].lower()}',
|
|
96
|
+
'label': 'Intel GPU (OpenVINO)',
|
|
97
|
+
})
|
|
98
|
+
else:
|
|
99
|
+
for gpu in gpus:
|
|
100
|
+
if '.' in gpu:
|
|
101
|
+
index = gpu.split('.', 1)[1]
|
|
102
|
+
label = f'Intel GPU {index} (OpenVINO)'
|
|
103
|
+
else:
|
|
104
|
+
label = 'Intel GPU (OpenVINO)'
|
|
105
|
+
options.append({
|
|
106
|
+
'value': f'intel:{gpu.lower()}',
|
|
107
|
+
'label': label,
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
if any(d == 'NPU' or d.startswith('NPU.') for d in devices):
|
|
111
|
+
options.append({'value': 'intel:npu', 'label': 'Intel NPU (OpenVINO)'})
|
|
112
|
+
|
|
113
|
+
return options
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def resolve_intel_device(device: str) -> str:
|
|
117
|
+
"""Resolve a plain ``intel:gpu`` to the first detected GPU.
|
|
118
|
+
|
|
119
|
+
``intel:gpu`` (any case) becomes e.g. ``intel:gpu.0`` on multi-GPU
|
|
120
|
+
systems so the exact device - not AUTO - is targeted. Anything else
|
|
121
|
+
(``intel:gpu.1``, ``intel:cpu``, ``cuda:0``, ``cpu`` ...) is returned
|
|
122
|
+
unchanged, and when detection is unavailable the input is returned as-is
|
|
123
|
+
(backward compatible with saved workflows on systems without openvino).
|
|
124
|
+
"""
|
|
125
|
+
if not isinstance(device, str) or device.lower() != 'intel:gpu':
|
|
126
|
+
return device
|
|
127
|
+
|
|
128
|
+
devices, detected = get_openvino_devices()
|
|
129
|
+
if not detected:
|
|
130
|
+
return device
|
|
131
|
+
|
|
132
|
+
gpus = _gpu_devices(devices)
|
|
133
|
+
if not gpus:
|
|
134
|
+
return device
|
|
135
|
+
return f'intel:{gpus[0].lower()}'
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def to_openvino_device_name(device: str) -> str:
|
|
139
|
+
"""Translate a config value to the OpenVINO device name Ultralytics uses.
|
|
140
|
+
|
|
141
|
+
``intel:gpu.1`` -> ``GPU.1``; ``intel:gpu`` -> first detected GPU (e.g.
|
|
142
|
+
``GPU.0``) or ``GPU`` when detection is unavailable; non-``intel:``
|
|
143
|
+
values are just uppercased (matching Ultralytics' behavior of
|
|
144
|
+
``device.split(':')[1].upper()``).
|
|
145
|
+
"""
|
|
146
|
+
resolved = resolve_intel_device(device)
|
|
147
|
+
if isinstance(resolved, str) and resolved.lower().startswith('intel:'):
|
|
148
|
+
return resolved.split(':', 1)[1].upper()
|
|
149
|
+
return str(resolved).upper()
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
from typing import Any, Dict, Optional
|
|
3
|
+
import logging
|
|
4
|
+
import numpy as np
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class BaseInferenceEngine(ABC):
|
|
8
|
+
"""Base class for all inference engines"""
|
|
9
|
+
|
|
10
|
+
# Class attribute that should be overridden by subclasses
|
|
11
|
+
display_name = "Base Inference Engine"
|
|
12
|
+
|
|
13
|
+
def __init__(self, **kwargs):
|
|
14
|
+
|
|
15
|
+
self.model_path = kwargs.get('model_path', None)
|
|
16
|
+
self.device = kwargs.get('device', "CPU") # Default device
|
|
17
|
+
|
|
18
|
+
self.is_loaded = False
|
|
19
|
+
self.type = self.__class__.__name__
|
|
20
|
+
self.logger = logging.getLogger(self.__class__.__name__)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def load(self, model_file: Optional[str] = None, device: Optional[str] = None) -> bool:
|
|
24
|
+
"""
|
|
25
|
+
Load the model for inference
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
model_file: Path to the model file. If None, uses self.model_path from constructor
|
|
29
|
+
device: Device to load the model on. If None, uses self.device from constructor
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
bool: True if model loaded successfully, False otherwise
|
|
33
|
+
"""
|
|
34
|
+
# Use constructor values if parameters are None
|
|
35
|
+
if model_file is None:
|
|
36
|
+
model_file = self.model_path
|
|
37
|
+
|
|
38
|
+
if device is None:
|
|
39
|
+
device = self.device
|
|
40
|
+
|
|
41
|
+
# Validate that we have required values
|
|
42
|
+
if model_file is None:
|
|
43
|
+
raise ValueError("model_file must be provided either as parameter or in constructor")
|
|
44
|
+
|
|
45
|
+
if device is None:
|
|
46
|
+
device = "CPU" # Default fallback
|
|
47
|
+
else:
|
|
48
|
+
device = device.upper()
|
|
49
|
+
|
|
50
|
+
# Store the values
|
|
51
|
+
self.model_path = model_file
|
|
52
|
+
self.device = device
|
|
53
|
+
|
|
54
|
+
# Delegate to subclass implementation
|
|
55
|
+
return self._load_model(model_file, device)
|
|
56
|
+
|
|
57
|
+
@abstractmethod
|
|
58
|
+
def _load_model(self, model_file: str, device: str) -> bool:
|
|
59
|
+
"""Load the model - must be implemented by subclasses"""
|
|
60
|
+
pass
|
|
61
|
+
|
|
62
|
+
def infer(self, image: np.ndarray) -> Dict[str, Any]:
|
|
63
|
+
"""
|
|
64
|
+
Runs inference on the input image
|
|
65
|
+
|
|
66
|
+
Args:
|
|
67
|
+
image: Input image data
|
|
68
|
+
|
|
69
|
+
Returns:
|
|
70
|
+
Dict containing inference results
|
|
71
|
+
"""
|
|
72
|
+
if not self.is_loaded:
|
|
73
|
+
return {
|
|
74
|
+
"success": False,
|
|
75
|
+
"error": "Model not loaded",
|
|
76
|
+
"device": self.device
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
try:
|
|
80
|
+
# Preprocessing
|
|
81
|
+
preprocessed_input = self._preprocess(image)
|
|
82
|
+
|
|
83
|
+
# Inference
|
|
84
|
+
raw_output = self._infer(preprocessed_input)
|
|
85
|
+
|
|
86
|
+
# Postprocessing
|
|
87
|
+
processed_output = self._postprocess(raw_output)
|
|
88
|
+
|
|
89
|
+
return processed_output
|
|
90
|
+
except Exception as e:
|
|
91
|
+
self.logger.error(f"Inference failed: {str(e)}")
|
|
92
|
+
return {
|
|
93
|
+
"success": False,
|
|
94
|
+
"error": str(e),
|
|
95
|
+
"device": self.device
|
|
96
|
+
}
|
|
97
|
+
# }
|
|
98
|
+
|
|
99
|
+
# except Exception as e:
|
|
100
|
+
# self.logger.error(f"Inference failed: {str(e)}")
|
|
101
|
+
# return {
|
|
102
|
+
# "success": False,
|
|
103
|
+
# "error": str(e),
|
|
104
|
+
# "device": self.device
|
|
105
|
+
# }
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
@abstractmethod
|
|
109
|
+
def check_valid_model(self, model_file: str) -> bool:
|
|
110
|
+
"""Check if the model file is valid"""
|
|
111
|
+
pass
|
|
112
|
+
|
|
113
|
+
@abstractmethod
|
|
114
|
+
def _preprocess(self, image: np.ndarray) -> np.ndarray:
|
|
115
|
+
"""Preprocess input data (expects a numpy array) - must be implemented by subclasses"""
|
|
116
|
+
pass
|
|
117
|
+
|
|
118
|
+
@abstractmethod
|
|
119
|
+
def _infer(self, preprocessed_input: np.ndarray) -> np.ndarray:
|
|
120
|
+
"""Run inference - must be implemented by subclasses"""
|
|
121
|
+
pass
|
|
122
|
+
|
|
123
|
+
@abstractmethod
|
|
124
|
+
def _postprocess(self, raw_output: Any) -> Any:
|
|
125
|
+
"""Postprocess model output - must be implemented by subclasses"""
|
|
126
|
+
pass
|
|
127
|
+
|
|
128
|
+
@classmethod
|
|
129
|
+
def get_display_name(cls) -> str:
|
|
130
|
+
"""Get the user-friendly display name for this engine"""
|
|
131
|
+
return cls.display_name
|
|
132
|
+
|
|
133
|
+
def get_info(self) -> Dict[str, Any]:
|
|
134
|
+
"""Get information about the current engine state"""
|
|
135
|
+
return {
|
|
136
|
+
"engine_type": self.__class__.__name__,
|
|
137
|
+
"display_name": self.__class__.display_name,
|
|
138
|
+
"device": self.device,
|
|
139
|
+
"is_loaded": self.is_loaded,
|
|
140
|
+
"model_path": self.model_path
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
def __str__(self):
|
|
144
|
+
info = self.get_info()
|
|
145
|
+
return f"Engine Type: {info['engine_type']}, Device: {info['device']}, Loaded: {info['is_loaded']}, Model Path: {info['model_path']}"
|
|
146
|
+
|
|
147
|
+
@abstractmethod
|
|
148
|
+
def draw(self, image: np.ndarray, results: Any) -> np.ndarray:
|
|
149
|
+
"""Draw the results on the image - must be implemented by subclasses"""
|
|
150
|
+
pass
|
|
151
|
+
|
|
152
|
+
@abstractmethod
|
|
153
|
+
def result_to_json(self, results: Any, output_format: str = "dict") -> Any:
|
|
154
|
+
"""Convert inference results to JSON format - must be implemented by subclasses"""
|
|
155
|
+
pass
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
Example template for creating a new inference engine.
|
|
4
|
+
This file demonstrates how to implement the BaseInferenceEngine interface.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
from typing import Any, Optional
|
|
9
|
+
import json
|
|
10
|
+
|
|
11
|
+
# Import the base engine class
|
|
12
|
+
try:
|
|
13
|
+
from .base_engine import BaseInferenceEngine
|
|
14
|
+
except ImportError:
|
|
15
|
+
from base_engine import BaseInferenceEngine
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class ExampleEngine(BaseInferenceEngine):
|
|
19
|
+
"""
|
|
20
|
+
Example inference engine template.
|
|
21
|
+
|
|
22
|
+
This class demonstrates how to create a new inference engine by:
|
|
23
|
+
1. Inheriting from BaseInferenceEngine
|
|
24
|
+
2. Setting the display_name class attribute
|
|
25
|
+
3. Implementing all required abstract methods
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
# REQUIRED: Define the user-friendly display name for your engine
|
|
29
|
+
display_name = "My Custom AI Engine"
|
|
30
|
+
|
|
31
|
+
def __init__(self, **kwargs):
|
|
32
|
+
"""
|
|
33
|
+
Initialize your engine.
|
|
34
|
+
|
|
35
|
+
You can add custom parameters here while calling super().__init__(**kwargs)
|
|
36
|
+
to handle standard parameters like model_path and device.
|
|
37
|
+
"""
|
|
38
|
+
super().__init__(**kwargs)
|
|
39
|
+
|
|
40
|
+
# Add any custom initialization here
|
|
41
|
+
self.custom_parameter = kwargs.get('custom_parameter', 'default_value')
|
|
42
|
+
self.model = None
|
|
43
|
+
|
|
44
|
+
def _load_model(self, model_file: str, device: str) -> bool:
|
|
45
|
+
"""
|
|
46
|
+
Load your model from the specified file.
|
|
47
|
+
|
|
48
|
+
Args:
|
|
49
|
+
model_file: Path to the model file
|
|
50
|
+
device: Target device (e.g., 'cpu', 'gpu', 'intel:gpu')
|
|
51
|
+
|
|
52
|
+
Returns:
|
|
53
|
+
bool: True if model loaded successfully, False otherwise
|
|
54
|
+
"""
|
|
55
|
+
try:
|
|
56
|
+
# Implement your model loading logic here
|
|
57
|
+
# For example:
|
|
58
|
+
# self.model = your_framework.load_model(model_file)
|
|
59
|
+
# self.model.to(device)
|
|
60
|
+
|
|
61
|
+
self.logger.info(f"Loading model from {model_file} on device {device}")
|
|
62
|
+
|
|
63
|
+
# For this example, we'll just simulate loading
|
|
64
|
+
self.model = {"loaded": True, "path": model_file, "device": device}
|
|
65
|
+
self.is_loaded = True
|
|
66
|
+
|
|
67
|
+
return True
|
|
68
|
+
|
|
69
|
+
except Exception as e:
|
|
70
|
+
self.logger.error(f"Failed to load model: {e}")
|
|
71
|
+
return False
|
|
72
|
+
|
|
73
|
+
def check_valid_model(self, model_file: str) -> bool:
|
|
74
|
+
"""
|
|
75
|
+
Check if the model file is valid for this engine.
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
model_file: Path to the model file
|
|
79
|
+
|
|
80
|
+
Returns:
|
|
81
|
+
bool: True if the model is valid, False otherwise
|
|
82
|
+
"""
|
|
83
|
+
# Implement your model validation logic here
|
|
84
|
+
# For example, check file extension, file format, etc.
|
|
85
|
+
|
|
86
|
+
# Example: Check if file exists and has correct extension
|
|
87
|
+
import os
|
|
88
|
+
if not os.path.exists(model_file):
|
|
89
|
+
return False
|
|
90
|
+
|
|
91
|
+
# Check for your specific model format
|
|
92
|
+
if model_file.endswith(('.pt', '.onnx', '.pb', '.h5')):
|
|
93
|
+
return True
|
|
94
|
+
|
|
95
|
+
return False
|
|
96
|
+
|
|
97
|
+
def _preprocess(self, image: np.ndarray) -> np.ndarray:
|
|
98
|
+
"""
|
|
99
|
+
Preprocess the input image for inference.
|
|
100
|
+
|
|
101
|
+
Args:
|
|
102
|
+
image: Input image as numpy array
|
|
103
|
+
|
|
104
|
+
Returns:
|
|
105
|
+
np.ndarray: Preprocessed image
|
|
106
|
+
"""
|
|
107
|
+
# Implement your preprocessing logic here
|
|
108
|
+
# For example: resize, normalize, convert color space, etc.
|
|
109
|
+
|
|
110
|
+
# Example preprocessing
|
|
111
|
+
if not isinstance(image, np.ndarray):
|
|
112
|
+
raise TypeError("Input image must be a numpy array")
|
|
113
|
+
|
|
114
|
+
# Example: Ensure image is in the right format
|
|
115
|
+
if len(image.shape) == 3 and image.shape[2] == 3:
|
|
116
|
+
# Convert BGR to RGB if needed
|
|
117
|
+
# preprocessed = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
|
118
|
+
preprocessed = image # For this example, no conversion
|
|
119
|
+
else:
|
|
120
|
+
preprocessed = image
|
|
121
|
+
|
|
122
|
+
return preprocessed
|
|
123
|
+
|
|
124
|
+
def _infer(self, preprocessed_input: np.ndarray) -> Any:
|
|
125
|
+
"""
|
|
126
|
+
Run inference on the preprocessed input.
|
|
127
|
+
|
|
128
|
+
Args:
|
|
129
|
+
preprocessed_input: Preprocessed image data
|
|
130
|
+
|
|
131
|
+
Returns:
|
|
132
|
+
Any: Raw inference results from your model
|
|
133
|
+
"""
|
|
134
|
+
if self.model is None:
|
|
135
|
+
return None
|
|
136
|
+
|
|
137
|
+
# Implement your inference logic here
|
|
138
|
+
# For example:
|
|
139
|
+
# results = self.model.predict(preprocessed_input)
|
|
140
|
+
|
|
141
|
+
# For this example, return simulated results
|
|
142
|
+
results = {
|
|
143
|
+
"detections": [
|
|
144
|
+
{
|
|
145
|
+
"class_id": 0,
|
|
146
|
+
"class_name": "example_object",
|
|
147
|
+
"confidence": 0.85,
|
|
148
|
+
"bbox": [100, 100, 200, 200]
|
|
149
|
+
}
|
|
150
|
+
]
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return results
|
|
154
|
+
|
|
155
|
+
def _postprocess(self, raw_output: Any) -> Any:
|
|
156
|
+
"""
|
|
157
|
+
Postprocess the raw inference results.
|
|
158
|
+
|
|
159
|
+
Args:
|
|
160
|
+
raw_output: Raw results from _infer method
|
|
161
|
+
|
|
162
|
+
Returns:
|
|
163
|
+
Any: Processed results ready for output
|
|
164
|
+
"""
|
|
165
|
+
# Implement your postprocessing logic here
|
|
166
|
+
# For example: apply NMS, filter by confidence, format results, etc.
|
|
167
|
+
|
|
168
|
+
# For this example, just return the raw output
|
|
169
|
+
return raw_output
|
|
170
|
+
|
|
171
|
+
def draw(self, image: np.ndarray, results: Any) -> np.ndarray:
|
|
172
|
+
"""
|
|
173
|
+
Draw inference results on the image.
|
|
174
|
+
|
|
175
|
+
Args:
|
|
176
|
+
image: Original input image
|
|
177
|
+
results: Processed inference results
|
|
178
|
+
|
|
179
|
+
Returns:
|
|
180
|
+
np.ndarray: Image with drawn annotations
|
|
181
|
+
"""
|
|
182
|
+
# Implement your visualization logic here
|
|
183
|
+
# For example: draw bounding boxes, labels, confidence scores, etc.
|
|
184
|
+
|
|
185
|
+
# For this example, just return the original image
|
|
186
|
+
# In a real implementation, you would draw the results
|
|
187
|
+
annotated_image = image.copy()
|
|
188
|
+
|
|
189
|
+
# Example drawing logic:
|
|
190
|
+
# if results and "detections" in results:
|
|
191
|
+
# for detection in results["detections"]:
|
|
192
|
+
# bbox = detection["bbox"]
|
|
193
|
+
# cv2.rectangle(annotated_image, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 0), 2)
|
|
194
|
+
# cv2.putText(annotated_image, f"{detection['class_name']}: {detection['confidence']:.2f}",
|
|
195
|
+
# (bbox[0], bbox[1]-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
|
196
|
+
|
|
197
|
+
return annotated_image
|
|
198
|
+
|
|
199
|
+
def result_to_json(self, results: Any, output_format: str = "dict") -> str:
|
|
200
|
+
"""
|
|
201
|
+
Convert inference results to JSON format.
|
|
202
|
+
|
|
203
|
+
Args:
|
|
204
|
+
results: Processed inference results
|
|
205
|
+
original_image: Optional original image for base64 encoding
|
|
206
|
+
|
|
207
|
+
Returns:
|
|
208
|
+
str: JSON string representation of results
|
|
209
|
+
"""
|
|
210
|
+
# Implement your JSON conversion logic here
|
|
211
|
+
|
|
212
|
+
# Example JSON formatting
|
|
213
|
+
json_results = {
|
|
214
|
+
"engine_type": self.__class__.__name__,
|
|
215
|
+
"display_name": self.display_name,
|
|
216
|
+
"results": results,
|
|
217
|
+
"original_image": None
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
# Optionally include base64 encoded image
|
|
221
|
+
# if original_image is not None:
|
|
222
|
+
# import cv2
|
|
223
|
+
# import base64
|
|
224
|
+
# success, buffer = cv2.imencode('.jpg', original_image)
|
|
225
|
+
# if success:
|
|
226
|
+
# json_results["original_image"] = base64.b64encode(buffer.tobytes()).decode('utf-8')
|
|
227
|
+
|
|
228
|
+
if output_format == "dict":
|
|
229
|
+
return json.dumps(json_results, default=str)
|
|
230
|
+
elif output_format == "json":
|
|
231
|
+
return json.dumps(json_results, default=str)
|
|
232
|
+
else:
|
|
233
|
+
raise ValueError(f"Unsupported output format: {output_format}")
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
# Example usage and testing
|
|
237
|
+
if __name__ == "__main__":
|
|
238
|
+
import logging
|
|
239
|
+
logging.basicConfig(level=logging.INFO)
|
|
240
|
+
|
|
241
|
+
# Create engine instance
|
|
242
|
+
engine = ExampleEngine(custom_parameter="test_value")
|
|
243
|
+
|
|
244
|
+
# Test display name
|
|
245
|
+
print(f"Engine display name: {engine.get_display_name()}")
|
|
246
|
+
print(f"Engine info: {engine.get_info()}")
|
|
247
|
+
|
|
248
|
+
# Test model loading (with a dummy model file)
|
|
249
|
+
dummy_model_path = "example_model.pt"
|
|
250
|
+
|
|
251
|
+
# Note: This will fail because the file doesn't exist, but shows the workflow
|
|
252
|
+
if engine.check_valid_model(dummy_model_path):
|
|
253
|
+
if engine.load(dummy_model_path):
|
|
254
|
+
print("Model loaded successfully!")
|
|
255
|
+
|
|
256
|
+
# Test inference with dummy image
|
|
257
|
+
dummy_image = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8)
|
|
258
|
+
results = engine.infer(dummy_image)
|
|
259
|
+
print(f"Inference results: {results}")
|
|
260
|
+
|
|
261
|
+
# Test visualization
|
|
262
|
+
annotated = engine.draw(dummy_image, results)
|
|
263
|
+
print(f"Annotated image shape: {annotated.shape}")
|
|
264
|
+
|
|
265
|
+
# Test JSON conversion
|
|
266
|
+
json_output = engine.result_to_json(results)
|
|
267
|
+
print(f"JSON output: {json_output}")
|
|
268
|
+
else:
|
|
269
|
+
print("Failed to load model")
|
|
270
|
+
else:
|
|
271
|
+
print(f"Invalid model file: {dummy_model_path}")
|
|
272
|
+
print("This is expected since the file doesn't exist.")
|