54 lines
1.8 KiB
Python
54 lines
1.8 KiB
Python
from fastapi import FastAPI, HTTPException, Query
|
|
from fastapi.responses import FileResponse, JSONResponse
|
|
import os
|
|
import traceback
|
|
from processor import process_video_to_panorama
|
|
|
|
app = FastAPI(title="Video Density Estimation API")
|
|
|
|
@app.get("/process")
|
|
async def process_video(
|
|
input_video: str = Query(..., description="Path to input video file"),
|
|
output_dir: str = Query(..., description="Output directory path")
|
|
):
|
|
"""
|
|
Process video to generate density estimation result
|
|
|
|
Args:
|
|
input_video: Path to input video file (e.g., /path/to/video.mp4)
|
|
output_dir: Output directory path (e.g., /path/to/output)
|
|
|
|
Returns:
|
|
JSON with result path or error message
|
|
"""
|
|
try:
|
|
# Validate input video
|
|
if not os.path.exists(input_video):
|
|
raise HTTPException(status_code=400, detail=f"Input video not found: {input_video}")
|
|
|
|
# Process video
|
|
result_path,total_count = process_video_to_panorama(input_video, output_dir)
|
|
|
|
return JSONResponse({
|
|
"status": "success",
|
|
"result_path": result_path,
|
|
"total_count": int(total_count),
|
|
"message": "Processing completed successfully"
|
|
})
|
|
|
|
except Exception as e:
|
|
error_msg = f"Processing failed: {str(e)}"
|
|
print(f"Error traceback:\n{traceback.format_exc()}")
|
|
raise HTTPException(status_code=500, detail=error_msg)
|
|
|
|
@app.get("/result")
|
|
async def get_result(result_path: str = Query(..., description="Path to result image")):
|
|
"""Download result image"""
|
|
if not os.path.exists(result_path):
|
|
raise HTTPException(status_code=404, detail="Result image not found")
|
|
return FileResponse(result_path)
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|