Comments (4)
π Hello @zixindh, 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):
- Notebooks with free GPU:
- Google Cloud Deep Learning VM. See GCP Quickstart Guide
- Amazon Deep Learning AMI. See AWS Quickstart Guide
- Docker Image. See Docker Quickstart Guide
Status
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.
from ultralytics.
@zixindh hi there! π
Thank you for providing a detailed description and the code snippet. Itβs great to see your enthusiasm for using YOLOv8 for object counting. Let's address the issue you're facing with counting objects moving 'out' of the defined line.
Steps to Troubleshoot:
-
Verify Latest Versions:
Ensure you are using the latest versions oftorch
andultralytics
. You can upgrade them using:pip install --upgrade torch ultralytics
-
Check Line Orientation:
The direction of movement detection might be influenced by the orientation and position of the line. Ensure that the line coordinates are correctly set to differentiate between 'in' and 'out' movements. -
Debugging with Visualization:
To better understand the issue, visualize the line and the detected objects. This can help verify if the objects are correctly crossing the line.
Example Code with Debugging:
Hereβs an updated version of your code with additional debugging and visualization to help identify the issue:
import cv2
from ultralytics import YOLO
from ultralytics.solutions import object_counter
import os
# Set working directory
new_directory = r'C:\Python\yolov8'
os.chdir(new_directory)
# Load the pre-trained YOLOv8 model
model = YOLO(r"C:\OneDrive\Python\models\yolov8l.pt")
# Open the video file
cap = cv2.VideoCapture(r"C:\OneDrive\Python\videos\0305out15s.mp4")
assert cap.isOpened(), "Error reading video file"
# Get video properties: width, height, and frames per second (fps)
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 points for a line or region of interest in the video frame
line_points = [(20, 400), (1080, 400)] # Line coordinates
# Specify classes to count, for example: person (0) and umbrella (25)
classes_to_count = [0, 25] # Class IDs for person and umbrella
# Initialize the video writer to save the output video
video_writer = cv2.VideoWriter("inandoutline.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
# Initialize the Object Counter with visualization options and other parameters
counter = object_counter.ObjectCounter(view_img=True,
reg_pts=line_points,
classes_names=model.names,
draw_tracks=True)
# Process video frames in a loop
while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
# Perform object tracking on the current frame, filtering by specified classes
tracks = model.track(im0, persist=True, show=False, classes=classes_to_count)
# Use the Object Counter to count objects in the frame and get the annotated image
im0 = counter.start_counting(im0, tracks)
# Draw the line for visualization
cv2.line(im0, line_points[0], line_points[1], (0, 255, 0), 2)
# Write the annotated frame to the output video
video_writer.write(im0)
# Display the frame for debugging
cv2.imshow("Frame", im0)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the video capture and writer objects
cap.release()
video_writer.release()
# Close all OpenCV windows
cv2.destroyAllWindows()
Additional Tips:
- Ensure Correct Line Placement: Make sure the line is placed correctly in the frame to capture the crossings accurately.
- Adjust Line Thickness: You can adjust the line thickness using the
--line_thickness
argument to ensure itβs visible in the video.
If the issue persists, please provide more details or any specific errors you encounter. This will help us further diagnose the problem. For more detailed guidance, you can refer to our Region Counting Guide.
Happy coding! π
from ultralytics.
Thank you @glenn-jocher for your prompt response. I've tested the updated code and unfortunately, the issue remains unresolved. Specifically, the 'out' count erroneously increases when people enter. While there are no errors in the code itself, the counts for entry and exit directions are not being tracked correctly. The entry count ('In') is accurate, but the exit count ('Out') is incorrectly tallying some entering individuals as exiting. To clarify, individuals moving in an upward direction should be counted as 'Out', and those moving in a downward direction should be counted as 'In'. I will further review the object_counter.py to find ways to solve this. but if you have more insights please share as well.
from ultralytics.
Hi @zixindh,
Thank you for your detailed follow-up. I appreciate your efforts in testing the updated code and providing clear feedback. Let's address the issue with the 'In' and 'Out' counts.
Steps to Troubleshoot and Resolve:
-
Direction-Based Counting:
Ensure that the counting logic inobject_counter.py
correctly differentiates between upward and downward movements. The direction of movement should be determined based on the change in the object's position relative to the defined line. -
Modify Counting Logic:
Update the counting logic to accurately track the direction of movement. Here's an example of how you can modify theObjectCounter
class to achieve this:
class ObjectCounter:
def __init__(self, view_img=False, reg_pts=None, classes_names=None, draw_tracks=False):
# Initialization code...
self.in_count = 0
self.out_count = 0
self.previous_positions = {}
def start_counting(self, im0, tracks):
for track in tracks:
track_id = track['id']
bbox = track['bbox']
x_center = (bbox[0] + bbox[2]) / 2
y_center = (bbox[1] + bbox[3]) / 2
if track_id in self.previous_positions:
prev_x, prev_y = self.previous_positions[track_id]
if prev_y < self.reg_pts[0][1] and y_center >= self.reg_pts[0][1]:
self.in_count += 1
elif prev_y > self.reg_pts[0][1] and y_center <= self.reg_pts[0][1]:
self.out_count += 1
self.previous_positions[track_id] = (x_center, y_center)
# Draw counts on the frame
cv2.putText(im0, f'In: {self.in_count}', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.putText(im0, f'Out: {self.out_count}', (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
return im0
- Verify Line Orientation:
Ensure that the line coordinates are correctly set to differentiate between 'In' and 'Out' movements. The y-coordinates of the line should be consistent with the direction of movement you want to track.
Additional Tips:
- Debugging with Visualization: Continue visualizing the line and the detected objects to ensure the logic is working as expected.
- Test with Different Videos: Test the updated logic with different videos to ensure robustness.
If the issue persists, please share any specific observations or additional details that might help us further diagnose the problem. Your proactive approach in reviewing the object_counter.py
is commendable, and I'm here to assist with any further insights you might need.
Happy coding! π
from ultralytics.
Related Issues (20)
- Failed to call AMP HOT 1
- When converting the ncnn model in Windows , the pnnx.exe system cannot move files to different disk drives HOT 1
- problem when loading my quantized model HOT 8
- Validation script reporting near-perfect results for mismatched model and dataset HOT 7
- How do you combine yolov8 with tracking algorithms other than botsort and bytetrack? HOT 6
- Pre & Post Processing (Yolov8 OBB, TFLite C++) HOT 5
- While using track and persist=True, different detections based on image position HOT 3
- Why the reasoning speed of yolov8-seg is getting slower and slower? HOT 6
- How to use FASTSAM with camera HOT 3
- Cannot get bounding boxes but `show` can still display the detected objects HOT 2
- Oriented Bounding Boxes for Cross Detection HOT 5
- Training a model using ARM64 devices utilizes only one core HOT 7
- Add hardware support for ARM64 NPUs (Hailo8L or RK3855 NPU) HOT 1
- Deployment of training nodes in a Kuberentes Cluster HOT 4
- yolo_world HOT 2
- The problem of weight transfer in YOLOv8s backbone HOT 36
- Export - Ultralytics YOLOv8 model to TFJS HOT 3
- Application of SAHI in YOLOV8-OBB mission HOT 1
- Frame drop when increasing the number of streams HOT 4
- How to ReID a person and visualize his route across multiple cameras in live time HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ultralytics.