Spaces:
Sleeping
Sleeping
refactor: output inference volume
Browse files
app.py
CHANGED
|
@@ -76,7 +76,8 @@ def calculate_sphere_volume(width, height):
|
|
| 76 |
@app.post("/inference_volume")
|
| 77 |
async def inference_volume(file: UploadFile):
|
| 78 |
"""
|
| 79 |
-
Endpoint untuk deteksi tumor dengan volume
|
|
|
|
| 80 |
"""
|
| 81 |
if file.content_type not in ["image/jpeg", "image/png", "image/jpg"]:
|
| 82 |
return JSONResponse(
|
|
@@ -91,11 +92,10 @@ async def inference_volume(file: UploadFile):
|
|
| 91 |
results = model.predict(
|
| 92 |
source=img,
|
| 93 |
conf=0.5,
|
| 94 |
-
iou=0.
|
| 95 |
)
|
| 96 |
|
| 97 |
-
|
| 98 |
-
detection_count = 0
|
| 99 |
|
| 100 |
for r in results:
|
| 101 |
boxes = r.boxes
|
|
@@ -107,23 +107,23 @@ async def inference_volume(file: UploadFile):
|
|
| 107 |
width = x2 - x1
|
| 108 |
height = y2 - y1
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
volume = AVERAGE_TUMOR_VOLUME
|
| 113 |
-
|
| 114 |
-
total_volume += volume
|
| 115 |
-
detection_count += 1
|
| 116 |
|
| 117 |
label = int(box.cls[0].item())
|
| 118 |
label_name = model.names[label]
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
# Draw bounding box
|
| 122 |
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 255), 2)
|
| 123 |
|
| 124 |
# Label dengan volume
|
| 125 |
-
text = f"{label_name}
|
| 126 |
-
vol_text = f"
|
| 127 |
|
| 128 |
(text_width, text_height), baseline = cv2.getTextSize(
|
| 129 |
text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2
|
|
@@ -132,7 +132,7 @@ async def inference_volume(file: UploadFile):
|
|
| 132 |
cv2.rectangle(
|
| 133 |
img,
|
| 134 |
(x1, y1 - text_height - baseline - 25),
|
| 135 |
-
(x1 + max(text_width,
|
| 136 |
(255, 0, 255),
|
| 137 |
-1
|
| 138 |
)
|
|
@@ -146,15 +146,16 @@ async def inference_volume(file: UploadFile):
|
|
| 146 |
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1
|
| 147 |
)
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
-
return
|
| 157 |
-
BytesIO(resp_img_bytes),
|
| 158 |
-
media_type="image/jpeg",
|
| 159 |
-
headers={"Content-Disposition": f"attachment; filename={resp_filename}"}
|
| 160 |
-
)
|
|
|
|
| 76 |
@app.post("/inference_volume")
|
| 77 |
async def inference_volume(file: UploadFile):
|
| 78 |
"""
|
| 79 |
+
Endpoint sederhana untuk deteksi tumor dengan volume
|
| 80 |
+
Return: JSON dengan volume_mm3, class, dan image_bytes
|
| 81 |
"""
|
| 82 |
if file.content_type not in ["image/jpeg", "image/png", "image/jpg"]:
|
| 83 |
return JSONResponse(
|
|
|
|
| 92 |
results = model.predict(
|
| 93 |
source=img,
|
| 94 |
conf=0.5,
|
| 95 |
+
iou=0.45
|
| 96 |
)
|
| 97 |
|
| 98 |
+
detections = []
|
|
|
|
| 99 |
|
| 100 |
for r in results:
|
| 101 |
boxes = r.boxes
|
|
|
|
| 107 |
width = x2 - x1
|
| 108 |
height = y2 - y1
|
| 109 |
|
| 110 |
+
# Hitung volume
|
| 111 |
+
volume_mm3 = calculate_sphere_volume(width, height)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
label = int(box.cls[0].item())
|
| 114 |
label_name = model.names[label]
|
| 115 |
+
|
| 116 |
+
detections.append({
|
| 117 |
+
"class": label_name,
|
| 118 |
+
"volume_mm3": volume_mm3
|
| 119 |
+
})
|
| 120 |
|
| 121 |
# Draw bounding box
|
| 122 |
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 255), 2)
|
| 123 |
|
| 124 |
# Label dengan volume
|
| 125 |
+
text = f"{label_name}"
|
| 126 |
+
vol_text = f"{volume_mm3} mm3"
|
| 127 |
|
| 128 |
(text_width, text_height), baseline = cv2.getTextSize(
|
| 129 |
text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2
|
|
|
|
| 132 |
cv2.rectangle(
|
| 133 |
img,
|
| 134 |
(x1, y1 - text_height - baseline - 25),
|
| 135 |
+
(x1 + max(text_width, 100), y1),
|
| 136 |
(255, 0, 255),
|
| 137 |
-1
|
| 138 |
)
|
|
|
|
| 146 |
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1
|
| 147 |
)
|
| 148 |
|
| 149 |
+
# Encode image ke bytes
|
| 150 |
+
_, buffer = cv2.imencode('.jpg', img)
|
| 151 |
+
img_bytes = buffer.tobytes()
|
| 152 |
|
| 153 |
+
# Convert ke base64 untuk JSON
|
| 154 |
+
img_base64 = base64.b64encode(img_bytes).decode('utf-8')
|
| 155 |
+
|
| 156 |
+
response = {
|
| 157 |
+
"detections": detections,
|
| 158 |
+
"image_bytes": img_base64
|
| 159 |
+
}
|
| 160 |
|
| 161 |
+
return JSONResponse(content=response)
|
|
|
|
|
|
|
|
|
|
|
|