This project uses the YOLOv8 model to detect objects in a single image and count how many instances of a specific class (e.g., people) are present.
The script loads a pre-trained YOLOv8 model (yolov8s.pt) and performs object detection on an input image. It identifies all detected objects and counts how many times a specific class appears (e.g., class 0 = 'person').
✅ The model file (
yolov8s.pt) will be automatically downloaded the first time you load the model. No need to manually download or place it in your project folder.
cd your_project
python -m venv .venv
/venv/Scripts/activate - Windows
source /venv/bin/activate - LinuxMake sure you have the following libraries installed:
ultralytics: For using the YOLOv8 model.opencv-python(optional): For displaying or saving annotated images.
Install them via pip:
pip install ultralytics opencv-pythonimage_detection_project/
│
├── yolov8s.pt # Pre-trained YOLOv8 model
├── your_image.png # Input image for detection
├── yolo.py # Main script
└── README.md- Place your image in the same directory as the script.
- Run the script:
python yolo.py- The output will show the number of detected objects of a given class (e.g., people).
⚠️ You can change the class index in the code if you want to count different types of objects.
- Detected classes are printed in numeric form by default.
- To get class names (like "person", "car", etc.), use
model.names.