-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_camera_node.py
More file actions
2059 lines (1694 loc) · 85.4 KB
/
Copy pathlocal_camera_node.py
File metadata and controls
2059 lines (1694 loc) · 85.4 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
#!/usr/bin/env python3
"""
COMPLETE FIXED Local Camera Node Service for control1 (rep8)
✅ Fixed: Syntax errors removed
✅ Fixed: High resolution stills (4056x3040 - 4:3 full sensor)
✅ Fixed: Correct color handling (RGB format for GUI)
✅ Fixed: Working transforms using shared.transforms system
✅ Fixed: Proper stop/capture/restart protocol
"""
import os
import json
import socket
import subprocess
import datetime
import time
import threading
import logging
import sys
import cv2
from picamera2 import Picamera2
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - [LOCAL-COMPLETE-FIX] %(levelname)s - %(message)s",
handlers=[logging.StreamHandler(sys.stdout), logging.FileHandler("/tmp/local_camera_debug.log")],
)
logging.info("=== LOCAL CAMERA COMPLETE FIXED VERSION STARTING ===")
# Fix Python path to find shared module
script_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.dirname(script_dir)
if project_root not in sys.path:
sys.path.insert(0, project_root)
# Import configuration with better error handling
try:
from shared.config import (
LOCAL_CONTROL_PORT,
LOCAL_VIDEO_PORT,
LOCAL_STILL_PORT,
LOCAL_HEARTBEAT_PORT,
LOCAL_IMAGE_DIR,
IMAGE_DIR,
VIDEO_PORT,
STILL_PORT,
HEARTBEAT_PORT,
CONTROLLER_IP as CONTROLLER_IP_FROM_CONFIG,
)
logging.info("✓ Loaded local camera configuration")
except ImportError as e:
logging.error(f"✗ Failed to import config: {e}")
# Fallback configuration
LOCAL_CONTROL_PORT = 5011
LOCAL_VIDEO_PORT = 5012
LOCAL_STILL_PORT = 6010
LOCAL_HEARTBEAT_PORT = 5013
LOCAL_IMAGE_DIR = "captured_images_local"
IMAGE_DIR = os.path.join(script_dir, "captured_images")
VIDEO_PORT = 5002
STILL_PORT = 6000
HEARTBEAT_PORT = 5003
CONTROLLER_IP_FROM_CONFIG = "127.0.0.1"
logging.info("Using fallback configuration")
# FIXED: Controller IP resolution for local camera
def resolve_controller_ip():
"""For local camera (rep8), always use localhost"""
return "127.0.0.1"
CONTROLLER_IP = resolve_controller_ip()
logging.info(f"Final Controller IP: {CONTROLLER_IP}")
# Global state
streaming = False
streaming_lock = threading.Lock()
video_thread = None
video_picam2 = None # Live Picamera2 instance during video stream (for AE controls)
jpeg_quality = 80
last_heartbeat = 0
HEARTBEAT_INTERVAL = 1.0
HEARTBEAT_TIMEOUT = 5.0
# --- SAFE CAMERA INITIALIZATION ---
# Prevents "list index out of range" from bare Picamera2() when camera is locked/missing
def diagnose_camera_availability():
"""Check what is holding /dev/video0 and whether libcamera can see cameras."""
diag = []
try:
result = subprocess.run(["lsof", "/dev/video0"], capture_output=True, text=True, timeout=5)
if result.stdout.strip():
diag.append(f"Processes holding /dev/video0:\n{result.stdout.strip()}")
else:
diag.append("/dev/video0 is not held by any process")
except Exception as e:
diag.append(f"Could not run lsof: {e}")
try:
result = subprocess.run(["libcamera-hello", "--list-cameras"], capture_output=True, text=True, timeout=10)
output = (result.stdout + result.stderr).strip()
if output:
diag.append(f"libcamera camera list:\n{output}")
else:
diag.append("libcamera-hello returned no output")
except FileNotFoundError:
diag.append("libcamera-hello not found on this system")
except Exception as e:
diag.append(f"Could not run libcamera-hello: {e}")
# Check if PipeWire is running (known camera lock cause)
try:
result = subprocess.run(["pgrep", "-a", "pipewire"], capture_output=True, text=True, timeout=5)
if result.stdout.strip():
diag.append(f"⚠️ PipeWire is RUNNING (known to lock cameras):\n{result.stdout.strip()}")
diag.append(
"FIX: Run setup_control1_camera.sh then reboot, "
"or temporarily: systemctl --user stop pipewire wireplumber"
)
else:
diag.append("✓ PipeWire is not running")
except Exception as e:
diag.append(f"Could not check PipeWire status: {e}")
return "\n".join(diag)
def safe_picamera2_init(purpose="general", max_retries=3, retry_delay=2.0):
"""
Safely create a Picamera2 instance with enumeration check, retries, and diagnostics.
Args:
purpose: Label for logging (e.g. "video_stream", "still_capture")
max_retries: Number of attempts before giving up
retry_delay: Seconds between retries (doubles each attempt)
Returns:
Picamera2 instance, or raises RuntimeError with diagnostic info
"""
delay = retry_delay
for attempt in range(1, max_retries + 1):
try:
# Step 1: Check if Picamera2 can see any cameras at all
from picamera2 import Picamera2 as Picamera2Class
camera_info = Picamera2Class.global_camera_info()
if not camera_info:
logging.warning(
f"[CAMERA-INIT] Attempt {attempt}/{max_retries} ({purpose}): "
f"No cameras found by Picamera2.global_camera_info()"
)
if attempt == max_retries:
diag = diagnose_camera_availability()
logging.error(
f"[CAMERA-INIT] All {max_retries} attempts failed for {purpose}. Diagnostics:\n{diag}"
)
raise RuntimeError(
f"No cameras found after {max_retries} attempts ({purpose}). "
f"Camera may be locked by PipeWire or another process. "
f"Run: systemctl --user stop pipewire wireplumber"
)
logging.info(f"[CAMERA-INIT] Retrying in {delay:.1f}s...")
time.sleep(delay)
delay *= 2
continue
logging.info(
f"[CAMERA-INIT] ({purpose}) Found {len(camera_info)} camera(s): "
f"{[c.get('Model', 'unknown') for c in camera_info]}"
)
# Step 2: Create instance (now safe - we know cameras exist)
picam2 = Picamera2()
logging.info(f"[CAMERA-INIT] ✅ Picamera2 created successfully ({purpose})")
return picam2
except RuntimeError:
raise # Re-raise our own RuntimeError from above
except IndexError as e:
# This is the exact "list index out of range" we are guarding against
logging.warning(
f"[CAMERA-INIT] Attempt {attempt}/{max_retries} ({purpose}): IndexError during Picamera2(): {e}"
)
if attempt == max_retries:
diag = diagnose_camera_availability()
logging.error(
f"[CAMERA-INIT] IndexError persisted after {max_retries} attempts ({purpose}). Diagnostics:\n{diag}"
)
raise RuntimeError(
f"Camera init failed with IndexError after {max_retries} attempts ({purpose}). "
f"Likely PipeWire is holding /dev/video0. "
f"Run: systemctl --user stop pipewire wireplumber"
) from e
logging.info(f"[CAMERA-INIT] Retrying in {delay:.1f}s...")
time.sleep(delay)
delay *= 2
except Exception as e:
logging.warning(
f"[CAMERA-INIT] Attempt {attempt}/{max_retries} ({purpose}): Unexpected error: {type(e).__name__}: {e}"
)
if attempt == max_retries:
diag = diagnose_camera_availability()
logging.error(f"[CAMERA-INIT] Failed after {max_retries} attempts ({purpose}). Diagnostics:\n{diag}")
raise RuntimeError(f"Camera init failed after {max_retries} attempts ({purpose}): {e}") from e
logging.info(f"[CAMERA-INIT] Retrying in {delay:.1f}s...")
time.sleep(delay)
delay *= 2
# ARCHITECTURAL FIX: Thread synchronization for Picamera2 instance management
# Prevents "camera is already open" errors by ensuring proper cleanup sequencing
stream_stopped_event = threading.Event() # Signals streaming thread has fully stopped & closed picam2
capture_complete_event = threading.Event() # Signals capture is done & picam2 closed, safe to restart stream
_last_video_exp_us = 0 # Last valid ExposureTime read from live video stream
_last_video_gain = 1.0 # Last valid AnalogueGain read from live video stream
# Spot meter pending request — set by command handler, consumed by streaming loop.
# Avoids touching video_picam2 from a foreign thread (race with STOP_STREAM).
_local_ae_meter_pending = False
_local_ae_meter_region = None
_local_ae_meter_lock = threading.Lock()
# WB meter pending request — same flag pattern as AE, serviced from streaming loop.
_local_wb_meter_pending = False
_local_wb_meter_region = None
_local_wb_meter_lock = threading.Lock()
_local_wb_cleared = False # suppress re-notification after CLEAR_WB_LOCK
# Camera settings (matching working version)
camera_settings = {
"brightness": 0, # FIXED: Match GUI default (-50 to +50, 0 = neutral)
"contrast": 50,
"iso": 100,
"shutter_speed": 1000,
"saturation": 50,
"exposure_mode": "auto",
"jpeg_quality": 95, # Default; override via Settings > JPEG Quality.
"fps": 30,
"resolution": "640x480",
"image_format": "JPEG",
"raw_enabled": False, # RAW capture (DNG) - for rep8
"crop_enabled": False,
"crop_x": 0,
"crop_y": 0,
"crop_width": 4056, # 4:3 sensor width (NOT 4608 which is 16:9!)
"crop_height": 3040, # 4:3 sensor height (NOT 2592 which is 16:9!)
"flip_horizontal": False,
"flip_vertical": False,
"grayscale": False,
"rotation": 0,
}
def send_local_heartbeat():
"""Enhanced heartbeat with better error handling and telemetry"""
global last_heartbeat
logging.info(f"Starting heartbeat service to {CONTROLLER_IP}:{HEARTBEAT_PORT}")
# Import telemetry (lazy, tolerant of import failure)
try:
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "node"))
from telemetry import build_telemetry_payload
telemetry_available = True
logging.info("[LOCAL] Telemetry module loaded")
except ImportError:
telemetry_available = False
logging.warning("[LOCAL] Telemetry module not available")
heartbeat_count = 0
TELEMETRY_INTERVAL = 5
while True:
try:
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.settimeout(1.0)
heartbeat_count += 1
if telemetry_available and heartbeat_count % TELEMETRY_INTERVAL == 0:
try:
payload = build_telemetry_payload("rep8")
sock.sendto(payload.encode(), (CONTROLLER_IP, HEARTBEAT_PORT))
except Exception:
sock.sendto(b"HEARTBEAT", (CONTROLLER_IP, HEARTBEAT_PORT))
else:
sock.sendto(b"HEARTBEAT", (CONTROLLER_IP, HEARTBEAT_PORT))
last_heartbeat = time.time()
if heartbeat_count % 10 == 0:
logging.info(f"[LOCAL] Heartbeat #{heartbeat_count} sent to {CONTROLLER_IP}:{HEARTBEAT_PORT}")
time.sleep(HEARTBEAT_INTERVAL)
except Exception as e:
logging.error(f"[LOCAL] Error sending heartbeat #{heartbeat_count}: {e}")
time.sleep(HEARTBEAT_INTERVAL)
def apply_safe_transforms(image_array):
"""
UNIFIED: Apply frame transforms using unified pipeline for local camera (rep8)
Uses apply_unified_transforms for consistency with other nodes
"""
try:
# CAL2-R3: Apply colour pipeline BEFORE geometric transforms
# Must match video_stream.py behaviour for consistent WB/exposure
try:
if not hasattr(apply_safe_transforms, "_colour_pipe_confirmed"):
apply_safe_transforms._colour_pipe_confirmed = True
except ImportError:
if not hasattr(apply_safe_transforms, "_colour_import_warned"):
apply_safe_transforms._colour_import_warned = True
except Exception:
if not hasattr(apply_safe_transforms, "_colour_err_logged"):
apply_safe_transforms._colour_err_logged = True
# Import unified transform function
from shared.transforms import apply_unified_transforms
# Use unified transform function for consistency
processed_image = apply_unified_transforms(image_array, "rep8")
logging.debug("[LOCAL] Applied unified transforms for rep8")
return processed_image
except Exception as e:
logging.error(f"[LOCAL] Error applying unified transforms: {e}")
# Fallback to original logic if unified fails
return apply_safe_transforms_fallback(image_array)
def apply_safe_transforms_fallback(image_array):
"""
Fallback transform function for local camera if unified transforms fail
"""
try:
logging.info("[LOCAL] Using fallback transforms for rep8")
# Load settings for rep8 to apply any configured transforms (WORKING METHOD)
from shared.transforms import load_device_settings
settings = load_device_settings("rep8")
# Start with RGB format (same as working version)
image = image_array.copy()
# Apply transforms if configured (same logic as working version)
# 1. Crop
if settings.get("crop_enabled", False):
x = max(0, settings.get("crop_x", 0))
y = max(0, settings.get("crop_y", 0))
w = max(100, settings.get("crop_width", image.shape[1]))
h = max(100, settings.get("crop_height", image.shape[0]))
height, width = image.shape[:2]
x = min(x, width - 100)
y = min(y, height - 100)
w = min(w, width - x)
h = min(h, height - y)
image = image[y : y + h, x : x + w]
# 2. Rotation
rotation = settings.get("rotation", 0)
if rotation == 90:
image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
elif rotation == 180:
image = cv2.rotate(image, cv2.ROTATE_180)
elif rotation == 270:
image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
# 3. Flips (maintain RGB format)
if settings.get("flip_horizontal", False):
image = cv2.flip(image, 1)
if settings.get("flip_vertical", False):
image = cv2.flip(image, 0)
# 4. Grayscale (stay in RGB format)
if settings.get("grayscale", False):
if len(image.shape) == 3:
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
image = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
logging.info("[LOCAL] ✅ RGB format preserved with transforms applied")
return image
except Exception as e:
logging.error(f"[LOCAL] Error applying transforms: {e}")
# Fallback: keep original RGB format
return image_array
def start_local_video_stream():
"""WORKING: Enhanced video streaming with proper RGB color handling"""
global streaming, jpeg_quality, video_picam2, _last_video_exp_us, _last_video_gain
with streaming_lock:
if streaming:
logging.warning("Local video stream already running")
return
streaming = True
# ARCHITECTURAL FIX: Clear stop event at stream start (will be set on cleanup)
stream_stopped_event.clear()
logging.info(f"[EVENT] stream_stopped_event CLEARED at {time.time():.3f} - starting stream")
logging.info(f"🚀 Starting LOCAL video stream to {CONTROLLER_IP}:{VIDEO_PORT}")
picam2 = None
sock = None
frame_count = 0
last_log_time = time.time()
error_count = 0
max_errors = 10
try:
# Initialize camera (same as working version)
logging.info("Initializing camera...")
picam2 = safe_picamera2_init(purpose="video_stream", max_retries=5, retry_delay=2.0)
# WYSIWYG FIX v3: Let camera auto-select appropriate 4:3 sensor mode
# IMPORTANT: Do NOT specify raw size! 4608x2592 was WRONG (16:9 binned mode)
# By omitting raw, camera selects optimal 4:3 mode for requested output
# Check for persisted AE lock — bake into config controls to avoid
# the 1-2 frame flicker that occurs when set_controls fires after start()
_boot_controls = {"FrameRate": 15}
_boot_ae_lock = None
_boot_wb_lock = None
try:
from shared.transforms import load_device_settings
_boot_ae_lock = load_device_settings("rep8").get("ae_lock")
if _boot_ae_lock and _boot_ae_lock.get("exp_us", 0) > 50:
_boot_controls["ExposureTime"] = int(_boot_ae_lock["exp_us"])
_boot_controls["AnalogueGain"] = float(_boot_ae_lock["gain"])
_boot_controls["ExposureTimeMode"] = 1
_boot_controls["AnalogueGainMode"] = 1
logging.info(
f"[AE] Baking lock into video config: "
f"exp={_boot_ae_lock['exp_us']}us gain={_boot_ae_lock['gain']:.2f}"
)
_boot_wb_lock = load_device_settings("rep8").get("wb_lock")
if _boot_wb_lock and _boot_wb_lock.get("red_gain") is not None:
_boot_controls["ColourGains"] = (float(_boot_wb_lock["red_gain"]), float(_boot_wb_lock["blue_gain"]))
logging.info(
f"[WB] Baking lock into video config: "
f"red={_boot_wb_lock['red_gain']:.3f} blue={_boot_wb_lock['blue_gain']:.3f}"
)
except Exception:
pass
video_config = picam2.create_video_configuration(
main={"size": (640, 480), "format": "RGB888"}, controls=_boot_controls
)
logging.info("[LOCAL] WYSIWYG v3: Auto sensor mode for 640x480 (4:3 preserved, no crop)")
picam2.configure(video_config)
picam2.start()
video_picam2 = picam2 # Expose for AE controls (spot metering)
# Restore AE lock via set_controls as well (belt-and-braces for v0.5.0)
try:
if _boot_ae_lock and _boot_ae_lock.get("exp_us", 0) > 50:
picam2.set_controls(
{
"ExposureTime": int(_boot_ae_lock["exp_us"]),
"AnalogueGain": float(_boot_ae_lock["gain"]),
"ExposureTimeMode": 1,
"AnalogueGainMode": 1,
}
)
logging.info(
f"[AE] ✅ Restored AE lock for rep8: "
f"exp={_boot_ae_lock['exp_us']}us gain={_boot_ae_lock['gain']:.2f}"
)
# Notify GUI so lock bar appears on session start / reconnect
_local_ae_notify("locked", int(_boot_ae_lock["exp_us"]), float(_boot_ae_lock["gain"]))
except Exception as _e:
logging.debug(f"[AE] Could not restore AE lock for rep8: {_e}")
# Belt-and-braces WB: apply ColourGains after start
try:
if _boot_wb_lock and _boot_wb_lock.get("red_gain") is not None and not _local_wb_cleared:
picam2.set_controls(
{"ColourGains": (float(_boot_wb_lock["red_gain"]), float(_boot_wb_lock["blue_gain"]))}
)
logging.info(
f"[WB] ✅ Restored WB lock for rep8: "
f"red={_boot_wb_lock['red_gain']:.3f} blue={_boot_wb_lock['blue_gain']:.3f}"
)
_local_wb_notify("locked", float(_boot_wb_lock["red_gain"]), float(_boot_wb_lock["blue_gain"]))
except Exception as _e:
logging.debug(f"[WB] Could not restore WB lock for rep8: {_e}")
logging.info("✓ Local camera initialized")
time.sleep(0.5) # Allow AE to stabilise before first frame
# Read initial exposure after AE settle — only update globals if no ae_lock active.
# When ae_lock is set, _last_video_exp_us is already correct from the lock.
try:
from shared.transforms import load_device_settings
_has_lock = load_device_settings("rep8").get("ae_lock") is not None
if not _has_lock:
_init_meta = picam2.capture_metadata()
_init_exp = _init_meta.get("ExposureTime", 0)
_init_gain = _init_meta.get("AnalogueGain", 1.0)
if _init_exp > 50:
_last_video_exp_us = _init_exp
_last_video_gain = _init_gain
logging.info(f"[AE] Initial exposure sampled: exp={_init_exp}us gain={_init_gain:.2f}")
else:
logging.info(f"[AE] Initial exposure low ({_init_exp}us) — will resample during stream")
else:
ae_lock = load_device_settings("rep8").get("ae_lock", {})
_last_video_exp_us = ae_lock.get("exp_us", _last_video_exp_us)
_last_video_gain = ae_lock.get("gain", _last_video_gain)
logging.info(f"[AE] Lock active — keeping exp={_last_video_exp_us}us (skipping warmup read)")
except Exception as _e:
logging.debug(f"[AE] Could not sample initial exposure: {_e}")
# Create UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 262144)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.settimeout(0.1)
logging.info(f"✓ Socket created, streaming to {CONTROLLER_IP}:{VIDEO_PORT}")
start_time = time.time()
while True:
with streaming_lock:
if not streaming:
logging.info("Stream stop requested, breaking loop")
break
try:
# Spot meter pending? Service from streaming thread (owns video_picam2).
global _local_ae_meter_pending, _local_ae_meter_region
with _local_ae_meter_lock:
_do_meter = _local_ae_meter_pending
_local_ae_meter_pending = False
if _do_meter:
_local_ae_meter_and_lock()
# Service WB meter from streaming thread (same safety pattern as AE)
global _local_wb_meter_pending, _local_wb_meter_region
with _local_wb_meter_lock:
_do_wb = _local_wb_meter_pending
_local_wb_meter_pending = False
if _do_wb:
_local_wb_sample_and_lock()
# Capture frame (RGB888 format from Picamera2)
frame_rgb = picam2.capture_array()
# Apply transforms keeping RGB format
frame_rgb_transformed = apply_safe_transforms(frame_rgb)
# NO cvtColor — same as remote nodes (a92bcbb revert).
# apply_safe_transforms returns RGB. cv2.imencode and QImage.fromData
# are both consistent — no swap needed, display is correct.
frame_bgr = frame_rgb_transformed # misleading name; contains RGB
# Encode as JPEG
encode_params = [cv2.IMWRITE_JPEG_QUALITY, 70]
success, encoded = cv2.imencode(".jpg", frame_bgr, encode_params)
if not success:
logging.warning("Failed to encode frame")
continue
frame_data = encoded.tobytes()
# Check frame size and reduce quality if needed
if len(frame_data) > 60000:
encode_params = [cv2.IMWRITE_JPEG_QUALITY, 50]
success, encoded = cv2.imencode(".jpg", frame_bgr, encode_params)
if success:
frame_data = encoded.tobytes()
# Send to controller GUI
try:
sock.sendto(frame_data, (CONTROLLER_IP, LOCAL_VIDEO_PORT))
error_count = 0
except TimeoutError:
pass
except OSError as e:
error_count += 1
if error_count <= 3:
logging.error(f"Socket error #{error_count}: {e}")
if error_count >= max_errors:
logging.error("Too many socket errors, stopping stream")
break
frame_count += 1
# Sample exposure every 30 frames for spot meter passthrough.
# Skip if ae_lock active — the lock value is the ground truth.
if frame_count % 30 == 1:
try:
from shared.transforms import load_device_settings
if not load_device_settings("rep8").get("ae_lock"):
_meta = picam2.capture_metadata()
_exp = _meta.get("ExposureTime", 0)
_gain = _meta.get("AnalogueGain", 1.0)
if _exp > 50:
_last_video_exp_us = _exp
_last_video_gain = _gain
logging.info(
f"[AE] Video exposure sampled: exp={_exp}us gain={_gain:.2f} (frame {frame_count})"
)
else:
logging.info(f"[AE] Video exposure still low: exp={_exp}us (frame {frame_count})")
except Exception:
pass
# Log stats every 5 seconds
current_time = time.time()
if current_time - last_log_time >= 5.0:
elapsed = current_time - start_time
fps = frame_count / elapsed if elapsed > 0 else 0
logging.info(f"📊 LOCAL: {fps:.1f} fps, {len(frame_data)} bytes/frame, {frame_count} total frames")
last_log_time = current_time
# Frame rate control removed - allow native 30 FPS
except Exception as e:
error_count += 1
if error_count <= 3:
logging.error(f"Error in video loop #{error_count}: {e}")
if error_count >= max_errors:
logging.error("Too many video loop errors, stopping")
break
time.sleep(0.1)
except Exception as e:
logging.error(f"Critical error in local video streaming: {e}")
finally:
# Cleanup
video_picam2 = None # Clear global reference before closing
if picam2:
try:
picam2.stop()
picam2.close()
logging.info("✓ Local camera stopped and closed")
except Exception as e:
logging.error(f"Error stopping camera: {e}")
if sock:
try:
sock.close()
logging.info("✓ Local socket closed")
except Exception as e:
logging.error(f"Error closing socket: {e}")
with streaming_lock:
streaming = False
# ARCHITECTURAL FIX: Signal that Picamera2 cleanup is complete
# This allows capture_local_still() to safely create new Picamera2 instance
stream_stopped_event.set()
logging.info(f"[EVENT] stream_stopped_event SET at {time.time():.3f} - Picamera2 fully closed")
logging.info("✓ Stream cleanup complete - Picamera2 closed and event signaled")
logging.info("🛑 Local video stream stopped")
def stop_local_video_stream():
"""Properly stop the local video stream"""
global streaming
logging.info("[LOCAL] 🛑 Stopping local video stream...")
with streaming_lock:
streaming = False
time.sleep(0.2) # Reduced from 1.0s - stream stops quickly
logging.info("[LOCAL] ✅ Local video stream stopped")
def capture_local_still():
"""ARCHITECTURAL FIX: Proper thread synchronization for Picamera2 instance management
S3 Speed Optimizations (25 Feb 2026):
- Stream restarts BEFORE transfer (overlaps recovery with upload)
- Timing instrumentation for end-to-end measurement
"""
global streaming
t_total_start = time.time()
logging.info("[LOCAL] Starting still capture protocol (S3 speed optimized)...")
was_streaming = False
# Step 1: Stop video stream to free the camera
# Snapshot current video exposure BEFORE stopping — used for spot meter passthrough
global _last_video_exp_us, _last_video_gain
_snapshot_exp = _last_video_exp_us
_snapshot_gain = _last_video_gain
if _snapshot_exp > 50:
logging.info(f"[AE] Snapshotted video exposure before stop: exp={_snapshot_exp}us gain={_snapshot_gain:.2f}")
with streaming_lock:
was_streaming = streaming
if streaming:
logging.info("[LOCAL] Step 1: Stopping video stream for dedicated still capture...")
streaming = False
stream_stopped_event.clear()
logging.info(f"[EVENT] stream_stopped_event CLEARED at {time.time():.3f}")
if was_streaming:
# ARCHITECTURAL FIX: Wait for Event instead of blind sleep
# This ensures streaming thread has ACTUALLY stopped and closed Picamera2
wait_start = time.time()
logging.info(f"[EVENT-WAIT] Waiting for stream_stopped_event (timeout=10s) at {wait_start:.3f}...")
event_received = stream_stopped_event.wait(timeout=10.0) # 10s max wait
wait_elapsed = time.time() - wait_start
if event_received:
logging.info(f"[EVENT-RESULT] stream_stopped_event RECEIVED in {wait_elapsed:.3f}s - SUCCESS")
logging.info("[LOCAL] ✓ Stream cleanup confirmed via Event - camera freed for high-res capture")
else:
logging.warning(f"[EVENT-RESULT] stream_stopped_event TIMEOUT after {wait_elapsed:.3f}s - FAILED")
logging.warning("[LOCAL] ⚠️ Timeout waiting for stream cleanup - proceeding anyway")
time.sleep(0.5) # Reduced fallback delay from 2.0s
try:
# Step 2: Clear capture complete event before starting capture
capture_complete_event.clear()
logging.info(f"[EVENT] capture_complete_event CLEARED at {time.time():.3f}")
capture_start_time = time.time()
logging.info(f"[TIMING] Capture cycle START at {capture_start_time:.3f}")
# Step 3: Capture dedicated high-resolution still (with RAW support)
raw_enabled = camera_settings.get("raw_enabled", False)
if raw_enabled:
# RAW capture path - uses libcamera-still with --raw flag
logging.info("[LOCAL] RAW capture enabled - using libcamera-still")
capture_result = capture_local_raw()
else:
# Standard JPEG capture
capture_result = capture_local_image_high_resolution(video_exp_us=_snapshot_exp, video_gain=_snapshot_gain)
# S3: Restart video stream BEFORE transfer to overlap recovery with upload
if was_streaming:
logging.info("[LOCAL] S3: Early stream restart (overlapping with transfer)...")
wait_start = time.time()
event_received = capture_complete_event.wait(timeout=5.0)
wait_elapsed = time.time() - wait_start
if event_received:
logging.info(f"[EVENT-RESULT] capture_complete_event RECEIVED in {wait_elapsed:.3f}s")
else:
logging.warning(f"[EVENT-RESULT] capture_complete_event TIMEOUT after {wait_elapsed:.3f}s")
time.sleep(0.3)
threading.Thread(target=start_local_video_stream, daemon=True).start()
logging.info("[LOCAL] Stream restart thread launched (transfer running in parallel)")
stream_already_restarted = True
else:
stream_already_restarted = False
# Now transfer to controller (stream restarting in parallel)
if raw_enabled and capture_result:
jpeg_filename, dng_filename = capture_result
success = send_local_raw(jpeg_filename, dng_filename)
elif capture_result:
success = send_local_image(capture_result)
else:
success = False
if success:
logging.info("[LOCAL] Still capture protocol completed successfully")
result = True
else:
logging.error("[LOCAL] Failed to capture or upload image")
result = False
except Exception as e:
logging.error(f"[LOCAL] Error during still capture protocol: {e}")
result = False
stream_already_restarted = False
# Step 5: Restart video stream if not already done above
if was_streaming and not stream_already_restarted:
logging.info("[LOCAL] Step 5: Preparing to restart video stream...")
# ARCHITECTURAL FIX: Wait for capture Picamera2 to be fully closed
# This ensures no instance conflict when new streaming thread creates Picamera2
wait_start = time.time()
logging.info(f"[EVENT-WAIT] Waiting for capture_complete_event (timeout=5s) at {wait_start:.3f}...")
event_received = capture_complete_event.wait(timeout=5.0) # 5s max wait
wait_elapsed = time.time() - wait_start
if event_received:
logging.info(f"[EVENT-RESULT] capture_complete_event RECEIVED in {wait_elapsed:.3f}s - SUCCESS")
logging.info("[LOCAL] ✓ Capture cleanup confirmed via Event - safe to restart stream")
else:
logging.warning(f"[EVENT-RESULT] capture_complete_event TIMEOUT after {wait_elapsed:.3f}s - FAILED")
logging.warning("[LOCAL] ⚠️ Timeout waiting for capture cleanup - proceeding with caution")
time.sleep(0.3) # Reduced safety delay from 1.0s
logging.info("[LOCAL] Starting new streaming thread...")
# BUGFIX: Don't set streaming=True here - let the thread set its own flag
# This prevents race condition where thread exits immediately due to guard check
threading.Thread(target=start_local_video_stream, daemon=True).start()
capture_cycle_time = time.time() - capture_start_time
logging.info(f"[TIMING] Capture cycle COMPLETE in {capture_cycle_time:.3f}s (fallback restart path)")
logging.info("[LOCAL] ✓ Video stream restarted - protocol complete with proper synchronization")
t_total = time.time() - t_total_start
logging.info(f"[PERF] rep8: STILL CYCLE TOTAL: {t_total * 1000:.0f}ms")
return result
def capture_local_image_high_resolution(video_exp_us: int = 0, video_gain: float = 1.0):
"""FIXED: Capture HIGH RESOLUTION still image (4056x3040 - 4:3) with working transforms
S3 Speed Optimizations (25 Feb 2026):
- Smart settle: 0s when exposure locked, 0.2s for auto (was 0.5s)
- Timing instrumentation for performance tracking
"""
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"/tmp/local_capture_{timestamp}.jpg"
t_start = time.time()
try:
logging.info("[LOCAL] Starting HIGH-RESOLUTION still capture (4056x3040 - 4:3)...")
t_cam_init = time.time()
picam2 = safe_picamera2_init(purpose="still_capture", max_retries=3, retry_delay=1.0)
# Determine exposure to use for still capture.
# Priority: (1) saved ae_lock from spot meter, (2) video stream snapshot passed in,
# (3) auto (no manual exposure).
# CRITICAL: libcamera v0.5.0 — AeEnable not handled. Use ExposureTimeMode=1 instead.
# IMX296 at 4K: capture_metadata() returns near-zero exposure on fresh init regardless
# of scene brightness — cannot read valid exposure from the still instance itself.
apply_exp_us = 0
apply_gain = 1.0
exp_source = None
try:
from shared.transforms import load_device_settings
ae_lock = load_device_settings("rep8").get("ae_lock")
if ae_lock and ae_lock.get("exp_us", 0) > 50:
apply_exp_us = int(ae_lock["exp_us"])
apply_gain = float(ae_lock["gain"])
exp_source = "ae_lock"
logging.info(f"[AE] Using saved ae_lock: exp={apply_exp_us}us gain={apply_gain:.2f}")
elif ae_lock:
logging.warning(f"[AE] Discarding stale ae_lock (exp={ae_lock.get('exp_us')}us < 500us)")
except Exception as _e:
logging.warning(f"[AE] Could not read ae_lock: {_e}")
# Fall back to video snapshot if no saved lock
if apply_exp_us == 0 and video_exp_us > 50:
apply_exp_us = video_exp_us
apply_gain = video_gain
exp_source = "video_snapshot"
logging.info(f"[AE] Using video snapshot: exp={apply_exp_us}us gain={apply_gain:.2f}")
# Build still controls
still_controls = {}
if apply_exp_us > 0:
still_controls["ExposureTime"] = apply_exp_us
still_controls["AnalogueGain"] = apply_gain
still_controls["ExposureTimeMode"] = 1 # Manual — AeEnable not handled on v0.5.0
still_controls["AnalogueGainMode"] = 1
# HIGH RESOLUTION configuration - use 4:3 full sensor
# format="RGB888": gives RGB memory layout (ch0=Red) — consistent with remote nodes
# and video stream. apply_unified_transforms_for_still(already_bgr=False) then
# applies the correct cvtColor(RGB->BGR) before imencode.
still_config = picam2.create_still_configuration(
main={"size": (4056, 3040), "format": "RGB888"}, controls=still_controls if still_controls else {}
)
picam2.configure(still_config)
picam2.start()
t_cam_ready = time.time()
logging.info(f"[PERF] rep8: Camera init+start: {(t_cam_ready - t_cam_init) * 1000:.0f}ms")
# Settle time
if apply_exp_us > 0:
time.sleep(0.05) # Minimal — exposure is deterministic
logging.info(f"[AE] ✅ Manual exposure active ({exp_source}): exp={apply_exp_us}us gain={apply_gain:.2f}")
else:
has_exposure_lock = not camera_settings.get("iso_auto", True)
if has_exposure_lock:
time.sleep(0.05)
logging.info("[LOCAL] Exposure locked - minimal settle (50ms)")
else:
time.sleep(0.2)
logging.info("[LOCAL] Auto-exposure settle (200ms)")
# Capture image (RGB format from Picamera2)
logging.info("[LOCAL] Capturing HIGH-RESOLUTION image...")
image_rgb = picam2.capture_array()
t_capture_done = time.time()
# Save lock from video snapshot only if ae_region pending and no lock yet.
# (Primary locking now done immediately in _local_ae_meter_and_lock via pixel metering.)
try:
from shared.transforms import load_device_settings, save_device_settings
s = load_device_settings("rep8")
if s.get("ae_region") and not s.get("ae_lock") and apply_exp_us > 50:
# Fallback: lock from video snapshot if pixel metering didn't fire
s["ae_lock"] = {"exp_us": apply_exp_us, "gain": apply_gain}
save_device_settings("rep8", s)
_local_ae_notify("locked", apply_exp_us, apply_gain)
logging.info(f"[AE] ✅ Fallback lock from video snapshot: exp={apply_exp_us}us gain={apply_gain:.2f}")
threading.Thread(target=restart_local_stream, daemon=True).start()
except Exception as _e:
logging.debug(f"[AE] Could not save fallback lock: {_e}")
# Apply unified transforms for still capture (proper BGR format for saving)
logging.info("[LOCAL] Applying unified still transforms...")
logging.info("[COLOUR] rep8: still path — format=RGB888 + already_bgr=False (16eb972)")
try:
from shared.transforms import apply_unified_transforms_for_still
image_bgr_transformed = apply_unified_transforms_for_still(image_rgb, "rep8")
logging.info("[LOCAL] ✅ Unified still transforms applied")
except Exception as e:
logging.error(f"[LOCAL] Unified still transforms failed: {e}, using fallback")
# Fallback: apply video transforms then convert to BGR
image_rgb_transformed = apply_safe_transforms(image_rgb)
if len(image_rgb_transformed.shape) == 3 and image_rgb_transformed.shape[2] == 3:
image_bgr_transformed = cv2.cvtColor(image_rgb_transformed, cv2.COLOR_RGB2BGR)
else:
image_bgr_transformed = image_rgb_transformed
logging.info("[LOCAL] ✅ Transforms applied - still should match video preview")
# Apply lens corrections (distortion, vignetting, CA)
try:
from shared.lens_correction import apply_lens_correction
image_bgr_transformed = apply_lens_correction(image_bgr_transformed, "rep8")
except ImportError:
logging.debug("[LOCAL] Lens correction module not available")
except Exception as e:
logging.warning(f"[LOCAL] Lens correction failed (using uncorrected): {e}")
# S3: Encode to memory for zero-copy transfer (same as remote nodes)
_still_quality = camera_settings.get("jpeg_quality", 95)
encode_params = [cv2.IMWRITE_JPEG_QUALITY, _still_quality]
success, encoded = cv2.imencode(".jpg", image_bgr_transformed, encode_params)
_jpeg_bytes = encoded.tobytes() if success else None
# Clean up camera
picam2.stop()
picam2.close()
# ARCHITECTURAL FIX: Signal that capture Picamera2 is closed
# This allows streaming to safely restart without instance conflict
capture_complete_event.set()
logging.info(f"[EVENT] capture_complete_event SET at {time.time():.3f} - Picamera2 closed")
logging.info("[LOCAL] ✓ Capture Picamera2 closed - signaled completion")
if success and _jpeg_bytes is not None:
file_size = len(_jpeg_bytes)
# Write to temp file for transfer (local uses temp files)
with open(filename, "wb") as f:
f.write(_jpeg_bytes)
t_total = time.time() - t_start
logging.info(f"[LOCAL] ✅ HIGH-RES CAPTURED: {filename} ({file_size} bytes, resolution: 4056x3040)")
logging.info(
f"[PERF] rep8: CAPTURE TOTAL: {t_total * 1000:.0f}ms "
f"(init={(t_cam_ready - t_cam_init) * 1000:.0f} "
f"capture={(t_capture_done - t_cam_ready) * 1000:.0f} "
f"save={(time.time() - t_capture_done) * 1000:.0f}ms)"
)
# Verify this is actually high resolution
if file_size > 1000000: # Should be >1MB for high-res
logging.info("[LOCAL] ✅ Confirmed high-resolution capture (>1MB)")
else:
logging.warning(f"[LOCAL] ⚠️ File size seems small for high-res: {file_size} bytes")
return filename
else:
logging.error("[LOCAL] ❌ Failed to save captured image")