-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgui.py
More file actions
3699 lines (3462 loc) · 168 KB
/
Copy pathgui.py
File metadata and controls
3699 lines (3462 loc) · 168 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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Desktop2Stereo Flet GUI
Flet desktop app, migrated from tkinter gui.py.
Feature-equivalent; all interaction logic matches the original.
Custom control overview:
CompactTextField (L535)
Compact text input, 32px height. Displays as bordered text label by default;
switches to TextField on click for editing.
Constructor params:
value — initial value
width — control width
read_only — read-only flag
on_change — value change callback (param: e.control.value)
tooltip — tooltip text
filter — regex to restrict allowed characters (e.g. r"[0-9]" for digits only)
max_length — max input length
On submit (Enter/Blur), non-matching characters are stripped.
Properties: .value (r/w), .set_tooltip(text)
CompactDropdown (L615)
Custom dropdown based on PopupMenuButton, 32px height, visually consistent with TextField.
Constructor params:
options — list of options
value — currently selected value
on_select — selection callback (param: e.control.value)
expand — expand_loose flag
dyna_width — dynamic width mode (uses LABEL_ALIGN_WIDTH)
width — fixed width
min_width — auto-width lower bound
max_width — auto-width upper bound
tooltip — tooltip text
Width priority: fixed > dynamic mode > auto-calculated (bounded by min/max).
Properties: .value (r/w), .options (r/w, triggers menu rebuild), .set_tooltip(text)
"""
import os
import platform
FLET_STORAGE_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "flet_client"))
def _ensure_flet_client():
"""Extract the bundled Flet client archive if an archive exists and no app/exe is present.
Returns True if a client is available locally (archive or extracted), False otherwise."""
system = platform.system()
if system == "Darwin":
app_path = os.path.join(FLET_STORAGE_PATH, "Flet.app")
archive = os.path.join(FLET_STORAGE_PATH, "flet-macos.tar.gz")
if os.path.exists(app_path):
return True
if os.path.exists(archive):
import tarfile
with tarfile.open(archive, "r:gz") as tar:
tar.extractall(path=FLET_STORAGE_PATH, filter="data")
# Strip quarantine so macOS doesn't block the extracted .app
import subprocess
subprocess.run(
["xattr", "-r", "-d", "com.apple.quarantine", app_path],
capture_output=True,
)
subprocess.run(
["codesign", "--force", "--deep", "-s", "-", app_path],
capture_output=True,
)
return True
return False
elif system == "Windows":
exe_path = os.path.join(FLET_STORAGE_PATH, "flet.exe")
archive = os.path.join(FLET_STORAGE_PATH, "flet-windows.zip")
if os.path.exists(exe_path):
return True
if os.path.exists(archive):
import zipfile
import shutil
nested = os.path.join(FLET_STORAGE_PATH, "flet")
with zipfile.ZipFile(archive, "r") as zf:
zf.extractall(path=FLET_STORAGE_PATH)
# The archive extracts to flet_client/flet/ — move contents up one level
if os.path.isdir(nested):
for name in os.listdir(nested):
src = os.path.join(nested, name)
dst = os.path.join(FLET_STORAGE_PATH, name)
if os.path.exists(dst):
if os.path.isdir(dst):
shutil.rmtree(dst)
else:
os.remove(dst)
shutil.move(src, dst)
os.rmdir(nested)
return True
return False
# Linux: no local client — let Flet download itself
return False
if _ensure_flet_client():
os.environ["FLET_VIEW_PATH"] = FLET_STORAGE_PATH
import sys
import subprocess
import time
def _sign_project_binaries():
"""macOS only: strip quarantine and ad-hoc sign project binaries so they
don't trigger Gatekeeper / 'unidentified developer' dialogs."""
if platform.system() != "Darwin":
return
project_root = os.path.dirname(os.path.abspath(__file__))
_sign_dirs = [
(project_root, 1), # root only (run_mac etc)
(os.path.join(project_root, "flet_client"), None), # full depth (Flet.app)
(os.path.join(project_root, ".env", "bin"), 1), # bin only
(os.path.join(project_root, "rtmp"), None), # full depth (ffmpeg, mediamtx)
]
for d, maxdepth in _sign_dirs:
if not os.path.isdir(d):
continue
# Strip quarantine
subprocess.run(
["xattr", "-r", "-d", "com.apple.quarantine", d],
capture_output=True,
)
# Ad-hoc sign .app bundles (handles all internals in one shot)
for name in os.listdir(d):
if name.endswith(".app"):
subprocess.run(
["codesign", "--force", "--deep", "-s", "-",
os.path.join(d, name)],
capture_output=True,
)
# Ad-hoc sign other standalone Mach-O files
find_args = ["find", d]
if maxdepth is not None:
find_args += ["-maxdepth", str(maxdepth)]
# Skip .app bundles — already signed above
find_args += ["-type", "f", "-perm", "+111", "-not", "-path", "*/*.app/*"]
result = subprocess.run(find_args, capture_output=True, text=True)
for path in result.stdout.strip().splitlines():
ft = subprocess.run(["file", "-b", path], capture_output=True, text=True)
if "Mach-O" in ft.stdout:
subprocess.run(
["codesign", "--force", "--deep", "-s", "-", path],
capture_output=True,
)
if platform.system() == "Darwin":
_sign_project_binaries()
import asyncio
import ctypes
import re
import json
import atexit
import traceback
# Force UTF-8 stdout/stderr on Windows so the console (and downstream
# TeeStream that mirrors writes to it) does not mangle messages
# containing em-dashes, arrows, or other non-ASCII characters. Must
# run BEFORE _setup_console_logging() captures the streams.
try:
if sys.stdout and sys.stdout.encoding and sys.stdout.encoding.lower() != "utf-8":
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
if sys.stderr and sys.stderr.encoding and sys.stderr.encoding.lower() != "utf-8":
sys.stderr.reconfigure(encoding="utf-8", errors="replace")
except Exception:
pass
import flet as ft
import yaml
from utils import (
VERSION, OS_NAME, ALL_MODELS, DEFAULT_PORT, STEREO_MIX_NAMES,
DISABLE_TRT_KEYWORDS, DISABLE_COREML_KEYWORDS, DISABLE_OPENVINO_KEYWORDS,
DISABLE_MIGRAPHX_KEYWORDS,
get_local_ip, shutdown_event, read_yaml
)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
LOG_DIR = os.path.join(BASE_DIR, "logs")
LOG_FILE = os.path.join(LOG_DIR, "desktop2stereo.log")
STOP_REQUEST_FILE = os.path.join(LOG_DIR, "stop.request")
_ENV_IMAGE_EXTS = (".png", ".jpg", ".jpeg", ".webp", ".bmp", ".tif", ".tiff")
_ENV_IMAGE_NAMES = ("background", "panorama", "equirectangular", "360", "sky", "skybox")
def _read_env_profile_for_gui(profile_path):
if not os.path.isfile(profile_path):
return {}
try:
with open(profile_path, "r", encoding="utf-8-sig") as f:
loaded = json.load(f)
return loaded if isinstance(loaded, dict) else {}
except Exception:
return {}
def _find_env_image_for_gui(room_dir):
if not os.path.isdir(room_dir):
return None
for stem in _ENV_IMAGE_NAMES:
for ext in _ENV_IMAGE_EXTS:
path = os.path.join(room_dir, stem + ext)
if os.path.isfile(path):
return path
try:
for name in sorted(os.listdir(room_dir), key=lambda v: v.lower()):
path = os.path.join(room_dir, name)
if os.path.isfile(path) and os.path.splitext(name)[1].lower() in _ENV_IMAGE_EXTS:
return path
except OSError:
pass
return None
def _is_panorama_profile_for_gui(profile):
if not isinstance(profile, dict):
return False
raw_bg = profile.get("background")
raw_panorama = profile.get("panorama")
cfg = {}
if isinstance(raw_bg, str):
cfg["image"] = raw_bg
elif isinstance(raw_bg, dict):
cfg.update(raw_bg)
if isinstance(raw_panorama, str):
cfg.setdefault("image", raw_panorama)
elif isinstance(raw_panorama, dict):
cfg.update(raw_panorama)
env_type = str(profile.get("environment_type", profile.get("type", "")) or "").strip().lower()
bg_type = str(cfg.get("type", cfg.get("kind", "")) or "").strip().lower()
projection = str(cfg.get("projection", cfg.get("format", "")) or "").strip().lower()
return (
env_type in ("panorama", "360", "360_photo", "360-photo", "photo_sphere", "photosphere")
or bg_type in ("panorama", "360", "360_photo", "360-photo", "equirectangular", "photo_sphere", "photosphere")
or projection in ("equirectangular", "360", "360_photo", "360-photo")
or raw_panorama is True
)
def _discover_gui_environment_folders(env_base):
panorama_dirs = []
glb_dirs = []
try:
names = sorted(os.listdir(env_base), key=lambda v: v.lower())
except (FileNotFoundError, OSError):
return []
for name in names:
room_dir = os.path.join(env_base, name)
if not os.path.isdir(room_dir):
continue
profile = _read_env_profile_for_gui(os.path.join(room_dir, "profile.json"))
glb_path = os.path.join(room_dir, "environment.glb")
if _is_panorama_profile_for_gui(profile) and _find_env_image_for_gui(room_dir):
panorama_dirs.append(name)
continue
if not os.path.isfile(glb_path):
glb_name = str(profile.get("glb", "environment.glb") or "environment.glb")
glb_path = glb_name if os.path.isabs(glb_name) else os.path.join(room_dir, glb_name)
if os.path.isfile(glb_path):
glb_dirs.append(name)
continue
if _find_env_image_for_gui(room_dir):
panorama_dirs.append(name)
return panorama_dirs + glb_dirs
# Kept as an alias for backward compatibility with any external tooling
# that referenced the old diag log path.
DIAG_LOG = LOG_FILE
_CHILD_PID = None # set when child starts, cleared on clean stop
@atexit.register
def _atexit_kill_child():
pid = _CHILD_PID
if pid is None:
return
try:
if OS_NAME == "Windows":
subprocess.run(
['taskkill', '/f', '/t', '/pid', str(pid)],
capture_output=True, timeout=5,
)
else:
import signal
try:
os.killpg(os.getpgid(pid), signal.SIGKILL)
except Exception:
pass
except Exception:
pass
def _setup_console_logging():
"""Mirror stdout/stderr to the single rolling log file.
Design goals (per user request):
* Console output is preserved unchanged — every ``print()`` still
reaches the original terminal so the user can watch progress live.
* Exactly ONE log file lives in ``logs/``. Any leftover files from
previous sessions (``child.log``, ``diag.log``, ``main_out.log`` …)
are swept on startup so the folder never accumulates clutter.
* Both GUI prints AND the child ``main.py`` process output land in the
same file, so a single tail is enough to diagnose any run.
"""
import datetime
import threading
os.makedirs(LOG_DIR, exist_ok=True)
# --- Sweep stale log files so only the new single file remains. ---
try:
for _name in os.listdir(LOG_DIR):
_path = os.path.join(LOG_DIR, _name)
if os.path.isfile(_path) and os.path.abspath(_path) != os.path.abspath(LOG_FILE):
try:
os.remove(_path)
except Exception:
pass
except Exception:
pass
# --- Truncate the single log file so each run starts fresh. ---
try:
with open(LOG_FILE, "w", encoding="utf-8") as _f:
_f.write(f"=== Desktop2Stereo log started {datetime.datetime.now().isoformat(timespec='seconds')} ===\n")
except Exception:
pass
_lock = threading.Lock()
class _TeeStream:
def __init__(self, original, label):
self.original = original
self.label = label
def write(self, data):
# 1) Always echo to the original terminal so the user sees prints live.
try:
self.original.write(data)
except Exception:
pass
# 2) Append every non-empty line to the single log file with a
# timestamp + source label. Lock to keep stdout/stderr
# interleaving readable when both fire from different threads.
if not data:
return
try:
ts = datetime.datetime.now().strftime("%H:%M:%S")
with _lock:
with open(LOG_FILE, "a", encoding="utf-8") as f:
for line in data.splitlines():
stripped = line.rstrip()
if stripped:
f.write(f"[{ts}] [{self.label}] {stripped}\n")
except Exception:
pass
def flush(self):
try:
self.original.flush()
except Exception:
pass
def isatty(self):
try:
return self.original.isatty()
except Exception:
return False
# Forward fileno when possible so libraries that probe the underlying
# OS handle (e.g. subprocess) still work; if the wrapped stream has
# no fileno (already wrapped), raise to fall back to PIPE handling.
def fileno(self):
return self.original.fileno()
sys.stdout = _TeeStream(sys.stdout, "out")
sys.stderr = _TeeStream(sys.stderr, "err")
# Known model size suffixes, longest-first for matching priority
_MODEL_SIZES = ["Small", "SmallPlus", "Base", "Large", "Giant"]
_SIZE_ORDER = {s: i for i, s in enumerate(_MODEL_SIZES)}
def parse_model_name(name):
"""Split model name into (family, size). DepthPro treated as Large."""
parts = name.split("-")
size_parts = []
i = len(parts) - 1
while i >= 0:
matched = None
for sz in _MODEL_SIZES:
if parts[i].upper() == sz.upper():
matched = sz
break
if matched:
size_parts.insert(0, matched)
i -= 1
else:
break
if size_parts:
family = "-".join(parts[:i + 1])
size = "-".join(size_parts)
return (family, size)
return (name, "")
def build_family_size_map(model_list):
"""Returns (families_ordered, family_to_sizes) from list of full model names."""
families = []
family_to_sizes = {}
for name in model_list:
family, size = parse_model_name(name)
if family not in family_to_sizes:
family_to_sizes[family] = []
families.append(family)
if size and size not in family_to_sizes[family]:
family_to_sizes[family].append(size)
for family in family_to_sizes:
family_to_sizes[family].sort(key=lambda s: _SIZE_ORDER.get(s, 99))
return families, family_to_sizes
# ── Disable console Quick Edit Mode (prevents console freeze on click) ──
try:
kernel32 = ctypes.windll.kernel32
kernel32.GetStdHandle.restype = ctypes.c_void_p
kernel32.GetStdHandle.argtypes = [ctypes.c_uint32]
STD_INPUT_HANDLE = -10
ENABLE_QUICK_EDIT_MODE = 0x0040
ENABLE_EXTENDED_FLAGS = 0x0080
hStdin = kernel32.GetStdHandle(STD_INPUT_HANDLE)
mode = ctypes.c_uint32()
if kernel32.GetConsoleMode(hStdin, ctypes.byref(mode)):
mode.value = (mode.value & ~ENABLE_QUICK_EDIT_MODE) | ENABLE_EXTENDED_FLAGS
kernel32.SetConsoleMode(hStdin, mode)
except Exception:
pass # Not a console or Windows — safe to ignore
# ─────────────────────────────────────────────
# UI Text Dictionary (EN / CN)
# ─────────────────────────────────────────────
UI_TEXTS = {
"EN": {
"Monitor": "Input Monitor",
"Window": "Input Window",
"Refresh": "Refresh",
"Show FPS": "Show FPS",
"IPD (m):": "IPD (mm):",
"Convergence:": "Convergence:",
"Display Mode:": "Display Mode:",
"Depth Model:": "Depth Model:",
"Depth Strength:": "Depth Strength:",
"Depth Resolution:": "Depth Resolution:",
"Anti-aliasing:": "Anti-aliasing:",
"Foreground Scale:": "Foreground Scale:",
"FP16": "FP16",
"Inference Acceleration:": "Acceleration:",
"Recompile TensorRT": "Recompile TensorRT",
"Recompile CoreML": "Recompile CoreML",
"Recompile OpenVINO": "Recompile OpenVINO",
"Recompile MIGraphX": "Recompile MIGraphX",
"Stop": "Stop",
"Computing Device:": "Computing Device:",
"Reset": "Reset",
"Run": "Run",
"Set Language:": "Set Language:",
"Error": "Error",
"Warning": "Warning",
"Saved": "Run Desktop2Stereo",
"PyYAML not installed, cannot save YAML file.": "PyYAML not installed, cannot save YAML file.",
"Settings saved to settings.yaml": "Settings saved to settings.yaml",
"Failed to save settings.yaml:": "Failed to save settings.yaml:",
"Could not retrieve monitor list.\nFalling back to indexes 1 and 2.": "Could not retrieve monitor list.\nFalling back to indexes 1 and 2.",
"Loaded settings.yaml at startup": "Loaded settings.yaml at startup",
"Running": "Running... (Hold ESC 3s to Stop)",
"Stopped": "Stopped.",
"Countdown": "Settings saved to settings.yaml, starting...",
"A thread already running!": "A thread already running!",
"No windows found": "No windows found",
"Selected input window:": "Selected input window:",
"Selected input monitor:": "Selected input monitor:",
"Run Mode:": "Run Mode:",
"Local Viewer": "Local Viewer",
"Legacy Streamer": "Legacy Streamer",
"MJPEG Streamer": "MJPEG Streamer",
"RTMP Streamer": "RTMP Streamer",
"Stream Protocol:": "Stream Protocol:",
"Stream Key": "Stream Key:",
"Stereo Mix": "Stereo Mix:",
"CRF": "CRF:",
"Audio Delay": "Audio Delay (s):",
"Lossless Scaling Support": "LSFG",
"3D Monitor": "3D Monitor",
"OpenXR Link": "OpenXR Link",
"XR Preview": "XR Preview",
"Crop Auto": "Auto",
"Crop Manual": "Manual",
"Crop Off": "Off",
"VSync": "VSync",
"Streamer Port:": "Streamer Port:",
"Streamer URL": "Streamer URL:",
"Preview":"Preview",
"Stream Quality:": "Stream Quality:",
"Host": "Host:",
"Invalid port number (1-65535)": "Invalid port number (must be between 1-65535)",
"Invalid port number": "Port must be a number",
"Please select a window before running in Window capture mode": "Please select a window before running in Window capture mode",
"The selected window no longer exists. Please refresh and select a valid window.": "The selected window no longer exists. Please refresh and select a valid window.",
"Failed to stop process on exit:": "Failed to stop process on exit:",
"Failed to stop process:": "Failed to stop process:",
"Failed to run process:": "Failed to run process:",
"Failed to load settings.yaml:": "Failed to load settings.yaml:",
"Opening URL in browser": "Opening URL in browser",
"Controller:": "Controller:",
"Crop Mode:": "Crop Mode:",
"Environment:": "Environment:",
"Capture Tool:": "Capture Tool:",
"Fill 16:9": "16:9",
"Fix Viewer Aspect": "Fix Aspect",
"Stereo Output:": "Stereo Output:",
"Theme:": "Theme:",
"DesktopDuplication selected: Window capture mode disabled.": "DesktopDuplication selected: Window capture mode disabled.",
"torch.compile": "torch.compile",
"TensorRT": "TensorRT",
"CoreML": "CoreML",
"OpenVINO": "OpenVINO",
"MIGraphX": "MIGraphX",
"tooltip_window": "Select a window to capture",
"tooltip_depth_model": "Depth estimation model",
"tooltip_model_size":"Model backbone size",
"tooltip_depth_res": "Depth map resolution",
"tooltip_convergence": "Stereo convergence",
"tooltip_depth_strength": "Depth effect intensity",
"tooltip_foreground_scale": "Foreground object scale",
"tooltip_antialiasing": "Anti-aliasing level",
"tooltip_ipd": "Interpupillary distance (mm)",
"tooltip_device": "Inference device",
"tooltip_capture_tool": "Capture backend",
"tooltip_run_mode": "Output mode",
"tooltip_display_mode": "Stereo display format",
"tooltip_vsync": "Synchronize the local viewer to the display refresh rate",
"tooltip_ctrl_model": "Controller model",
"tooltip_env_model": "Background environment: Default (black), a 360 image folder, or a 3D scene from xr_viewer/environments/. Cinema glow can be toggled via long-press X in VR.",
"tooltip_crop_mode": "Auto: detect letterbox bars. Manual: set your own crop. Off: no crop.\nManual mode (in VR, laser off screen): hold left trigger 3s to cycle mode, double-tap left trigger to start/stop adjusting, then push the left stick — X crops the sides, Y crops top/bottom (dominant axis only).",
"Default": "Default",
"tooltip_capture_mode": "Source: monitor or window",
"tooltip_monitor": "Input monitor",
"tooltip_stereo_monitor": "Stereo output monitor",
"tooltip_lang": "Interface language",
"tooltip_theme": "Color theme",
"tooltip_stream_quality": "Encode quality",
"tooltip_stream_proto": "Streaming protocol",
"tooltip_audio": "Stereo mix device",
"tooltip_stream_port": "Server port",
"tooltip_stream_key": "Stream key",
"tooltip_crf": "Quality factor (0-51)",
"tooltip_audio_delay": "Audio offset (s)",
"err_crf": "CRF must be between 0-51",
"err_audio_delay": "Audio Delay must be between -10 and 10",
"err_stream_key": "Stream Key can only contain letters, digits, underscore, hyphen, max 64 chars",
"err_start_failed": "Start failed: {}",
"esc_stop": "Hold ESC 3s — stopping!",
"exited_with_code": "Exited with code {}",
"failed_save_yaml": "Failed to save YAML: {}",
"invalid_url_scheme": "Invalid URL scheme: {}",
"error_preview": "Failed to preview: {}",
"url_copied": "URL copied to clipboard",
"Log Folder": "Log Folder",
"tooltip_open_log": "Please send the log file for debugging.",
},
"CN": {
"Monitor": "输入屏幕",
"Window": "输入窗口",
"Refresh": "刷新",
"Show FPS": "显示帧率",
"IPD (m):": "瞳距 (mm):",
"Convergence:": "会聚点:",
"Display Mode:": "显示模式:",
"Depth Model:": "深度模型:",
"Depth Strength:": "深度强度:",
"Depth Resolution:": "深度分辨率:",
"Anti-aliasing:": "抗锯齿:",
"Foreground Scale:": "前景缩放:",
"FP16": "FP16",
"Inference Acceleration:": "推理加速:",
"Recompile TensorRT": "重译TensorRT",
"Recompile CoreML": "重译CoreML",
"Recompile OpenVINO": "重译OpenVINO",
"Recompile MIGraphX": "重译MIGraphX",
"Stop": "停止",
"Computing Device:": "计算设备:",
"Reset": "重置",
"Run": "运行",
"Set Language:": "设置语言:",
"Error": "错误",
"Warning": "警告",
"Saved": "运行Desktop2Stereo",
"PyYAML not installed, cannot save YAML file.": "未安装PyYAML,无法保存YAML文件。",
"Settings saved to settings.yaml": "设置已保存到 settings.yaml",
"Failed to save settings.yaml:": "保存 settings.yaml 失败:",
"Could not retrieve monitor list.\nFalling back to indexes 1 and 2.": "无法获取显示器列表。\n回退到索引1和2。",
"Loaded settings.yaml at startup": "启动时已加载 settings.yaml",
"Running": "运行中...(长按ESC 3秒停止)",
"Stopped": "已停止。",
"Countdown": "设置已保存到 settings.yaml,启动中...",
"A thread already running!": "一个进程已经运行!",
"No windows found": "未找到窗口",
"Selected input window:": "已选择输入窗口:",
"Selected input monitor:": "已选择输入显示器 :",
"Run Mode:": "运行模式:",
"Local Viewer": "本地查看",
"Legacy Streamer": "旧网络推流",
"MJPEG Streamer": "MJPEG推流",
"RTMP Streamer": "RTMP推流",
"Stream Protocol:": "流协议:",
"Stream Key": "推流密钥:",
"Stereo Mix": "混音设备:",
"CRF": "恒定质量:",
"Audio Delay": "音频延迟 (秒):",
"system": "系统",
"blue": "蓝色",
"green": "绿色",
"red": "红色",
"purple": "紫色",
"orange": "橙色",
"teal": "青色",
"pink": "粉色",
"grey": "灰色",
"Lossless Scaling Support": "小黄鸭",
"3D Monitor": "3D显示器",
"OpenXR Link": "OpenXR串流",
"XR Preview": "XR预览",
"Crop Auto": "自动",
"Crop Manual": "手动",
"Crop Off": "关闭",
"VSync": "垂直同步",
"Streamer Port:": "推流端口:",
"Streamer URL": "推流网址:",
"Preview": "预览",
"Stream Quality:": "推流质量:",
"Host": "主机:",
"Invalid port number (1-65535)": "端口号无效 (必须介于1-65535之间)",
"Invalid port number": "端口必须是数字",
"Please select a window before running in Window capture mode": "请在窗口捕获模式下选择一个窗口再运行",
"The selected window no longer exists. Please refresh and select a valid window.": "所选窗口已不存在。请刷新并选择一个有效的窗口。",
"Failed to stop process on exit:": "退出时停止进程失败:",
"Failed to stop process:": "停止进程失败:",
"Failed to run process:": "运行进程失败:",
"Failed to load settings.yaml:": "加载 settings.yaml 失败:",
"Opening URL in browser": "正在浏览器中打开网址",
"Controller:": "手柄模型:",
"Crop Mode:": "裁剪模式:",
"Environment:": "虚拟环境:",
"Capture Tool:": "捕获工具:",
"Fill 16:9": "16:9",
"Fix Viewer Aspect": "锁定比例",
"Stereo Output:": "立体输出:",
"Theme:": "主题颜色:",
"DesktopDuplication selected: Window capture mode disabled.": "已选择DesktopDuplication:窗口捕获模式已禁用。",
"torch.compile": "torch.compile",
"TensorRT": "TensorRT",
"CoreML": "CoreML",
"OpenVINO": "OpenVINO",
"MIGraphX": "MIGraphX",
"tooltip_window": "选择要捕获的窗口",
"tooltip_depth_model": "选择深度估计模型",
"tooltip_model_size":"模型骨架大小",
"tooltip_depth_res": "深度图分辨率",
"tooltip_convergence": "立体会聚点",
"tooltip_depth_strength": "深度效果强度",
"tooltip_foreground_scale": "前景缩放比例",
"tooltip_antialiasing": "抗锯齿级别",
"tooltip_ipd": "瞳距(毫米)",
"tooltip_device": "计算设备",
"tooltip_capture_tool": "捕获后端",
"tooltip_run_mode": "输出模式",
"tooltip_display_mode": "立体显示格式",
"tooltip_vsync": "将本地查看窗口同步到显示器刷新率,关闭可用于帧率对比测试",
"tooltip_ctrl_model": "手柄型号",
"tooltip_env_model": "背景环境:默认(黑色)、360 图像文件夹,或 xr_viewer/environments/ 中的 3D 场景。影院辉光可在 VR 中长按 X 键切换。",
"tooltip_crop_mode": "自动:检测黑边。手动:自定义裁剪。关闭:不裁剪。\n手动模式(VR 中,激光不指屏幕):按住左扳机 3 秒循环模式,双击左扳机开始/停止调整,然后推动左摇杆——X 裁剪左右,Y 裁剪上下(仅主轴)。",
"Default": "默认",
"tooltip_capture_mode": "捕获源:屏幕或窗口",
"tooltip_monitor": "输入显示器",
"tooltip_stereo_monitor": "立体输出显示器",
"tooltip_lang": "界面语言",
"tooltip_theme": "主题颜色",
"tooltip_stream_quality": "编码质量",
"tooltip_stream_proto": "推流协议",
"tooltip_audio": "混音设备",
"tooltip_stream_port": "推流端口",
"tooltip_stream_key": "推流密钥",
"tooltip_crf": "质量因子 (0-51)",
"tooltip_audio_delay": "音频偏移(秒)",
"err_crf": "CRF 必须是 0-51 之间的整数",
"err_audio_delay": "Audio Delay 必须是 -10 到 10 之间的数值",
"err_stream_key": "Stream Key 只能包含字母、数字、下划线和连字符,最长 64 字符",
"err_start_failed": "启动失败: {}",
"esc_stop": "长按ESC 3秒停止",
"exited_with_code": "退出码 {}",
"failed_save_yaml": "保存 YAML 失败: {}",
"invalid_url_scheme": "无效 URL 协议: {}",
"error_preview": "打开浏览器失败: {}",
"url_copied": "已复制网址到剪贴板",
"Log Folder": "日志文件夹",
"tooltip_open_log": "请将日志文件发送用于排查bug。",
}
}
# ─────────────────────────────────────────────
# Default Configuration
# ─────────────────────────────────────────────
DEFAULT_MODEL_LIST = list(ALL_MODELS.keys())
DEFAULT_FAMILIES, FAMILY_TO_SIZES = build_family_size_map(DEFAULT_MODEL_LIST)
FAMILY_SIZE_TO_MODEL = {}
for name in DEFAULT_MODEL_LIST:
f, s = parse_model_name(name)
FAMILY_SIZE_TO_MODEL[(f, s)] = name
DEFAULTS = {
"Capture Mode": "Monitor",
"Monitor Index": 1,
"Window Title": "",
"Show FPS": False,
"Model List": DEFAULT_MODEL_LIST,
"Depth Model": DEFAULT_MODEL_LIST[0] if DEFAULT_MODEL_LIST else "",
"Depth Strength": 4.0,
"Depth Resolution": 322,
"Anti-aliasing": 2,
"Foreground Scale": 0.5,
"IPD": 0.064,
"Convergence": 0.0,
"Display Mode": "Half-SBS",
"FP16": True,
"torch.compile": None,
"TensorRT": None,
"Recompile TensorRT": False,
"CoreML": None,
"Recompile CoreML": False,
"OpenVINO": None,
"Recompile OpenVINO": False,
"MIGraphX": None,
"Recompile MIGraphX": False,
"Computing Device": 0,
"Language": "EN",
"Run Mode": "OpenXR Link",
"XR Preview": False,
"Crop Mode": "manual",
"VSync": False,
"Stream Protocol": "HLS",
"Streamer Port": DEFAULT_PORT,
"Stream Quality": 100,
"Stream Key": "live",
"Stereo Mix": None,
"CRF": 20,
"Audio Delay": -0.15,
"Controller Model": "PICO",
"Environment Model": "Default",
"Lossless Scaling Support": False,
"Capture Tool": "none",
"Fill 16:9": True,
"Fix Viewer Aspect": False,
"Stereo Output": None,
}
# Protocol → default port mapping for RTMP streamer (port is read-only in RTMP mode)
PROTOCOL_DEFAULT_PORTS = {
"RTMP": "1935",
"RTSP": "8554",
"HLS": "8888",
"HLS M3U8": "8888",
"WebRTC": "8889",
}
# ─────────────────────────────────────────────
# Global Font Size
# ─────────────────────────────────────────────
SCALE = 0.9 # Global UI scale factor (fonts, spacing, widget + window sizes)
def S(v):
"""Scale a dimension literal by the global UI SCALE factor."""
return round(v * SCALE)
FONT_SIZE = S(14)
LABEL_ALIGN_WIDTH = 0 # Set by _auto_align_labels after UI is built
# ─────────────────────────────────────────────
# Device & Window Helpers (from gui.py)
# ─────────────────────────────────────────────
PRIMARY_MONITOR_SUFFIX = " [Main]"
def get_devices():
"""
Returns (devices_dict, is_rocm).
devices_dict: {0: {"name": str, "Computing Device": torch.device}, ...}
"""
is_rocm = False
devices = {}
count = 0
try:
import torch_directml
if torch_directml.is_available():
for i in range(torch_directml.device_count()):
dev_name = torch_directml.device_name(i).strip().rstrip('\x00')
devices[count] = {
"name": f"DirectML{i}: {dev_name}",
"Computing Device": torch_directml.device(i),
}
count += 1
except ImportError:
pass
try:
import torch
if torch.cuda.is_available():
for i in range(torch.cuda.device_count()):
name = torch.cuda.get_device_name(i)
if torch.version.hip is not None:
is_rocm = True
devices[count] = {"name": f"CUDA {i}: {name}", "Computing Device": torch.device(f"cuda:{i}")}
count += 1
if getattr(torch.backends, "mps", None) and torch.backends.mps.is_available():
devices[count] = {"name": "MPS: Apple Silicon", "Computing Device": torch.device("mps")}
count += 1
if hasattr(torch, "xpu") and torch.xpu.is_available():
for i in range(torch.xpu.device_count()):
name = torch.xpu.get_device_name(i)
devices[count] = {"name": f"XPU {i}: {name}", "Computing Device": torch.device(f"xpu:{i}")}
count += 1
devices[count] = {"name": "CPU", "Computing Device": torch.device("cpu")}
except ImportError:
raise ImportError("PyTorch Not Found! Make sure you have deployed the Python environment in '.env'.")
return devices, is_rocm
class _LazyDevices(dict):
"""Lazy hardware detection — only runs get_devices() on first dict API access."""
def __init__(self):
self._loaded = False
def _ensure(self):
if not self._loaded:
self._loaded = True
try:
d, r = get_devices()
self.update(d)
global IS_ROCM; IS_ROCM = r
except Exception as e:
print(f"[Warning] Hardware detection failed, fallback to CPU: {e}")
self.update({0: {"name": "CPU", "Computing Device": None}})
def __getitem__(self, k): self._ensure(); return super().__getitem__(k)
def __iter__(self): self._ensure(); return super().__iter__()
def __len__(self): self._ensure(); return super().__len__()
def values(self): self._ensure(); return super().values()
def keys(self): self._ensure(); return super().keys()
def items(self): self._ensure(); return super().items()
def get(self, k, d=None): self._ensure(); return super().get(k, d)
def __contains__(self, k): self._ensure(); return super().__contains__(k)
DEVICES = _LazyDevices()
IS_ROCM = False
def get_default_windows_capture_tool():
if "CUDA" in DEVICES.get(0, {}).get("name", "") and not IS_ROCM:
return "WindowsCaptureCUDA"
elif "CUDA" in DEVICES.get(0, {}).get("name", "") and IS_ROCM:
return "WindowsCaptureROCm"
else:
return "DXCamera"
def get_primary_monitor_index():
if OS_NAME != "Windows":
return _get_primary_monitor_index_unix()
try:
import win32api, win32con
primary_monitor_handle = win32api.MonitorFromPoint((0, 0), win32con.MONITOR_DEFAULTTOPRIMARY)
primary_monitor_info = win32api.GetMonitorInfo(primary_monitor_handle)
primary_rect = primary_monitor_info["Monitor"]
primary_left, primary_top, _, _ = primary_rect
import mss
with mss.mss() as sct:
for idx, monitor in enumerate(sct.monitors[1:], start=1):
if monitor["left"] == primary_left and monitor["top"] == primary_top:
return idx
return 1
except Exception:
return 1
def _get_primary_monitor_index_unix():
try:
import mss
with mss.mss() as sct:
for idx, monitor in enumerate(sct.monitors[1:], start=1):
if monitor["left"] == 0 and monitor["top"] == 0:
return idx
except Exception:
pass
return 1
def list_windows():
"""Return list of {title, handle, rect} for visible windows."""
if OS_NAME == "Windows":
return _list_windows_win()
elif OS_NAME == "Darwin":
return _list_windows_mac()
else:
return _list_windows_linux()
def _list_windows_win():
import win32gui
windows = []
def callback(hwnd, _):
if win32gui.IsWindowVisible(hwnd):
title = win32gui.GetWindowText(hwnd)
if title:
client_rect = win32gui.GetClientRect(hwnd)
left, top = win32gui.ClientToScreen(hwnd, (client_rect[0], client_rect[1]))
windows.append({
"title": title,
"handle": hwnd,
"rect": (left, top, client_rect[2], client_rect[3]),
})
return True
win32gui.EnumWindows(callback, None)
return windows
def _list_windows_mac():
from Quartz import (
CGWindowListCopyWindowInfo,
kCGWindowListOptionOnScreenOnly,
kCGWindowListExcludeDesktopElements,
kCGNullWindowID,
)
windows = []
options = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements
window_info = CGWindowListCopyWindowInfo(options, kCGNullWindowID)
blacklist = [
"Window Server", "ControlCenter", "NotificationCenter", "Spotlight",
"Dock", "FocusModes", "WiFi", "Sound", "UserSwitcher", "Clock",
"BentoBox", "Bluetooth", "popdown", "AudioVideoModule",
"ScreenMirroring", "SystemUIServer", "CoreServicesUIAgent",
"TextInputMenuAgent", "com.apple.controlcenter", "loginwindow",
]
for win in window_info:
title = win.get("kCGWindowName", "") or ""
owner = win.get("kCGWindowOwnerName", "")
layer = win.get("kCGWindowLayer", 0)
bounds = win.get("kCGWindowBounds", {})
if not title.strip():
continue
if owner in blacklist or title in blacklist:
continue
if layer >= 1000:
continue
if win.get("kCGWindowAlpha", 1.0) == 0.0:
continue
if title.strip().lower().startswith(("item-", "window-")):
continue
if "X" in bounds and "Y" in bounds and "Width" in bounds and "Height" in bounds:
w, h = bounds["Width"], bounds["Height"]
if w < 10 or h < 10:
continue
windows.append({
"title": title.strip(),
"handle": win["kCGWindowNumber"],
"rect": (bounds["X"], bounds["Y"], w, h),
})
return windows
def _list_windows_linux():
windows = []
try:
result = subprocess.check_output(["wmctrl", "-lG"], timeout=2).decode("utf-8").splitlines()
for line in result:
parts = line.split(None, 7)
if len(parts) >= 8:
_, _, x_str, y_str, w_str, h_str, _, title = parts
try:
x, y, w, h = int(x_str), int(y_str), int(w_str), int(h_str)
if title.strip():
windows.append({"title": title.strip(), "handle": None, "rect": (x, y, w, h)})
except ValueError:
continue
except Exception as e:
print(f"[Warning] Linux window enumeration failed (install wmctrl): {e}")
return windows
def get_monitor_index_for_point(x, y):
try:
import mss
with mss.mss() as sct:
for idx, mon in enumerate(sct.monitors[1:], start=1):
left, top = mon["left"], mon["top"]
right, bottom = left + mon["width"], top + mon["height"]
if left <= x < right and top <= y < bottom:
return idx
except Exception:
pass
return get_primary_monitor_index()
def _get_capture_tool_options(device_label):
if OS_NAME == "Darwin":
return ["ScreenCaptureKit", "Quartz"]
if OS_NAME != "Windows":
return ["DXCamera"]
is_nvidia = "CUDA" in device_label.upper() and not IS_ROCM
if is_nvidia:
return ["WindowsCaptureCUDA", "WindowsCapture", "DXCamera", "DesktopDuplication"]
elif "CUDA" in device_label.upper() and IS_ROCM:
return ["WindowsCaptureROCm", "WindowsCapture", "DXCamera", "DesktopDuplication"]
else:
return ["DXCamera", "WindowsCapture", "DesktopDuplication"]
# ─────────────────────────────────────────────
# Save YAML (with encoding fallback)
# ─────────────────────────────────────────────
HAVE_YAML = True
def save_yaml(path, cfg):
if not HAVE_YAML:
return False, "PyYAML not installed"
try:
tmp = path + ".tmp"