Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,47 @@
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
-
from fastapi.responses import StreamingResponse
|
| 4 |
from ultralytics import YOLO
|
| 5 |
-
|
| 6 |
-
import
|
| 7 |
|
| 8 |
-
app = FastAPI(
|
| 9 |
|
| 10 |
-
# Load
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
print("✅ Loaded best.pt")
|
| 14 |
-
except:
|
| 15 |
-
model = YOLO("last.pt")
|
| 16 |
-
print("⚠️ Loaded last.pt")
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
return {"message": "YOLO Football Model API is running!"}
|
| 21 |
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
contents = await file.read()
|
| 26 |
-
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 27 |
-
|
| 28 |
-
# Run inference
|
| 29 |
-
results = model(image)
|
| 30 |
-
|
| 31 |
-
# Get JSON detections
|
| 32 |
detections = []
|
| 33 |
-
for
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
return
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File
|
| 2 |
from fastapi.responses import JSONResponse
|
|
|
|
| 3 |
from ultralytics import YOLO
|
| 4 |
+
import os
|
| 5 |
+
import shutil
|
| 6 |
|
| 7 |
+
app = FastAPI()
|
| 8 |
|
| 9 |
+
# ✅ Load both YOLO models
|
| 10 |
+
FIELD_MODEL_PATH = "models/field/best.pt"
|
| 11 |
+
PLAYER_MODEL_PATH = "models/player/last.pt"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
field_model = YOLO(FIELD_MODEL_PATH)
|
| 14 |
+
player_model = YOLO(PLAYER_MODEL_PATH)
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
+
def run_detection(model, file_path):
|
| 18 |
+
results = model(file_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
detections = []
|
| 20 |
+
for r in results:
|
| 21 |
+
for box in r.boxes:
|
| 22 |
+
detections.append({
|
| 23 |
+
"class": int(box.cls),
|
| 24 |
+
"confidence": float(box.conf),
|
| 25 |
+
"bbox": box.xyxy[0].tolist()
|
| 26 |
+
})
|
| 27 |
+
return detections
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
@app.post("/predict/field")
|
| 31 |
+
async def predict_field(file: UploadFile = File(...)):
|
| 32 |
+
temp_path = f"/tmp/{file.filename}"
|
| 33 |
+
with open(temp_path, "wb") as buffer:
|
| 34 |
+
shutil.copyfileobj(file.file, buffer)
|
| 35 |
+
detections = run_detection(field_model, temp_path)
|
| 36 |
+
os.remove(temp_path)
|
| 37 |
+
return JSONResponse({"model": "field", "detections": detections})
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
@app.post("/predict/player")
|
| 41 |
+
async def predict_player(file: UploadFile = File(...)):
|
| 42 |
+
temp_path = f"/tmp/{file.filename}"
|
| 43 |
+
with open(temp_path, "wb") as buffer:
|
| 44 |
+
shutil.copyfileobj(file.file, buffer)
|
| 45 |
+
detections = run_detection(player_model, temp_path)
|
| 46 |
+
os.remove(temp_path)
|
| 47 |
+
return JSONResponse({"model": "player", "detections": detections})
|