forked from Kazuhito00/Image-Processing-Node-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_operator.py
More file actions
283 lines (236 loc) · 10 KB
/
Copy pathnode_operator.py
File metadata and controls
283 lines (236 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import dearpygui.dearpygui as dpg
from node_editor.util import dpg_get_value, dpg_set_value
from node.node_abc import DpgNodeABC
from node.basenode import Node
class FactoryNode:
node_label = 'Operator'
node_tag = 'Operator'
def __init__(self):
pass
def add_node(
self,
parent,
node_id,
pos=[0, 0],
opencv_setting_dict=None,
callback=None,
):
node = OperatorNode()
node.tag_node_name = str(node_id) + ':' + node.node_tag
# Input A (JSON from first IOU node)
node.tag_node_input_a_name = node.tag_node_name + ':' + node.TYPE_JSON + ':InputA'
node.tag_node_input_a_value_name = node.tag_node_name + ':' + node.TYPE_JSON + ':InputAValue'
# Input B (JSON from second IOU node)
node.tag_node_input_b_name = node.tag_node_name + ':' + node.TYPE_JSON + ':InputB'
node.tag_node_input_b_value_name = node.tag_node_name + ':' + node.TYPE_JSON + ':InputBValue'
# JSON Output (for Chart node)
node.tag_node_output_json_name = node.tag_node_name + ':' + node.TYPE_JSON + ':Output'
node.tag_node_output_json_value_name = node.tag_node_name + ':' + node.TYPE_JSON + ':OutputValue'
# Time output (optional)
node.tag_node_output_time_name = node.tag_node_name + ':' + node.TYPE_TIME_MS + ':OutputTime'
node.tag_node_output_time_value_name = node.tag_node_name + ':' + node.TYPE_TIME_MS + ':OutputTimeValue'
# Operation selector
node.tag_operation_name = node.tag_node_name + ':Operation'
node.tag_operation_value_name = node.tag_node_name + ':OperationValue'
# Status display
node.tag_status_name = node.tag_node_name + ':Status'
node.tag_status_value_name = node.tag_node_name + ':StatusValue'
node._opencv_setting_dict = opencv_setting_dict
use_pref_counter = node._opencv_setting_dict.get('use_pref_counter', False)
with dpg.node(
tag=node.tag_node_name,
parent=parent,
label=node.node_label,
pos=pos,
):
# Input A
with dpg.node_attribute(
tag=node.tag_node_input_a_name,
attribute_type=dpg.mvNode_Attr_Input,
):
dpg.add_text(
tag=node.tag_node_input_a_value_name,
default_value='Input A (JSON)',
)
# Input B
with dpg.node_attribute(
tag=node.tag_node_input_b_name,
attribute_type=dpg.mvNode_Attr_Input,
):
dpg.add_text(
tag=node.tag_node_input_b_value_name,
default_value='Input B (JSON)',
)
# Operation selector
with dpg.node_attribute(
tag=node.tag_operation_name,
attribute_type=dpg.mvNode_Attr_Static,
):
dpg.add_combo(
tag=node.tag_operation_value_name,
label='Operation',
items=['Addition (+)', 'Subtraction (-)', 'Multiplication (*)', 'Division (/)', 'Fusion'],
default_value='Addition (+)',
width=200,
)
# Status display
with dpg.node_attribute(
tag=node.tag_status_name,
attribute_type=dpg.mvNode_Attr_Static,
):
dpg.add_text(
tag=node.tag_status_value_name,
default_value='Ready',
)
# JSON Output
with dpg.node_attribute(
tag=node.tag_node_output_json_name,
attribute_type=dpg.mvNode_Attr_Output,
):
dpg.add_text(
tag=node.tag_node_output_json_value_name,
default_value='Result (JSON)',
)
# Time output (if performance counter is enabled)
if use_pref_counter:
with dpg.node_attribute(
tag=node.tag_node_output_time_name,
attribute_type=dpg.mvNode_Attr_Output,
):
dpg.add_text(
tag=node.tag_node_output_time_value_name,
default_value='Elapsed time(ms)',
)
return node
class OperatorNode(Node):
_ver = '0.0.1'
node_label = 'Operator'
node_tag = 'Operator'
_opencv_setting_dict = None
def __init__(self):
pass
def _get_source_for_input(self, connection_list, node_result_dict, input_suffix):
"""Return the JSON dict connected to the given input slot."""
for connection_info in connection_list:
destination = connection_info[1]
source = connection_info[0]
connection_type = source.split(':')[2]
if connection_type.upper() != self.TYPE_JSON.upper():
continue
if not destination.endswith(input_suffix):
continue
source_key = ':'.join(source.split(':')[:2])
return node_result_dict.get(source_key, None)
return None
def _apply_operation(self, value_a, value_b, operation):
"""Apply the selected operation to two values."""
try:
a = float(value_a)
b = float(value_b)
if operation == 'Addition (+)':
return a + b
elif operation == 'Subtraction (-)':
return a - b
elif operation == 'Multiplication (*)':
return a * b
elif operation == 'Division (/)':
# Handle division by zero
if b == 0:
return float('inf') if a >= 0 else float('-inf')
return a / b
else:
return 0.0
except (ValueError, TypeError):
return 0.0
def get_setting_dict(self, node_id):
tag_node_name = str(node_id) + ':' + self.node_tag
operation_tag = tag_node_name + ':OperationValue'
pos = dpg.get_item_pos(tag_node_name)
setting_dict = {}
setting_dict['ver'] = self._ver
setting_dict['pos'] = pos
setting_dict[operation_tag] = dpg_get_value(operation_tag)
return setting_dict
def set_setting_dict(self, node_id, setting_dict):
tag_node_name = str(node_id) + ':' + self.node_tag
operation_tag = tag_node_name + ':OperationValue'
if operation_tag in setting_dict and setting_dict[operation_tag] is not None:
dpg_set_value(operation_tag, setting_dict[operation_tag])
def update(
self,
node_id,
connection_list,
node_image_dict,
node_result_dict,
node_audio_dict,
):
tag_node_name = str(node_id) + ':' + self.node_tag
operation_tag = tag_node_name + ':OperationValue'
status_tag = tag_node_name + ':StatusValue'
output_time_tag = tag_node_name + ':' + self.TYPE_TIME_MS + ':OutputTimeValue'
use_pref_counter = self._opencv_setting_dict.get('use_pref_counter', False)
if use_pref_counter:
start_time = time.monotonic()
# Get operation type
operation = dpg_get_value(operation_tag)
if operation is None:
operation = 'Addition (+)'
# Get input data from both sources
json_a = self._get_source_for_input(connection_list, node_result_dict, 'InputA')
json_b = self._get_source_for_input(connection_list, node_result_dict, 'InputB')
result = None
if isinstance(json_a, dict) and isinstance(json_b, dict):
if operation == 'Fusion':
# Merge both JSONs with prefixes A_ and B_
result = {}
for key, value in json_a.items():
result['A_' + key] = value
for key, value in json_b.items():
result['B_' + key] = value
dpg_set_value(
status_tag,
f'Fusion: {len(result)} keys',
)
else:
# Apply operation key-by-key on matching keys
result = {}
# Get all keys that exist in both dictionaries
common_keys = set(json_a.keys()) & set(json_b.keys())
# Process only numeric values
for key in common_keys:
value_a = json_a[key]
value_b = json_b[key]
# Only process numeric values (int or float)
if isinstance(value_a, (int, float)) and isinstance(value_b, (int, float)):
result[key] = self._apply_operation(value_a, value_b, operation)
# Update status
if result:
dpg_set_value(
status_tag,
f'{operation}: {len(result)} keys processed',
)
else:
dpg_set_value(
status_tag,
'No matching numeric keys',
)
else:
# No valid inputs
if json_a is None and json_b is None:
dpg_set_value(status_tag, 'Waiting for inputs A & B')
elif json_a is None:
dpg_set_value(status_tag, 'Waiting for input A')
elif json_b is None:
dpg_set_value(status_tag, 'Waiting for input B')
else:
dpg_set_value(status_tag, 'Invalid input data')
if use_pref_counter:
elapsed_time = time.monotonic() - start_time
elapsed_time = int(elapsed_time * 1000)
dpg_set_value(output_time_tag, str(elapsed_time).zfill(4) + 'ms')
return {"image": None, "json": result, "audio": None}
# Alias for compatibility
Node = OperatorNode