feat: add session image retrieval endpoint

Add HTTP endpoint to retrieve saved session images by session ID.
Images are saved during car_fueling progression stage.

- Add GET /session-image/{session_id} endpoint
- Search images directory for files matching session ID pattern
- Return most recent image if multiple exist
- Proper error handling (404 for not found, 500 for errors)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Siwat Sirichai 2025-10-01 01:27:12 +07:00
parent 402f7732a8
commit b2e7bc499d

57
app.py
View file

@ -302,6 +302,63 @@ async def get_camera_image(camera_id: str):
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
@app.get("/session-image/{session_id}")
async def get_session_image(session_id: int):
"""
HTTP endpoint to retrieve the saved session image by session ID.
Args:
session_id: The session ID to retrieve the image for
Returns:
JPEG image as binary response
Raises:
HTTPException: 404 if no image found for the session
HTTPException: 500 if reading image fails
"""
try:
from pathlib import Path
import glob
# Images directory
images_dir = Path("images")
if not images_dir.exists():
logger.warning(f"Images directory does not exist")
raise HTTPException(
status_code=404,
detail=f"No images directory found"
)
# Search for files matching session ID pattern: {session_id}_*
pattern = str(images_dir / f"{session_id}_*.jpg")
matching_files = glob.glob(pattern)
if not matching_files:
logger.warning(f"No image found for session {session_id}")
raise HTTPException(
status_code=404,
detail=f"No image found for session {session_id}"
)
# Get the most recent file if multiple exist
most_recent_file = max(matching_files, key=os.path.getmtime)
logger.info(f"Found session image for session {session_id}: {most_recent_file}")
# Read the image file
image_data = open(most_recent_file, 'rb').read()
# Return image as binary response
return Response(content=image_data, media_type="image/jpeg")
except HTTPException:
raise
except Exception as e:
logger.error(f"Error retrieving session image for session {session_id}: {str(e)}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
@app.get("/health")
async def health_check():
"""Health check endpoint for monitoring."""