People Counting from image. #14265
Replies: 1 comment 2 replies
-
@memoriesbytalha hi there! 👋 Thank you for sharing your code and the detailed error message. It looks like you're encountering a Here's a refined version of your code with a focus on ensuring that class IDs are integers: import cv2
from ultralytics import YOLO, solutions
import os
class CustomObjectCounter(solutions.ObjectCounter):
def __init__(self, classes_names):
super().__init__(
view_img=False, # Disable internal display
reg_pts=[], # No line points
classes_names=classes_names,
draw_tracks=True,
line_thickness=2,
)
def draw_boxes(self, img, boxes, line_thickness=2):
for box in boxes:
x1, y1, x2, y2, cls_id, conf = map(int, box[:6])
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), line_thickness)
def initialize_yolo_model(model_path):
return YOLO(model_path)
def process_image(yolo_model, counter, image):
classes_to_count = [0] # Example classes to count
tracks = yolo_model.track(image, persist=False, show=False, classes=classes_to_count)
# Ensure class IDs are integers
for track in tracks:
track[5] = int(track[5])
image = counter.start_counting(image, tracks)
return image
yolo_model_path = os.getenv('model_path', 'yolov8n.pt')
yolo_model = initialize_yolo_model(yolo_model_path)
# Example usage
if __name__ == "__main__":
yolo_model_path = os.getenv('model_path', 'yolov8n.pt')
yolo_model = initialize_yolo_model(yolo_model_path)
# Replace with your actual class names for detection
counter = CustomObjectCounter(['Person']) # Example class names
# Example image (replace with the path to your actual image file)
image_path = '1.jpg'
image = cv2.imread(image_path)
# Process the image
processed_image = process_image(yolo_model, counter, image)
# Display or save the processed image as needed
cv2.imshow('Processed Image', processed_image)
cv2.waitKey(0)
cv2.destroyAllWindows() Key Changes:
Please verify that your environment is up-to-date with the latest versions of the Ultralytics packages. If the issue persists, providing a minimum reproducible example can be very helpful. You can find more details on creating one here: Minimum Reproducible Example. Feel free to reach out if you have any further questions or run into other issues. Happy coding! 😊 |
Beta Was this translation helpful? Give feedback.
-
2nd day in the world of YOLO and I think I am not made for it:
import cv2
from ultralytics import YOLO, solutions
import os
class CustomObjectCounter(solutions.ObjectCounter):
def init(self, classes_names):
super().init(
view_img=False, # Disable internal display
reg_pts=[], # No line points
classes_names=classes_names,
draw_tracks=True,
line_thickness=2,
)
def initialize_yolo_model(model_path):
return YOLO(model_path)
def process_image(yolo_model, counter, image):
classes_to_count = [0] # Example classes to count
tracks = yolo_model.track(image, persist=False, show=False, classes=classes_to_count)
image = counter.start_counting(image, tracks)
return image
yolo_model_path = os.getenv('model_path', 'yolov8n.pt')
yolo_model = initialize_yolo_model(yolo_model_path)
Example usage
if name == "main":
yolo_model_path = os.getenv('model_path', 'yolov8n.pt')
yolo_model = initialize_yolo_model(yolo_model_path)
This is my code and I am continuosly getting errors:
File "/home/memoriesbytalha/People_counting/scratch/monolytic/new.py", line 45, in
processed_image = process_image(yolo_model, counter, image)
File "/home/memoriesbytalha/People_counting/scratch/monolytic/new.py", line 26, in process_image
image = counter.start_counting(image, tracks)
File "/home/memoriesbytalha/People_counting/source_code/env/lib/python3.10/site-packages/ultralytics/solutions/object_counter.py", line 245, in start_counting
self.extract_and_process_tracks(tracks) # draw region even if no objects
File "/home/memoriesbytalha/People_counting/source_code/env/lib/python3.10/site-packages/ultralytics/solutions/object_counter.py", line 159, in extract_and_process_tracks
self.annotator.box_label(box, label=f"{self.names[cls]}#{track_id}", color=colors(int(track_id), True))
TypeError: list indices must be integers or slices, not float
Beta Was this translation helpful? Give feedback.
All reactions