-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlive_stream_from_json.py
More file actions
803 lines (704 loc) · 31.8 KB
/
live_stream_from_json.py
File metadata and controls
803 lines (704 loc) · 31.8 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
import asyncio
import copy
import json
import socket
import sys
import time
from pathlib import Path
import websockets
"""
Simple WebSocket server that streams frames from an existing visualizer JSON
file one-by-one so we can test real-time visualization without running IK.
Playback is paced using the actual timestamps in the JSON so that, on
average, wall-clock time matches recorded time (optionally scaled by a
speed factor).
Usage (single subject):
python live_stream_from_json.py subject.json [speed]
Usage (two subjects):
python live_stream_from_json.py subject1.json subject2.json [speed]
Coloring options:
# Uniform color per subject
--subject-colors "#d3d3d3,#4995e0"
# Whole-model transparency per subject (0=invisible, 1=opaque)
--subject-opacity 0.5,0.8
# Same per-bone style applied to both subjects
--body-style '{"pelvis": {"color": "#ff0000"}, "femur_r": {"color": "#00ff00"}}'
# Different per-bone styles for each subject (JSON array, one dict per subject)
--body-style '[{"pelvis": {"color": "#ff0000"}}, {"pelvis": {"color": "#0000ff"}}]'
Camera options:
# Anatomical presets (recommended for OpenCap data):
# anterior – facing the subject's front
# posterior – behind the subject
# sagittal_right – subject's right side
# sagittal_left – subject's left side
# superior – looking down from above
# inferior – looking up from below
--camera anterior
# Generic axis presets: front | back | left | right | top | bottom | isometric | default
# corner views: frontTopRight | frontTopLeft | backTopRight | backTopLeft | ...
--camera front
# Exact position + look-at target (meters)
--camera '{"position": [3, 2, -4], "target": [0, 1, 0]}'
Model options:
# Single model for all subjects (folder_name from the visualizer model list)
--model LaiArnold
--model Hu_ISB_shoulder
# Different model per subject (comma-separated, one per subject)
--model "LaiArnold,Hu_ISB_shoulder"
Server:
# If default port 8765 is already in use, pick another:
--port 8766
Interactive commands (type while the server is running):
notify Good job! → info banner on the visualizer
notify success Great technique! → colored banner (info/success/warning/error)
dismiss → dismiss the current notification banner
camera anterior → update the viewer camera without reloading subjects
camera {"position":[3,2,-4]} → exact camera position (optional target)
scores 85 72 90 68 88 [label1 ...] [colors: g o r g o] [title: text] → trial scores (g/r/o = green/red/orange)
hidescores → hide the trial scores plot
hide subject_0 → hide a subject
show subject_0 → show a subject
help → list all commands
Then in the browser connect to ws://localhost:8765 from the visualizer.
Same WiFi (stream on one computer, view on another):
The server binds to all interfaces (0.0.0.0). On the streaming machine, run this script;
on the viewing machine open the visualizer and set WebSocket URL to ws://<streaming-IP>:8765
(e.g. ws://192.168.1.50:8765). If you use https://visualizer.opencap.ai, browsers block
ws:// (mixed content); run the visualizer locally (npm run serve) on the viewing machine
and use the ws:// URL there, or use a tunnel (e.g. ngrok) for wss://.
"""
DEFAULT_JSON_PATH = Path(__file__).parent / "mono.json"
# ---------------------------------------------------------------------------
# Connected clients registry – lets helper functions reach all open sockets
# ---------------------------------------------------------------------------
_CONNECTED_CLIENTS: set = set()
async def send_notification(message: str, level: str = "info", duration: int = 5000):
"""
Show a notification banner on every connected visualizer client.
level : "info" | "success" | "warning" | "error"
duration : how long (ms) the banner stays visible (0 = until dismissed)
Can also be called programmatically when embedding this script:
asyncio.run(send_notification("Great job!", level="success"))
"""
msg = json.dumps({"type": "notification", "message": message, "level": level, "duration": duration})
for ws in list(_CONNECTED_CLIENTS):
try:
await ws.send(msg)
except Exception:
pass
async def send_dismiss_notification():
"""Dismiss (hide) the current notification banner on every connected visualizer client."""
msg = json.dumps({"type": "dismissNotification"})
for ws in list(_CONNECTED_CLIENTS):
try:
await ws.send(msg)
except Exception:
pass
async def send_subject_visibility(subject_id: str, visible: bool):
"""
Hide or show a subject on every connected visualizer client.
subject_id : the subject ID used in the init message, typically the JSON file
stem (e.g. "s1" for s1.json)
visible : True to show, False to hide
"""
msg = json.dumps({"type": "subjectVisibility", "subjectId": subject_id, "visible": visible})
for ws in list(_CONNECTED_CLIENTS):
try:
await ws.send(msg)
except Exception:
pass
async def hide_subject(subject_id: str):
"""Hide a subject on every connected visualizer client."""
await send_subject_visibility(subject_id, False)
async def show_subject(subject_id: str):
"""Show a subject on every connected visualizer client."""
await send_subject_visibility(subject_id, True)
_COLOR_MAP = {"g": "green", "o": "orange", "r": "red"}
async def send_trial_scores(
scores: list,
labels: list | None = None,
title: str | None = None,
colors: list[str] | None = None,
):
"""
Show the trial scores plot on every connected visualizer client.
scores : list of numbers (0–100, percentages); the number of bars matches len(scores)
labels : optional list of strings for bar labels (truncated to len(scores) if longer)
title : optional string for the plot title
colors : optional list of chars: g=green, o=orange, r=red (truncated to len(scores) if longer)
"""
n = len(scores)
msg = {"type": "trialScores", "scores": scores}
if labels:
msg["labels"] = labels[:n]
if title:
msg["title"] = str(title).strip()
if colors:
validated = [
c if c.lower() in _COLOR_MAP else "g"
for c in [str(x).lower()[0] for x in colors[:n]]
]
msg["colors"] = validated
for ws in list(_CONNECTED_CLIENTS):
try:
await ws.send(json.dumps(msg))
except Exception:
pass
async def send_hide_scores():
"""Hide the trial scores plot on every connected visualizer client."""
msg = json.dumps({"type": "hideScores"})
for ws in list(_CONNECTED_CLIENTS):
try:
await ws.send(msg)
except Exception:
pass
async def send_camera(camera: "dict | str | None"):
"""
Update the camera on every connected visualizer client without reinitializing.
camera : preset string (e.g. "anterior") or dict with
{"position": [x, y, z], "target": [x, y, z]}
"""
if camera is None:
return
payload = camera if isinstance(camera, dict) else {"view": camera}
msg = json.dumps({"type": "camera", "camera": payload})
for ws in list(_CONNECTED_CLIENTS):
try:
await ws.send(msg)
except Exception:
pass
def _estimate_fps(time_array):
if not isinstance(time_array, list) or len(time_array) < 2:
return 60.0
duration = time_array[-1] - time_array[0]
if duration <= 0:
return 60.0
fps = (len(time_array) - 1) / duration
# Clamp to a reasonable range
fps = max(10.0, min(120.0, fps))
return fps
async def stream_from_json(
websocket,
json_paths,
speed: float = 1.0,
body_style: dict | None = None,
subject_colors: list[str] | None = None,
subject_opacity: list[float] | None = None,
camera: "dict | str | None" = None,
models: "list[str] | None" = None,
):
"""
Stream one or two visualizer JSON files over WebSocket in (optionally downsampled)
real time.
body_style: flat dict { bodyName: { "visible": bool, "color": "#RRGGBB" } } applied to every
subject, OR a JSON array [ {subject1 styles}, {subject2 styles} ] for per-subject
per-bone control.
subject_colors: list of hex color strings, one per subject (e.g. ["#d3d3d3", "#4995e0"]).
Colors every bone of that subject uniformly. Takes priority over body_style.
subject_opacity: list of floats 0-1, one per subject (e.g. [0.5, 0.8]). Whole-model transparency.
Protocol:
init:
{
"type": "init",
"frameRate": <fps_after_downsample_and_speed>,
"subjects": [
{ "id": "subj1", "label": "Subject 1", "bodies": { ... }, "bodyStyle": { ... } },
{ "id": "subj2", "label": "Subject 2", "bodies": { ... }, "bodyStyle": { ... } }
],
"bodies": { ... }, // flat, first subject only (single-subject frontend compat)
"bodyStyle": { ... } // optional global per-body visibility/color
}
frame:
{
"type": "frame",
"streams": {
"subj1": { "time": t, "bodies": { ... } },
"subj2": { "time": t, "bodies": { ... } }
}
}
"""
if speed <= 0:
speed = 1.0
# Load all JSON files
data_list = []
for path in json_paths:
with path.open("r", encoding="utf-8") as f:
data_list.append(json.load(f))
if not data_list:
raise ValueError("No JSON data loaded")
# Use first JSON's time as master
master_time = data_list[0].get("time", [])
if not isinstance(master_time, list) or not master_time:
raise ValueError("Master JSON must contain a non-empty 'time' array")
fps = _estimate_fps(master_time)
print(f"Estimated FPS from master JSON: {fps:.2f}, speed factor: {speed:.2f}")
# Basic consistency check for other subjects
for i, data in enumerate(data_list[1:], start=2):
t = data.get("time", [])
if not isinstance(t, list) or not t:
print(f"Warning: subject {i} has invalid or empty 'time' array; using master timing.")
continue
if len(t) != len(master_time) or abs(t[0] - master_time[0]) > 1e-3 or abs(t[-1] - master_time[-1]) > 1e-3:
print(
f"Warning: subject {i} time array differs from master; "
"assuming roughly aligned and sampling by index."
)
# Decide downsampling to target ~30 Hz based on master timeline
target_fps = 30.0
num_frames = len(master_time)
start_time = master_time[0]
if fps <= target_fps:
kept_indices = list(range(num_frames))
effective_base_fps = fps
else:
period = 1.0 / target_fps
kept_indices = []
next_t = start_time
tol = period * 0.25
for idx, t_val in enumerate(master_time):
if t_val + tol >= next_t:
kept_indices.append(idx)
next_t += period
if kept_indices and kept_indices[-1] != num_frames - 1:
kept_indices.append(num_frames - 1)
duration = master_time[kept_indices[-1]] - master_time[kept_indices[0]]
effective_base_fps = (len(kept_indices) - 1) / duration if duration > 0 else target_fps
print(
f"Downsampling from ~{fps:.2f} Hz to ~{effective_base_fps:.2f} Hz "
f"({len(kept_indices)}/{num_frames} frames)"
)
# Build subjects metadata for init
subjects_meta = []
subject_ids = []
for idx, (path, data) in enumerate(zip(json_paths, data_list), start=1):
bodies = data.get("bodies", {})
if not isinstance(bodies, dict) or not bodies:
raise ValueError(f"JSON {path} must contain a non-empty 'bodies' dict")
bodies_meta = {}
for name, bd in bodies.items():
bodies_meta[name] = {
"attachedGeometries": bd.get("attachedGeometries", []),
"scaleFactors": bd.get("scaleFactors", [1.0, 1.0, 1.0]),
}
# Construct a unique and stable subject id from the file stem
base_id = path.stem
subject_id = base_id
# Ensure uniqueness in case of duplicate filenames
suffix = 2
while subject_id in subject_ids:
subject_id = f"{base_id}_{suffix}"
suffix += 1
subject_ids.append(subject_id)
# Resolve model for this subject:
# models list is indexed by subject position (0-based); a single entry applies to all.
model: str | None = None
if models:
model = models[idx - 1] if idx - 1 < len(models) else models[0]
subject_entry: dict = {
"id": subject_id,
"label": path.stem,
"bodies": bodies_meta,
}
if model:
subject_entry["model"] = model
# Per-subject bodyStyle resolution (highest to lowest priority):
# 1. --subject-colors entry for this subject (single hex color → all bodies)
# 2. --body-style entry for this subject when body_style is a list
# 3. --body-style as a flat dict (applied to every subject)
color = subject_colors[idx - 1] if subject_colors and idx - 1 < len(subject_colors) else None
if color:
subject_entry["bodyStyle"] = {name: {"color": color} for name in bodies_meta}
elif isinstance(body_style, list):
if idx - 1 < len(body_style) and isinstance(body_style[idx - 1], dict):
subject_entry["bodyStyle"] = body_style[idx - 1]
elif body_style:
subject_entry["bodyStyle"] = body_style
# Whole-model transparency (--subject-opacity): apply to all bodies
if subject_opacity and idx - 1 < len(subject_opacity):
opacity_val = subject_opacity[idx - 1]
opacity_val = max(0.0, min(1.0, float(opacity_val)))
if subject_entry.get("bodyStyle") is not None:
subject_entry["bodyStyle"] = copy.deepcopy(subject_entry["bodyStyle"])
else:
subject_entry["bodyStyle"] = {}
for name in bodies_meta:
subject_entry["bodyStyle"].setdefault(name, {})["opacity"] = opacity_val
subjects_meta.append(subject_entry)
init_msg = {
"type": "init",
# Inform client about nominal frame rate (after speed scaling)
"frameRate": effective_base_fps * speed,
"subjects": subjects_meta,
}
# Single-subject frontend compatibility: send flat bodies (and bodyStyle) at top level
if len(subjects_meta) == 1:
init_msg["bodies"] = subjects_meta[0]["bodies"]
if body_style and not isinstance(body_style, list):
init_msg["bodyStyle"] = body_style
if camera is not None:
init_msg["camera"] = camera if isinstance(camera, dict) else {"view": camera}
await websocket.send(json.dumps(init_msg))
print(f" Subject IDs: {subject_ids} (use these with hide/show commands)")
# Give the client a brief moment to load meshes
await asyncio.sleep(1.0)
# Stream frames in a loop; when we reach the end, loop again.
# We use the master timeline to pace playback against wall-clock time.
while True:
loop_start_wall = time.perf_counter()
for frame_idx in kept_indices:
# Target elapsed time (recorded) scaled by speed factor
recorded_t = master_time[frame_idx] - start_time
target_elapsed = recorded_t / speed
# How much time has passed in wall-clock since this loop started
now_elapsed = time.perf_counter() - loop_start_wall
delay = target_elapsed - now_elapsed
if delay > 0:
await asyncio.sleep(delay)
t_val = master_time[frame_idx]
# Build per-subject streams using the same subject ids as in init
streams = {}
for subject_id, data in zip(subject_ids, data_list):
bodies = data.get("bodies", {})
frame_bodies = {}
for name, bd in bodies.items():
rotations = bd.get("rotation", [])
translations = bd.get("translation", [])
if frame_idx >= len(rotations) or frame_idx >= len(translations):
continue
frame_bodies[name] = {
"rotation": rotations[frame_idx],
"translation": translations[frame_idx],
}
streams[subject_id] = {
"time": t_val,
"bodies": frame_bodies,
}
frame_msg = {
"type": "frame",
"streams": streams,
}
await websocket.send(json.dumps(frame_msg))
_CAMERA_PRESETS = {
# Generic axis-aligned presets (visualizer coordinate names)
"front", "back", "left", "right", "top", "bottom",
"isometric", "default",
"frontTopRight", "frontTopLeft", "frontBottomRight", "frontBottomLeft",
"backTopRight", "backTopLeft", "backBottomRight", "backBottomLeft",
# OpenCap anatomical aliases (map to the correct axis for a standing subject)
"anterior", # frontal plane, facing subject (+X in OpenCap) = same as 'right'
"posterior", # behind subject (-X in OpenCap) = same as 'left'
"sagittal_right", # subject's right side (+Z in OpenCap) = same as 'front'
"sagittal_left", # subject's left side (-Z in OpenCap) = same as 'back'
"superior", # looking down (+Y) = same as 'top'
"inferior", # looking up (-Y) = same as 'bottom'
}
def _parse_camera(arg: str) -> "dict | str | None":
"""Parse --camera: a named preset string or inline JSON with position/target.
Named presets: front | back | left | right | top | bottom | isometric | default
frontTopRight | frontTopLeft | backTopRight | backTopLeft | ...
JSON format: {"position": [x, y, z], "target": [x, y, z]}
(target defaults to [0, 1, 0] if omitted)
"""
if not arg or not arg.strip():
return None
arg = arg.strip()
if arg in _CAMERA_PRESETS:
return arg # plain preset name
# Try JSON
try:
data = json.loads(arg)
if isinstance(data, dict) and "position" in data:
return data
print(f"--camera JSON must contain a 'position' key; got: {arg[:60]}")
return None
except json.JSONDecodeError:
print(f"Unknown --camera value '{arg}'. Valid presets: {sorted(_CAMERA_PRESETS)}")
return None
def _parse_body_style(arg: str) -> "dict | list | None":
"""Parse --body-style: either a path to a JSON file or inline JSON string.
Accepts:
- A flat dict: {"pelvis": {"color": "#ff0000"}, ...} → same style for every subject
- A JSON array: [{"pelvis": {"color": "#ff0000"}}, {"pelvis": {"color": "#0000ff"}}]
→ per-subject styles indexed by position
"""
if not arg or not arg.strip():
return None
arg = arg.strip()
# Inline JSON: try parse first to avoid path.is_file() on long strings (OS "file name too long")
if arg.startswith("{") or arg.startswith("["):
try:
return json.loads(arg)
except json.JSONDecodeError:
print(f"Invalid body-style JSON: {arg[:60]}...")
return None
path = Path(arg)
if path.is_file():
with path.open("r", encoding="utf-8") as f:
return json.load(f)
try:
return json.loads(arg)
except json.JSONDecodeError:
print(f"Invalid body-style (not a file or JSON): {arg[:60]}...")
return None
async def main():
args = sys.argv[1:]
# Extract --body-style if present
body_style = None
if "--body-style" in args:
idx = args.index("--body-style")
if idx + 1 < len(args):
body_style = _parse_body_style(args[idx + 1])
args = args[:idx] + args[idx + 2 :]
else:
args = args[:idx] + args[idx + 1 :]
# Extract --subject-colors if present (comma-separated hex colors, one per subject)
# e.g. --subject-colors "#d3d3d3,#4995e0"
subject_colors: list[str] | None = None
if "--subject-colors" in args:
idx = args.index("--subject-colors")
if idx + 1 < len(args):
subject_colors = [c.strip() for c in args[idx + 1].split(",")]
args = args[:idx] + args[idx + 2 :]
else:
args = args[:idx] + args[idx + 1 :]
# Extract --subject-opacity if present (comma-separated floats 0-1, one per subject)
# e.g. --subject-opacity 0.5,0.8
subject_opacity: list[float] | None = None
if "--subject-opacity" in args:
idx = args.index("--subject-opacity")
if idx + 1 < len(args):
raw = [x.strip() for x in args[idx + 1].split(",")]
subject_opacity = []
for v in raw:
try:
f = float(v)
subject_opacity.append(max(0.0, min(1.0, f)))
except ValueError:
print(f"Invalid opacity '{v}' in --subject-opacity, skipping")
args = args[:idx] + args[idx + 2 :]
else:
args = args[:idx] + args[idx + 1 :]
# Extract --camera if present
# Accepts a named preset (e.g. "front") or inline JSON {"position":[x,y,z],"target":[x,y,z]}
camera: "dict | str | None" = None
if "--camera" in args:
idx = args.index("--camera")
if idx + 1 < len(args):
camera = _parse_camera(args[idx + 1])
args = args[:idx] + args[idx + 2 :]
else:
args = args[:idx] + args[idx + 1 :]
# Extract --model if present
# Single value ("LaiArnold") or comma-separated per-subject ("LaiArnold,Hu_ISB_shoulder")
models: "list[str] | None" = None
if "--model" in args:
idx = args.index("--model")
if idx + 1 < len(args):
models = [m.strip() for m in args[idx + 1].split(",")]
args = args[:idx] + args[idx + 2 :]
else:
args = args[:idx] + args[idx + 1 :]
port = 8765
if "--port" in args:
idx = args.index("--port")
if idx + 1 < len(args):
try:
port = int(args[idx + 1])
if not (1 <= port <= 65535):
raise ValueError("out of range")
except ValueError:
print("Error: --port must be an integer 1–65535", file=sys.stderr)
sys.exit(1)
args = args[:idx] + args[idx + 2 :]
else:
print("Error: --port requires a value (e.g. --port 8766)", file=sys.stderr)
sys.exit(1)
json_args = [a for a in args if a.lower().endswith(".json")]
other_args = [a for a in args if not a.lower().endswith(".json")]
if not json_args:
if DEFAULT_JSON_PATH.is_file():
json_paths = [DEFAULT_JSON_PATH.resolve()]
print(f"No JSON file arguments provided; using default mono.json at {json_paths[0]}")
else:
print("Error: at least one JSON file path is required (mono.json not found)")
sys.exit(1)
else:
# Support up to two subjects
json_paths = [Path(p).resolve() for p in json_args[:2]]
for p in json_paths:
if not p.is_file():
print(f"JSON file not found: {p}")
sys.exit(1)
# Optional speed multiplier: >1.0 = faster than recorded, <1.0 = slower
speed = 1.0
if other_args:
try:
speed = float(other_args[-1])
except ValueError:
print(f"Invalid speed '{other_args[-1]}', using 1.0")
speed = 1.0
async def handler(websocket):
_CONNECTED_CLIENTS.add(websocket)
print(f"Client connected from {websocket.remote_address}")
try:
await stream_from_json(
websocket, json_paths, speed,
body_style=body_style,
subject_colors=subject_colors,
subject_opacity=subject_opacity,
camera=camera,
models=models,
)
except Exception as e:
print(f"Error during streaming: {e}")
finally:
_CONNECTED_CLIENTS.discard(websocket)
print("Client disconnected")
async def stdin_command_loop():
"""
Read commands from stdin while the server is running.
Commands
--------
notify <message> – info banner
notify <level> <message> – banner with level (info/success/warning/error)
dismiss – dismiss the current notification banner
scores <n1> <n2> <n3> <n4> <n5> [label1 ... label5] [colors: g o r g o] [title: text] – trial scores (g/r/o=green/red/orange)
hidescores – hide the trial scores plot
hide <subject_id> – hide a subject
show <subject_id> – show a subject
help – print this list
"""
loop = asyncio.get_event_loop()
reader = asyncio.StreamReader()
protocol = asyncio.StreamReaderProtocol(reader)
try:
await loop.connect_read_pipe(lambda: protocol, sys.stdin)
except Exception:
return # stdin not a pipe (e.g. IDE terminal) — skip gracefully
print("Interactive commands ready (type 'help' for usage).")
while True:
try:
line = await reader.readline()
if not line:
break
cmd = line.decode().strip()
if not cmd:
continue
parts = cmd.split(" ", 1)
verb = parts[0].lower()
rest = parts[1].strip() if len(parts) > 1 else ""
if verb == "help":
print(
"Commands:\n"
" notify <message> – info notification\n"
" notify <level> <message> – notification with level (info/success/warning/error)\n"
" dismiss – dismiss current notification banner\n"
" camera <preset|json> – update camera without reloading subjects\n"
" scores <n1> <n2> <n3> <n4> <n5> [label1 ... label5] [colors: g o r g o] [title: text] – trial scores (g/r/o=green/red/orange)\n"
" hidescores – hide trial scores plot\n"
" hide <subject_id> – hide subject\n"
" show <subject_id> – show subject\n"
)
elif verb == "notify":
notify_parts = rest.split(" ", 1) if rest else []
if len(notify_parts) == 2 and notify_parts[0] in ("info", "success", "warning", "error"):
await send_notification(notify_parts[1], level=notify_parts[0])
print(f"[notify:{notify_parts[0]}] {notify_parts[1]}")
elif rest:
msg_text = rest
await send_notification(msg_text)
print(f"[notify:info] {msg_text}")
else:
print("Usage: notify [level] <message>")
elif verb == "dismiss":
await send_dismiss_notification()
print("[dismiss] notification hidden")
elif verb == "camera":
parsed_camera = _parse_camera(rest)
if parsed_camera is None:
print("Usage: camera <preset|json>")
else:
await send_camera(parsed_camera)
print(f"[camera] {parsed_camera}")
elif verb == "scores" and rest:
remainder = rest
title = None
colors = None
if "title:" in remainder:
idx = remainder.index("title:")
title = remainder[idx + 6:].strip()
remainder = remainder[:idx].strip()
if "colors:" in remainder:
idx = remainder.index("colors:")
raw = remainder[idx + 7:].strip()
remainder = remainder[:idx].strip()
if " " in raw:
colors = raw.split()[:5]
else:
colors = list(raw)[:5]
tokens = remainder.split()
if len(tokens) >= 5:
try:
scores_list = [float(x) for x in tokens[:5]]
labels = tokens[5:10] if len(tokens) >= 10 else None
await send_trial_scores(scores_list, labels=labels, title=title, colors=colors)
out = f"[scores] {scores_list}"
if labels:
out += f" labels={labels}"
if colors:
out += f" colors={colors}"
if title:
out += f" title={title!r}"
print(out)
except ValueError as e:
print(f"Usage: scores <n1> <n2> <n3> <n4> <n5> [label1 ... label5] [colors: g o r g o] [title: <text>]. Error: {e}")
else:
print("Usage: scores <n1> <n2> <n3> <n4> <n5> [label1 label2 label3 label4 label5] [colors: g o r g o] [title: optional title]")
elif verb == "hidescores":
await send_hide_scores()
print("[hidescores]")
elif verb == "hide" and rest:
await send_subject_visibility(rest, False)
print(f"[hide] {rest}")
elif verb == "show" and rest:
await send_subject_visibility(rest, True)
print(f"[show] {rest}")
else:
print(f"Unknown command '{cmd}'. Type 'help' for usage.")
except Exception as e:
print(f"[stdin] Error: {e}")
print(f"Streaming from {[str(p) for p in json_paths]}")
host = "0.0.0.0"
try:
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.connect(("8.8.8.8", 80))
lan_ip = s.getsockname()[0]
except Exception:
lan_ip = "127.0.0.1"
print(f"WebSocket server listening on ws://localhost:{port} (this machine)")
if lan_ip != "127.0.0.1":
print(f" On same WiFi, use ws://{lan_ip}:{port} from another computer")
try:
async with websockets.serve(handler, host, port):
await asyncio.gather(
asyncio.Future(), # keep server alive forever
stdin_command_loop(),
)
except OSError as e:
errno = getattr(e, "errno", None)
winerr = getattr(e, "winerror", None)
if (
errno in (48, 98)
or winerr == 10048
or "Address already in use" in str(e)
):
print(
f"\nPort {port} is already in use (another live stream or app is bound there).\n"
f" • Stop the other process, e.g. macOS/Linux: lsof -i :{port}\n"
f" • Or use a different port, e.g. --port 8766\n",
file=sys.stderr,
)
sys.exit(1)
raise
if __name__ == "__main__":
asyncio.run(main())