trannam1084 commited on
Commit
7198c83
·
verified ·
1 Parent(s): 623838a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -12
app.py CHANGED
@@ -24,8 +24,8 @@ line_zone_annotator = sv.LineZoneAnnotator(
24
  )
25
 
26
 
27
- def process_video(video_path, orientation="Horizontal"):
28
- """Process video: detect objects and return annotated video."""
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("Upload video, hệ thống sẽ nhận dạng phương tiện trong video.")
137
 
138
  with gr.Row():
139
- video_input = gr.Video(label="Video input")
140
- video_output = gr.Video(label="Video output")
141
 
142
  orientation_input = gr.Radio(
143
- choices=["Horizontal", "Vertical"],
144
- value="Horizontal",
145
- label="Line orientation"
146
  )
147
 
148
- btn = gr.Button("▶️ Process 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__":
 
24
  )
25
 
26
 
27
+ def process_video(video_path, orientation="Ngang", frame_skip=1):
28
+ """Xử video: nhận dạng phương tiện 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__":