Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,7 +24,7 @@ line_zone_annotator = sv.LineZoneAnnotator(
|
|
| 24 |
)
|
| 25 |
|
| 26 |
|
| 27 |
-
def process_video(video_path, orientation="Ngang"
|
| 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
|
|
@@ -59,17 +59,9 @@ def process_video(video_path, orientation="Ngang", frame_skip=1):
|
|
| 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
|
| 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,8 +120,6 @@ def process_video(video_path, orientation="Ngang", frame_skip=1):
|
|
| 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
|
|
@@ -155,23 +145,11 @@ with gr.Blocks(title="Object Detection", theme=gr.themes.Soft()) as demo:
|
|
| 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 |
-
###
|
| 175 |
""")
|
| 176 |
|
| 177 |
if __name__ == "__main__":
|
|
|
|
| 24 |
)
|
| 25 |
|
| 26 |
|
| 27 |
+
def process_video(video_path, orientation="Ngang"):
|
| 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
|
|
|
|
| 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 |
(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
|
|
|
|
| 145 |
label="Vị trí line"
|
| 146 |
)
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
btn = gr.Button("▶️ Xử lý video")
|
| 149 |
+
btn.click(fn=process_video, inputs=[video_input, orientation_input], outputs=video_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
gr.Markdown("""
|
| 152 |
+
### Author: Trần Hải Nam - 223332840
|
| 153 |
""")
|
| 154 |
|
| 155 |
if __name__ == "__main__":
|