Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,62 @@
|
|
| 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 |
-
from cv2_utils import getContours
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
# 1) 옵션 클래스 (DeepCrack 기본 설정)
|
| 10 |
-
# ------------------------------
|
| 11 |
class Opt:
|
|
|
|
|
|
|
|
|
|
| 12 |
gpu_ids = []
|
| 13 |
isTrain = False
|
| 14 |
-
|
| 15 |
-
model = "deepcrack"
|
| 16 |
input_nc = 3
|
| 17 |
-
|
| 18 |
ngf = 64
|
| 19 |
-
|
| 20 |
-
norm = "
|
| 21 |
-
use_dropout = False
|
| 22 |
init_type = "normal"
|
| 23 |
init_gain = 0.02
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
# ------------------------------
|
| 30 |
print("🔥 Loading DeepCrack model...")
|
|
|
|
| 31 |
model = create_model(opt, cp_path="pretrained_net_G.pth")
|
| 32 |
-
print("
|
| 33 |
-
|
| 34 |
-
# ------------------------------
|
| 35 |
-
# 3) 예측 함수
|
| 36 |
-
# ------------------------------
|
| 37 |
-
def predict(img: Image.Image):
|
| 38 |
-
|
| 39 |
-
# PIL → bytes 변환
|
| 40 |
-
buf = Image.new("RGB", img.size)
|
| 41 |
-
buf.paste(img)
|
| 42 |
-
bytes_img = cv2.imencode(".jpg", np.array(buf))[1].tobytes()
|
| 43 |
-
|
| 44 |
-
# 추론 실행
|
| 45 |
-
result_img, visuals = inference(
|
| 46 |
-
model,
|
| 47 |
-
bytes_img,
|
| 48 |
-
dim=img.size,
|
| 49 |
-
unit="px"
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
# Pillow로 변환하여 출력
|
| 53 |
-
out_img = Image.fromarray(result_img)
|
| 54 |
|
| 55 |
-
# JSON은 “균열 여부 + 확률”만 전달
|
| 56 |
-
prob = float(visuals["fused"].max() / 255.0)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
}
|
| 64 |
|
| 65 |
-
|
| 66 |
-
#
|
| 67 |
-
# ------------------------------
|
| 68 |
demo = gr.Interface(
|
| 69 |
fn=predict,
|
| 70 |
inputs=gr.Image(type="pil"),
|
| 71 |
-
outputs=
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
title="DeepCrack Segmentation Model",
|
| 76 |
-
description="Detects crack regions and generates segmentation overlays."
|
| 77 |
)
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
+
|
| 5 |
from inference_utils import create_model, inference
|
|
|
|
| 6 |
|
| 7 |
+
# --------- DeepCrack 옵션 구성 ---------
|
|
|
|
|
|
|
| 8 |
class Opt:
|
| 9 |
+
# 기본값
|
| 10 |
+
checkpoints_dir = "./checkpoints"
|
| 11 |
+
name = "deepcrack"
|
| 12 |
gpu_ids = []
|
| 13 |
isTrain = False
|
| 14 |
+
|
|
|
|
| 15 |
input_nc = 3
|
| 16 |
+
num_classes = 1
|
| 17 |
ngf = 64
|
| 18 |
+
|
| 19 |
+
norm = "instance"
|
|
|
|
| 20 |
init_type = "normal"
|
| 21 |
init_gain = 0.02
|
| 22 |
|
| 23 |
+
display_sides = False
|
| 24 |
+
loss_mode = "bce"
|
| 25 |
+
lr = 0.001
|
| 26 |
|
| 27 |
+
|
| 28 |
+
# --------- 모델 로드 ---------
|
|
|
|
| 29 |
print("🔥 Loading DeepCrack model...")
|
| 30 |
+
opt = Opt()
|
| 31 |
model = create_model(opt, cp_path="pretrained_net_G.pth")
|
| 32 |
+
print("🔥 DeepCrack model loaded!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# --------- 예측 함수 ---------
|
| 36 |
+
def predict(img: Image.Image):
|
| 37 |
+
output_img, confidence = inference(model, img)
|
| 38 |
+
|
| 39 |
+
has_crack = confidence > 0.5
|
| 40 |
+
label = "crack" if has_crack else "normal"
|
| 41 |
+
|
| 42 |
+
return {
|
| 43 |
+
"data": [
|
| 44 |
+
{
|
| 45 |
+
"label": label,
|
| 46 |
+
"confidence": float(confidence)
|
| 47 |
+
}
|
| 48 |
+
]
|
| 49 |
}
|
| 50 |
|
| 51 |
+
|
| 52 |
+
# --------- Gradio API 인터페이스 ---------
|
|
|
|
| 53 |
demo = gr.Interface(
|
| 54 |
fn=predict,
|
| 55 |
inputs=gr.Image(type="pil"),
|
| 56 |
+
outputs=gr.JSON(label="Detection Result"),
|
| 57 |
+
title="DeepCrack — Concrete Crack Detection",
|
| 58 |
+
description="딥러닝 기반 콘크리트 균열 segmentation 모델 DeepCrack",
|
| 59 |
+
flagging_mode="never"
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|