Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,44 +11,43 @@ API_URL = f"https://serverless.roboflow.com/{MODEL_ID}"
|
|
| 11 |
|
| 12 |
|
| 13 |
# -----------------------------------
|
| 14 |
-
# 2) confidence
|
| 15 |
# -----------------------------------
|
| 16 |
def get_color(conf):
|
| 17 |
if conf < 0.30:
|
| 18 |
-
return (0, 255, 0, 120)
|
| 19 |
elif conf < 0.60:
|
| 20 |
-
return (255, 165, 0, 120)
|
| 21 |
else:
|
| 22 |
-
return (255, 0, 0, 120)
|
| 23 |
|
| 24 |
|
| 25 |
# -----------------------------------
|
| 26 |
-
# 3) Roboflow ํธ์ถ +
|
| 27 |
# -----------------------------------
|
| 28 |
def predict(image: Image.Image):
|
| 29 |
temp_path = "temp.jpg"
|
| 30 |
image.save(temp_path)
|
| 31 |
|
| 32 |
-
#
|
| 33 |
with open(temp_path, "rb") as f:
|
| 34 |
response = requests.post(
|
| 35 |
API_URL,
|
| 36 |
files={"file": f},
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
)
|
| 42 |
|
| 43 |
result = response.json()
|
| 44 |
preds = result.get("predictions", [])
|
| 45 |
|
| 46 |
-
# ๋ฒ ์ด์ค ์ด๋ฏธ์ง
|
| 47 |
base = image.convert("RGBA")
|
| 48 |
-
overlay = Image.new("RGBA", base.size, (0,
|
| 49 |
draw = ImageDraw.Draw(overlay, "RGBA")
|
| 50 |
|
| 51 |
-
#
|
| 52 |
if not preds:
|
| 53 |
blended = Image.alpha_composite(base, overlay)
|
| 54 |
return blended, {
|
|
@@ -58,30 +57,32 @@ def predict(image: Image.Image):
|
|
| 58 |
}]
|
| 59 |
}
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
if points:
|
| 70 |
-
polygon = [(p["x"], p["y"]) for p in points]
|
| 71 |
-
draw.polygon(polygon, fill=get_color(conf))
|
| 72 |
|
| 73 |
blended = Image.alpha_composite(base, overlay)
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
return blended, {
|
| 76 |
"data": [{
|
| 77 |
-
"label":
|
| 78 |
-
"confidence":
|
| 79 |
}]
|
| 80 |
}
|
| 81 |
|
| 82 |
|
| 83 |
# -----------------------------------
|
| 84 |
-
# 4) Gradio
|
| 85 |
# -----------------------------------
|
| 86 |
demo = gr.Interface(
|
| 87 |
fn=predict,
|
|
@@ -90,8 +91,8 @@ demo = gr.Interface(
|
|
| 90 |
gr.Image(label="Crack Heatmap"),
|
| 91 |
gr.JSON(label="Detection Data"),
|
| 92 |
],
|
| 93 |
-
title="Crack Detection + Heatmap
|
| 94 |
-
description="
|
| 95 |
flagging_mode="never"
|
| 96 |
)
|
| 97 |
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
# -----------------------------------
|
| 14 |
+
# 2) confidence ์์ ๊ท์น
|
| 15 |
# -----------------------------------
|
| 16 |
def get_color(conf):
|
| 17 |
if conf < 0.30:
|
| 18 |
+
return (0, 255, 0, 120) # Green
|
| 19 |
elif conf < 0.60:
|
| 20 |
+
return (255, 165, 0, 120) # Orange
|
| 21 |
else:
|
| 22 |
+
return (255, 0, 0, 120) # Red
|
| 23 |
|
| 24 |
|
| 25 |
# -----------------------------------
|
| 26 |
+
# 3) Roboflow ํธ์ถ + ๋ค์ค segmentation ์๊ฐํ
|
| 27 |
# -----------------------------------
|
| 28 |
def predict(image: Image.Image):
|
| 29 |
temp_path = "temp.jpg"
|
| 30 |
image.save(temp_path)
|
| 31 |
|
| 32 |
+
# threshold ๋ฎ์ถ๊ธฐ โ crack ๊ฐ์ง ํ์ฑํ
|
| 33 |
with open(temp_path, "rb") as f:
|
| 34 |
response = requests.post(
|
| 35 |
API_URL,
|
| 36 |
files={"file": f},
|
| 37 |
+
data={
|
| 38 |
+
"api_key": API_KEY,
|
| 39 |
+
"confidence": 0.15 # โ
๋งค์ฐ ์ค์: threshold ๋ฎ์ถ๊ธฐ
|
| 40 |
+
}
|
| 41 |
)
|
| 42 |
|
| 43 |
result = response.json()
|
| 44 |
preds = result.get("predictions", [])
|
| 45 |
|
|
|
|
| 46 |
base = image.convert("RGBA")
|
| 47 |
+
overlay = Image.new("RGBA", base.size, (0,0,0,0))
|
| 48 |
draw = ImageDraw.Draw(overlay, "RGBA")
|
| 49 |
|
| 50 |
+
# ๊ฐ์ฒด ์์ โ ์ ์ ์ฒ๋ฆฌ
|
| 51 |
if not preds:
|
| 52 |
blended = Image.alpha_composite(base, overlay)
|
| 53 |
return blended, {
|
|
|
|
| 57 |
}]
|
| 58 |
}
|
| 59 |
|
| 60 |
+
# โ ๋ค์ค crack ์๊ฐํ (for all preds)
|
| 61 |
+
for p in preds:
|
| 62 |
+
conf = float(p.get("confidence", 0))
|
| 63 |
+
color = get_color(conf)
|
| 64 |
+
points = p.get("points")
|
| 65 |
+
if points:
|
| 66 |
+
polygon = [(pt["x"], pt["y"]) for pt in points]
|
| 67 |
+
draw.polygon(polygon, fill=color)
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
blended = Image.alpha_composite(base, overlay)
|
| 70 |
|
| 71 |
+
# โก ๊ฐ์ฅ confidence ๋์ crack ํ๋๋ง ํ๋ก ํธ๋ก ์ ๋ฌ
|
| 72 |
+
best = max(preds, key=lambda p: p.get("confidence", 0))
|
| 73 |
+
best_conf = float(best.get("confidence", 0))
|
| 74 |
+
best_label = best.get("class", "crack")
|
| 75 |
+
|
| 76 |
return blended, {
|
| 77 |
"data": [{
|
| 78 |
+
"label": best_label,
|
| 79 |
+
"confidence": best_conf
|
| 80 |
}]
|
| 81 |
}
|
| 82 |
|
| 83 |
|
| 84 |
# -----------------------------------
|
| 85 |
+
# 4) Gradio UI
|
| 86 |
# -----------------------------------
|
| 87 |
demo = gr.Interface(
|
| 88 |
fn=predict,
|
|
|
|
| 91 |
gr.Image(label="Crack Heatmap"),
|
| 92 |
gr.JSON(label="Detection Data"),
|
| 93 |
],
|
| 94 |
+
title="Crack Detection + Multi-Seg Heatmap",
|
| 95 |
+
description="์ฌ๋ฌ ๊ท ์ด์ ๊ฐ๊ฐ ๋ค๋ฅธ ์์ผ๋ก ํ์ํ๊ณ , ๊ฐ์ฅ ๋์ ๊ท ์ด ํ๋ฅ ์ UI์ ์ ๊ณตํฉ๋๋ค.",
|
| 96 |
flagging_mode="never"
|
| 97 |
)
|
| 98 |
|