Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ from ultralytics import YOLO
|
|
| 8 |
|
| 9 |
DEFAULT_MAX_FRAME_SIZE = 640
|
| 10 |
DEFAULT_DETECT_EVERY_N_FRAMES = 2
|
| 11 |
-
DEFAULT_ZONE_MARGIN = 0.10
|
| 12 |
|
| 13 |
model = YOLO("yolov8n.pt")
|
| 14 |
CLASS_NAMES_DICT = model.model.names
|
|
@@ -76,7 +76,6 @@ def process_video(
|
|
| 76 |
frame_infer = frame
|
| 77 |
|
| 78 |
fh, fw = frame_infer.shape[:2]
|
| 79 |
-
# Vị trí line theo hướng người dùng chọn
|
| 80 |
if line_orientation == "Dọc":
|
| 81 |
line_pos = int(fw * 0.5)
|
| 82 |
is_horizontal = False
|
|
@@ -84,7 +83,6 @@ def process_video(
|
|
| 84 |
line_pos = int(fh * 0.5)
|
| 85 |
is_horizontal = True
|
| 86 |
|
| 87 |
-
# Vùng đếm (zone) quanh line, dày theo tỉ lệ zone_margin
|
| 88 |
if zone_margin is None or zone_margin <= 0:
|
| 89 |
zm_ratio = DEFAULT_ZONE_MARGIN
|
| 90 |
else:
|
|
@@ -101,7 +99,6 @@ def process_video(
|
|
| 101 |
|
| 102 |
if detect_every > 1 and index % detect_every != 0:
|
| 103 |
annotator_frame = frame_infer.copy()
|
| 104 |
-
# Vẽ vùng đếm (zone) + line trung tâm (không cập nhật đếm để tiết kiệm CPU)
|
| 105 |
overlay_zone = annotator_frame.copy()
|
| 106 |
if is_horizontal:
|
| 107 |
cv2.rectangle(
|
|
@@ -205,7 +202,6 @@ def process_video(
|
|
| 205 |
detections = detections[np.isin(detections.class_id, SELECTED_CLASS_IDS)]
|
| 206 |
detections = byte_tracker.update_with_detections(detections)
|
| 207 |
|
| 208 |
-
# Đếm theo loại khi đi vào vùng đếm (zone) quanh line
|
| 209 |
if detections.tracker_id is not None:
|
| 210 |
xyxy = detections.xyxy
|
| 211 |
for i in range(len(detections)):
|
|
@@ -215,7 +211,6 @@ def process_video(
|
|
| 215 |
cx = (xyxy[i, 0] + xyxy[i, 2]) / 2
|
| 216 |
cy = (xyxy[i, 1] + xyxy[i, 3]) / 2
|
| 217 |
|
| 218 |
-
# Đếm một lần khi ID lần đầu đi vào vùng đếm
|
| 219 |
if cls_name in SELECTED_CLASS_NAMES and tid not in counted_ids:
|
| 220 |
if is_horizontal and z_top <= cy <= z_bot:
|
| 221 |
class_counts[cls_name] = class_counts.get(cls_name, 0) + 1
|
|
@@ -238,7 +233,6 @@ def process_video(
|
|
| 238 |
scene=annotator_frame, detections=detections, labels=labels
|
| 239 |
)
|
| 240 |
|
| 241 |
-
# Vẽ vùng đếm (zone) + line trung tâm
|
| 242 |
overlay_zone = annotator_frame.copy()
|
| 243 |
if is_horizontal:
|
| 244 |
cv2.rectangle(
|
|
|
|
| 8 |
|
| 9 |
DEFAULT_MAX_FRAME_SIZE = 640
|
| 10 |
DEFAULT_DETECT_EVERY_N_FRAMES = 2
|
| 11 |
+
DEFAULT_ZONE_MARGIN = 0.10
|
| 12 |
|
| 13 |
model = YOLO("yolov8n.pt")
|
| 14 |
CLASS_NAMES_DICT = model.model.names
|
|
|
|
| 76 |
frame_infer = frame
|
| 77 |
|
| 78 |
fh, fw = frame_infer.shape[:2]
|
|
|
|
| 79 |
if line_orientation == "Dọc":
|
| 80 |
line_pos = int(fw * 0.5)
|
| 81 |
is_horizontal = False
|
|
|
|
| 83 |
line_pos = int(fh * 0.5)
|
| 84 |
is_horizontal = True
|
| 85 |
|
|
|
|
| 86 |
if zone_margin is None or zone_margin <= 0:
|
| 87 |
zm_ratio = DEFAULT_ZONE_MARGIN
|
| 88 |
else:
|
|
|
|
| 99 |
|
| 100 |
if detect_every > 1 and index % detect_every != 0:
|
| 101 |
annotator_frame = frame_infer.copy()
|
|
|
|
| 102 |
overlay_zone = annotator_frame.copy()
|
| 103 |
if is_horizontal:
|
| 104 |
cv2.rectangle(
|
|
|
|
| 202 |
detections = detections[np.isin(detections.class_id, SELECTED_CLASS_IDS)]
|
| 203 |
detections = byte_tracker.update_with_detections(detections)
|
| 204 |
|
|
|
|
| 205 |
if detections.tracker_id is not None:
|
| 206 |
xyxy = detections.xyxy
|
| 207 |
for i in range(len(detections)):
|
|
|
|
| 211 |
cx = (xyxy[i, 0] + xyxy[i, 2]) / 2
|
| 212 |
cy = (xyxy[i, 1] + xyxy[i, 3]) / 2
|
| 213 |
|
|
|
|
| 214 |
if cls_name in SELECTED_CLASS_NAMES and tid not in counted_ids:
|
| 215 |
if is_horizontal and z_top <= cy <= z_bot:
|
| 216 |
class_counts[cls_name] = class_counts.get(cls_name, 0) + 1
|
|
|
|
| 233 |
scene=annotator_frame, detections=detections, labels=labels
|
| 234 |
)
|
| 235 |
|
|
|
|
| 236 |
overlay_zone = annotator_frame.copy()
|
| 237 |
if is_horizontal:
|
| 238 |
cv2.rectangle(
|