Spaces:
Runtime error
Runtime error
Commit
·
99bdfbb
1
Parent(s):
39c55ce
Adicionar arquivos iniciais
Browse files- Dockerfile +18 -0
- app.py +36 -0
- detect.py +54 -0
- requirements.txt +10 -0
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.13.3
|
| 2 |
+
|
| 3 |
+
RUN useradd -m -u 1000 user
|
| 4 |
+
RUN apt-get update && apt-get install -y \
|
| 5 |
+
python3 \
|
| 6 |
+
python3-pip \
|
| 7 |
+
libgl1
|
| 8 |
+
USER user
|
| 9 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 10 |
+
|
| 11 |
+
COPY . /app
|
| 12 |
+
|
| 13 |
+
WORKDIR /app
|
| 14 |
+
|
| 15 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 16 |
+
|
| 17 |
+
COPY --chown=user . /app
|
| 18 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# uvicorn app:app --reload
|
| 2 |
+
|
| 3 |
+
from fastapi import Body, FastAPI, UploadFile, File
|
| 4 |
+
from fastapi.responses import JSONResponse
|
| 5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
+
from detect import DengueDetector
|
| 7 |
+
import traceback
|
| 8 |
+
|
| 9 |
+
# Inicializar detector e preditor
|
| 10 |
+
detector = DengueDetector()
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
# --- CORS ---
|
| 14 |
+
origins = ["https://previdengue.vercel.app", "*"]
|
| 15 |
+
app.add_middleware(
|
| 16 |
+
CORSMiddleware,
|
| 17 |
+
allow_origins=origins,
|
| 18 |
+
allow_credentials=True,
|
| 19 |
+
allow_methods=["*"],
|
| 20 |
+
allow_headers=["*"]
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# --- Rotas ---
|
| 24 |
+
@app.get("/")
|
| 25 |
+
def health_check():
|
| 26 |
+
return {"status": "ok", "message": "API de Dengue rodando!"}
|
| 27 |
+
|
| 28 |
+
# --- Rota de detecção ---
|
| 29 |
+
@app.post("/detect/")
|
| 30 |
+
async def detect(file: UploadFile = File(...)):
|
| 31 |
+
try:
|
| 32 |
+
content = await file.read()
|
| 33 |
+
result = detector.detect_image(content)
|
| 34 |
+
return JSONResponse(content=result)
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
detect.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from collections import Counter
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
from ultralytics import YOLO
|
| 6 |
+
|
| 7 |
+
class DengueDetector:
|
| 8 |
+
def __init__(self, model_path="./models/DetectsmallTest1.pt"):
|
| 9 |
+
self.model = YOLO(model_path)
|
| 10 |
+
self.names = self.model.names
|
| 11 |
+
|
| 12 |
+
def calculate_intensity(self, objects):
|
| 13 |
+
weights = {"piscina": 9, "caixa_agua": 4, "carro": 1}
|
| 14 |
+
score = sum(weights.get(obj["class"], 0) for obj in objects)
|
| 15 |
+
return score
|
| 16 |
+
|
| 17 |
+
def detect_image(self, image_bytes):
|
| 18 |
+
# Carregar imagem da memória
|
| 19 |
+
img = Image.open(BytesIO(image_bytes)).convert("RGB")
|
| 20 |
+
img_np = np.array(img) # YOLO aceita np.array diretamente
|
| 21 |
+
height, width = img_np.shape[:2]
|
| 22 |
+
|
| 23 |
+
# Detectar objetos
|
| 24 |
+
results = self.model(img_np)
|
| 25 |
+
result = results[0]
|
| 26 |
+
boxes = result.boxes
|
| 27 |
+
class_ids = boxes.cls.tolist()
|
| 28 |
+
confidences = boxes.conf.tolist()
|
| 29 |
+
class_names = [self.names[int(cls)] for cls in class_ids]
|
| 30 |
+
counts = Counter(class_names)
|
| 31 |
+
|
| 32 |
+
# Construir lista de detecções
|
| 33 |
+
detections = []
|
| 34 |
+
for i in range(len(boxes)):
|
| 35 |
+
x1, y1, x2, y2 = map(float, boxes.xyxy[i])
|
| 36 |
+
conf = float(confidences[i])
|
| 37 |
+
cls_id = int(class_ids[i])
|
| 38 |
+
detections.append({
|
| 39 |
+
"class": self.names[cls_id],
|
| 40 |
+
"confidence": round(conf, 4),
|
| 41 |
+
"box": {
|
| 42 |
+
"x1": x1, "y1": y1, "x2": x2, "y2": y2,
|
| 43 |
+
"original_width": width, "original_height": height
|
| 44 |
+
}
|
| 45 |
+
})
|
| 46 |
+
|
| 47 |
+
intensity_score = self.calculate_intensity(detections)
|
| 48 |
+
|
| 49 |
+
return {
|
| 50 |
+
"total": len(class_ids),
|
| 51 |
+
"contagem": counts,
|
| 52 |
+
"objetos": detections,
|
| 53 |
+
"intensity_score": intensity_score
|
| 54 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.115.12
|
| 2 |
+
uvicorn==0.34.0
|
| 3 |
+
pillow==11.1.0
|
| 4 |
+
opencv-python-headless
|
| 5 |
+
numpy==2.1.1
|
| 6 |
+
ultralytics==8.3.105
|
| 7 |
+
torch==2.6.0
|
| 8 |
+
python-multipart
|
| 9 |
+
pandas==2.2.3
|
| 10 |
+
pyarrow==20.0.0
|