qonitasal commited on
Commit
1f0f7e8
·
verified ·
1 Parent(s): ee47cdd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -9
app.py CHANGED
@@ -28,12 +28,15 @@ color_map = {
28
  def predict_segmentation(image_pil):
29
  image_pil = image_pil.convert("RGB")
30
  image_np = np.array(image_pil)
31
-
32
  results = model.predict(image_pil, conf=0.25, iou=0.5)
33
  result = results[0]
34
 
35
- if result.masks is None:
36
- return image_pil
 
 
 
 
37
 
38
  masks = result.masks.data.cpu().numpy()
39
  boxes = result.boxes.xyxy.cpu().numpy()
@@ -53,20 +56,18 @@ def predict_segmentation(image_pil):
53
  for c in range(3):
54
  overlay[:, :, c] = np.where(mask > 0, overlay[:, :, c] * 0.5 + color[c] * 0.5, overlay[:, :, c])
55
 
56
- # Gambar bounding box
57
  cv2.rectangle(overlay, (box[0], box[1]), (box[2], box[3]), color, 2)
58
-
59
- # Label + score
60
  text = f"{label} {score:.2f}"
61
  cv2.putText(overlay, text, (box[0], box[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
62
 
63
- # Tambah legend
64
  legend_height = 60
65
  h, w, _ = overlay.shape
66
  combined = np.ones((h + legend_height, w, 3), dtype=np.uint8) * 255
67
  combined[:h] = overlay
68
 
69
- # Gambar legenda
70
  x = 10
71
  for cid, label in label_map.items():
72
  color = color_map[cid]
@@ -82,7 +83,7 @@ iface = gr.Interface(
82
  inputs=gr.Image(type="pil"),
83
  outputs=gr.Image(type="pil"),
84
  title="YOLOv8 Segmentasi Sonar",
85
- description="Upload citra Side Scan Sonar. Akan ditampilkan: mask, bounding box, confidence score, dan legenda kelas."
86
  )
87
 
88
  if __name__ == "__main__":
 
28
  def predict_segmentation(image_pil):
29
  image_pil = image_pil.convert("RGB")
30
  image_np = np.array(image_pil)
 
31
  results = model.predict(image_pil, conf=0.25, iou=0.5)
32
  result = results[0]
33
 
34
+ # Jika tidak ada deteksi sama sekali
35
+ if result.boxes is None or len(result.boxes) == 0 or result.masks is None:
36
+ image_np = np.array(image_pil)
37
+ overlay = image_np.copy()
38
+ cv2.putText(overlay, "No detection found", (30, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 255), 3)
39
+ return Image.fromarray(overlay)
40
 
41
  masks = result.masks.data.cpu().numpy()
42
  boxes = result.boxes.xyxy.cpu().numpy()
 
56
  for c in range(3):
57
  overlay[:, :, c] = np.where(mask > 0, overlay[:, :, c] * 0.5 + color[c] * 0.5, overlay[:, :, c])
58
 
59
+ # Gambar bounding box dan label
60
  cv2.rectangle(overlay, (box[0], box[1]), (box[2], box[3]), color, 2)
 
 
61
  text = f"{label} {score:.2f}"
62
  cv2.putText(overlay, text, (box[0], box[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
63
 
64
+ # Tambahkan legend
65
  legend_height = 60
66
  h, w, _ = overlay.shape
67
  combined = np.ones((h + legend_height, w, 3), dtype=np.uint8) * 255
68
  combined[:h] = overlay
69
 
70
+ # Legend
71
  x = 10
72
  for cid, label in label_map.items():
73
  color = color_map[cid]
 
83
  inputs=gr.Image(type="pil"),
84
  outputs=gr.Image(type="pil"),
85
  title="YOLOv8 Segmentasi Sonar",
86
+ description="Upload citra Side Scan Sonar. Jika terdeteksi objek, akan ditampilkan mask, box, dan confidence. Jika tidak, akan muncul pesan."
87
  )
88
 
89
  if __name__ == "__main__":