fix: camera api endpoint
This commit is contained in:
parent
83aaf95f59
commit
519e073f7f
5 changed files with 69 additions and 68 deletions
56
app.py
56
app.py
|
@ -6,8 +6,9 @@ import json
|
|||
import logging
|
||||
import os
|
||||
import time
|
||||
import cv2
|
||||
from contextlib import asynccontextmanager
|
||||
from fastapi import FastAPI, WebSocket, HTTPException, Request
|
||||
from fastapi import FastAPI, WebSocket, HTTPException
|
||||
from fastapi.responses import Response
|
||||
|
||||
# Import new modular communication system
|
||||
|
@ -27,8 +28,8 @@ logging.basicConfig(
|
|||
logger = logging.getLogger("detector_worker")
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
# Store cached frames for REST API access (temporary storage)
|
||||
latest_frames = {}
|
||||
# Frames are now stored in the shared cache buffer from core.streaming.buffers
|
||||
# latest_frames = {} # Deprecated - using shared_cache_buffer instead
|
||||
|
||||
# Lifespan event handler (modern FastAPI approach)
|
||||
@asynccontextmanager
|
||||
|
@ -49,7 +50,7 @@ async def lifespan(app: FastAPI):
|
|||
worker_state.set_subscriptions([])
|
||||
worker_state.session_ids.clear()
|
||||
worker_state.progression_stages.clear()
|
||||
latest_frames.clear()
|
||||
# latest_frames.clear() # No longer needed - frames are in shared_cache_buffer
|
||||
logger.info("Detector Worker shutdown complete")
|
||||
|
||||
# Create FastAPI application with detailed WebSocket logging
|
||||
|
@ -90,8 +91,8 @@ from core.streaming import initialize_stream_manager
|
|||
initialize_stream_manager(max_streams=config.get('max_streams', 10))
|
||||
logger.info(f"Initialized stream manager with max_streams={config.get('max_streams', 10)}")
|
||||
|
||||
# Store cached frames for REST API access (temporary storage)
|
||||
latest_frames = {}
|
||||
# Frames are now stored in the shared cache buffer from core.streaming.buffers
|
||||
# latest_frames = {} # Deprecated - using shared_cache_buffer instead
|
||||
|
||||
logger.info("Starting detector worker application (refactored)")
|
||||
logger.info(f"Configuration: Target FPS: {config.get('target_fps', 10)}, "
|
||||
|
@ -150,31 +151,36 @@ async def get_camera_image(camera_id: str):
|
|||
detail=f"Camera {camera_id} not found or not active"
|
||||
)
|
||||
|
||||
# Check if we have a cached frame for this camera
|
||||
if camera_id not in latest_frames:
|
||||
logger.warning(f"No cached frame available for camera '{camera_id}'")
|
||||
# Extract actual camera_id from subscription identifier (displayId;cameraId)
|
||||
# Frames are stored using just the camera_id part
|
||||
actual_camera_id = camera_id.split(';')[-1] if ';' in camera_id else camera_id
|
||||
|
||||
# Get frame from the shared cache buffer
|
||||
from core.streaming.buffers import shared_cache_buffer
|
||||
|
||||
# Debug: Log available cameras in buffer
|
||||
available_cameras = shared_cache_buffer.frame_buffer.get_camera_list()
|
||||
logger.debug(f"Available cameras in buffer: {available_cameras}")
|
||||
logger.debug(f"Looking for camera: '{actual_camera_id}'")
|
||||
|
||||
frame = shared_cache_buffer.get_frame(actual_camera_id)
|
||||
if frame is None:
|
||||
logger.warning(f"No cached frame available for camera '{actual_camera_id}' (from subscription '{camera_id}')")
|
||||
logger.warning(f"Available cameras in buffer: {available_cameras}")
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"No frame available for camera {camera_id}"
|
||||
detail=f"No frame available for camera {actual_camera_id}"
|
||||
)
|
||||
|
||||
frame = latest_frames[camera_id]
|
||||
logger.debug(f"Retrieved cached frame for camera '{camera_id}', shape: {frame.shape}")
|
||||
logger.debug(f"Retrieved cached frame for camera '{actual_camera_id}' (from subscription '{camera_id}'), shape: {frame.shape}")
|
||||
|
||||
# TODO: This import will be replaced in Phase 3 (Streaming System)
|
||||
# For now, we need to handle the case where OpenCV is not available
|
||||
try:
|
||||
import cv2
|
||||
# Encode frame as JPEG
|
||||
success, buffer_img = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 85])
|
||||
if not success:
|
||||
raise HTTPException(status_code=500, detail="Failed to encode image as JPEG")
|
||||
# Encode frame as JPEG
|
||||
success, buffer_img = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 85])
|
||||
if not success:
|
||||
raise HTTPException(status_code=500, detail="Failed to encode image as JPEG")
|
||||
|
||||
# Return image as binary response
|
||||
return Response(content=buffer_img.tobytes(), media_type="image/jpeg")
|
||||
except ImportError:
|
||||
logger.error("OpenCV not available for image encoding")
|
||||
raise HTTPException(status_code=500, detail="Image processing not available")
|
||||
# Return image as binary response
|
||||
return Response(content=buffer_img.tobytes(), media_type="image/jpeg")
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue