This repository contains a lightweight, efficient script for detecting persons in images using the YOLOv8 Nano model in ONNX format. It is designed to be simple to understand and easy to run/deploy without needing the full Ultralytics YOLO package.
person_detection_using_yolov8n_onnx/
├── models/
│ └── yolov8n.onnx # Pre-trained YOLOv8 Nano model
├── detect.py # Main detection script
├── test.jpg # Sample input image
├── README.md # This documentation
└── ...
To run this code, you need Python installed along with the following libraries:
- OpenCV (
cv2): For image processing and visualization. - NumPy: For numerical operations and array manipulation.
- ONNX Runtime: For loading and running the ONNX model efficiently.
You can install the required dependencies using pip:
pip install opencv-python numpy onnxruntime(Note: If you have a GPU, you might want to install onnxruntime-gpu instead for faster inference).
-
Place your image: Ensure you have an image named
test.jpgin the project root directory (or update theIMAGE_PATHvariable indetect.pyto point to your image). -
Run the script:
python detect.py
-
View Results: The script will open a window named "Result" showing the detected persons with bounding boxes and confidence scores. It will also print the total count of detected persons in the console.
This script (detect.py) is capable of:
- Loading the Model: It initializes an ONNX Inference Session with
models/yolov8n.onnx. - Preprocessing:
- The input image is resized to 640x640 (the standard input size for YOLOv8).
- It is converted from BGR to RGB.
- Pixel values are normalized to
[0, 1]. - Approximately reshapes the dimensions to
(Batch, Channels, Height, Width).
- Inference: The processed image is passed through the model to get raw predictions.
- Post-Processing:
- Filtering: We iterate through the predictions and only keep those that match the Person class (Class ID
0) and have a confidence score higher than0.5. - Bounding Box Scaling: The coordinates are scaled back from the 640x640 model size to the original image dimensions.
- NMS (Non-Maximum Suppression): We use
cv2.dnn.NMSBoxesto remove overlapping boxes and keep only the best detection for each person.
- Filtering: We iterate through the predictions and only keep those that match the Person class (Class ID
- Visualization: Finally, it draws green bounding boxes and labels on the image and displays the result.
You can easily tweak these variables at the top of detect.py:
CONF_THRESHOLD: Minimum confidence score to detect a person (default:0.5).NMS_THRESHOLD: Threshold for removing overlapping boxes (default:0.45).IMAGE_PATH: Path to the input image.
Created for review and educational purposes.