Itoshi321/road_damage
from fastapi import FastAPI, UploadFile, File from ultralytics import YOLO import cv2 import numpy as np import shutil from fastapi.responses import JSONResponse import os
app = FastAPI()
Load the trained model
MODELPATH = "potholebest.pt" model = YOLO(MODEL_PATH)
@app.post("/predict/") async def predict(file: UploadFile = File(...)): try: # Save uploaded file filepath = f"temp{file.filename}" with open(file_path, "wb") as buffer: shutil.copyfileobj(file.file, buffer)
# Read video cap = cv2.VideoCapture(filepath) frameresults = []
while cap.isOpened(): ret, frame = cap.read() if not ret: break
results = model(frame) # Run YOLO inference detections = results.pandas().xyxy[0].todict(orient="records") frameresults.append(detections)
cap.release() os.remove(file_path)
return JSONResponse(content={"detections": frameresults}) except Exception as e: return JSONResponse(content={"error": str(e)}, statuscode=500)
