Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,110 @@
|
|
| 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 = "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 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# 3) 예측 함수
|
| 25 |
-
#
|
|
|
|
| 26 |
def predict(img: Image.Image):
|
| 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 |
demo = gr.Interface(
|
| 63 |
fn=predict,
|
| 64 |
-
inputs=gr.Image(type="pil", label="Input
|
| 65 |
-
outputs=gr.JSON(label="Detection
|
| 66 |
-
title="
|
| 67 |
-
description="
|
| 68 |
flagging_mode="never",
|
| 69 |
)
|
| 70 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torchvision.transforms as T
|
| 5 |
import numpy as np
|
| 6 |
+
import cv2
|
| 7 |
from PIL import Image
|
|
|
|
| 8 |
|
| 9 |
+
# ---------------------------------------------------
|
| 10 |
+
# 1) 모델 로드
|
| 11 |
+
# ---------------------------------------------------
|
|
|
|
| 12 |
|
| 13 |
+
MODEL_PATH = "pretrained_net_G.pth"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
class DeepCrackNet(nn.Module):
|
| 16 |
+
def __init__(self):
|
| 17 |
+
super().__init__()
|
| 18 |
+
# 원본 DeepCrack 구조 축약본
|
| 19 |
+
# pretrained 모델은 UNet-like 구조 기반
|
| 20 |
+
self.model = torch.load(MODEL_PATH, map_location="cpu")
|
| 21 |
+
self.model.eval()
|
| 22 |
+
|
| 23 |
+
def forward(self, x):
|
| 24 |
+
return self.model(x)
|
| 25 |
|
| 26 |
+
net = DeepCrackNet()
|
| 27 |
+
print("🔥 Loaded DeepCrack segmentation model")
|
| 28 |
|
| 29 |
+
# ---------------------------------------------------
|
| 30 |
+
# 2) 이미지 전처리 & 후처리
|
| 31 |
+
# ---------------------------------------------------
|
| 32 |
+
|
| 33 |
+
transform = T.Compose([
|
| 34 |
+
T.Resize((256, 256)),
|
| 35 |
+
T.ToTensor(),
|
| 36 |
+
])
|
| 37 |
+
|
| 38 |
+
def postprocess_mask(mask_tensor):
|
| 39 |
+
"""tensor → numpy mask (0/255)"""
|
| 40 |
+
mask = mask_tensor.squeeze().detach().cpu().numpy()
|
| 41 |
+
mask = (mask * 255).astype(np.uint8)
|
| 42 |
+
return mask
|
| 43 |
+
|
| 44 |
+
# ---------------------------------------------------
|
| 45 |
# 3) 예측 함수
|
| 46 |
+
# ---------------------------------------------------
|
| 47 |
+
|
| 48 |
def predict(img: Image.Image):
|
| 49 |
+
try:
|
| 50 |
+
# PIL → tensor
|
| 51 |
+
x = transform(img).unsqueeze(0)
|
| 52 |
+
|
| 53 |
+
# DeepCrack forward
|
| 54 |
+
with torch.no_grad():
|
| 55 |
+
output = net(x)
|
| 56 |
+
|
| 57 |
+
# 모델 출력은 (1, 1, H, W)
|
| 58 |
+
mask = postprocess_mask(output)
|
| 59 |
+
|
| 60 |
+
# 원본 이미지 크기 맞추기
|
| 61 |
+
orig = np.array(img)
|
| 62 |
+
mask_resized = cv2.resize(mask, (orig.shape[1], orig.shape[0]))
|
| 63 |
+
|
| 64 |
+
# crack 여부 판정
|
| 65 |
+
crack_pixels = np.sum(mask_resized > 127)
|
| 66 |
+
total_pixels = mask_resized.size
|
| 67 |
+
crack_ratio = crack_pixels / total_pixels
|
| 68 |
+
|
| 69 |
+
if crack_ratio > 0.01: # **1% 이상이면 crack**
|
| 70 |
+
label = "crack"
|
| 71 |
+
confidence = float(crack_ratio)
|
| 72 |
+
else:
|
| 73 |
+
label = "normal"
|
| 74 |
+
confidence = 1.0 - float(crack_ratio)
|
| 75 |
+
|
| 76 |
+
# JSON 출력
|
| 77 |
+
return {
|
| 78 |
+
"data": [
|
| 79 |
+
{
|
| 80 |
+
"label": label,
|
| 81 |
+
"confidence": confidence
|
| 82 |
+
}
|
| 83 |
+
]
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
except Exception as e:
|
| 87 |
+
print("❌ ERROR:", e)
|
| 88 |
+
return {
|
| 89 |
+
"data": [
|
| 90 |
+
{
|
| 91 |
+
"label": "error",
|
| 92 |
+
"confidence": 0.0,
|
| 93 |
+
"message": str(e)
|
| 94 |
+
}
|
| 95 |
+
]
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
# ---------------------------------------------------
|
| 99 |
+
# 4) Gradio Interface
|
| 100 |
+
# ---------------------------------------------------
|
| 101 |
+
|
| 102 |
demo = gr.Interface(
|
| 103 |
fn=predict,
|
| 104 |
+
inputs=gr.Image(type="pil", label="Input"),
|
| 105 |
+
outputs=gr.JSON(label="Detection"),
|
| 106 |
+
title="DeepCrack Segmentation API",
|
| 107 |
+
description="Concrete crack detection using DeepCrack pretrained model.",
|
| 108 |
flagging_mode="never",
|
| 109 |
)
|
| 110 |
|