This tutorial walks you through how to perform real-time video object detection using Ultralytics YOLO11 models in Java via OpenCV’s DNN module. Perfect for Java developers exploring computer vision and deep-learning pipelines.
Versions used in this tutorial:
- JDK 25
- Python 3.14.0
- OpenCV 4.12.0
- YOLO 11
This version of tutorial runs only on Windows OS
OpenCV is a set of libs written in C++ and the compiled into platform-native lib format: *.dll - for Windows, or *.dylib - for Linux / Mac OS. They can be accessed from Java via Java wrapper included into OpenCV distribution.
Object detection pipeline will look like this:
- Download and install Visual Studio Build Tools for C++
The only workolad needed to be installed is Visual Studio Build Tools for C++
This is needed for compiling C++ libs coming with some Python packages
- Download and install Git
Check if Git properly installed:
git --versionDownload and extract OpenCV Distribution (Windows)
Ultralytics YOLO11 models by default available in PyTorch format (*.pt) and for interacting with the model from Java, it should be first exported into ONNX format.
Here is a full ONNX Export for YOLO11 Models article.
python -m pip --version- Create new virtual Python environment
Navigate to your development folder:
cd C:\<my_dev_folder>Tip: Good thing to do when installing custom Python packages - to install them into virtual env, to avoid currupting and lib conflicts with your base Python installation.
python -m venv C:\<my_dev_folder>\venv\myultravenvActivate virtual environment:
myultravenv\Scripts\activate- Install Ultralytics Python package to your newly created virtual env:
pip install ultralytics- Install ONNX Runtime
pip install onnx- Finally, export YOLO11n PyTorch (*.pt) model to ONNX format
yolo export model=yolo11n.pt format=onnx- Deactivate Python virtual environment
deactivateAs result, you should see now two models added to your C:\<my_dev_folder>
yolo11n.ptyolo11n.onnx- this is the one we need!
Open this project in your IDE and add opencv-*.jar lib to the classpath
Note: OpenCV JAR is not available in Maven repo so have to add it to the project manually
The jar is located in OpenCV distribution folder:
...\opencv\build\java\
Add the jar to the project classpath via JAVA PROJECTS menu:
In IntelliJ IDEA it can be done via Project Structure > Modules > Dependencies menu:
In YoloObjectDetector.java:
- Set full path (including filename) to
yolo11*.onnxmodel:
static final String MODEL_PATH = "C:\\<full_path_to_the_model>\\yolo11n.onnx";- Set full path (including filename) to your video file model:
static final String VIDEO_PATH = "C:\\<full_path_to_the_video_file>\\<filename>.mp4";Optional: To add frame by frame processing, awaiting key press, set long delay (30s):
HighGui.waitKey(30000);To run it from VS Code first we have to add native opencv-*.dll to the launch config:
- Edit launch config via Run > Add Configuration... menu:
This will create .vscode\launch.json file in the project folder.
- Add vmArg to the config:
"vmArgs": "-Djava.library.path=C:\\Users\\vadym\\dev\\opencv\\build\\java\\x64"As result launch.json should look like this:

- Run
YoloObjectDetector.main()
To run it from IDEA first we have to add native opencv-*.dll to the project:
-
Navigate to Project Structure > Modules > Dependencies menu, right-click on
opencv-*.jarand click Edit...
-
Click Add and select
opencv_java*.dllfile
The dll is available in OpenCV distribution folder:
...\opencv\build\java\x64\opencv_java4120.dll
As result, you should see native lib added to the list:

- Run
YoloObjectDetector.main()






