forked from Kazuhito00/Image-Processing-Node-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
453 lines (376 loc) · 17.7 KB
/
Copy pathmain.py
File metadata and controls
453 lines (376 loc) · 17.7 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
# Add the current directory to Python path to allow imports from src package
# This is necessary when running main.py directly without installing the package
if __name__ == "__main__":
current_dir = os.path.dirname(os.path.abspath(__file__))
if current_dir not in sys.path:
sys.path.insert(0, current_dir)
import copy
import json
import asyncio
import argparse
from collections import OrderedDict
import time
import multiprocessing
import cv2
import dearpygui.dearpygui as dpg
from src.utils.logging import setup_logging, get_logger
from src.utils.gpu_utils import log_gpu_info
from node_editor.util import check_camera_connection
from node_editor.node_main import DpgNodeEditor
from node_editor.node_main import update_uptime_display
# Import timestamped queue system
from node.timestamped_queue import NodeDataQueueManager
from node.queue_adapter import QueueBackedDict
# Setup logging
logger = get_logger(__name__)
from node_editor.splash import show_splash_screen as _show_splash_screen
# Reference to the node editor instance, used by nodes that need editor access
# (e.g., Deploy node for schema export)
_node_editor_ref = None
def get_resource_path(relative_path):
"""
Get the absolute path to a resource, works for both development and frozen mode.
When running as a script, returns the path relative to the script directory.
When running as a PyInstaller executable (.exe), returns the path relative to
the temporary directory where PyInstaller extracts files (sys._MEIPASS).
Args:
relative_path (str): Relative path to the resource (e.g., 'assets/image.png')
Returns:
str: Absolute path to the resource
"""
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
frozen = True
except AttributeError:
# Running in normal Python environment (script mode)
base_path = os.path.dirname(os.path.abspath(__file__))
frozen = False
# Normalize path separators for cross-platform compatibility
# This handles cases where relative_path uses forward slashes on Windows
resource_path = os.path.normpath(os.path.join(base_path, relative_path))
# Debug logging to help troubleshoot path issues
logger.debug(
f"Resource path resolution:\n"
f" Frozen mode: {frozen}\n"
f" Base path: {base_path}\n"
f" Relative path: {relative_path}\n"
f" Resolved path: {resource_path}\n"
f" Path exists: {os.path.exists(resource_path)}"
)
return resource_path
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--setting",
type=str,
default=get_resource_path("node_editor/setting/setting.json"),
)
parser.add_argument("--unuse_async_draw", action="store_true")
parser.add_argument("--use_debug_print", action="store_true")
args = parser.parse_args()
return args
def async_main(node_editor, queue_manager):
# Create queue-backed dictionaries for backward compatibility
node_image_dict = QueueBackedDict(queue_manager, "image")
node_result_dict = QueueBackedDict(queue_manager, "json")
node_audio_dict = QueueBackedDict(queue_manager, "audio")
logger.info("Async main loop started with timestamped queue system")
while not node_editor.get_terminate_flag():
update_node_info(
node_editor, node_image_dict, node_result_dict, node_audio_dict
)
# Small sleep to prevent CPU hogging and keep UI responsive
# Note: This function runs in a thread executor (not asyncio coroutine),
# so time.sleep() is appropriate here to yield CPU to other threads
# Increased to 10ms to ensure UI remains responsive during video playback
time.sleep(0.01) # 10ms sleep to yield CPU and keep UI responsive
def update_node_info(
node_editor,
node_image_dict,
node_result_dict,
node_audio_dict,
mode_async=True,
):
editor_width = dpg.get_viewport_client_width()
editor_height = dpg.get_viewport_client_height()
# Update uptime display
update_uptime_display()
try:
dpg.set_item_pos(node_editor.window, [0, 0])
dpg.set_item_width(node_editor.window, dpg.get_viewport_client_width())
dpg.set_item_height(node_editor.window, dpg.get_viewport_client_height())
except Exception as e:
logger.error(f"Failed to set node editor window properties: {e}")
node_list = node_editor.get_node_list()
sorted_node_connection_dict = node_editor.get_sorted_node_connection()
for node_id_name in node_list:
if node_id_name not in node_image_dict:
node_image_dict[node_id_name] = None
node_id, _ = node_id_name.split(":")
connection_list = sorted_node_connection_dict.get(node_id_name, [])
node_instance = node_editor.get_node_instances(node_id_name)
logger.debug(
f"Processing node {node_id_name} with connections: {connection_list}"
)
if mode_async:
try:
data = node_instance.update(
node_id,
connection_list,
node_image_dict,
node_result_dict,
node_audio_dict,
)
except Exception as e:
logger.error(f"Error updating node {node_id_name}: {e}", exc_info=True)
# sys.exit()
else:
data = node_instance.update(
node_id,
connection_list,
node_image_dict,
node_result_dict,
node_audio_dict,
)
try:
# Determine if this is an input node or a processing node
# Input nodes have no IMAGE/AUDIO/JSON input connections
# Processing nodes have at least one IMAGE/AUDIO/JSON input connection
has_data_input = False
source_timestamp = None
for connection_info in connection_list:
# Validate connection_info structure before accessing
if not connection_info or len(connection_info) < 2:
continue
connection_parts = connection_info[0].split(":")
if len(connection_parts) < 3:
continue
connection_type = connection_parts[2]
if connection_type in ["IMAGE", "AUDIO", "JSON"]:
has_data_input = True
# Get the timestamp from the source node
source_node_id = ":".join(connection_parts[:2])
# Try to get timestamp based on connection type
if connection_type == "IMAGE":
source_timestamp = node_image_dict.get_timestamp(source_node_id)
elif connection_type == "AUDIO":
source_timestamp = node_audio_dict.get_timestamp(source_node_id)
elif connection_type == "JSON":
source_timestamp = node_result_dict.get_timestamp(source_node_id)
# Use the first data connection's timestamp
if source_timestamp is not None:
break
# Check if the node provided an explicit timestamp (e.g., FPS-based timestamp from Video node)
# This allows input nodes to specify timestamps based on their internal timing (FPS, audio chunks, etc.)
node_provided_timestamp = data.get("timestamp", None) if isinstance(data, dict) else None
# Store data with appropriate timestamp
# Priority:
# 1. For processing nodes: preserve source timestamp from connected input
# 2. For input nodes with explicit timestamp: use the node-provided timestamp (FPS-based, etc.)
# 3. For input nodes without explicit timestamp: create new timestamp automatically
if has_data_input and source_timestamp is not None:
# Processing node - preserve source timestamp
node_image_dict.set_with_timestamp(node_id_name, copy.deepcopy(data["image"]), source_timestamp)
node_result_dict.set_with_timestamp(node_id_name, copy.deepcopy(data["json"]), source_timestamp)
node_audio_dict.set_with_timestamp(node_id_name, copy.deepcopy(data["audio"]), source_timestamp)
logger.debug(f"Node {node_id_name} preserved timestamp {source_timestamp:.6f} from source")
elif node_provided_timestamp is not None:
# Input node with explicit timestamp (e.g., Video node with FPS-based timing)
node_image_dict.set_with_timestamp(node_id_name, copy.deepcopy(data["image"]), node_provided_timestamp)
node_result_dict.set_with_timestamp(node_id_name, copy.deepcopy(data["json"]), node_provided_timestamp)
node_audio_dict.set_with_timestamp(node_id_name, copy.deepcopy(data["audio"]), node_provided_timestamp)
logger.debug(f"Node {node_id_name} used explicit timestamp {node_provided_timestamp:.6f}")
else:
# Input node without explicit timestamp - create new timestamp automatically
node_image_dict[node_id_name] = copy.deepcopy(data["image"])
node_result_dict[node_id_name] = copy.deepcopy(data["json"])
node_audio_dict[node_id_name] = copy.deepcopy(data["audio"])
logger.debug(f"Node {node_id_name} created new timestamp (input node)")
except Exception as e:
logger.error(f"Error processing node {node_id_name} results: {e}")
def show_splash_screen(duration_seconds=5.0, steps=150):
"""Delegate to the Apple-style splash screen module."""
_show_splash_screen(duration_seconds=duration_seconds, steps=steps)
def main():
args = get_args()
setting = args.setting
unuse_async_draw = args.unuse_async_draw
use_debug_print = args.use_debug_print
# Setup logging based on debug flag
log_level = "DEBUG" if use_debug_print else "INFO"
setup_logging(level=getattr(__import__("logging"), log_level))
logger.info("=" * 60)
logger.info("CV_STUDIO Starting")
logger.info("=" * 60)
# Initialize timestamped buffer system
logger.info("Initializing timestamped buffer system")
queue_manager = NodeDataQueueManager(default_maxsize=10)
logger.info("Buffer system initialized: keeps last 10 timestamped items per node for synchronization")
logger.info("Loading configuration")
logger.debug(f"Configuration file path: {setting}")
# Verify the configuration file exists before attempting to load
if not os.path.exists(setting):
logger.error(f"Configuration file not found: {setting}")
# Check if we're in a frozen (PyInstaller) environment
if getattr(sys, 'frozen', False):
logger.error(f"Running in frozen mode. Base path (_MEIPASS): {sys._MEIPASS}")
logger.error("The setting.json file may not have been properly bundled with PyInstaller.")
logger.error("Please ensure CV_Studio.spec includes: datas.append(('node_editor', 'node_editor'))")
else:
logger.error("Running in script mode. The setting.json file should be in node_editor/setting/")
raise FileNotFoundError(f"Configuration file not found: {setting}")
opencv_setting_dict = None
with open(setting) as fp:
opencv_setting_dict = json.load(fp)
logger.info("Configuration loaded successfully")
webcam_width = opencv_setting_dict["webcam_width"]
webcam_height = opencv_setting_dict["webcam_height"]
# Log GPU information
if opencv_setting_dict.get("use_gpu", False):
log_gpu_info()
logger.info("Checking camera connections")
device_no_list = check_camera_connection()
camera_capture_list = []
for device_no in device_no_list:
video_capture = cv2.VideoCapture(device_no)
video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, webcam_width)
video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, webcam_height)
camera_capture_list.append(video_capture)
logger.info(f"Camera {device_no} connected")
opencv_setting_dict["device_no_list"] = device_no_list
opencv_setting_dict["camera_capture_list"] = camera_capture_list
editor_width = opencv_setting_dict["editor_width"]
editor_height = opencv_setting_dict["editor_height"]
serial_device_no_list = []
serial_connection_list = []
use_serial = opencv_setting_dict["use_serial"]
if use_serial == True:
try:
from .node_editor.util import check_serial_connection
except:
from node_editor.util import check_serial_connection
logger.info("Checking serial device connections")
serial_device_no_list = check_serial_connection()
for serial_device_no in serial_device_no_list:
ser = serial.Serial(serial_device_no, 115200)
serial_connection_list.append(ser)
logger.info(f"Serial device {serial_device_no} connected")
opencv_setting_dict["serial_device_no_list"] = serial_device_no_list
opencv_setting_dict["serial_connection_list"] = serial_connection_list
logger.info("Setting up DearPyGui")
dpg.create_context()
dpg.setup_dearpygui()
dpg.create_viewport(
title="CV_STUDIO",
width=editor_width,
height=editor_height,
x_pos=0,
y_pos=0,
)
# Load Space Grotesk font (minimalist, futuristic, elegant, architectural)
_font_dir = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"node_editor", "font", "SpaceGrotesk",
)
_font_path = os.path.join(_font_dir, "SpaceGrotesk-Medium.otf")
if not os.path.isfile(_font_path):
_font_path = os.path.join(_font_dir, "SpaceGrotesk-Regular.otf")
if os.path.isfile(_font_path):
with dpg.font_registry():
default_font = dpg.add_font(_font_path, 18)
dpg.bind_font(default_font)
logger.info(f"Loaded custom font: {os.path.basename(_font_path)}")
else:
logger.warning("Space Grotesk font not found, using default DearPyGui font")
# Viewport must be visible before rendering splash frames.
dpg.show_viewport(maximized=True)
show_splash_screen()
logger.info("Creating Node Editor")
menu_dict = OrderedDict(
{
"Input": "InputNode",
"VisionProcess": "ProcessNode",
"VisionModel": "DLNode",
"AudioProcess": "AudioProcessNode",
"AudioModel": "AudioModelNode",
"DataProcess": "StatsNode",
"DataModel": "TimeseriesNode",
"NLPModel": "NLPModelNode",
"Trigger": "TriggerNode",
"Router": "RouterNode",
"Action": "ActionNode",
"Overlay": "OverlayNode",
"Tracking": "TrackerNode",
"Visual": "VisualNode",
"Video": "VideoNode",
"System": "SystemNode",
"Map": "MapNode",
}
)
current_path = os.path.dirname(os.path.abspath(__file__))
node_editor = DpgNodeEditor(
width=editor_width,
height=editor_height,
opencv_setting_dict=opencv_setting_dict,
menu_dict=menu_dict,
use_debug_print=use_debug_print,
node_dir=current_path + "/node",
)
# Store reference for nodes that need access to the editor (e.g., Deploy node)
import sys
sys.modules[__name__]._node_editor_ref = node_editor
logger.info("Starting main event loop")
event_loop = None
if not unuse_async_draw:
logger.info("Async draw is enabled")
event_loop = asyncio.get_event_loop()
event_loop.run_in_executor(None, async_main, node_editor, queue_manager)
dpg.start_dearpygui()
else:
logger.info("Async draw is disabled")
# Create queue-backed dictionaries
node_image_dict = QueueBackedDict(queue_manager, "image")
node_result_dict = QueueBackedDict(queue_manager, "json")
node_audio_dict = QueueBackedDict(queue_manager, "audio")
while dpg.is_dearpygui_running():
update_node_info(
node_editor,
node_image_dict,
node_result_dict,
node_audio_dict,
mode_async=False,
)
dpg.render_dearpygui_frame()
logger.info("Terminating process")
# Signal async loop to stop before closing nodes so update calls cease
node_editor.set_terminate_flag()
logger.info("Closing all nodes")
node_list = node_editor.get_node_list()
for node_id_name in node_list:
node_id, _ = node_id_name.split(":")
node_instance = node_editor.get_node_instances(node_id_name)
if node_instance is not None:
node_instance.close(node_id)
else:
logger.warning(f"No instance found for node {node_id_name}, skipping close")
logger.info("Releasing all video captures")
for camera_capture in camera_capture_list:
camera_capture.release()
if event_loop is not None:
logger.info("Stopping event loop")
event_loop.stop()
logger.info("Destroying DearPyGui context")
dpg.destroy_context()
logger.info("CV_STUDIO shutdown complete")
if __name__ == "__main__":
# Enable multiprocessing support for frozen executables (PyInstaller)
# This must be called before any multiprocessing code runs
# On Windows, when the executable spawns child processes for multiprocessing,
# they will re-execute this script with special arguments that freeze_support() handles
multiprocessing.freeze_support()
main()