ultralytics / ultralytics

NEW - YOLOv8 πŸš€ in PyTorch > ONNX > OpenVINO > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
28.08k stars 5.58k forks source link

Object Counting using YOLOv8 not working properly when multiple objects(same objects) passes at the same time through the line #14291

Open Asad127 opened 1 month ago

Asad127 commented 1 month ago

Search before asking

YOLOv8 Component

Integrations

Bug

image

Environment

No response

Minimal Reproducible Example

No response

Additional

No response

Are you willing to submit a PR?

github-actions[bot] commented 1 month ago

πŸ‘‹ Hello @Asad127, thank you for your interest in Ultralytics YOLOv8 πŸš€! We recommend a visit to the Docs for new users where you can find many Python and CLI usage examples and where many of the most common questions may already be answered.

If this is a πŸ› Bug Report, please provide a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Join the vibrant Ultralytics Discord 🎧 community for real-time conversations and collaborations. This platform offers a perfect space to inquire, showcase your work, and connect with fellow Ultralytics users.

Install

Pip install the ultralytics package including all requirements in a Python>=3.8 environment with PyTorch>=1.8.

pip install ultralytics

Environments

YOLOv8 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

Ultralytics CI

If this badge is green, all Ultralytics CI tests are currently passing. CI tests verify correct operation of all YOLOv8 Modes and Tasks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

glenn-jocher commented 1 month ago

@Asad127 thank you for reporting this issue with object counting in YOLOv8. To help us diagnose and resolve the problem effectively, could you please provide a minimal reproducible example? This will allow us to better understand the context and replicate the issue on our end. You can find guidance on creating a reproducible example here: Minimum Reproducible Example.

Additionally, please ensure that you are using the latest version of the YOLOv8 package, as updates often include important bug fixes and improvements.

In the meantime, here's a quick example of how you might set up object counting with a line to ensure proper functionality:

import cv2
from ultralytics import YOLO, solutions

model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
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
line_points = [(20, 400), (1080, 400)]

# Video writer
video_writer = cv2.VideoWriter("object_counting_output.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))

# Init Object Counter
counter = solutions.ObjectCounter(
    view_img=True,
    reg_pts=line_points,
    classes_names=model.names,
    draw_tracks=True,
    line_thickness=2,
)

while cap.isOpened():
    success, im0 = cap.read()
    if not success:
        print("Video frame is empty or video processing has been successfully completed.")
        break
    tracks = model.track(im0, persist=True, show=False)

    im0 = counter.start_counting(im0, tracks)
    video_writer.write(im0)

cap.release()
video_writer.release()
cv2.destroyAllWindows()

This script sets up a line-based counting region and processes each frame to count objects crossing the line. If you continue to experience issues, please share more details or any error messages you encounter.

Looking forward to your PR and further details to help resolve this! 😊

Asad127 commented 1 month ago

@glenn-jocher Thanks for your response. This scripts work well on the video where objects are at a distance from each other but it completely fails when multiple objects simultaneously pass the line. In this case, the number of In and Out objects are incorrect. I have a video link for your reference. https://drive.google.com/file/d/1-3zzWI6dKyM1qektSuVS0A8BnySk_nRD/view?usp=sharing

glenn-jocher commented 1 month ago

Thank you for providing the video link, @Asad127. It’s crucial to ensure that our object counting solution can handle scenarios with multiple objects crossing the line simultaneously.

To address this, we recommend the following steps:

  1. Update to the Latest Version: Ensure you are using the latest version of the YOLOv8 package, as updates may include important fixes and improvements.

  2. Adjust Tracking Parameters: Fine-tuning the tracking parameters can help improve accuracy in crowded scenes. For example, you can adjust the iou and conf thresholds to better handle overlapping objects.

  3. Provide a Reproducible Example: Sharing a minimal reproducible example will help us diagnose the issue more effectively. You can find guidance on creating one here: Minimum Reproducible Example.

Here’s an enhanced example that includes parameter adjustments:

import cv2
from ultralytics import YOLO, solutions

model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
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
line_points = [(20, 400), (1080, 400)]

# Video writer
video_writer = cv2.VideoWriter("object_counting_output.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))

# Init Object Counter
counter = solutions.ObjectCounter(
    view_img=True,
    reg_pts=line_points,
    classes_names=model.names,
    draw_tracks=True,
    line_thickness=2,
)

while cap.isOpened():
    success, im0 = cap.read()
    if not success:
        print("Video frame is empty or video processing has been successfully completed.")
        break
    tracks = model.track(im0, persist=True, show=False, conf=0.4, iou=0.6)

    im0 = counter.start_counting(im0, tracks)
    video_writer.write(im0)

cap.release()
video_writer.release()
cv2.destroyAllWindows()

Please try these suggestions and let us know if the issue persists. Your feedback is invaluable in helping us improve our solutions. 😊

github-actions[bot] commented 3 weeks ago

πŸ‘‹ Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLO πŸš€ and Vision AI ⭐