Commit changes before merging to main

This commit is contained in:
salirezav
2025-12-18 14:33:05 -05:00
parent 9159ab68f3
commit 6cf67822dc
20 changed files with 3571 additions and 1311 deletions

View File

@@ -459,6 +459,13 @@ class CameraRecorder:
with self._lock:
if self.recording:
self.logger.warning("Already recording!")
# #region agent log
import json
try:
with open('/home/alireza/Desktop/USDA-VISION/.cursor/debug.log', 'a') as f:
f.write(json.dumps({"location": "recorder.py:460", "message": "start_recording: already recording check", "data": {"camera": self.camera_config.name, "recording_flag": self.recording, "thread_alive": self._recording_thread.is_alive() if self._recording_thread else False}, "timestamp": time.time(), "sessionId": "debug-session", "runId": "run1", "hypothesisId": "G"}) + "\n")
except: pass
# #endregion
return False
# Check if streamer is active - if so, we can share frames without opening a new camera connection
@@ -503,6 +510,13 @@ class CameraRecorder:
# Update state
self.recording = True
# #region agent log
import json
try:
with open('/home/alireza/Desktop/USDA-VISION/.cursor/debug.log', 'a') as f:
f.write(json.dumps({"location": "recorder.py:505", "message": "start_recording: setting recording=True", "data": {"camera": self.camera_config.name, "filename": filename}, "timestamp": time.time(), "sessionId": "debug-session", "runId": "run1", "hypothesisId": "A"}) + "\n")
except: pass
# #endregion
recording_id = self.state_manager.start_recording(self.camera_config.name, output_path)
# Publish event
@@ -567,6 +581,13 @@ class CameraRecorder:
with self._lock:
# Update state
self.recording = False
# #region agent log
import json
try:
with open('/home/alireza/Desktop/USDA-VISION/.cursor/debug.log', 'a') as f:
f.write(json.dumps({"location": "recorder.py:569", "message": "stop_recording: setting recording=False", "data": {"camera": self.camera_config.name}, "timestamp": time.time(), "sessionId": "debug-session", "runId": "run1", "hypothesisId": "F"}) + "\n")
except: pass
# #endregion
# Calculate duration and file size
duration = 0
@@ -620,9 +641,23 @@ class CameraRecorder:
while initial_frame is None and time.time() - timeout_start < 5.0:
if self._stop_recording_event.is_set():
self.logger.error("Stop event set before getting initial frame")
# #region agent log
import json
try:
with open('/home/alireza/Desktop/USDA-VISION/.cursor/debug.log', 'a') as f:
f.write(json.dumps({"location": "recorder.py:623", "message": "Early return: stop event set", "data": {"camera": self.camera_config.name, "recording_flag": self.recording}, "timestamp": time.time(), "sessionId": "debug-session", "runId": "run1", "hypothesisId": "A"}) + "\n")
except: pass
# #endregion
return
if not self.streamer.streaming:
self.logger.error("Streamer stopped before getting initial frame")
# #region agent log
import json
try:
with open('/home/alireza/Desktop/USDA-VISION/.cursor/debug.log', 'a') as f:
f.write(json.dumps({"location": "recorder.py:625", "message": "Early return: streamer stopped", "data": {"camera": self.camera_config.name, "recording_flag": self.recording}, "timestamp": time.time(), "sessionId": "debug-session", "runId": "run1", "hypothesisId": "B"}) + "\n")
except: pass
# #endregion
return
try:
initial_frame = self.streamer._recording_frame_queue.get(timeout=0.5)
@@ -632,11 +667,25 @@ class CameraRecorder:
if initial_frame is None:
self.logger.error("Failed to get initial frame from streamer for video writer initialization")
# #region agent log
import json
try:
with open('/home/alireza/Desktop/USDA-VISION/.cursor/debug.log', 'a') as f:
f.write(json.dumps({"location": "recorder.py:634", "message": "Early return: failed to get initial frame", "data": {"camera": self.camera_config.name, "recording_flag": self.recording}, "timestamp": time.time(), "sessionId": "debug-session", "runId": "run1", "hypothesisId": "C"}) + "\n")
except: pass
# #endregion
return
# Initialize video writer (with initial frame dimensions if using streamer frames)
if not self._initialize_video_writer(use_streamer_frames=use_streamer_frames, initial_frame=initial_frame):
self.logger.error("Failed to initialize video writer")
# #region agent log
import json
try:
with open('/home/alireza/Desktop/USDA-VISION/.cursor/debug.log', 'a') as f:
f.write(json.dumps({"location": "recorder.py:640", "message": "Early return: failed to initialize video writer", "data": {"camera": self.camera_config.name, "recording_flag": self.recording}, "timestamp": time.time(), "sessionId": "debug-session", "runId": "run1", "hypothesisId": "D"}) + "\n")
except: pass
# #endregion
return
self.logger.info(f"Recording loop started (using {'streamer frames' if use_streamer_frames else 'direct capture'})")
@@ -734,8 +783,26 @@ class CameraRecorder:
finally:
self.logger.info("Cleaning up recording resources...")
self._cleanup_recording()
# Note: Don't set self.recording = False here - let stop_recording() handle it
# to avoid race conditions where stop_recording thinks recording already stopped
# #region agent log
import json
try:
with open('/home/alireza/Desktop/USDA-VISION/.cursor/debug.log', 'a') as f:
f.write(json.dumps({"location": "recorder.py:734", "message": "Finally block: before setting recording flag", "data": {"camera": self.camera_config.name, "recording_flag_before": self.recording, "stop_event_set": self._stop_recording_event.is_set()}, "timestamp": time.time(), "sessionId": "debug-session", "runId": "post-fix", "hypothesisId": "E"}) + "\n")
except: pass
# #endregion
# Reset recording flag if thread exits early (due to error) or if stop_recording wasn't called
# This prevents the flag from staying True when the thread exits early
# Using lock to ensure thread safety - if stop_recording() already set it to False, this is harmless
with self._lock:
if self.recording:
self.logger.warning("Recording thread exited but flag was still True - resetting to False")
self.recording = False
# #region agent log
try:
with open('/home/alireza/Desktop/USDA-VISION/.cursor/debug.log', 'a') as f:
f.write(json.dumps({"location": "recorder.py:745", "message": "Finally block: after setting recording flag", "data": {"camera": self.camera_config.name, "recording_flag_after": self.recording}, "timestamp": time.time(), "sessionId": "debug-session", "runId": "post-fix", "hypothesisId": "E"}) + "\n")
except: pass
# #endregion
def _initialize_video_writer(self, use_streamer_frames: bool = False, initial_frame: Optional[np.ndarray] = None) -> bool:
"""Initialize OpenCV video writer
@@ -768,15 +835,27 @@ class CameraRecorder:
self.logger.info(f"Got frame dimensions from streamer's camera: {frame_size}")
except Exception as e:
self.logger.error(f"Failed to get frame dimensions from streamer camera: {e}")
# Use camera config defaults as last resort
camera_config = self.camera_config
frame_size = (camera_config.resolution_width or 1280, camera_config.resolution_height or 1024)
self.logger.warning(f"Using default frame size from config: {frame_size}")
# #region agent log
import json
try:
with open('/home/alireza/Desktop/USDA-VISION/.cursor/debug.log', 'a') as f:
f.write(json.dumps({"location": "recorder.py:837", "message": "Fallback path triggered: failed to get dimensions from streamer camera", "data": {"camera": self.camera_config.name, "error": str(e)}, "timestamp": time.time(), "sessionId": "debug-session", "runId": "run1", "hypothesisId": "A"}) + "\n")
except: pass
# #endregion
# Use hardcoded defaults as last resort (CameraConfig doesn't have resolution_width/height fields)
frame_size = (1280, 1024)
self.logger.warning(f"Using hardcoded default frame size: {frame_size}")
else:
# Use camera config defaults as last resort
camera_config = self.camera_config
frame_size = (camera_config.resolution_width or 1280, camera_config.resolution_height or 1024)
self.logger.warning(f"Using default frame size from config: {frame_size}")
# #region agent log
import json
try:
with open('/home/alireza/Desktop/USDA-VISION/.cursor/debug.log', 'a') as f:
f.write(json.dumps({"location": "recorder.py:842", "message": "Fallback path triggered: no camera handle available", "data": {"camera": self.camera_config.name, "use_streamer_frames": use_streamer_frames, "has_streamer": self.streamer is not None, "has_streamer_camera": self.streamer.hCamera is not None if self.streamer else False}, "timestamp": time.time(), "sessionId": "debug-session", "runId": "run1", "hypothesisId": "B"}) + "\n")
except: pass
# #endregion
# Use hardcoded defaults as last resort (CameraConfig doesn't have resolution_width/height fields)
frame_size = (1280, 1024)
self.logger.warning(f"Using hardcoded default frame size: {frame_size}")
# Set up video writer with configured codec
fourcc = cv2.VideoWriter_fourcc(*self.camera_config.video_codec)