Spaces:
Sleeping
Sleeping
atef commited on
Commit ·
7534e3c
0
Parent(s):
detect api yolov8s
Browse files- Dockerfile +10 -0
- README.md +10 -0
- main.py +31 -0
- requirements.txt +4 -0
Dockerfile
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
RUN apt-get update && apt-get install -y libgl1 libglib2.0-0 && rm -rf /var/lib/apt/lists/*
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
ENV YOLO_CONFIG_DIR=/tmp/Ultralytics MPLCONFIGDIR=/tmp/mpl HF_HOME=/tmp/hf
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
COPY . .
|
| 8 |
+
RUN python -c "from ultralytics import YOLO; YOLO('yolov8s.pt')"
|
| 9 |
+
EXPOSE 7860
|
| 10 |
+
CMD ["uvicorn","main:app","--host","0.0.0.0","--port","7860"]
|
README.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: SCA Detect
|
| 3 |
+
emoji: 🚤
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: gray
|
| 6 |
+
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
+
pinned: false
|
| 9 |
+
---
|
| 10 |
+
Détection d'objets maritimes (YOLOv8s) pour Hacktogone × Toud Gward. Endpoint POST /detect (image binaire) → JSON.
|
main.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
from fastapi import FastAPI, Request
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from fastapi.responses import JSONResponse
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from ultralytics import YOLO
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
| 10 |
+
model = YOLO("yolov8s.pt")
|
| 11 |
+
KEEP = {"boat": "SHIP", "person": "HUMAN"}
|
| 12 |
+
|
| 13 |
+
@app.get("/")
|
| 14 |
+
def root():
|
| 15 |
+
return {"ok": True, "model": "yolov8s"}
|
| 16 |
+
|
| 17 |
+
@app.post("/detect")
|
| 18 |
+
async def detect(req: Request):
|
| 19 |
+
data = await req.body()
|
| 20 |
+
img = Image.open(io.BytesIO(data)).convert("RGB")
|
| 21 |
+
W, H = img.size
|
| 22 |
+
r = model.predict(img, conf=0.35, imgsz=640, verbose=False)[0]
|
| 23 |
+
dets = []
|
| 24 |
+
for b in r.boxes:
|
| 25 |
+
c = model.names[int(b.cls)]
|
| 26 |
+
if c not in KEEP:
|
| 27 |
+
continue
|
| 28 |
+
x1, y1, x2, y2 = [float(v) for v in b.xyxy[0]]
|
| 29 |
+
dets.append({"label": KEEP[c], "score": round(float(b.conf), 3),
|
| 30 |
+
"box": {"xmin": x1, "ymin": y1, "xmax": x2, "ymax": y2}})
|
| 31 |
+
return JSONResponse({"w": W, "h": H, "dets": dets})
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
ultralytics
|
| 4 |
+
pillow
|