Thompson001 commited on
Commit
69a5ea6
·
verified ·
1 Parent(s): 94c8bb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -49
app.py CHANGED
@@ -1,63 +1,63 @@
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__":
63
- demo.launch()
 
1
+ # app.py
2
+ from fastapi import FastAPI, File, UploadFile
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ from ultralytics import YOLO
5
+ import uvicorn
6
  import numpy as np
7
  from PIL import Image
8
+ import io
9
 
10
+ app = FastAPI()
11
 
12
+ # CORS 활성화 (ConcreteAI 웹과 연결)
13
+ app.add_middleware(
14
+ CORSMiddleware,
15
+ allow_origins=["*"],
16
+ allow_credentials=True,
17
+ allow_methods=["*"],
18
+ allow_headers=["*"],
19
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ # ---- YOLOv8 모델 로드 ----
22
+ print("🔵 Loading YOLOv8 crack model...")
23
+ model = YOLO("keremberke/yolov8n-concrete-crack")
24
+ print("✅ Model loaded!")
25
+
26
+ @app.post("/predict")
27
+ async def predict(img: UploadFile = File(...)):
28
+ # 이미지 읽기
29
+ image_bytes = await img.read()
30
+ image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
31
+ np_img = np.array(image)
32
+
33
+ # YOLOv8 추론
34
+ results = model(np_img)[0]
35
+
36
+ # 박스 리스트
37
+ detections = results.boxes
38
+
39
+ if detections is None or len(detections) == 0:
40
+ # 균열 없음
41
+ return {
42
+ "data": [
43
+ {
44
+ "label": "normal",
45
+ "confidence": 1.0
46
+ }
47
+ ]
48
+ }
49
+
50
+ # 가장 높은 confidence 선택
51
+ max_conf = float(max(d.conf[0].item() for d in detections))
52
 
53
  return {
54
  "data": [
55
  {
56
+ "label": "crack",
57
+ "confidence": max_conf
58
  }
59
  ]
60
  }
61
 
 
 
 
 
 
 
 
 
 
 
 
62
  if __name__ == "__main__":
63
+ uvicorn.run(app, host="0.0.0.0", port=7860)