Spaces:
Sleeping
Sleeping
File size: 1,196 Bytes
36e7618 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | 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=["*"],
)
@app.post("/predict-image")
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}
@app.post("/predict-video")
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) |