guides/object-counting/ #8112
Replies: 101 comments 377 replies
-
Very cool and simple implementation! |
Beta Was this translation helpful? Give feedback.
-
hey can you tell me how and where to set the args in the above code |
Beta Was this translation helpful? Give feedback.
-
Hi,how can I use object counting with 3 lines and 3 different counters ? Line pedestrianline_pedestrian_1 = [(234,244),(131,218)] |
Beta Was this translation helpful? Give feedback.
-
hello, how can show the inn and out count of classes aswell. i want to do traffic monitoring so i want to get count of nunbers of truck,car,bycycles,bus,moving in and out how can i do that. |
Beta Was this translation helpful? Give feedback.
-
How can I integrate speed estimation of vehicles with in and out counting? |
Beta Was this translation helpful? Give feedback.
-
Hi can I use this to setup my laptop camera for live streaming analysis ? |
Beta Was this translation helpful? Give feedback.
-
User |
Beta Was this translation helpful? Give feedback.
-
Hello, I trained a YOLOv8 on my custom dataset of 3 classes. I need to count detections of each class and display them. I used your code but it is showing error: in <cell line: 27>() TypeError: 'int' object is not iterable Following is my code: MODEL= "/content/drive/MyDrive/content/runs/detect/train/weights/best.pt" model = YOLO(MODEL) classes_to_count = [0,1,2] cap = cv2.VideoCapture("/content/drive/MyDrive/DJI_0495.MP4") line_points = [(1126, 1118), (2786, 1118)] # Adjust these points as needed counter = object_counter.ObjectCounter() while cap.isOpened():
cap.release() ` |
Beta Was this translation helpful? Give feedback.
-
Just want to say this object counter is great! I am having trouble understanding how to keep count on how many of each class (i am tracking two classes) has been counted. Is it possible to do so? :) |
Beta Was this translation helpful? Give feedback.
-
Is there any possibility to crop a image of the objects touching the line? |
Beta Was this translation helpful? Give feedback.
-
It is excellent, and it works great. Thank you. It works great when I run the object count with the standalone video file. But in the same program, when I tried with the RSTP URL, I saw that sometimes objects suddenly disappear and are missing from the frame(Which gives the wrong count). Initially, I thought it was a problem with the feed, and I checked through the extended time with VLC player, and there was no interruption from the rstp feed. This happens only when I run through the object detection example with the feed. Do I have to do any specific settings I have to do? Initially, I thought this was an issue because of the CPU. Later, I moved my code to the Lambda machine, where it runs as CUDA. Still, I see the missing frames. I would appreciate any suggestions/guidance here. I have tried using nano and small models. One more question.. cv2.putText(im0, 'Arrival Count::' + str(arr_counter.in_counts + arr_counter.out_counts), arr_count_loc, Thank you. |
Beta Was this translation helpful? Give feedback.
-
Hi , I'm building a project in which I'm using a WEBRTC protocol to stream it via my input device then converting it to numpy arrays and then running the model on that AI model code which handles the actual detection of defects and their parameters.from ultralytics import YOLO def main():
if name == 'main': |
Beta Was this translation helpful? Give feedback.
-
Is it possible to make a counter that resets every 10 sec and post out the count at the 10sec mark and then start over, repeat. For like a video running 40-50min or more? counting different plants in video Would this work?
|
Beta Was this translation helpful? Give feedback.
-
when i upload model then procssing speed of output video is very low as compared to real video , so how to incease from ultralytics import YOLO Load your modelmodel = YOLO(r"D:\Suraj\yolov8l_walltie.pt") Setup video capturecap = cv2.VideoCapture(r"D:\Suraj\HWvideo.mp4") Define your line points for countingline_points = [(5, 400), (500, 400)] # Adjust these points as needed Initialize Object Counter with desired settingsqcounter = object_counter.ObjectCounter() while cap.isOpened():
cap.release() |
Beta Was this translation helpful? Give feedback.
-
my problem is, i have different classes of vehicle like bus,bike,truck,and threewheeler. now i want to detect this classes into the given image and i want to store count of different classes into a variable. please help me |
Beta Was this translation helpful? Give feedback.
-
Hi, how can i add classes like bike, motorcycle, truck and etc, in the codes there is just two classes. Also i want to count left and right turns in an intersection but don't know how. |
Beta Was this translation helpful? Give feedback.
-
I get the each class count and tracking id in the video frame. I wrote the code for writing the tracking id and count for each class in the text file. only tracking id is written in to the text file. counts for each class is remains zero. import cv2 model = YOLO("best.pt") line_points = [(1000, 0), (1000, 1000)] # line or region points Video writervideo_writer = cv2.VideoWriter("object_counting_output1.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h)) Init Object Countercounter = solutions.ObjectCounter( Initialize a dictionary to count class occurrencesclass_counts = {class_id: 0 for class_id in classes_to_count} while cap.isOpened():
Write the tracking IDs and class counts to a text filewith open("tracking_info.txt", "w") as file: print("Tracking information has been written to tracking_info.txt") cap.release() This is the code. i get the correct count value in video, but i can't get the count value in the text file |
Beta Was this translation helpful? Give feedback.
-
Hi, this is my code for tracking vehicles in a traffic, but at the moment it is also detecting vehicles which are not moving and stationary.
Current code:
|
Beta Was this translation helpful? Give feedback.
-
can anyone tell me whenever I tried to run the code for region based counting in jupyter notebook it's output video doesn't show the region for counting even though it assign individual no. for each boxes enclosing the vehicles which passes a certain region |
Beta Was this translation helpful? Give feedback.
-
hi I hope this message finds you well. I am currently working on a project where I need to use the YOLO v8 model for tracking, counting objects and speed estimating in a video. I combined object counting code with speed estimating code and it worked fine but I need to save the result of the vehicles counted in a CSV file. How can I do it? I tried multiple ways but all of them gave me errors. this is my code now model = YOLO("yolov8x.pt") line_points = [(500, 1100), (3500, 1100)] # line or region points Video writervideo_writer = cv2.VideoWriter("/content/drive/MyDrive/Video/object_counting_output3.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h)) Init Object Countercounter = solutions.ObjectCounter( while cap.isOpened():
cap.release() |
Beta Was this translation helpful? Give feedback.
-
Hi, how can i select the exact coordinates for counting line in my video, i can not place the lines exactly where i need it. |
Beta Was this translation helpful? Give feedback.
-
Hi, how can i put the counting lines exactly where i need it, i mean how it is possible to find the coordonates of the places i want to put the counter line. |
Beta Was this translation helpful? Give feedback.
-
Hello! I used the same model to 1) detect a custom class and 2) count custom class. I noticed that the tracker don't detect the object sometimes, or create duplicates when lighting changes. Here're 2 videos to demonstrate what I meant: 1) moss rose detection 2) moss rose counter.
Can you help me understand what I should look into to address this type of behavior? I used the default arguments in model.predict() for detection. And in model.track() for counting, I used Thank you in advance! |
Beta Was this translation helpful? Give feedback.
-
AttributeError: module 'ultralytics.solutions' has no attribute 'ObjectCounter' |
Beta Was this translation helpful? Give feedback.
-
Hi team, |
Beta Was this translation helpful? Give feedback.
-
Hello, Is it possible to perform detection and tracking in a region of interest, lets say 400x400 pixels in the center of the frame? When I am using the ObejctCounter, I am getting the results related to the specific region which is really great, but the detection and tracking are performing on the whole frame here, my query is, can we detect and track only in the specific region instead of the whole frame? if yes can you provide some insights into it. Thankyou, |
Beta Was this translation helpful? Give feedback.
-
cannot import name 'ObjectCounter' from 'ultralytics.solutions' (E:\YOLOv8\ultralytics\solutions_init_.py) |
Beta Was this translation helpful? Give feedback.
-
Hi! How can I hide the "#" in the labels? I just want to display the name of the class without the "#" in the video. For example, if the label of the detected object shows "lemon#3", I just want it to display "lemon". Here's my code. Thanks and nice work!
|
Beta Was this translation helpful? Give feedback.
-
Hello, I would like to use your program to count grey sections. I have an i9 vPro and a Nvidia A2000. Here's the program I use: import cv2
from ultralytics import YOLO, solutions
MODEL_PATH = r'model\best.pt'
model=YOLO(MODEL_PATH)
cap = cv2.VideoCapture('test_26_50mm.avi')
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
# Define line points
# Conveyor
#line_points = [(0, 800), (2000, 800)]
# Bar line vertical
line_points = [(2000, 0), (2000, 2500)]
# Bar line horizontal
#line_points = [(0,2200), (2500, 2200)]
# Video writer
video_writer = cv2.VideoWriter('results\output_bar_counting.avi', cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
# Init Object Counter
counter = solutions.ObjectCounter(
view_img=False,
reg_pts=line_points,
classes_names=model.names,
draw_tracks=False,
track_thickness=2,
line_thickness=5,
line_dist_thresh = 200,
view_out_counts=False
)
while cap.isOpened():
success, frame = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(frame, persist=True, show=False, tracker = 'botsort.yaml', conf = 0.6)
frame = counter.start_counting(frame, tracks)
video_writer.write(frame)
cap.release()
video_writer.release()
cv2.destroyAllWindows() I would have liked to share some pictures with you, but I couldn't. If you can help me, I would appreciate it. |
Beta Was this translation helpful? Give feedback.
-
Hi , Load a pretrained YOLOv8n model img = Image.open('./image_1.jpg') Define line points width = int(img_array.get(cv2.CAP_PROP_FRAME_WIDTH)) pt11 = (x_line-100, height) region_points = [pt1,pt2,pt11,pt22] Keep track of detected defect IDs def image_pred(image): img_array = np.copy(image_arr)Init Object Countercounter = object_counter.ObjectCounter() Run inference on the sourcetracks = model.track(image, persist=True, show=False,tracker = "bytetrack.yaml",classes = [0,1],verbose=True) # list of Results objects
img_ = counter.start_counting(image, tracks) print(f"In_Counts - {in_counts} and Out_Counts - {out_counts}")Store the processed image as a numpy arrayfor defect_id in result_dict: Display the imageplt.imshow(image) print(result_list)pprint(list(result_dict.values())) image_pred(img) |
Beta Was this translation helpful? Give feedback.
-
guides/object-counting/
Object Counting Using Ultralytics YOLOv8
https://docs.ultralytics.com/guides/object-counting/
Beta Was this translation helpful? Give feedback.
All reactions