Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,111 +1,78 @@
|
|
| 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 |
-
#
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
#
|
| 30 |
-
# 2)
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 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 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
mask_resized = cv2.resize(mask, (orig.shape[1], orig.shape[0]))
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
total_pixels = mask_resized.size
|
| 67 |
-
crack_ratio = crack_pixels / total_pixels
|
| 68 |
|
| 69 |
-
|
| 70 |
-
label = "crack"
|
| 71 |
-
confidence = float(crack_ratio)
|
| 72 |
-
else:
|
| 73 |
-
label = "normal"
|
| 74 |
-
confidence = 1.0 - float(crack_ratio)
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
"label": label,
|
| 81 |
-
"confidence": confidence
|
| 82 |
-
}
|
| 83 |
-
]
|
| 84 |
-
}
|
| 85 |
|
| 86 |
-
|
| 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"
|
| 105 |
-
outputs=
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
| 109 |
)
|
| 110 |
|
| 111 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
|
|
|
|
|
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
+
from inference_utils import create_model, inference
|
| 6 |
+
|
| 7 |
+
# ------------------------------
|
| 8 |
+
# 1) 옵션 클래스 (DeepCrack 기본 설정)
|
| 9 |
+
# ------------------------------
|
| 10 |
+
class Opt:
|
| 11 |
+
gpu_ids = []
|
| 12 |
+
isTrain = False
|
| 13 |
+
checkpoints_dir = "."
|
| 14 |
+
model = "deepcrack"
|
| 15 |
+
input_nc = 3
|
| 16 |
+
output_nc = 3
|
| 17 |
+
ngf = 64
|
| 18 |
+
netG = "deepcrack"
|
| 19 |
+
norm = "batch"
|
| 20 |
+
use_dropout = False
|
| 21 |
+
init_type = "normal"
|
| 22 |
+
init_gain = 0.02
|
| 23 |
+
|
| 24 |
+
opt = Opt()
|
| 25 |
+
|
| 26 |
+
# ------------------------------
|
| 27 |
+
# 2) 모델 로드
|
| 28 |
+
# ------------------------------
|
| 29 |
+
print("🔥 Loading DeepCrack model...")
|
| 30 |
+
model = create_model(opt, cp_path="pretrained_net_G.pth")
|
| 31 |
+
print("✅ Model loaded successfully!")
|
| 32 |
+
|
| 33 |
+
# ------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# 3) 예측 함수
|
| 35 |
+
# ------------------------------
|
|
|
|
| 36 |
def predict(img: Image.Image):
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# PIL → bytes 변환
|
| 39 |
+
buf = Image.new("RGB", img.size)
|
| 40 |
+
buf.paste(img)
|
| 41 |
+
bytes_img = cv2.imencode(".jpg", np.array(buf))[1].tobytes()
|
| 42 |
|
| 43 |
+
# 추론 실행
|
| 44 |
+
result_img, visuals = inference(
|
| 45 |
+
model,
|
| 46 |
+
bytes_img,
|
| 47 |
+
dim=img.size,
|
| 48 |
+
unit="px"
|
| 49 |
+
)
|
| 50 |
|
| 51 |
+
# Pillow로 변환하여 출력
|
| 52 |
+
out_img = Image.fromarray(result_img)
|
|
|
|
| 53 |
|
| 54 |
+
# JSON은 “균열 여부 + 확률”만 전달
|
| 55 |
+
prob = float(visuals["fused"].max() / 255.0)
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
has_crack = prob >= 0.5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
return out_img, {
|
| 60 |
+
"hasCrack": has_crack,
|
| 61 |
+
"confidence": round(prob, 4)
|
| 62 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
# ------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
# 4) Gradio Interface
|
| 66 |
+
# ------------------------------
|
|
|
|
| 67 |
demo = gr.Interface(
|
| 68 |
fn=predict,
|
| 69 |
+
inputs=gr.Image(type="pil"),
|
| 70 |
+
outputs=[
|
| 71 |
+
gr.Image(label="Crack Segmentation Output"),
|
| 72 |
+
gr.JSON(label="Prediction")
|
| 73 |
+
],
|
| 74 |
+
title="DeepCrack Segmentation Model",
|
| 75 |
+
description="Detects crack regions and generates segmentation overlays."
|
| 76 |
)
|
| 77 |
|
| 78 |
if __name__ == "__main__":
|