guides/object-counting/ #8112
Replies: 111 comments 402 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 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.
-
Hello, However, despite several attempts to modify the bot_sort and byte_tracker files, nothing changes. But I don't think I made the right changes. 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 region points
#bar region vertical
#region_points = [(1800, 400), (2100, 400), (2100, 2000), (1800, 2000)]
region_points = [(1800, 400), (2100, 400), (2100, 2000), (1800, 2000)]
#bar region horizontal
#region_points = [(200, 1900), (1900, 1900), (1900,1300), (200, 1300)]
# Video writer
video_writer = cv2.VideoWriter('results\output_bar_counting.avi', cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
# Save time of first identification
first_identified = {}
# Init Object Counter
counter = solutions.ObjectCounter(
view_img=False,
reg_pts=region_points,
classes_names=model.names,
draw_tracks=False,
track_thickness=2,
line_thickness=5,
line_dist_thresh = 200,
view_out_counts=False,
region_thickness=10
)
while cap.isOpened():
success, frame = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
frame_number = cap.get(cv2.CAP_PROP_POS_FRAMES)
tracks = model.track(frame, persist=True, show=False, tracker = 'botsort.yaml', conf = 0.6, vid_stride = 1,iou = 0.9)
"""
for i, box in enumerate(tracks[0].boxes):
obj_id = int(box.id)
if obj_id not in first_identified:
first_identified[obj_id] = frame_number / fps
#print(f"Objet ID {obj_id} identifié à {first_identified[obj_id]:.2f} secondes")
"""
frame = counter.start_counting(frame, tracks)
video_writer.write(frame)
"""
# Display time for identification
for obj_id, time in first_identified.items():
print(f"Objet ID {obj_id} was identifie the first time at {time:.2f} secondes")
"""
cap.release()
video_writer.release()
cv2.destroyAllWindows() I've also tried different methods by adding a ROI or using the "line counter" instead of the "region counter". I would be very grateful if you could help me. my config : i9 vPRO and NVIDIA |
Beta Was this translation helpful? Give feedback.
-
Hello team, |
Beta Was this translation helpful? Give feedback.
-
Hi! 😊 I am trying to figure out how to modify the display of label names in the video. For example, I want to hide the label names so that only the detection boxes appear, or instead of "lemon#4," just display "lemon." I am sharing the following code recommended by @pderrenger, trying to hide everything that is "#" and the associated number, but there are no changes in the video. My GitHub account was suspended for a few days, but I have it back now. Possibly because of this, the conversation couldn't continue, so I will repost just in case.
|
Beta Was this translation helpful? Give feedback.
-
I have a question about the total number of objects detected vs the counts displayed. I have set the polygon region to be the entire video area, but my counts 'in' and 'out' do not equal either the number of boxes drawn and manually counted or the ID number displayed against each object, which is around double the number of boxes actually shown. Could you explain why this is and how to obtain a final number of unique objects detected? Thanks! |
Beta Was this translation helpful? Give feedback.
-
I have a question about the custom model I built. It cannot correctly use the tracking mode (mode.track) for counting. However, the prediction mode (mode.predict) works fine with a confidence level of 0.9. When I tried using yolov8.pt with the tracking mode, it worked as expected, so I think the issue is not with the script. What do you think could be causing this problem? Please advise. |
Beta Was this translation helpful? Give feedback.
-
I'm kinda newbie with computer vision, and I'm trying to adapt the code to detect white cars. Maybe I could do this with track_color arg? And in my case, of an urban avenue at night, maybe due to video quality , many of the cars are not identified. Is there a way to enhance its performance? |
Beta Was this translation helpful? Give feedback.
-
import cv2 from ultralytics import YOLO, solutions model = YOLO("yolov8s.pt") Define line pointsline_points = [(0, 400), (w, 400)] Video writervideo_writer = cv2.VideoWriter("object_counting_output.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h)) Init Object Countercounter = solutions.ObjectCounter( while cap.isOpened():
cap.release() |
Beta Was this translation helpful? Give feedback.
-
Can I use my own trained model ? |
Beta Was this translation helpful? Give feedback.
-
Hello, i want to count the left turn and right turns of the vehicles when passing the counter lines, is it possible to do it with yolo? |
Beta Was this translation helpful? Give feedback.
-
I used the code above and I get this error. |
Beta Was this translation helpful? Give feedback.
-
Hi, how can I switch between objects by id in real time using yolov8? |
Beta Was this translation helpful? Give feedback.
-
I’m working on an object detection and counting application using YOLOv8 and Flask. I want to trigger a function or simply print the count of objects in and out when they pass a predefined line in the video. Here is my current code:
|
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