Thompson001 commited on
Commit
75cdfe2
ยท
verified ยท
1 Parent(s): 8f2a680

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -29
app.py CHANGED
@@ -11,44 +11,43 @@ API_URL = f"https://serverless.roboflow.com/{MODEL_ID}"
11
 
12
 
13
  # -----------------------------------
14
- # 2) confidence โ†’ color ๊ทœ์น™
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 ํ˜ธ์ถœ + mask ์‹œ๊ฐํ™” + JSON ์ถœ๋ ฅ
27
  # -----------------------------------
28
  def predict(image: Image.Image):
29
  temp_path = "temp.jpg"
30
  image.save(temp_path)
31
 
32
- # Roboflow API ํ˜ธ์ถœ
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.2 # <-- ์ž„๊ณ„๊ฐ’ ๋‚ฎ์ถ”๊ธฐ
40
- }
41
  )
42
 
43
  result = response.json()
44
  preds = result.get("predictions", [])
45
 
46
- # ๋ฒ ์ด์Šค ์ด๋ฏธ์ง€
47
  base = image.convert("RGBA")
48
- overlay = Image.new("RGBA", base.size, (0, 0, 0, 0))
49
  draw = ImageDraw.Draw(overlay, "RGBA")
50
 
51
- # ์˜ˆ์ธก ์—†์Œ โ†’ ์ •์ƒ ์ฒ˜๋ฆฌ
52
  if not preds:
53
  blended = Image.alpha_composite(base, overlay)
54
  return blended, {
@@ -58,30 +57,32 @@ def predict(image: Image.Image):
58
  }]
59
  }
60
 
61
- # ๊ฐ€์žฅ confidence ๋†’์€ ๊ฐ์ฒด ์„ ํƒ
62
- best = max(preds, key=lambda p: p.get("confidence", 0))
63
-
64
- conf = float(best.get("confidence", 0))
65
- label = best.get("class", "crack")
66
-
67
- # segmentation polygon ์ƒ‰์น 
68
- points = best.get("points")
69
- if points:
70
- polygon = [(p["x"], p["y"]) for p in points]
71
- draw.polygon(polygon, fill=get_color(conf))
72
 
73
  blended = Image.alpha_composite(base, overlay)
74
 
 
 
 
 
 
75
  return blended, {
76
  "data": [{
77
- "label": label,
78
- "confidence": conf
79
  }]
80
  }
81
 
82
 
83
  # -----------------------------------
84
- # 4) Gradio Interface
85
  # -----------------------------------
86
  demo = gr.Interface(
87
  fn=predict,
@@ -90,8 +91,8 @@ demo = gr.Interface(
90
  gr.Image(label="Crack Heatmap"),
91
  gr.JSON(label="Detection Data"),
92
  ],
93
- title="Crack Detection + Heatmap (Roboflow)",
94
- description="Segmentation ๊ธฐ๋ฐ˜ ๊ท ์—ด ์˜์—ญ์„ ์ƒ‰์œผ๋กœ ํ‘œ์‹œํ•˜๊ณ  JSON์„ ๋ฐ˜ํ™˜ํ•˜๋Š” API",
95
  flagging_mode="never"
96
  )
97
 
 
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, {
 
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,
 
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