Thompson001 commited on
Commit
2afc026
ยท
verified ยท
1 Parent(s): 75cdfe2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -83
app.py CHANGED
@@ -1,98 +1,105 @@
1
  import gradio as gr
2
- import requests
3
- from PIL import Image, ImageDraw
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- # -----------------------------------
6
- # 1) Roboflow API ์„ค์ •
7
- # -----------------------------------
8
- API_KEY = "UXTLYuI2sw5z7OtCpXdz"
9
- MODEL_ID = "creacks-eapny/7"
10
- API_URL = f"https://serverless.roboflow.com/{MODEL_ID}"
11
-
12
-
13
- # -----------------------------------
14
- # 2) confidence ์ƒ‰์ƒ ๊ทœ์น™
15
- # -----------------------------------
16
- def get_color(conf):
17
- if conf < 0.30:
18
- return (0, 255, 0, 120) # Green
19
- elif conf < 0.60:
20
- return (255, 165, 0, 120) # Orange
21
  else:
22
- return (255, 0, 0, 120) # Red
23
-
24
-
25
- # -----------------------------------
26
- # 3) Roboflow ํ˜ธ์ถœ + ๋‹ค์ค‘ segmentation ์‹œ๊ฐํ™”
27
- # -----------------------------------
28
- def predict(image: Image.Image):
29
- temp_path = "temp.jpg"
30
- image.save(temp_path)
31
-
32
- # threshold ๋‚ฎ์ถ”๊ธฐ โ†’ crack ๊ฐ์ง€ ํ™œ์„ฑํ™”
33
- with open(temp_path, "rb") as f:
34
- response = requests.post(
35
- API_URL,
36
- files={"file": f},
37
- data={
38
- "api_key": API_KEY,
39
- "confidence": 0.15 # โ˜… ๋งค์šฐ ์ค‘์š”: threshold ๋‚ฎ์ถ”๊ธฐ
40
- }
41
  )
 
42
 
43
- result = response.json()
44
- preds = result.get("predictions", [])
45
-
46
- base = image.convert("RGBA")
47
- overlay = Image.new("RGBA", base.size, (0,0,0,0))
48
- draw = ImageDraw.Draw(overlay, "RGBA")
49
 
50
- # ๊ฐ์ฒด ์—†์Œ โ†’ ์ •์ƒ ์ฒ˜๋ฆฌ
51
- if not preds:
52
- blended = Image.alpha_composite(base, overlay)
53
- return blended, {
54
- "data": [{
55
- "label": "normal",
56
- "confidence": 1.0
57
- }]
58
- }
59
-
60
- # โ‘  ๋‹ค์ค‘ crack ์‹œ๊ฐํ™” (for all preds)
61
- for p in preds:
62
- conf = float(p.get("confidence", 0))
63
- color = get_color(conf)
64
- points = p.get("points")
65
- if points:
66
- polygon = [(pt["x"], pt["y"]) for pt in points]
67
- draw.polygon(polygon, fill=color)
68
-
69
- blended = Image.alpha_composite(base, overlay)
70
-
71
- # โ‘ก ๊ฐ€์žฅ confidence ๋†’์€ crack ํ•˜๋‚˜๋งŒ ํ”„๋ก ํŠธ๋กœ ์ „๋‹ฌ
72
- best = max(preds, key=lambda p: p.get("confidence", 0))
73
- best_conf = float(best.get("confidence", 0))
74
- best_label = best.get("class", "crack")
75
-
76
- return blended, {
77
- "data": [{
78
- "label": best_label,
79
- "confidence": best_conf
80
- }]
81
  }
82
 
83
 
84
- # -----------------------------------
85
- # 4) Gradio UI
86
- # -----------------------------------
87
  demo = gr.Interface(
88
  fn=predict,
89
  inputs=gr.Image(type="pil"),
90
- outputs=[
91
- gr.Image(label="Crack Heatmap"),
92
- gr.JSON(label="Detection Data"),
93
- ],
94
- title="Crack Detection + Multi-Seg Heatmap",
95
- description="์—ฌ๋Ÿฌ ๊ท ์—ด์„ ๊ฐ๊ฐ ๋‹ค๋ฅธ ์ƒ‰์œผ๋กœ ํ‘œ์‹œํ•˜๊ณ , ๊ฐ€์žฅ ๋†’์€ ๊ท ์—ด ํ™•๋ฅ ์„ UI์— ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.",
96
  flagging_mode="never"
97
  )
98
 
 
1
  import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image
4
+ import os
5
+
6
+ # ------------------------------
7
+ # 1) ๋ชจ๋ธ ํŒŒ์ผ ๊ฒฝ๋กœ ์ง€์ •
8
+ # ------------------------------
9
+ MODEL_PATH = "crack_classifier.h5" # ๋˜๋Š” .pt ๋กœ ๊ต์ฒด ๊ฐ€๋Šฅ
10
+
11
+ IS_TF = MODEL_PATH.endswith(".h5") or MODEL_PATH.endswith(".keras")
12
+
13
+ # ------------------------------
14
+ # 2) TensorFlow ๋ชจ๋ธ ๋กœ๋“œ
15
+ # ------------------------------
16
+ if IS_TF:
17
+ import tensorflow as tf
18
+ model = tf.keras.models.load_model(MODEL_PATH)
19
+ print("๐Ÿ”ฅ Loaded TensorFlow crack classifier")
20
+
21
+ # ------------------------------
22
+ # 3) PyTorch ๋ชจ๋ธ ๋กœ๋“œ
23
+ # ------------------------------
24
+ else:
25
+ import torch
26
+ from torch import nn
27
+
28
+ class CNN(nn.Module):
29
+ # ๋„ค๊ฐ€ ๊ฐ€์ง„ ๋ชจ๋ธ ๊ตฌ์กฐ ๋งž๊ฒŒ ์กฐ์ • ํ•„์š”
30
+ def __init__(self):
31
+ super().__init__()
32
+ self.net = nn.Sequential(
33
+ nn.Conv2d(3, 16, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2),
34
+ nn.Conv2d(16, 32, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2),
35
+ nn.Flatten(),
36
+ nn.Linear(32 * 56 * 56, 2) # ์ด๋ฏธ์ง€ ํฌ๊ธฐ ๋งž๊ฒŒ ์กฐ์ • ํ•„์š”
37
+ )
38
+ def forward(self, x):
39
+ return self.net(x)
40
+
41
+ model = CNN()
42
+ model.load_state_dict(torch.load(MODEL_PATH, map_location="cpu"))
43
+ model.eval()
44
+ print("๐Ÿ”ฅ Loaded PyTorch crack classifier")
45
+
46
+
47
+ # ------------------------------
48
+ # 4) ์ „์ฒด ์˜ˆ์ธก ํ•จ์ˆ˜
49
+ # ------------------------------
50
+ def predict(img: Image.Image):
51
+ # ์ž…๋ ฅ ๋ณ€ํ™˜
52
+ img_resized = img.resize((224, 224))
53
+ arr = np.array(img_resized) / 255.0
54
+
55
+ if IS_TF:
56
+ # TensorFlow
57
+ X = arr.reshape(1, 224, 224, 3)
58
+ probs = model.predict(X)[0] # [p_normal, p_crack]
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  else:
61
+ # PyTorch
62
+ import torch
63
+ X = (
64
+ torch.tensor(arr)
65
+ .permute(2, 0, 1)
66
+ .unsqueeze(0)
67
+ .float()
 
 
 
 
 
 
 
 
 
 
 
 
68
  )
69
+ probs = torch.softmax(model(X), dim=1).detach().numpy()[0]
70
 
71
+ p_normal = float(probs[0])
72
+ p_crack = float(probs[1])
 
 
 
 
73
 
74
+ if p_crack > p_normal:
75
+ label = "crack"
76
+ conf = p_crack
77
+ else:
78
+ label = "normal"
79
+ conf = p_normal
80
+
81
+ # ------------------------------
82
+ # ํ”„๋ก ํŠธ๊ฐ€ ์š”๊ตฌํ•˜๋Š” JSON ๊ตฌ์กฐ
83
+ # ------------------------------
84
+ return {
85
+ "data": [
86
+ {
87
+ "label": label,
88
+ "confidence": conf
89
+ }
90
+ ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  }
92
 
93
 
94
+ # ------------------------------
95
+ # 5) Gradio API Interface
96
+ # ------------------------------
97
  demo = gr.Interface(
98
  fn=predict,
99
  inputs=gr.Image(type="pil"),
100
+ outputs=gr.JSON(label="Detection Result"),
101
+ title="Crack Detection Classifier",
102
+ description="์‚ฌ์ง„์„ ์—…๋กœ๋“œํ•˜๋ฉด ๊ท ์—ด/์ •์ƒ ์—ฌ๋ถ€์™€ ํ™•๋ฅ (%)์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.",
 
 
 
103
  flagging_mode="never"
104
  )
105