Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ from ultralytics import YOLO
|
|
| 8 |
|
| 9 |
DEFAULT_MAX_FRAME_SIZE = 640
|
| 10 |
DEFAULT_DETECT_EVERY_N_FRAMES = 2
|
|
|
|
| 11 |
|
| 12 |
model = YOLO("yolov8n.pt")
|
| 13 |
CLASS_NAMES_DICT = model.model.names
|
|
@@ -27,6 +28,7 @@ def process_video(
|
|
| 27 |
max_frame_size: int = DEFAULT_MAX_FRAME_SIZE,
|
| 28 |
detect_every_n: int = DEFAULT_DETECT_EVERY_N_FRAMES,
|
| 29 |
line_orientation: str = "Ngang",
|
|
|
|
| 30 |
):
|
| 31 |
if video_path is None:
|
| 32 |
return None
|
|
@@ -49,22 +51,38 @@ def process_video(
|
|
| 49 |
class_counts = {name: 0 for name in SELECTED_CLASS_NAMES}
|
| 50 |
crossed_ids = set()
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
def callback(frame: np.ndarray, index: int) -> np.ndarray:
|
| 53 |
nonlocal previous_positions, class_counts, crossed_ids
|
| 54 |
|
| 55 |
-
if max_frame_size is None or max_frame_size <= 0:
|
| 56 |
-
max_size = DEFAULT_MAX_FRAME_SIZE
|
| 57 |
-
else:
|
| 58 |
-
max_size = int(max_frame_size)
|
| 59 |
-
|
| 60 |
-
if detect_every_n is None or detect_every_n < 1:
|
| 61 |
-
detect_every = 1
|
| 62 |
-
else:
|
| 63 |
-
detect_every = int(detect_every_n)
|
| 64 |
-
|
| 65 |
fh_orig, fw_orig = frame.shape[:2]
|
| 66 |
if use_resize:
|
| 67 |
-
scale = min(1.0,
|
| 68 |
if scale < 1.0:
|
| 69 |
frame_infer = cv2.resize(
|
| 70 |
frame, (int(fw_orig * scale), int(fh_orig * scale))
|
|
@@ -135,7 +153,11 @@ def process_video(
|
|
| 135 |
|
| 136 |
results = model(frame_infer, verbose=False)[0]
|
| 137 |
detections = sv.Detections.from_ultralytics(results)
|
|
|
|
| 138 |
detections = detections[np.isin(detections.class_id, SELECTED_CLASS_IDS)]
|
|
|
|
|
|
|
|
|
|
| 139 |
detections = byte_tracker.update_with_detections(detections)
|
| 140 |
|
| 141 |
if detections.tracker_id is not None:
|
|
@@ -175,7 +197,8 @@ def process_video(
|
|
| 175 |
]
|
| 176 |
|
| 177 |
annotator_frame = frame_infer.copy()
|
| 178 |
-
|
|
|
|
| 179 |
annotator_frame = box_annotator.annotate(scene=annotator_frame, detections=detections)
|
| 180 |
annotator_frame = label_annotator.annotate(
|
| 181 |
scene=annotator_frame, detections=detections, labels=labels
|
|
@@ -278,26 +301,39 @@ with gr.Blocks(title="Nhận dạng phương tiện giao thông", theme=gr.theme
|
|
| 278 |
value="Ngang",
|
| 279 |
label="Hướng phương tiện di chuyển",
|
| 280 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
with gr.Row():
|
| 282 |
max_frame_size = gr.Slider(
|
| 283 |
minimum=320,
|
| 284 |
maximum=1280,
|
| 285 |
value=DEFAULT_MAX_FRAME_SIZE,
|
| 286 |
step=64,
|
| 287 |
-
label="Kích thước tối đa (px)",
|
| 288 |
)
|
| 289 |
detect_every_n = gr.Slider(
|
| 290 |
minimum=1,
|
| 291 |
maximum=5,
|
| 292 |
value=DEFAULT_DETECT_EVERY_N_FRAMES,
|
| 293 |
step=1,
|
| 294 |
-
label="Detect mỗi N frame (
|
| 295 |
)
|
| 296 |
|
| 297 |
btn = gr.Button("▶️ Xử lý video", variant="primary")
|
| 298 |
btn.click(
|
| 299 |
fn=process_video,
|
| 300 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
outputs=video_output,
|
| 302 |
)
|
| 303 |
|
|
|
|
| 8 |
|
| 9 |
DEFAULT_MAX_FRAME_SIZE = 640
|
| 10 |
DEFAULT_DETECT_EVERY_N_FRAMES = 2
|
| 11 |
+
DEFAULT_CONF_THRESHOLD = 0.3
|
| 12 |
|
| 13 |
model = YOLO("yolov8n.pt")
|
| 14 |
CLASS_NAMES_DICT = model.model.names
|
|
|
|
| 28 |
max_frame_size: int = DEFAULT_MAX_FRAME_SIZE,
|
| 29 |
detect_every_n: int = DEFAULT_DETECT_EVERY_N_FRAMES,
|
| 30 |
line_orientation: str = "Ngang",
|
| 31 |
+
performance_mode: str = "Cân bằng", # "Nhanh" | "Cân bằng" | "Đẹp" | "Tuỳ chỉnh"
|
| 32 |
):
|
| 33 |
if video_path is None:
|
| 34 |
return None
|
|
|
|
| 51 |
class_counts = {name: 0 for name in SELECTED_CLASS_NAMES}
|
| 52 |
crossed_ids = set()
|
| 53 |
|
| 54 |
+
# Cấu hình hiệu năng theo chế độ
|
| 55 |
+
if performance_mode == "Nhanh":
|
| 56 |
+
effective_max_size = 480
|
| 57 |
+
detect_every = 4
|
| 58 |
+
conf_threshold = 0.5
|
| 59 |
+
enable_trace = False
|
| 60 |
+
elif performance_mode == "Đẹp":
|
| 61 |
+
effective_max_size = 800
|
| 62 |
+
detect_every = 1
|
| 63 |
+
conf_threshold = 0.3
|
| 64 |
+
enable_trace = True
|
| 65 |
+
elif performance_mode == "Cân bằng":
|
| 66 |
+
effective_max_size = DEFAULT_MAX_FRAME_SIZE
|
| 67 |
+
detect_every = DEFAULT_DETECT_EVERY_N_FRAMES
|
| 68 |
+
conf_threshold = 0.4
|
| 69 |
+
enable_trace = True
|
| 70 |
+
else: # Tuỳ chỉnh
|
| 71 |
+
effective_max_size = (
|
| 72 |
+
int(max_frame_size) if max_frame_size and max_frame_size > 0 else DEFAULT_MAX_FRAME_SIZE
|
| 73 |
+
)
|
| 74 |
+
detect_every = (
|
| 75 |
+
int(detect_every_n) if detect_every_n and detect_every_n >= 1 else 1
|
| 76 |
+
)
|
| 77 |
+
conf_threshold = DEFAULT_CONF_THRESHOLD
|
| 78 |
+
enable_trace = True
|
| 79 |
+
|
| 80 |
def callback(frame: np.ndarray, index: int) -> np.ndarray:
|
| 81 |
nonlocal previous_positions, class_counts, crossed_ids
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
fh_orig, fw_orig = frame.shape[:2]
|
| 84 |
if use_resize:
|
| 85 |
+
scale = min(1.0, effective_max_size / max(fh_orig, fw_orig))
|
| 86 |
if scale < 1.0:
|
| 87 |
frame_infer = cv2.resize(
|
| 88 |
frame, (int(fw_orig * scale), int(fh_orig * scale))
|
|
|
|
| 153 |
|
| 154 |
results = model(frame_infer, verbose=False)[0]
|
| 155 |
detections = sv.Detections.from_ultralytics(results)
|
| 156 |
+
# Lọc theo class quan tâm
|
| 157 |
detections = detections[np.isin(detections.class_id, SELECTED_CLASS_IDS)]
|
| 158 |
+
# Lọc thêm theo confidence để giảm số lượng box
|
| 159 |
+
if len(detections) > 0 and hasattr(detections, "confidence"):
|
| 160 |
+
detections = detections[detections.confidence >= conf_threshold]
|
| 161 |
detections = byte_tracker.update_with_detections(detections)
|
| 162 |
|
| 163 |
if detections.tracker_id is not None:
|
|
|
|
| 197 |
]
|
| 198 |
|
| 199 |
annotator_frame = frame_infer.copy()
|
| 200 |
+
if enable_trace:
|
| 201 |
+
annotator_frame = trace_annotator.annotate(scene=annotator_frame, detections=detections)
|
| 202 |
annotator_frame = box_annotator.annotate(scene=annotator_frame, detections=detections)
|
| 203 |
annotator_frame = label_annotator.annotate(
|
| 204 |
scene=annotator_frame, detections=detections, labels=labels
|
|
|
|
| 301 |
value="Ngang",
|
| 302 |
label="Hướng phương tiện di chuyển",
|
| 303 |
)
|
| 304 |
+
with gr.Row():
|
| 305 |
+
performance_mode = gr.Radio(
|
| 306 |
+
choices=["Nhanh", "Cân bằng", "Đẹp", "Tuỳ chỉnh"],
|
| 307 |
+
value="Cân bằng",
|
| 308 |
+
label="Chế độ hiệu năng",
|
| 309 |
+
)
|
| 310 |
with gr.Row():
|
| 311 |
max_frame_size = gr.Slider(
|
| 312 |
minimum=320,
|
| 313 |
maximum=1280,
|
| 314 |
value=DEFAULT_MAX_FRAME_SIZE,
|
| 315 |
step=64,
|
| 316 |
+
label="Kích thước tối đa (px) (chỉ dùng khi 'Tuỳ chỉnh')",
|
| 317 |
)
|
| 318 |
detect_every_n = gr.Slider(
|
| 319 |
minimum=1,
|
| 320 |
maximum=5,
|
| 321 |
value=DEFAULT_DETECT_EVERY_N_FRAMES,
|
| 322 |
step=1,
|
| 323 |
+
label="Detect mỗi N frame (chỉ dùng khi 'Tuỳ chỉnh')",
|
| 324 |
)
|
| 325 |
|
| 326 |
btn = gr.Button("▶️ Xử lý video", variant="primary")
|
| 327 |
btn.click(
|
| 328 |
fn=process_video,
|
| 329 |
+
inputs=[
|
| 330 |
+
video_input,
|
| 331 |
+
use_resize,
|
| 332 |
+
max_frame_size,
|
| 333 |
+
detect_every_n,
|
| 334 |
+
line_orientation,
|
| 335 |
+
performance_mode,
|
| 336 |
+
],
|
| 337 |
outputs=video_output,
|
| 338 |
)
|
| 339 |
|