how to save the number of counted objects? #119
-
hello, i took the code to count the objects i have in figure via a linecounter from this video: https://www.youtube.com/watch?v=Mi9iHFd0_Bo I found it very fascinating, but I was wondering how and if it was possible to save the number of objects counted in real time on an external file or similar. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hi @Stefano0513 👋🏻! The simplest idea I have is to create a simple
def append_line_to_file(file_path: str, line: str) -> None:
with open(file_path, "a") as file:
file.write(line + "\n")
line_counter.in_count
line_counter.out_count
import cv2
from ultralytics import YOLO
import supervision as sv
import numpy as np
LINE_START = sv.Point(320, 0)
LINE_END = sv.Point(320, 480)
def main():
line_counter = sv.LineZone(start=LINE_START, end=LINE_END)
line_annotator = sv.LineZoneAnnotator(thickness=2, text_thickness=1, text_scale=0.5)
box_annotator = sv.BoxAnnotator(
thickness=2,
text_thickness=1,
text_scale=0.5
)
model = YOLO("yolov8l.pt")
for result in model.track(source=0, show=True, stream=True, agnostic_nms=True):
frame = result.orig_img
detections = sv.Detections.from_yolov8(result)
if result.boxes.id is not None:
detections.tracker_id = result.boxes.id.cpu().numpy().astype(int)
detections = detections[(detections.class_id != 60) & (detections.class_id != 0)]
labels = [
f"{tracker_id} {model.model.names[class_id]} {confidence:0.2f}"
for _, confidence, class_id, tracker_id
in detections
]
frame = box_annotator.annotate(
scene=frame,
detections=detections,
labels=labels
)
line_counter.trigger(detections=detections)
line_annotator.annotate(frame=frame, line_counter=line_counter)
append_line_to_file('counts.txt', f'{line_counter.in_count}, {line_counter.out_count}')
cv2.imshow("yolov8", frame)
if (cv2.waitKey(30) == 27):
break
if __name__ == "__main__":
main() |
Beta Was this translation helpful? Give feedback.
-
We have documentation, and you can learn here a lot about most of the stuff we support. Unfortunately, |
Beta Was this translation helpful? Give feedback.
thank you very much!! May I ask where can I find a "documentation" about the linezone and all the other instances?