Spaces:
Sleeping
Sleeping
Commit ·
0dc4562
1
Parent(s): 0dd1ea6
Add application file
Browse files- Dockerfile +13 -0
- app.py +102 -0
- models/DetectsmallTest1.pt +3 -0
- requirements.txt +8 -0
Dockerfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 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 ./requirements.txt requirements.txt
|
| 10 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 11 |
+
|
| 12 |
+
COPY --chown=user . /app
|
| 13 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# uvicorn app:app --reload
|
| 2 |
+
|
| 3 |
+
import uuid
|
| 4 |
+
from fastapi import FastAPI, UploadFile, File
|
| 5 |
+
from fastapi.responses import JSONResponse
|
| 6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import os
|
| 9 |
+
from collections import Counter
|
| 10 |
+
from ultralytics import YOLO
|
| 11 |
+
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
|
| 14 |
+
app.add_middleware(
|
| 15 |
+
CORSMiddleware,
|
| 16 |
+
allow_origins=["*"],
|
| 17 |
+
allow_credentials=True,
|
| 18 |
+
allow_methods=["*"],
|
| 19 |
+
allow_headers=["*"],
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
detect_model_path = "./models/DetectsmallTest1.pt"
|
| 23 |
+
detect_model = YOLO(detect_model_path)
|
| 24 |
+
|
| 25 |
+
@app.get("/")
|
| 26 |
+
def read_root():
|
| 27 |
+
return {"message": "Hello!"}
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def caculateIntensity(objetos):
|
| 31 |
+
count_piscina = sum(1 for o in objetos if o["class"] == "piscina")
|
| 32 |
+
count_caixa = sum(1 for o in objetos if o["class"] == "caixa_agua")
|
| 33 |
+
count_carro = sum(1 for o in objetos if o["class"] == "carro")
|
| 34 |
+
|
| 35 |
+
w_piscina = 9
|
| 36 |
+
w_caixa = 4
|
| 37 |
+
w_carro = 1
|
| 38 |
+
|
| 39 |
+
score = count_piscina * w_piscina + count_caixa * w_caixa + count_carro * w_carro
|
| 40 |
+
return score
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
@app.post("/detect/")
|
| 44 |
+
async def upload_image(file: UploadFile = File(...)):
|
| 45 |
+
print("-" * 50)
|
| 46 |
+
print("Detecting...")
|
| 47 |
+
|
| 48 |
+
image_uuid = str(uuid.uuid4())
|
| 49 |
+
image_path = f"{image_uuid}.png"
|
| 50 |
+
|
| 51 |
+
contents = await file.read()
|
| 52 |
+
with open(image_path, "wb") as f:
|
| 53 |
+
f.write(contents)
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
with Image.open(image_path) as image:
|
| 57 |
+
width, height = image.size
|
| 58 |
+
|
| 59 |
+
results = detect_model(image_path)
|
| 60 |
+
result = results[0]
|
| 61 |
+
boxes = result.boxes
|
| 62 |
+
names = detect_model.names
|
| 63 |
+
|
| 64 |
+
class_ids = boxes.cls.tolist()
|
| 65 |
+
confidences = boxes.conf.tolist()
|
| 66 |
+
class_names = [names[int(cls)] for cls in class_ids]
|
| 67 |
+
counts = Counter(class_names)
|
| 68 |
+
|
| 69 |
+
detections = []
|
| 70 |
+
for i in range(len(boxes)):
|
| 71 |
+
x1, y1, x2, y2 = map(float, boxes.xyxy[i])
|
| 72 |
+
conf = float(confidences[i])
|
| 73 |
+
cls_id = int(class_ids[i])
|
| 74 |
+
detections.append({
|
| 75 |
+
"class": names[cls_id],
|
| 76 |
+
"confidence": round(conf, 4),
|
| 77 |
+
"box": {
|
| 78 |
+
"x1": x1,
|
| 79 |
+
"y1": y1,
|
| 80 |
+
"x2": x2,
|
| 81 |
+
"y2": y2,
|
| 82 |
+
"original_width": width,
|
| 83 |
+
"original_height": height
|
| 84 |
+
}
|
| 85 |
+
})
|
| 86 |
+
os.remove(image_path)
|
| 87 |
+
|
| 88 |
+
intensity_score = caculateIntensity(detections)
|
| 89 |
+
|
| 90 |
+
return JSONResponse(content={
|
| 91 |
+
"total": len(class_ids),
|
| 92 |
+
"contagem": counts,
|
| 93 |
+
"objetos": detections,
|
| 94 |
+
"intensity_score": intensity_score
|
| 95 |
+
})
|
| 96 |
+
|
| 97 |
+
except Exception as e:
|
| 98 |
+
if os.path.exists(image_path):
|
| 99 |
+
os.remove(image_path)
|
| 100 |
+
|
| 101 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 102 |
+
|
models/DetectsmallTest1.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3b4b91b6b7d56edae2d369cb6b7306690a9864a1a51d2bf4f1bd6d2877ccf79c
|
| 3 |
+
size 22508451
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.115.12
|
| 2 |
+
uvicorn==0.34.0
|
| 3 |
+
pillow==11.1.0
|
| 4 |
+
opencv-python==4.11.0.86
|
| 5 |
+
numpy==2.1.1
|
| 6 |
+
ultralytics==8.3.105
|
| 7 |
+
torch==2.6.0
|
| 8 |
+
python-multipart
|