Fix Gradio 6 startup: move css to launch and disable SSR
Browse files
app.py
CHANGED
|
@@ -117,11 +117,71 @@ def draw_detection_chart(detected: int) -> np.ndarray:
|
|
| 117 |
return cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
|
| 118 |
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
def run_check_and_classify(
|
| 121 |
-
image: np.ndarray,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
) -> Tuple[np.ndarray, str, np.ndarray, str, str]:
|
| 123 |
try:
|
| 124 |
attendance = int(attendance_count)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
annotated, detected = infer_image(image, conf_threshold, model_choice)
|
| 126 |
status_presence = classify_presence(detected)
|
| 127 |
status_match = classify_match(detected, attendance)
|
|
@@ -416,7 +476,7 @@ CUSTOM_CSS = """
|
|
| 416 |
"""
|
| 417 |
|
| 418 |
|
| 419 |
-
with gr.Blocks(
|
| 420 |
gr.HTML(
|
| 421 |
"""
|
| 422 |
<h1 style="text-align:center;margin-bottom:4px;">Pengecekan dan Klasifikasi Kehadiran Berbasis YOLOv8</h1>
|
|
@@ -437,6 +497,11 @@ with gr.Blocks(css=CUSTOM_CSS, title="Pengecekan dan Klasifikasi Mahasiswa - YOL
|
|
| 437 |
|
| 438 |
with gr.Column(elem_classes=["section-box"]):
|
| 439 |
gr.Markdown("## Tahap 1 - Akuisisi Data", elem_classes=["section-title"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
model_choice = gr.Dropdown(
|
| 441 |
choices=["YOLOv8n", "Best.pt"],
|
| 442 |
value="YOLOv8n",
|
|
@@ -512,7 +577,7 @@ with gr.Blocks(css=CUSTOM_CSS, title="Pengecekan dan Klasifikasi Mahasiswa - YOL
|
|
| 512 |
|
| 513 |
run_btn.click(
|
| 514 |
fn=run_check_and_classify,
|
| 515 |
-
inputs=[image_input, conf_input, attendance_input, model_choice],
|
| 516 |
outputs=[output_image, class_result_md, comparison_chart, predicted_state, predicted_label_md],
|
| 517 |
)
|
| 518 |
|
|
@@ -535,4 +600,4 @@ with gr.Blocks(css=CUSTOM_CSS, title="Pengecekan dan Klasifikasi Mahasiswa - YOL
|
|
| 535 |
|
| 536 |
|
| 537 |
if __name__ == "__main__":
|
| 538 |
-
demo.launch()
|
|
|
|
| 117 |
return cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
|
| 118 |
|
| 119 |
|
| 120 |
+
def draw_compare_chart(detected_before: int, detected_after: int, attendance: int) -> np.ndarray:
|
| 121 |
+
fig, ax = plt.subplots(figsize=(6.2, 3.4))
|
| 122 |
+
labels = ["YOLOv8n", "Best.pt", "Presensi"]
|
| 123 |
+
values = [detected_before, detected_after, attendance]
|
| 124 |
+
bars = ax.bar(labels, values, color=["#2b6cb0", "#805ad5", "#2f855a"])
|
| 125 |
+
ax.set_title("Perbandingan Sebelum vs Sesudah Training")
|
| 126 |
+
ax.set_ylabel("Jumlah")
|
| 127 |
+
for b in bars:
|
| 128 |
+
ax.text(
|
| 129 |
+
b.get_x() + b.get_width() / 2,
|
| 130 |
+
b.get_height() + 0.05,
|
| 131 |
+
str(int(b.get_height())),
|
| 132 |
+
ha="center",
|
| 133 |
+
va="bottom",
|
| 134 |
+
)
|
| 135 |
+
plt.tight_layout()
|
| 136 |
+
buf = io.BytesIO()
|
| 137 |
+
fig.savefig(buf, format="png", dpi=140)
|
| 138 |
+
plt.close(fig)
|
| 139 |
+
buf.seek(0)
|
| 140 |
+
data = np.frombuffer(buf.read(), dtype=np.uint8)
|
| 141 |
+
img_bgr = cv2.imdecode(data, cv2.IMREAD_COLOR)
|
| 142 |
+
return cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def _merge_annotated_images(left: np.ndarray, right: np.ndarray) -> np.ndarray:
|
| 146 |
+
h = max(left.shape[0], right.shape[0])
|
| 147 |
+
w1 = int(left.shape[1] * (h / left.shape[0]))
|
| 148 |
+
w2 = int(right.shape[1] * (h / right.shape[0]))
|
| 149 |
+
left_r = cv2.resize(left, (w1, h), interpolation=cv2.INTER_AREA)
|
| 150 |
+
right_r = cv2.resize(right, (w2, h), interpolation=cv2.INTER_AREA)
|
| 151 |
+
combined = np.concatenate([left_r, right_r], axis=1)
|
| 152 |
+
cv2.putText(combined, "YOLOv8n", (12, 28), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
|
| 153 |
+
cv2.putText(combined, "Best.pt", (w1 + 12, 28), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 0), 2)
|
| 154 |
+
return combined
|
| 155 |
+
|
| 156 |
+
|
| 157 |
def run_check_and_classify(
|
| 158 |
+
image: np.ndarray,
|
| 159 |
+
conf_threshold: float,
|
| 160 |
+
attendance_count: float,
|
| 161 |
+
model_choice: str,
|
| 162 |
+
inference_mode: str,
|
| 163 |
) -> Tuple[np.ndarray, str, np.ndarray, str, str]:
|
| 164 |
try:
|
| 165 |
attendance = int(attendance_count)
|
| 166 |
+
if inference_mode == "Bandingkan (YOLOv8n vs Best.pt)":
|
| 167 |
+
ann_before, det_before = infer_image(image, conf_threshold, "YOLOv8n")
|
| 168 |
+
ann_after, det_after = infer_image(image, conf_threshold, "Best.pt")
|
| 169 |
+
match_before = classify_match(det_before, attendance)
|
| 170 |
+
match_after = classify_match(det_after, attendance)
|
| 171 |
+
presence_before = classify_presence(det_before)
|
| 172 |
+
presence_after = classify_presence(det_after)
|
| 173 |
+
combined = _merge_annotated_images(ann_before, ann_after)
|
| 174 |
+
chart = draw_compare_chart(det_before, det_after, attendance)
|
| 175 |
+
summary = (
|
| 176 |
+
"### Ringkasan Perbandingan Model\n"
|
| 177 |
+
f"- Presensi input: **{attendance}**\n"
|
| 178 |
+
f"- **YOLOv8n**: terdeteksi={det_before}, keberadaan={presence_before}, validasi={match_before}\n"
|
| 179 |
+
f"- **Best.pt**: terdeteksi={det_after}, keberadaan={presence_after}, validasi={match_after}\n"
|
| 180 |
+
"- Catatan evaluasi: label prediksi untuk confusion matrix menggunakan hasil **Best.pt**."
|
| 181 |
+
)
|
| 182 |
+
pred_md = f"Label prediksi validasi (Best.pt): **{match_after}**"
|
| 183 |
+
return combined, summary, chart, match_after, pred_md
|
| 184 |
+
|
| 185 |
annotated, detected = infer_image(image, conf_threshold, model_choice)
|
| 186 |
status_presence = classify_presence(detected)
|
| 187 |
status_match = classify_match(detected, attendance)
|
|
|
|
| 476 |
"""
|
| 477 |
|
| 478 |
|
| 479 |
+
with gr.Blocks(title="Pengecekan dan Klasifikasi Mahasiswa - YOLOv8") as demo:
|
| 480 |
gr.HTML(
|
| 481 |
"""
|
| 482 |
<h1 style="text-align:center;margin-bottom:4px;">Pengecekan dan Klasifikasi Kehadiran Berbasis YOLOv8</h1>
|
|
|
|
| 497 |
|
| 498 |
with gr.Column(elem_classes=["section-box"]):
|
| 499 |
gr.Markdown("## Tahap 1 - Akuisisi Data", elem_classes=["section-title"])
|
| 500 |
+
inference_mode = gr.Dropdown(
|
| 501 |
+
choices=["Single Model", "Bandingkan (YOLOv8n vs Best.pt)"],
|
| 502 |
+
value="Single Model",
|
| 503 |
+
label="Mode Inferensi",
|
| 504 |
+
)
|
| 505 |
model_choice = gr.Dropdown(
|
| 506 |
choices=["YOLOv8n", "Best.pt"],
|
| 507 |
value="YOLOv8n",
|
|
|
|
| 577 |
|
| 578 |
run_btn.click(
|
| 579 |
fn=run_check_and_classify,
|
| 580 |
+
inputs=[image_input, conf_input, attendance_input, model_choice, inference_mode],
|
| 581 |
outputs=[output_image, class_result_md, comparison_chart, predicted_state, predicted_label_md],
|
| 582 |
)
|
| 583 |
|
|
|
|
| 600 |
|
| 601 |
|
| 602 |
if __name__ == "__main__":
|
| 603 |
+
demo.launch(css=CUSTOM_CSS, ssr_mode=False)
|