qonitasal commited on
Commit
1f0179c
·
verified ·
1 Parent(s): b6a5674

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -15
app.py CHANGED
@@ -1,3 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def predict_segmentation_with_legend(image):
2
  try:
3
  if model is None:
@@ -16,7 +49,6 @@ def predict_segmentation_with_legend(image):
16
  class_ids = result.boxes.cls.cpu().numpy().astype(int) if result.boxes else []
17
 
18
  image_np = np.array(image).copy()
19
- draw = ImageDraw.Draw(image)
20
 
21
  try:
22
  font = ImageFont.truetype("DejaVuSans.ttf", 18)
@@ -26,16 +58,16 @@ def predict_segmentation_with_legend(image):
26
  CONFIDENCE_THRESHOLD = 0.5
27
 
28
  if len(boxes) == 0:
 
29
  draw.text((10, 10), "No detections", fill="red", font=font)
30
  return image
31
 
 
32
  indices_to_use = [i for i, s in enumerate(scores) if s >= CONFIDENCE_THRESHOLD]
33
-
34
  if len(indices_to_use) == 0:
35
- top_idx = int(np.argmax(scores))
36
- indices_to_use = [top_idx]
37
 
38
- # Tampilkan mask
39
  for i in indices_to_use:
40
  mask = masks[i]
41
  class_id = class_ids[i]
@@ -52,12 +84,10 @@ def predict_segmentation_with_legend(image):
52
  image_np * 0.5 + color_mask * 0.5, image_np)
53
 
54
  final_image = Image.fromarray(image_np.astype(np.uint8)).convert("RGBA")
55
-
56
- # Layer teks transparan
57
  overlay = Image.new("RGBA", final_image.size, (255, 255, 255, 0))
58
  draw_overlay = ImageDraw.Draw(overlay)
59
 
60
- # Tampilkan box & label
61
  for i in indices_to_use:
62
  box = boxes[i].astype(int).tolist()
63
  score = scores[i]
@@ -66,10 +96,9 @@ def predict_segmentation_with_legend(image):
66
  color = color_map.get(class_id, (255, 255, 0))
67
  text_color = "white"
68
 
69
- # Gambar kotak
70
  draw_overlay.rectangle(box, outline=color + (255,), width=2)
71
 
72
- # Teks dan ukurannya
73
  label_text = label
74
  score_text = f"Conf: {score:.2f}"
75
  label_size = draw_overlay.textbbox((0, 0), label_text, font=font)
@@ -78,7 +107,6 @@ def predict_segmentation_with_legend(image):
78
  text_x = max(box[0], 0)
79
  text_y = max(box[1] - (label_size[3] + score_size[3] + 8), 0)
80
 
81
- # Background teks semi-transparan
82
  draw_overlay.rectangle(
83
  [text_x - 2, text_y - 2,
84
  text_x + max(label_size[2], score_size[2]) + 4,
@@ -86,14 +114,12 @@ def predict_segmentation_with_legend(image):
86
  fill=(0, 0, 0, 160)
87
  )
88
 
89
- # Tulis teks
90
  draw_overlay.text((text_x, text_y), label_text, fill=text_color, font=font)
91
  draw_overlay.text((text_x, text_y + label_size[3] + 2), score_text, fill=text_color, font=font)
92
 
93
- # Gabungkan overlay ke gambar
94
  final_image = Image.alpha_composite(final_image, overlay).convert("RGB")
95
 
96
- # Buat legenda
97
  legend = Image.new("RGB", (500, 50), (255, 255, 255))
98
  draw_legend = ImageDraw.Draw(legend)
99
  x = 10
@@ -102,7 +128,7 @@ def predict_segmentation_with_legend(image):
102
  draw_legend.text((x + 25, 10), label, fill="black", font=font)
103
  x += 130
104
 
105
- # Gabungkan dengan gambar
106
  combined = Image.new("RGB", (final_image.width, final_image.height + 50), (255, 255, 255))
107
  combined.paste(final_image, (0, 0))
108
  combined.paste(legend, (10, final_image.height))
@@ -112,3 +138,15 @@ def predict_segmentation_with_legend(image):
112
  except Exception as e:
113
  print("❌ Error saat segmentasi:", e)
114
  return image
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image, ImageDraw, ImageFont
3
+ from ultralytics import YOLO
4
+ import numpy as np
5
+ import os
6
+
7
+ # === Load Model ===
8
+ try:
9
+ model_path = "best.pt"
10
+ if not os.path.exists(model_path):
11
+ raise FileNotFoundError(f"❌ File {model_path} tidak ditemukan. Upload 'best.pt' ke root.")
12
+ model = YOLO(model_path)
13
+ print("✅ Model loaded successfully!")
14
+ except Exception as e:
15
+ print("❌ Gagal load model:", e)
16
+ model = None
17
+
18
+ # === Peta Label dan Warna ===
19
+ label_map = {
20
+ 0: "coral or rock",
21
+ 1: "pipeline",
22
+ 2: "ripple marks",
23
+ 3: "shipwreck"
24
+ }
25
+
26
+ color_map = {
27
+ 0: (0, 255, 0), # Hijau
28
+ 1: (255, 0, 0), # Merah
29
+ 2: (255, 165, 0), # Oranye
30
+ 3: (0, 0, 255) # Biru
31
+ }
32
+
33
+ # === Fungsi Prediksi dan Visualisasi ===
34
  def predict_segmentation_with_legend(image):
35
  try:
36
  if model is None:
 
49
  class_ids = result.boxes.cls.cpu().numpy().astype(int) if result.boxes else []
50
 
51
  image_np = np.array(image).copy()
 
52
 
53
  try:
54
  font = ImageFont.truetype("DejaVuSans.ttf", 18)
 
58
  CONFIDENCE_THRESHOLD = 0.5
59
 
60
  if len(boxes) == 0:
61
+ draw = ImageDraw.Draw(image)
62
  draw.text((10, 10), "No detections", fill="red", font=font)
63
  return image
64
 
65
+ # Ambil index dengan skor tertinggi atau di atas threshold
66
  indices_to_use = [i for i, s in enumerate(scores) if s >= CONFIDENCE_THRESHOLD]
 
67
  if len(indices_to_use) == 0:
68
+ indices_to_use = [int(np.argmax(scores))]
 
69
 
70
+ # === MASKING ===
71
  for i in indices_to_use:
72
  mask = masks[i]
73
  class_id = class_ids[i]
 
84
  image_np * 0.5 + color_mask * 0.5, image_np)
85
 
86
  final_image = Image.fromarray(image_np.astype(np.uint8)).convert("RGBA")
 
 
87
  overlay = Image.new("RGBA", final_image.size, (255, 255, 255, 0))
88
  draw_overlay = ImageDraw.Draw(overlay)
89
 
90
+ # === Gambar box dan label ===
91
  for i in indices_to_use:
92
  box = boxes[i].astype(int).tolist()
93
  score = scores[i]
 
96
  color = color_map.get(class_id, (255, 255, 0))
97
  text_color = "white"
98
 
 
99
  draw_overlay.rectangle(box, outline=color + (255,), width=2)
100
 
101
+ # Teks dan background transparan
102
  label_text = label
103
  score_text = f"Conf: {score:.2f}"
104
  label_size = draw_overlay.textbbox((0, 0), label_text, font=font)
 
107
  text_x = max(box[0], 0)
108
  text_y = max(box[1] - (label_size[3] + score_size[3] + 8), 0)
109
 
 
110
  draw_overlay.rectangle(
111
  [text_x - 2, text_y - 2,
112
  text_x + max(label_size[2], score_size[2]) + 4,
 
114
  fill=(0, 0, 0, 160)
115
  )
116
 
 
117
  draw_overlay.text((text_x, text_y), label_text, fill=text_color, font=font)
118
  draw_overlay.text((text_x, text_y + label_size[3] + 2), score_text, fill=text_color, font=font)
119
 
 
120
  final_image = Image.alpha_composite(final_image, overlay).convert("RGB")
121
 
122
+ # === Buat legenda ===
123
  legend = Image.new("RGB", (500, 50), (255, 255, 255))
124
  draw_legend = ImageDraw.Draw(legend)
125
  x = 10
 
128
  draw_legend.text((x + 25, 10), label, fill="black", font=font)
129
  x += 130
130
 
131
+ # === Gabungkan gambar & legenda ===
132
  combined = Image.new("RGB", (final_image.width, final_image.height + 50), (255, 255, 255))
133
  combined.paste(final_image, (0, 0))
134
  combined.paste(legend, (10, final_image.height))
 
138
  except Exception as e:
139
  print("❌ Error saat segmentasi:", e)
140
  return image
141
+
142
+ iface = gr.Interface(
143
+ fn=predict_segmentation_with_legend,
144
+ inputs=gr.Image(type="pil", label="Upload Citra Side Scan Sonar"),
145
+ outputs=gr.Image(type="pil", label="Hasil Segmentasi"),
146
+ title="YOLOv8/YOLOv11 Segmentasi Citra Sonar",
147
+ description="Upload citra sonar dan dapatkan hasil segmentasi lengkap dengan bounding box, mask, label, confidence, dan legenda warna.",
148
+ allow_flagging="never"
149
+ )
150
+
151
+ if __name__ == "__main__":
152
+ iface.launch(share=True)