Upload 4 files
Browse files
Requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
python-multipart
|
| 4 |
+
ultralytics
|
| 5 |
+
opencv-python-headless
|
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
from ultralytics import YOLO
|
| 6 |
+
|
| 7 |
+
app = FastAPI(title="GiziTrace ML API")
|
| 8 |
+
|
| 9 |
+
model = YOLO("best.onnx", task="detect")
|
| 10 |
+
|
| 11 |
+
@app.post("/predict")
|
| 12 |
+
async def predict_waste(file: UploadFile = File(...)):
|
| 13 |
+
try:
|
| 14 |
+
|
| 15 |
+
contents = await file.read()
|
| 16 |
+
nparr = np.frombuffer(contents, np.uint8)
|
| 17 |
+
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 18 |
+
|
| 19 |
+
results = model(img, conf=0.10)
|
| 20 |
+
|
| 21 |
+
area_plate = 0
|
| 22 |
+
area_food = 0
|
| 23 |
+
|
| 24 |
+
for result in results:
|
| 25 |
+
if len(result.boxes) == 0:
|
| 26 |
+
continue
|
| 27 |
+
|
| 28 |
+
boxes = result.boxes.xyxy.cpu().numpy()
|
| 29 |
+
clss = result.boxes.cls.cpu().numpy()
|
| 30 |
+
|
| 31 |
+
for idx, box in enumerate(boxes):
|
| 32 |
+
x1, y1, x2, y2 = box
|
| 33 |
+
box_area = (x2 - x1) * (y2 - y1)
|
| 34 |
+
class_id = int(clss[idx])
|
| 35 |
+
|
| 36 |
+
if class_id == 0:
|
| 37 |
+
area_plate += box_area
|
| 38 |
+
elif class_id == 1:
|
| 39 |
+
area_food += box_area
|
| 40 |
+
|
| 41 |
+
if area_plate == 0:
|
| 42 |
+
return JSONResponse(content={
|
| 43 |
+
"status": "manual_review",
|
| 44 |
+
"message": "Piring tidak terdeteksi, butuh input manual",
|
| 45 |
+
"waste_percent": 0.0,
|
| 46 |
+
"consumption_percent": 0.0
|
| 47 |
+
})
|
| 48 |
+
|
| 49 |
+
waste_percent = min(round((area_food / area_plate) * 100, 2), 100.0)
|
| 50 |
+
consumption_percent = round(100 - waste_percent, 2)
|
| 51 |
+
|
| 52 |
+
return JSONResponse(content={
|
| 53 |
+
"status": "success",
|
| 54 |
+
"waste_percent": waste_percent,
|
| 55 |
+
"consumption_percent": consumption_percent
|
| 56 |
+
})
|
| 57 |
+
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return JSONResponse(status_code=500, content={"message": str(e)})
|
best.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1a486f3759075590ddbfecf99e788bec894c068b4d5719f801f2588b34b7938c
|
| 3 |
+
size 12265978
|
docker
ADDED
|
File without changes
|