refactor: change temporary file format from JPG to PPM for improved frame reading
All checks were successful
Build Worker Base and Application Images / check-base-changes (push) Successful in 8s
Build Worker Base and Application Images / build-base (push) Has been skipped
Build Worker Base and Application Images / build-docker (push) Successful in 2m52s
Build Worker Base and Application Images / deploy-stack (push) Successful in 13s

This commit is contained in:
Siwat Sirichai 2025-09-26 02:55:26 +07:00
parent 73c3367681
commit fe0da18d0f

View file

@ -94,8 +94,8 @@ class FFmpegRTSPReader:
self.temp_file = f"/tmp/claude/camera_{self.camera_id.replace(' ', '_')}.raw"
os.makedirs("/tmp/claude", exist_ok=True)
# Change to JPG format which properly supports -update 1
self.temp_file = f"/tmp/claude/camera_{self.camera_id.replace(' ', '_')}.jpg"
# Use PPM format - uncompressed with header, supports -update 1
self.temp_file = f"/tmp/claude/camera_{self.camera_id.replace(' ', '_')}.ppm"
cmd = [
'ffmpeg',
@ -104,8 +104,8 @@ class FFmpegRTSPReader:
'-rtsp_transport', 'tcp',
'-i', self.rtsp_url,
'-f', 'image2',
'-update', '1', # This actually works with image2 format
'-q:v', '2', # High quality JPEG
'-update', '1', # Works with image2 format
'-pix_fmt', 'rgb24', # PPM uses RGB not BGR
'-an', # No audio
'-y', # Overwrite output file
self.temp_file
@ -176,26 +176,13 @@ class FFmpegRTSPReader:
if self.frame_ready_event.wait(timeout=restart_check_interval):
self.frame_ready_event.clear()
# Read JPEG frame with concurrency safety
# Read PPM frame (uncompressed with header)
try:
# Try to read JPEG multiple times to handle race conditions
frame = None
for attempt in range(3):
try:
# Read and decode JPEG directly
if os.path.exists(self.temp_file):
# Read PPM with OpenCV (handles RGB->BGR conversion automatically)
frame = cv2.imread(self.temp_file)
if frame is not None and frame.shape == (self.height, self.width, 3):
break
else:
logger.debug(f"Camera {self.camera_id}: Invalid frame shape or None, attempt {attempt+1}")
time.sleep(0.01) # Brief wait before retry
except (IOError, OSError) as e:
logger.debug(f"Camera {self.camera_id}: Read error on attempt {attempt+1}: {e}")
time.sleep(0.01)
if frame is not None:
# Call frame callback directly
if self.frame_callback:
self.frame_callback(self.camera_id, frame)
@ -205,10 +192,12 @@ class FFmpegRTSPReader:
# Log progress
current_time = time.time()
if current_time - last_log_time >= 30:
logger.info(f"Camera {self.camera_id}: {frame_count} JPEG frames processed reactively")
logger.info(f"Camera {self.camera_id}: {frame_count} PPM frames processed reactively")
last_log_time = current_time
else:
logger.debug(f"Camera {self.camera_id}: Failed to read valid JPEG after retries")
logger.debug(f"Camera {self.camera_id}: Invalid PPM frame")
else:
logger.debug(f"Camera {self.camera_id}: PPM file not found yet")
except (IOError, OSError) as e:
logger.debug(f"Camera {self.camera_id}: File read error: {e}")