Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,98 +1,105 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# -----------------------------------
|
| 6 |
-
# 1) Roboflow API ์ค์
|
| 7 |
-
# -----------------------------------
|
| 8 |
-
API_KEY = "UXTLYuI2sw5z7OtCpXdz"
|
| 9 |
-
MODEL_ID = "creacks-eapny/7"
|
| 10 |
-
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) # Green
|
| 19 |
-
elif conf < 0.60:
|
| 20 |
-
return (255, 165, 0, 120) # Orange
|
| 21 |
else:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 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 |
-
|
| 44 |
-
|
| 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 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 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 |
-
#
|
| 86 |
-
#
|
| 87 |
demo = gr.Interface(
|
| 88 |
fn=predict,
|
| 89 |
inputs=gr.Image(type="pil"),
|
| 90 |
-
outputs=
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
],
|
| 94 |
-
title="Crack Detection + Multi-Seg Heatmap",
|
| 95 |
-
description="์ฌ๋ฌ ๊ท ์ด์ ๊ฐ๊ฐ ๋ค๋ฅธ ์์ผ๋ก ํ์ํ๊ณ , ๊ฐ์ฅ ๋์ ๊ท ์ด ํ๋ฅ ์ UI์ ์ ๊ณตํฉ๋๋ค.",
|
| 96 |
flagging_mode="never"
|
| 97 |
)
|
| 98 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# ------------------------------
|
| 7 |
+
# 1) ๋ชจ๋ธ ํ์ผ ๊ฒฝ๋ก ์ง์
|
| 8 |
+
# ------------------------------
|
| 9 |
+
MODEL_PATH = "crack_classifier.h5" # ๋๋ .pt ๋ก ๊ต์ฒด ๊ฐ๋ฅ
|
| 10 |
+
|
| 11 |
+
IS_TF = MODEL_PATH.endswith(".h5") or MODEL_PATH.endswith(".keras")
|
| 12 |
+
|
| 13 |
+
# ------------------------------
|
| 14 |
+
# 2) TensorFlow ๋ชจ๋ธ ๋ก๋
|
| 15 |
+
# ------------------------------
|
| 16 |
+
if IS_TF:
|
| 17 |
+
import tensorflow as tf
|
| 18 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
| 19 |
+
print("๐ฅ Loaded TensorFlow crack classifier")
|
| 20 |
+
|
| 21 |
+
# ------------------------------
|
| 22 |
+
# 3) PyTorch ๋ชจ๋ธ ๋ก๋
|
| 23 |
+
# ------------------------------
|
| 24 |
+
else:
|
| 25 |
+
import torch
|
| 26 |
+
from torch import nn
|
| 27 |
+
|
| 28 |
+
class CNN(nn.Module):
|
| 29 |
+
# ๋ค๊ฐ ๊ฐ์ง ๋ชจ๋ธ ๊ตฌ์กฐ ๋ง๊ฒ ์กฐ์ ํ์
|
| 30 |
+
def __init__(self):
|
| 31 |
+
super().__init__()
|
| 32 |
+
self.net = nn.Sequential(
|
| 33 |
+
nn.Conv2d(3, 16, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2),
|
| 34 |
+
nn.Conv2d(16, 32, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2),
|
| 35 |
+
nn.Flatten(),
|
| 36 |
+
nn.Linear(32 * 56 * 56, 2) # ์ด๋ฏธ์ง ํฌ๊ธฐ ๋ง๊ฒ ์กฐ์ ํ์
|
| 37 |
+
)
|
| 38 |
+
def forward(self, x):
|
| 39 |
+
return self.net(x)
|
| 40 |
+
|
| 41 |
+
model = CNN()
|
| 42 |
+
model.load_state_dict(torch.load(MODEL_PATH, map_location="cpu"))
|
| 43 |
+
model.eval()
|
| 44 |
+
print("๐ฅ Loaded PyTorch crack classifier")
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# ------------------------------
|
| 48 |
+
# 4) ์ ์ฒด ์์ธก ํจ์
|
| 49 |
+
# ------------------------------
|
| 50 |
+
def predict(img: Image.Image):
|
| 51 |
+
# ์
๋ ฅ ๋ณํ
|
| 52 |
+
img_resized = img.resize((224, 224))
|
| 53 |
+
arr = np.array(img_resized) / 255.0
|
| 54 |
+
|
| 55 |
+
if IS_TF:
|
| 56 |
+
# TensorFlow
|
| 57 |
+
X = arr.reshape(1, 224, 224, 3)
|
| 58 |
+
probs = model.predict(X)[0] # [p_normal, p_crack]
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
else:
|
| 61 |
+
# PyTorch
|
| 62 |
+
import torch
|
| 63 |
+
X = (
|
| 64 |
+
torch.tensor(arr)
|
| 65 |
+
.permute(2, 0, 1)
|
| 66 |
+
.unsqueeze(0)
|
| 67 |
+
.float()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
)
|
| 69 |
+
probs = torch.softmax(model(X), dim=1).detach().numpy()[0]
|
| 70 |
|
| 71 |
+
p_normal = float(probs[0])
|
| 72 |
+
p_crack = float(probs[1])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
if p_crack > p_normal:
|
| 75 |
+
label = "crack"
|
| 76 |
+
conf = p_crack
|
| 77 |
+
else:
|
| 78 |
+
label = "normal"
|
| 79 |
+
conf = p_normal
|
| 80 |
+
|
| 81 |
+
# ------------------------------
|
| 82 |
+
# ํ๋ก ํธ๊ฐ ์๊ตฌํ๋ JSON ๊ตฌ์กฐ
|
| 83 |
+
# ------------------------------
|
| 84 |
+
return {
|
| 85 |
+
"data": [
|
| 86 |
+
{
|
| 87 |
+
"label": label,
|
| 88 |
+
"confidence": conf
|
| 89 |
+
}
|
| 90 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
}
|
| 92 |
|
| 93 |
|
| 94 |
+
# ------------------------------
|
| 95 |
+
# 5) Gradio API Interface
|
| 96 |
+
# ------------------------------
|
| 97 |
demo = gr.Interface(
|
| 98 |
fn=predict,
|
| 99 |
inputs=gr.Image(type="pil"),
|
| 100 |
+
outputs=gr.JSON(label="Detection Result"),
|
| 101 |
+
title="Crack Detection Classifier",
|
| 102 |
+
description="์ฌ์ง์ ์
๋ก๋ํ๋ฉด ๊ท ์ด/์ ์ ์ฌ๋ถ์ ํ๋ฅ (%)์ ๋ฐํํฉ๋๋ค.",
|
|
|
|
|
|
|
|
|
|
| 103 |
flagging_mode="never"
|
| 104 |
)
|
| 105 |
|