I was facing problem that I was trying to vehicle count in cctv fotage. which was dav format. I did. I use yolo custom train model , & default model also. used bytetrack & supervision for tracking & counting. the problem is my all vehicle was not couting which are crossing line. I know there might be line axis problem but I tried with different axis of counting line. But facing problem. & my video was in dav format which was generated by cc camera. when I process my video a 5 minutes video become 600-700 mb. here I upload some process video. I compressed them. machine video - Google Drive
& here is my code
‘’’
import cv2
import supervision as sv
from ultralytics import YOLO
import numpy as np
Initialize YOLO model and tracker
model = YOLO(“/content/best.pt”) # Specify your model path
tracker = sv.ByteTrack()
class_names = model.names
Annotators and line zone
bounding_box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
trace_annotator = sv.TraceAnnotator(thickness=4)
#start, end = sv.Point(x=75, y=479), sv.Point(x=482, y=162)
#start, end = sv.Point(x=252, y=479), sv.Point(x=266, y=228) accurate more
start, end = sv.Point(x=258, y=478), sv.Point(x=223, y=204)
line_zone = sv.LineZone(start=start, end=end)
Initialize the LineZone for triggering
line_zone = sv.LineZone(
start=start, end=end
)
line_zone_annotator = sv.LineZoneAnnotator(
thickness=4,
text_thickness=4,
text_scale=1
)
line_zone_annotator_multiclass = sv.LineZoneAnnotatorMulticlass(
table_position=sv.Position.TOP_RIGHT,
table_color=sv.Color.BLACK,
text_color=sv.Color.WHITE,
text_scale=0.7,
)
Callback function to process each frame
def process_frame(frame: np.ndarray, index: int) → np.ndarray:
# Perform object detection
results = model(frame)[0]
# Convert results to detections and update tracker
detections = sv.Detections.from_ultralytics(results)
detections = tracker.update_with_detections(detections)
# Construct labels: Class Name and Tracker ID
labels = [
f"{class_names[class_id]} #{tracker_id}"
for tracker_id, class_id in zip(detections.tracker_id, detections.class_id)
]
annotated_frame = frame.copy()
annotated_frame = trace_annotator.annotate(
scene=annotated_frame,
detections=detections)
annotated_frame = bounding_box_annotator.annotate(
scene=annotated_frame,
detections=detections)
annotated_frame = label_annotator.annotate(
scene=annotated_frame,
detections=detections,
labels=labels)
# Update line zone and add to frame
line_zone.trigger(detections)
annotated_frame = line_zone_annotator.annotate(annotated_frame, line_counter=line_zone)
# Annotate with multiclass counts table
annotated_frame = line_zone_annotator_multiclass.annotate(
frame=annotated_frame,
line_zones=[line_zone] # List of line zones for multiclass annotator
)
return annotated_frame
Process the video
sv.process_video(
source_path=“/content/compressed_input_video.mp4”, # Add your input video path
target_path=“output_video_scale7.mp4”, # Add your output video path
callback=process_frame
)
Print results in a formatted way
print(“Line Zone Results:”)
print(“In Count:”)
for class_id, count in line_zone.in_count_per_class.items():
print(f"Class: {class_names[class_id]}, Count: {count}")
print(“\nOut Count:”)
for class_id, count in line_zone.out_count_per_class.items():
print(f"Class: {class_names[class_id]}, Count: {count}")
print(“Line count in class:”, line_zone.in_count_per_class)
print(“Line count out class:”, line_zone.out_count_per_class)
‘’’