Skip to content

vvorobiov/opencv_yolo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Video Object Detection in Java with OpenCV + YOLO11 — full end-to-end tutorial

About

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.

Here is the end result

OpenCV Java YOLO tutorial OpenCV Java YOLO tutorial

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.

Overview

Object detection pipeline will look like this:

Object Detection Pipeline

I. Installation

Tools

  1. 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

  1. Download and install Git

Check if Git properly installed:

git --version

OpenCV

Download and extract OpenCV Distribution (Windows)

YOLO 11

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.

  1. Install lastest Python
  2. Make sure you can run pip - Python package installer:
python -m pip --version
  1. 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\myultravenv

Activate virtual environment:

myultravenv\Scripts\activate
  1. Install Ultralytics Python package to your newly created virtual env:
pip install ultralytics
  1. Install ONNX Runtime
pip install onnx
  1. Finally, export YOLO11n PyTorch (*.pt) model to ONNX format
yolo export model=yolo11n.pt format=onnx
  1. Deactivate Python virtual environment
deactivate

As result, you should see now two models added to your C:\<my_dev_folder>

  • yolo11n.pt
  • yolo11n.onnx - this is the one we need!

II. Project setup

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\

Visual Studio Code

Add the jar to the project classpath via JAVA PROJECTS menu:

Adding OpenCV JAR to the project

IntelliJ IDEA

In IntelliJ IDEA it can be done via Project Structure > Modules > Dependencies menu:

Adding OpenCV JAR to the project

III. Code

In YoloObjectDetector.java:

  1. Set full path (including filename) to yolo11*.onnx model:
static final String MODEL_PATH = "C:\\<full_path_to_the_model>\\yolo11n.onnx";
  1. 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);

IV. Run

Running from Visual Studio Code

To run it from VS Code first we have to add native opencv-*.dll to the launch config:

  1. Edit launch config via Run > Add Configuration... menu:

Adding OpenCV Dll to the project

This will create .vscode\launch.json file in the project folder.

  1. 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: Adding OpenCV Dll to the project

  1. Run YoloObjectDetector.main()

Running from IntelliJ IDEA

To run it from IDEA first we have to add native opencv-*.dll to the project:

  1. Navigate to Project Structure > Modules > Dependencies menu, right-click on opencv-*.jar and click Edit... Adding OpenCV Dll to the project

  2. Click Add and select opencv_java*.dll file

The dll is available in OpenCV distribution folder: ...\opencv\build\java\x64\opencv_java4120.dll

Adding OpenCV Dll to the project

As result, you should see native lib added to the list: Adding OpenCV Dll to the project

  1. Run YoloObjectDetector.main()

Happy Detecting!

About

Video Object Detection in Java with OpenCV + YOLO11 - full end-to-end tutorial

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages