Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,37 @@
|
|
| 1 |
-
from fastapi import FastAPI,
|
| 2 |
-
from ultralytics import YOLO
|
| 3 |
-
from
|
| 4 |
-
import io
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
app = FastAPI()
|
| 8 |
-
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
model = YOLO(
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
@app.
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
app = FastAPI(title="YOLO API - Football Model")
|
| 8 |
+
|
| 9 |
+
# Load model from local file
|
| 10 |
+
try:
|
| 11 |
+
model = YOLO("best.pt")
|
| 12 |
+
print("✅ Loaded best.pt model")
|
| 13 |
+
except Exception as e:
|
| 14 |
+
print("⚠️ best.pt not found, trying last.pt:", e)
|
| 15 |
+
model = YOLO("last.pt")
|
| 16 |
+
|
| 17 |
+
@app.get("/")
|
| 18 |
+
def home():
|
| 19 |
+
return {"message": "YOLO Football Model API is running!"}
|
| 20 |
+
|
| 21 |
+
@app.post("/predict")
|
| 22 |
+
async def predict(file: UploadFile = File(...)):
|
| 23 |
+
contents = await file.read()
|
| 24 |
+
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 25 |
+
|
| 26 |
+
# Run inference
|
| 27 |
+
results = model(image)
|
| 28 |
+
|
| 29 |
+
detections = []
|
| 30 |
+
for box in results[0].boxes:
|
| 31 |
+
detections.append({
|
| 32 |
+
"class": int(box.cls),
|
| 33 |
+
"confidence": float(box.conf),
|
| 34 |
+
"bbox": box.xyxy[0].tolist()
|
| 35 |
+
})
|
| 36 |
+
|
| 37 |
+
return {"detections": detections}
|