Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, UploadFile, File | |
| from fastapi.middleware.cors import CORSMiddleware | |
| import numpy as np | |
| import cv2 | |
| import shutil | |
| import os | |
| import uuid | |
| from utils import process_image, process_video_stream | |
| app = FastAPI(title="DeepSight Lite API") | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| async def predict_image(file: UploadFile = File(...)): | |
| contents = await file.read() | |
| nparr = np.frombuffer(contents, np.uint8) | |
| img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) | |
| results = process_image(img) | |
| return {"status": "success", "data": results} | |
| async def predict_video(file: UploadFile = File(...)): | |
| temp_path = f"temp_{uuid.uuid4()}.mp4" | |
| with open(temp_path, "wb") as buffer: | |
| shutil.copyfileobj(file.file, buffer) | |
| try: | |
| detected_plates = process_video_stream(temp_path) | |
| return {"status": "success", "plates": detected_plates} | |
| finally: | |
| if os.path.exists(temp_path): | |
| os.remove(temp_path) | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=7860) |