firgiawann commited on
Commit
ef62966
·
verified ·
1 Parent(s): 7b2ca4a

Use person instance numbering labels instead of confidence

Browse files
Files changed (1) hide show
  1. app.py +43 -4
app.py CHANGED
@@ -91,6 +91,46 @@ def _resolve_person_class_id(result: Any) -> int:
91
  return 0
92
 
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  def infer_image(image: np.ndarray, conf_threshold: float, model_choice: str) -> Tuple[np.ndarray, int]:
95
  if image is None:
96
  raise ValueError("Silakan unggah gambar atau gunakan kamera.")
@@ -98,9 +138,7 @@ def infer_image(image: np.ndarray, conf_threshold: float, model_choice: str) ->
98
  bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
99
  result = model.predict(source=bgr, conf=conf_threshold, verbose=False)[0]
100
  person_id = _resolve_person_class_id(result)
101
- cls = result.boxes.cls.cpu().numpy() if result.boxes is not None else np.array([])
102
- detected_count = int(np.sum(cls == person_id))
103
- annotated = cv2.cvtColor(result.plot(), cv2.COLOR_BGR2RGB)
104
  return annotated, detected_count
105
 
106
 
@@ -754,7 +792,8 @@ def run_training(
754
  f"- {prep_info}\n"
755
  f"- Run dir: {save_dir}\n"
756
  f"- best.pt: {best_out if best_out.exists() else best_pt}\n"
757
- f"- {hf_sync_msg}"
 
758
  )
759
  return msg, str(best_out) if best_out.exists() else ""
760
  except Exception as err:
 
91
  return 0
92
 
93
 
94
+ def _annotate_person_instances(image_rgb: np.ndarray, result: Any, person_id: int) -> Tuple[np.ndarray, int]:
95
+ annotated = image_rgb.copy()
96
+ if result.boxes is None:
97
+ return annotated, 0
98
+
99
+ classes = result.boxes.cls.cpu().numpy()
100
+ boxes = result.boxes.xyxy.cpu().numpy()
101
+ person_idx = np.where(classes == person_id)[0]
102
+ if len(person_idx) == 0:
103
+ return annotated, 0
104
+
105
+ person_boxes = []
106
+ for i in person_idx:
107
+ x1, y1, x2, y2 = boxes[i].tolist()
108
+ person_boxes.append((int(x1), int(y1), int(x2), int(y2)))
109
+
110
+ # Stabilkan urutan nomor: atas-ke-bawah lalu kiri-ke-kanan.
111
+ person_boxes.sort(key=lambda b: (b[1], b[0]))
112
+
113
+ for idx, (x1, y1, x2, y2) in enumerate(person_boxes, start=1):
114
+ cv2.rectangle(annotated, (x1, y1), (x2, y2), (34, 197, 94), 2)
115
+ label = f"person {idx}"
116
+ (tw, th), baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 2)
117
+ text_y1 = max(0, y1 - th - baseline - 6)
118
+ text_y2 = y1
119
+ text_x2 = min(annotated.shape[1], x1 + tw + 10)
120
+ cv2.rectangle(annotated, (x1, text_y1), (text_x2, text_y2), (34, 197, 94), -1)
121
+ cv2.putText(
122
+ annotated,
123
+ label,
124
+ (x1 + 4, max(14, y1 - 6)),
125
+ cv2.FONT_HERSHEY_SIMPLEX,
126
+ 0.6,
127
+ (0, 0, 0),
128
+ 2,
129
+ cv2.LINE_AA,
130
+ )
131
+ return annotated, len(person_boxes)
132
+
133
+
134
  def infer_image(image: np.ndarray, conf_threshold: float, model_choice: str) -> Tuple[np.ndarray, int]:
135
  if image is None:
136
  raise ValueError("Silakan unggah gambar atau gunakan kamera.")
 
138
  bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
139
  result = model.predict(source=bgr, conf=conf_threshold, verbose=False)[0]
140
  person_id = _resolve_person_class_id(result)
141
+ annotated, detected_count = _annotate_person_instances(image, result, person_id)
 
 
142
  return annotated, detected_count
143
 
144
 
 
792
  f"- {prep_info}\n"
793
  f"- Run dir: {save_dir}\n"
794
  f"- best.pt: {best_out if best_out.exists() else best_pt}\n"
795
+ f"- {hf_sync_msg}\n"
796
+ "- Lanjut uji di inferensi: pilih `Best.pt` atau mode perbandingan untuk melihat peningkatan deteksi person."
797
  )
798
  return msg, str(best_out) if best_out.exists() else ""
799
  except Exception as err: