Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,91 +1,132 @@
|
|
1 |
-
import os
|
2 |
-
import cv2
|
3 |
-
import numpy as np
|
4 |
import gradio as gr
|
5 |
-
import
|
6 |
from ultralytics import YOLO
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
"
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
if __name__ == "__main__":
|
91 |
-
|
|
|
|
|
|
|
|
|
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 |
+
# Label dan Warna untuk tiap class
|
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), # coral or rock - hijau
|
28 |
+
1: (255, 0, 0), # pipeline - merah
|
29 |
+
2: (255, 165, 0), # ripple marks - oranye
|
30 |
+
3: (0, 0, 255) # shipwreck - biru
|
31 |
+
}
|
32 |
+
|
33 |
+
def predict_segmentation_with_legend(image):
|
34 |
+
try:
|
35 |
+
if model is None:
|
36 |
+
print("❌ Model belum dimuat.")
|
37 |
+
return image
|
38 |
+
|
39 |
+
image = image.convert("RGB")
|
40 |
+
print("📥 Gambar diterima:", image.size)
|
41 |
+
|
42 |
+
results = model.predict(image, conf=0.0, iou=0.5)
|
43 |
+
result = results[0]
|
44 |
+
|
45 |
+
masks = result.masks.data.cpu().numpy() if result.masks else []
|
46 |
+
boxes = result.boxes.xyxy.cpu().numpy() if result.boxes else []
|
47 |
+
scores = result.boxes.conf.cpu().numpy() if result.boxes else []
|
48 |
+
class_ids = result.boxes.cls.cpu().numpy().astype(int) if result.boxes else []
|
49 |
+
|
50 |
+
image_np = np.array(image).copy()
|
51 |
+
draw = ImageDraw.Draw(image)
|
52 |
+
|
53 |
+
try:
|
54 |
+
font = ImageFont.truetype("DejaVuSans.ttf", 18)
|
55 |
+
except:
|
56 |
+
font = ImageFont.load_default()
|
57 |
+
|
58 |
+
CONFIDENCE_THRESHOLD = 0.5
|
59 |
+
|
60 |
+
if len(boxes) == 0:
|
61 |
+
draw.text((10, 10), "No detections", fill="red", font=font)
|
62 |
+
return image
|
63 |
+
|
64 |
+
indices_to_use = [i for i, s in enumerate(scores) if s >= CONFIDENCE_THRESHOLD]
|
65 |
+
|
66 |
+
if len(indices_to_use) == 0:
|
67 |
+
top_idx = int(np.argmax(scores))
|
68 |
+
indices_to_use = [top_idx]
|
69 |
+
|
70 |
+
# Tampilkan mask
|
71 |
+
for i in indices_to_use:
|
72 |
+
mask = masks[i]
|
73 |
+
class_id = class_ids[i]
|
74 |
+
color = color_map.get(class_id, (255, 255, 0))
|
75 |
+
mask_pil = Image.fromarray((mask * 255).astype(np.uint8))
|
76 |
+
mask_resized = mask_pil.resize((image_np.shape[1], image_np.shape[0]), resample=Image.BILINEAR)
|
77 |
+
mask_resized = np.array(mask_resized) / 255.0
|
78 |
+
|
79 |
+
color_mask = np.zeros_like(image_np)
|
80 |
+
for c in range(3):
|
81 |
+
color_mask[:, :, c] = mask_resized * color[c]
|
82 |
+
|
83 |
+
image_np = np.where(mask_resized[..., None] > 0.5,
|
84 |
+
image_np * 0.5 + color_mask * 0.5, image_np)
|
85 |
+
|
86 |
+
final_image = Image.fromarray(image_np.astype(np.uint8))
|
87 |
+
draw = ImageDraw.Draw(final_image)
|
88 |
+
|
89 |
+
# Tampilkan box & label
|
90 |
+
for i in indices_to_use:
|
91 |
+
box = boxes[i].astype(int).tolist()
|
92 |
+
score = scores[i]
|
93 |
+
class_id = class_ids[i]
|
94 |
+
label = label_map.get(class_id, str(class_id))
|
95 |
+
color = color_map.get(class_id, (255, 255, 0))
|
96 |
+
text_color = "white" if score >= CONFIDENCE_THRESHOLD else "gray"
|
97 |
+
|
98 |
+
draw.rectangle(box, outline=color, width=2)
|
99 |
+
draw.text((box[0], box[1] - 30), label, fill=text_color, font=font)
|
100 |
+
draw.text((box[0], box[1] - 10), f"Confidence: {score:.2f}", fill=text_color, font=font)
|
101 |
+
|
102 |
+
# Buat legenda
|
103 |
+
legend = Image.new("RGB", (500, 50), (255, 255, 255))
|
104 |
+
draw_legend = ImageDraw.Draw(legend)
|
105 |
+
x = 10
|
106 |
+
for cid, label in label_map.items():
|
107 |
+
draw_legend.rectangle([x, 10, x + 20, 30], fill=color_map[cid])
|
108 |
+
draw_legend.text((x + 25, 10), label, fill="black", font=font)
|
109 |
+
x += 130
|
110 |
+
|
111 |
+
# Gabungkan dengan gambar
|
112 |
+
combined = Image.new("RGB", (final_image.width, final_image.height + 50), (255, 255, 255))
|
113 |
+
combined.paste(final_image, (0, 0))
|
114 |
+
combined.paste(legend, (10, final_image.height))
|
115 |
+
|
116 |
+
return combined
|
117 |
+
|
118 |
+
except Exception as e:
|
119 |
+
print("❌ Error saat segmentasi:", e)
|
120 |
+
return image
|
121 |
+
|
122 |
+
iface = gr.Interface(
|
123 |
+
fn=predict_segmentation_with_legend,
|
124 |
+
inputs=gr.Image(type="pil", label="Upload Citra Side Scan Sonar"),
|
125 |
+
outputs=gr.Image(type="pil", label="Hasil Segmentasi"),
|
126 |
+
title="YOLOv11 Segmentasi Citra Sonar",
|
127 |
+
description="Upload citra sonar dan dapatkan hasil segmentasi lengkap dengan warna mask, label, confidence, dan legenda warna.",
|
128 |
+
allow_flagging="never"
|
129 |
+
)
|
130 |
|
131 |
if __name__ == "__main__":
|
132 |
+
iface.launch(share=True)
|