Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,8 +24,8 @@ line_zone_annotator = sv.LineZoneAnnotator(
|
|
| 24 |
)
|
| 25 |
|
| 26 |
|
| 27 |
-
def process_video(video_path, orientation="
|
| 28 |
-
"""
|
| 29 |
if video_path is None:
|
| 30 |
return None
|
| 31 |
|
|
@@ -59,9 +59,17 @@ def process_video(video_path, orientation="Horizontal"):
|
|
| 59 |
previous_positions = {}
|
| 60 |
class_counts = {name: 0 for name in SELECTED_CLASS_NAMES}
|
| 61 |
crossed_ids = set()
|
|
|
|
| 62 |
|
| 63 |
def callback(frame: np.ndarray, index: int) -> np.ndarray:
|
| 64 |
-
nonlocal previous_positions, class_counts, crossed_ids
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
results = model(frame, verbose=False)[0]
|
| 67 |
detections = sv.Detections.from_ultralytics(results)
|
|
@@ -120,6 +128,8 @@ def process_video(video_path, orientation="Horizontal"):
|
|
| 120 |
(x0 + 10, y0 + 60 + i * 28),
|
| 121 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
|
| 122 |
|
|
|
|
|
|
|
| 123 |
return annotator_frame
|
| 124 |
|
| 125 |
output_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
|
|
@@ -133,23 +143,35 @@ def process_video(video_path, orientation="Horizontal"):
|
|
| 133 |
|
| 134 |
with gr.Blocks(title="Object Detection", theme=gr.themes.Soft()) as demo:
|
| 135 |
gr.Markdown("# 🚗 Nhận dạng phương tiện (YOLOv8 + ByteTrack)")
|
| 136 |
-
gr.Markdown("
|
| 137 |
|
| 138 |
with gr.Row():
|
| 139 |
-
video_input = gr.Video(label="Video
|
| 140 |
-
video_output = gr.Video(label="Video
|
| 141 |
|
| 142 |
orientation_input = gr.Radio(
|
| 143 |
-
choices=["
|
| 144 |
-
value="
|
| 145 |
-
label="
|
| 146 |
)
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
gr.Markdown("""
|
| 152 |
-
###
|
| 153 |
""")
|
| 154 |
|
| 155 |
if __name__ == "__main__":
|
|
|
|
| 24 |
)
|
| 25 |
|
| 26 |
|
| 27 |
+
def process_video(video_path, orientation="Ngang", frame_skip=1):
|
| 28 |
+
"""Xử lý video: nhận dạng phương tiện và trả về video đã annotate."""
|
| 29 |
if video_path is None:
|
| 30 |
return None
|
| 31 |
|
|
|
|
| 59 |
previous_positions = {}
|
| 60 |
class_counts = {name: 0 for name in SELECTED_CLASS_NAMES}
|
| 61 |
crossed_ids = set()
|
| 62 |
+
last_annotated_frame = None
|
| 63 |
|
| 64 |
def callback(frame: np.ndarray, index: int) -> np.ndarray:
|
| 65 |
+
nonlocal previous_positions, class_counts, crossed_ids, last_annotated_frame
|
| 66 |
+
|
| 67 |
+
try:
|
| 68 |
+
fs = max(1, int(frame_skip))
|
| 69 |
+
except Exception:
|
| 70 |
+
fs = 1
|
| 71 |
+
if fs > 1 and index % fs != 0:
|
| 72 |
+
return last_annotated_frame if last_annotated_frame is not None else frame
|
| 73 |
|
| 74 |
results = model(frame, verbose=False)[0]
|
| 75 |
detections = sv.Detections.from_ultralytics(results)
|
|
|
|
| 128 |
(x0 + 10, y0 + 60 + i * 28),
|
| 129 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
|
| 130 |
|
| 131 |
+
last_annotated_frame = annotator_frame
|
| 132 |
+
|
| 133 |
return annotator_frame
|
| 134 |
|
| 135 |
output_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
|
|
|
|
| 143 |
|
| 144 |
with gr.Blocks(title="Object Detection", theme=gr.themes.Soft()) as demo:
|
| 145 |
gr.Markdown("# 🚗 Nhận dạng phương tiện (YOLOv8 + ByteTrack)")
|
| 146 |
+
gr.Markdown("Tải lên video, hệ thống sẽ nhận dạng phương tiện trong video.")
|
| 147 |
|
| 148 |
with gr.Row():
|
| 149 |
+
video_input = gr.Video(label="Video đầu vào")
|
| 150 |
+
video_output = gr.Video(label="Video đã xử lý")
|
| 151 |
|
| 152 |
orientation_input = gr.Radio(
|
| 153 |
+
choices=["Ngang", "Dọc"],
|
| 154 |
+
value="Ngang",
|
| 155 |
+
label="Vị trí line"
|
| 156 |
)
|
| 157 |
|
| 158 |
+
frame_skip_input = gr.Slider(
|
| 159 |
+
minimum=1,
|
| 160 |
+
maximum=10,
|
| 161 |
+
step=1,
|
| 162 |
+
value=1,
|
| 163 |
+
label="Frame skip (1 = xử lý mọi frame)"
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
btn = gr.Button("▶️ Xử lý video")
|
| 167 |
+
btn.click(
|
| 168 |
+
fn=process_video,
|
| 169 |
+
inputs=[video_input, orientation_input, frame_skip_input],
|
| 170 |
+
outputs=video_output
|
| 171 |
+
)
|
| 172 |
|
| 173 |
gr.Markdown("""
|
| 174 |
+
### Sinh viên: Trần Hải Nam - 223332840
|
| 175 |
""")
|
| 176 |
|
| 177 |
if __name__ == "__main__":
|