Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,105 +1,108 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
label
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
|
| 6 |
+
# ------------------------------
|
| 7 |
+
# 1) ๋ชจ๋ธ ํ์ผ ๊ฒฝ๋ก
|
| 8 |
+
# ------------------------------
|
| 9 |
+
MODEL_PATH = "crack_detection.h5"
|
| 10 |
+
|
| 11 |
+
# ------------------------------
|
| 12 |
+
# 2) ๋ชจ๋ธ ๋ก๋
|
| 13 |
+
# ------------------------------
|
| 14 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
| 15 |
+
print("๐ฅ Loaded TensorFlow crack classifier")
|
| 16 |
+
print(" Input shape :", model.input_shape)
|
| 17 |
+
print(" Output shape:", model.output_shape)
|
| 18 |
+
|
| 19 |
+
_, H, W, C = model.input_shape
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# ------------------------------
|
| 23 |
+
# 3) ์์ธก ํจ์
|
| 24 |
+
# ------------------------------
|
| 25 |
+
def predict(img: Image.Image):
|
| 26 |
+
try:
|
| 27 |
+
img = img.convert("RGB")
|
| 28 |
+
img_resized = img.resize((W, H))
|
| 29 |
+
|
| 30 |
+
arr = np.array(img_resized).astype("float32") / 255.0
|
| 31 |
+
X = np.expand_dims(arr, axis=0) # (1, H, W, C)
|
| 32 |
+
|
| 33 |
+
raw = model.predict(X)[0]
|
| 34 |
+
probs = np.array(raw, dtype="float32").flatten()
|
| 35 |
+
|
| 36 |
+
# ------------------------------
|
| 37 |
+
# 1-output (sigmoid) ๋ชจ๋ธ
|
| 38 |
+
# ------------------------------
|
| 39 |
+
if probs.shape[0] == 1:
|
| 40 |
+
p_crack = float(probs[0])
|
| 41 |
+
p_normal = 1.0 - p_crack
|
| 42 |
+
|
| 43 |
+
if p_crack >= 0.5:
|
| 44 |
+
label = "crack"
|
| 45 |
+
conf = p_crack
|
| 46 |
+
else:
|
| 47 |
+
label = "normal"
|
| 48 |
+
conf = p_normal
|
| 49 |
+
|
| 50 |
+
# ------------------------------
|
| 51 |
+
# 2-output (softmax) ๋ชจ๋ธ
|
| 52 |
+
# probs = [p_normal, p_crack]
|
| 53 |
+
# ------------------------------
|
| 54 |
+
elif probs.shape[0] >= 2:
|
| 55 |
+
p_normal = float(probs[0])
|
| 56 |
+
p_crack = float(probs[1])
|
| 57 |
+
|
| 58 |
+
if p_crack >= p_normal:
|
| 59 |
+
label = "crack"
|
| 60 |
+
conf = p_crack
|
| 61 |
+
else:
|
| 62 |
+
label = "normal"
|
| 63 |
+
conf = p_normal
|
| 64 |
+
|
| 65 |
+
else:
|
| 66 |
+
idx = int(np.argmax(probs))
|
| 67 |
+
label = f"class_{idx}"
|
| 68 |
+
conf = float(probs[idx])
|
| 69 |
+
|
| 70 |
+
# ------------------------------
|
| 71 |
+
# ํ๋ก ํธ ์๊ตฌ ๊ตฌ์กฐ
|
| 72 |
+
# ------------------------------
|
| 73 |
+
return {
|
| 74 |
+
"data": [
|
| 75 |
+
{
|
| 76 |
+
"label": label,
|
| 77 |
+
"confidence": float(conf),
|
| 78 |
+
}
|
| 79 |
+
]
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print("โ Error:", e)
|
| 84 |
+
return {
|
| 85 |
+
"data": [
|
| 86 |
+
{
|
| 87 |
+
"label": "error",
|
| 88 |
+
"confidence": 0.0,
|
| 89 |
+
"message": str(e),
|
| 90 |
+
}
|
| 91 |
+
]
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# ------------------------------
|
| 96 |
+
# 4) Gradio API UI
|
| 97 |
+
# ------------------------------
|
| 98 |
+
demo = gr.Interface(
|
| 99 |
+
fn=predict,
|
| 100 |
+
inputs=gr.Image(type="pil", label="Input image"),
|
| 101 |
+
outputs=gr.JSON(label="Detection Result"),
|
| 102 |
+
title="Crack Detection Classifier (Keras .h5)",
|
| 103 |
+
description="์ฌ์ง์ ์
๋ก๋ํ๋ฉด ๊ท ์ด/์ ์ ์ฌ๋ถ์ ํ๋ฅ (%)์ ๋ฐํํฉ๋๋ค.",
|
| 104 |
+
flagging_mode="never",
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
if __name__ == "__main__":
|
| 108 |
+
demo.launch()
|