aiengrimran's picture
add api
4630e04
raw
history blame contribute delete
644 Bytes
from fastapi import FastAPI,UploadFile,File
import numpy as np
from ultralytics import YOLO
import cv2
app = FastAPI()
model = YOLO("models/yolo11n-cls.pt")
@app.get("/")
def home():
return {"message", "YOLO FastAPI Server Running"}
@app.post("/classify")
async def classify_image(file:UploadFile = File(...)):
contents = await file.read()
nparr = np.frombuffer(contents, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
results = model(img)
return {
"detect image in class":results[0].names[results[0].probs.top1],
"confidence":f"{results[0].probs.top1conf.item():.2f}"
}