MuhFaridanSutariya commited on
Commit
45d532d
1 Parent(s): 7e5c89f

feat: add line counter

Browse files
Files changed (2) hide show
  1. app.py +10 -3
  2. utils.py +4 -2
app.py CHANGED
@@ -3,6 +3,8 @@ from ultralytics import YOLO
3
  from supervision.draw.color import ColorPalette
4
  from supervision.tracker.byte_tracker.core import ByteTrack
5
  from supervision.detection.annotate import BoxAnnotator
 
 
6
  from streamlit_webrtc import webrtc_streamer, RTCConfiguration
7
  from video_processor import VideoProcessor
8
  from utils import process_image, process_video_realtime
@@ -20,6 +22,11 @@ def main():
20
  label_map = model.model.names
21
  byte_tracker = ByteTrack()
22
  box_annotator = BoxAnnotator(color=ColorPalette.default(), thickness=4, text_thickness=4, text_scale=2)
 
 
 
 
 
23
 
24
  source_option = st.selectbox("Select Source Data", ("Webcam", "Image file", "Video file"))
25
 
@@ -57,12 +64,12 @@ def main():
57
  f.write(uploaded_video.read())
58
  st.video(temp_video_path)
59
  if st.button('Process Uploaded Video'):
60
- process_video_realtime(temp_video_path, model, label_map, byte_tracker, box_annotator)
61
  else:
62
  st.header("Demo Video Preview")
63
  st.video(demo_video)
64
  if st.button('Process Demo Video'):
65
- process_video_realtime(demo_video, model, label_map, byte_tracker, box_annotator)
66
 
67
  if __name__ == "__main__":
68
- main()
 
3
  from supervision.draw.color import ColorPalette
4
  from supervision.tracker.byte_tracker.core import ByteTrack
5
  from supervision.detection.annotate import BoxAnnotator
6
+ from supervision.detection.line_counter import LineZone, LineZoneAnnotator
7
+ from supervision.geometry.core import Point
8
  from streamlit_webrtc import webrtc_streamer, RTCConfiguration
9
  from video_processor import VideoProcessor
10
  from utils import process_image, process_video_realtime
 
22
  label_map = model.model.names
23
  byte_tracker = ByteTrack()
24
  box_annotator = BoxAnnotator(color=ColorPalette.default(), thickness=4, text_thickness=4, text_scale=2)
25
+
26
+ line_start = Point(50, 1500)
27
+ line_end = Point(3840 - 50, 1500)
28
+ line_counter = LineZone(start=line_start, end=line_end)
29
+ line_annotator = LineZoneAnnotator(thickness=4, text_thickness=4, text_scale=2)
30
 
31
  source_option = st.selectbox("Select Source Data", ("Webcam", "Image file", "Video file"))
32
 
 
64
  f.write(uploaded_video.read())
65
  st.video(temp_video_path)
66
  if st.button('Process Uploaded Video'):
67
+ process_video_realtime(temp_video_path, model, label_map, byte_tracker, box_annotator, line_counter, line_annotator)
68
  else:
69
  st.header("Demo Video Preview")
70
  st.video(demo_video)
71
  if st.button('Process Demo Video'):
72
+ process_video_realtime(demo_video, model, label_map, byte_tracker, box_annotator, line_counter, line_annotator)
73
 
74
  if __name__ == "__main__":
75
+ main()
utils.py CHANGED
@@ -20,7 +20,7 @@ def process_image(image, model, label_map, byte_tracker, box_annotator):
20
 
21
  return annotated_image, object_counts
22
 
23
- def process_video_realtime(input_video_path, model, label_map, byte_tracker, box_annotator):
24
  video_info = VideoInfo.from_video_path(input_video_path)
25
  generator = get_video_frames_generator(input_video_path)
26
  stframe = st.empty()
@@ -30,5 +30,7 @@ def process_video_realtime(input_video_path, model, label_map, byte_tracker, box
30
  detections = Detections.from_ultralytics(results)
31
  detections = byte_tracker.update_with_detections(detections=detections)
32
  labels = [f"{label_map[class_id]} {confidence:0.2f} -track_id:{tracker_id}" for _, _, confidence, class_id, tracker_id in detections]
 
33
  annotated_frame = box_annotator.annotate(scene=frame, detections=detections, labels=labels)
34
- stframe.image(annotated_frame, channels="BGR")
 
 
20
 
21
  return annotated_image, object_counts
22
 
23
+ def process_video_realtime(input_video_path, model, label_map, byte_tracker, box_annotator, line_counter, line_annotator):
24
  video_info = VideoInfo.from_video_path(input_video_path)
25
  generator = get_video_frames_generator(input_video_path)
26
  stframe = st.empty()
 
30
  detections = Detections.from_ultralytics(results)
31
  detections = byte_tracker.update_with_detections(detections=detections)
32
  labels = [f"{label_map[class_id]} {confidence:0.2f} -track_id:{tracker_id}" for _, _, confidence, class_id, tracker_id in detections]
33
+ line_counter.trigger(detections=detections)
34
  annotated_frame = box_annotator.annotate(scene=frame, detections=detections, labels=labels)
35
+ line_annotator.annotate(frame=annotated_frame, line_counter=line_counter)
36
+ stframe.image(annotated_frame, channels="BGR")