Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,16 +6,17 @@ import tensorflow as tf
|
|
| 6 |
# ------------------------------
|
| 7 |
# 1) 모델 파일 경로
|
| 8 |
# ------------------------------
|
| 9 |
-
MODEL_PATH = "
|
| 10 |
|
| 11 |
# ------------------------------
|
| 12 |
-
# 2) 모델 로드
|
| 13 |
# ------------------------------
|
| 14 |
model = tf.keras.models.load_model(MODEL_PATH)
|
| 15 |
-
print("🔥 Loaded
|
| 16 |
print(" Input shape :", model.input_shape)
|
| 17 |
print(" Output shape:", model.output_shape)
|
| 18 |
|
|
|
|
| 19 |
_, H, W, C = model.input_shape
|
| 20 |
|
| 21 |
|
|
@@ -23,84 +24,47 @@ _, H, W, C = model.input_shape
|
|
| 23 |
# 3) 예측 함수
|
| 24 |
# ------------------------------
|
| 25 |
def predict(img: Image.Image):
|
| 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 |
-
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
|
| 97 |
# ------------------------------
|
| 98 |
demo = gr.Interface(
|
| 99 |
fn=predict,
|
| 100 |
-
inputs=gr.Image(type="pil", label="Input
|
| 101 |
outputs=gr.JSON(label="Detection Result"),
|
| 102 |
-
title="Crack
|
| 103 |
-
description="
|
| 104 |
flagging_mode="never",
|
| 105 |
)
|
| 106 |
|
|
|
|
| 6 |
# ------------------------------
|
| 7 |
# 1) 모델 파일 경로
|
| 8 |
# ------------------------------
|
| 9 |
+
MODEL_PATH = "model.h5" # GitHub 모델 그대로 사용
|
| 10 |
|
| 11 |
# ------------------------------
|
| 12 |
+
# 2) TensorFlow 모델 로드
|
| 13 |
# ------------------------------
|
| 14 |
model = tf.keras.models.load_model(MODEL_PATH)
|
| 15 |
+
print("🔥 Loaded Concrete Crack Classification Model")
|
| 16 |
print(" Input shape :", model.input_shape)
|
| 17 |
print(" Output shape:", model.output_shape)
|
| 18 |
|
| 19 |
+
# expected input: (None, 227, 227, 3)
|
| 20 |
_, H, W, C = model.input_shape
|
| 21 |
|
| 22 |
|
|
|
|
| 24 |
# 3) 예측 함수
|
| 25 |
# ------------------------------
|
| 26 |
def predict(img: Image.Image):
|
| 27 |
+
# 1) 이미지 전처리
|
| 28 |
+
img = img.convert("RGB")
|
| 29 |
+
img_resized = img.resize((W, H))
|
| 30 |
+
arr = np.array(img_resized).astype("float32") / 255.0
|
| 31 |
+
X = np.expand_dims(arr, axis=0) # (1, 227, 227, 3)
|
| 32 |
+
|
| 33 |
+
# 2) 모델 추론
|
| 34 |
+
raw = model.predict(X)[0]
|
| 35 |
+
probs = np.array(raw).flatten()
|
| 36 |
+
|
| 37 |
+
# Output structure: [no_crack, crack]
|
| 38 |
+
p_normal = float(probs[0])
|
| 39 |
+
p_crack = float(probs[1])
|
| 40 |
+
|
| 41 |
+
if p_crack > p_normal:
|
| 42 |
+
label = "crack"
|
| 43 |
+
conf = p_crack
|
| 44 |
+
else:
|
| 45 |
+
label = "normal"
|
| 46 |
+
conf = p_normal
|
| 47 |
+
|
| 48 |
+
# 3) JSON 구조 (프론트 요구사항대로)
|
| 49 |
+
return {
|
| 50 |
+
"data": [
|
| 51 |
+
{
|
| 52 |
+
"label": label,
|
| 53 |
+
"confidence": float(conf)
|
| 54 |
+
}
|
| 55 |
+
]
|
| 56 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
|
| 59 |
# ------------------------------
|
| 60 |
+
# 4) Gradio API Interface
|
| 61 |
# ------------------------------
|
| 62 |
demo = gr.Interface(
|
| 63 |
fn=predict,
|
| 64 |
+
inputs=gr.Image(type="pil", label="Input Image"),
|
| 65 |
outputs=gr.JSON(label="Detection Result"),
|
| 66 |
+
title="Concrete Crack Classification (227×227 CNN)",
|
| 67 |
+
description="균열 여부를 판단하고 crack일 때만 확률(%)을 반환합니다.",
|
| 68 |
flagging_mode="never",
|
| 69 |
)
|
| 70 |
|