Spaces:
Sleeping
Sleeping
feat: init fastapi
Browse files- app.py +47 -0
- best.pt +3 -0
- dockerfile +12 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, status
|
| 2 |
+
from fastapi.responses import StreamingResponse, JSONResponse
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
app.add_middleware(
|
| 11 |
+
CORSMiddleware,
|
| 12 |
+
allow_origins=["*"],
|
| 13 |
+
allow_credentials=True,
|
| 14 |
+
allow_methods=["POST"],
|
| 15 |
+
allow_headers=["content-type", "accept"],
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
model = YOLO("best.pt")
|
| 19 |
+
|
| 20 |
+
@app.post("/inference")
|
| 21 |
+
async def inference(file: UploadFile):
|
| 22 |
+
if file.content_type != "image/jpeg" and file.content_type != "image/png" and file.content_type != "image/jpg":
|
| 23 |
+
return JSONResponse(
|
| 24 |
+
status_code=status.HTTP_400_BAD_REQUEST,
|
| 25 |
+
content={"error": "Invalid file format"}
|
| 26 |
+
)
|
| 27 |
+
image_bytes = await file.read()
|
| 28 |
+
img = np.frombuffer(image_bytes, dtype=np.uint8)
|
| 29 |
+
img = cv2.imdecode(img, cv2.IMREAD_COLOR)
|
| 30 |
+
results = model.predict(source=img, conf=0.5)
|
| 31 |
+
for r in results:
|
| 32 |
+
boxes = r.boxes
|
| 33 |
+
for box in boxes:
|
| 34 |
+
x1, y1, x2, y2 = box.xyxy[0]
|
| 35 |
+
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
| 36 |
+
|
| 37 |
+
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 255), 2)
|
| 38 |
+
|
| 39 |
+
label = box.cls[0].item()
|
| 40 |
+
label_name = model.names[label]
|
| 41 |
+
(text_width, text_height), baseline = cv2.getTextSize(label_name, cv2.FONT_HERSHEY_SIMPLEX, 0.9, 2)
|
| 42 |
+
cv2.rectangle(img, (x1, y1 - text_height - baseline - 5), (x1 + text_width, y1), (255, 0, 255), -1)
|
| 43 |
+
cv2.putText(img, label_name, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 0), 2)
|
| 44 |
+
|
| 45 |
+
resp_img_bytes = cv2.imencode('.jpg', img)[1].tobytes()
|
| 46 |
+
resp_filename = f"inferenced_{file.filename}" if file.filename else "inferenced_image.jpg"
|
| 47 |
+
return StreamingResponse(BytesIO(resp_img_bytes), media_type="image/jpg", headers={"Content-Disposition": f"attachment; filename={resp_filename}"})
|
best.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9e3e7ca8fa30f286ae485a1b568de3c68d71ea3113b54fb20caecdd9e42377ba
|
| 3 |
+
size 22504803
|
dockerfile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
RUN useradd -m -u 1000 user
|
| 4 |
+
USER user
|
| 5 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 6 |
+
|
| 7 |
+
WORKDIR /app
|
| 8 |
+
|
| 9 |
+
COPY --chown=user . .
|
| 10 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 11 |
+
|
| 12 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.116.1
|
| 2 |
+
numpy==2.2.6
|
| 3 |
+
opencv-python==4.12.0.88
|
| 4 |
+
python-multipart==0.0.20
|
| 5 |
+
ultralytics==8.3.193
|
| 6 |
+
ultralytics-thop==2.0.17
|
| 7 |
+
uvicorn==0.35.0
|