Are you sure you want to delete this access key?
This example demonstrates how to implement Ultralytics YOLOv8 object detection using OpenCV in Python, leveraging the ONNX (Open Neural Network Exchange) model format for efficient inference.
Follow these simple steps to get the example running on your local machine.
Clone the Repository: If you haven't already, clone the Ultralytics repository to access the example code:
git clone https://github.com/ultralytics/ultralytics.git
cd ultralytics/examples/YOLOv8-OpenCV-ONNX-Python/
Install Requirements:
Install the necessary Python packages listed in the requirements.txt
file. We recommend using a virtual environment.
pip install -r requirements.txt
Run the Detection Script:
Execute the main Python script, specifying the ONNX model path and the input image. A pre-exported yolov8n.onnx
model is included for convenience.
python main.py --model yolov8n.onnx --img image.jpg
The script will perform object detection on image.jpg
using the yolov8n.onnx
model and display the results.
If you want to use a different Ultralytics YOLOv8 model or one you've trained yourself, you need to export it to the ONNX format first.
Install Ultralytics:
If you don't have it installed, get the latest ultralytics
package:
pip install ultralytics
Export the Model:
Use the yolo export
command to convert your desired model (e.g., yolov8n.pt
) to ONNX. Ensure you specify opset=12
or higher for compatibility with OpenCV's DNN module. You can find more details in the Ultralytics Export documentation.
yolo export model=yolov8n.pt imgsz=640 format=onnx opset=12
This command will generate a yolov8n.onnx
file (or the corresponding name for your model) in your working directory. You can then use this .onnx
file with the main.py
script.
Contributions are welcome! If you find any issues or have suggestions for improvement, please feel free to open an issue or submit a pull request to the main Ultralytics repository. Thank you for helping us make Ultralytics YOLO even better!
Press p or to see the previous file or, n or to see the next file
Browsing data directories saved to S3 is possible with DAGsHub. Let's configure your repository to easily display your data in the context of any commit!
ultralytics is now integrated with AWS S3!
Are you sure you want to delete this access key?
Browsing data directories saved to Google Cloud Storage is possible with DAGsHub. Let's configure your repository to easily display your data in the context of any commit!
ultralytics is now integrated with Google Cloud Storage!
Are you sure you want to delete this access key?
Browsing data directories saved to Azure Cloud Storage is possible with DAGsHub. Let's configure your repository to easily display your data in the context of any commit!
ultralytics is now integrated with Azure Cloud Storage!
Are you sure you want to delete this access key?
Browsing data directories saved to S3 compatible storage is possible with DAGsHub. Let's configure your repository to easily display your data in the context of any commit!
ultralytics is now integrated with your S3 compatible storage!
Are you sure you want to delete this access key?