Path: blob/master/DNN-OpenCV-Classification-with-Java/README.md
3119 views
This contains the code for Image Classification with OpenCV Java. For more information - visit Image Classification with OpenCV Java
Getting Started
Our code is tested using Python 3.7.5, but it should also work with any other python3.x. If you'd like to check your version run:
Virtual Environment
Let's create a new virtual environment. You'll need to install virtualenv package if you don't have it:
Now we can create a new virtualenv variable and call it env
:
The last thing we have to do is to activate it:
To install the required python dependencies run:
OpenCV
In this blog post we are using OpenCV 4.3.0 unavailable via pip
and OpenCV for Java. That is why, we first need to build the OpenCV library. To do so:
Check the list of the below libraries. Install the missed dependencies:
For OpenCV Java installation we used default Java Runtime Environment and Java Development Kit:
Download the latest OpenCV version from the official repository:
Unzip the downloaded archives:
Rename the directories to match CMake paths:
Compile OpenCV. Create and enter a build directory:
Run CMake to configure the OpenCV build. Don't forget to set the right pass to the
PYTHON_EXECUTABLE
:
If you want to configure the build with some specific Java version, please, add the following fields, verifying the paths:
Check the output and make sure that everything is set correctly. After that we're ready to build it with:
Make sure, you didn't get any errors. In case of successful completion you will find the following files in the build
directory:
bin/opencv-430.jar
lib/libopencv_java430.so
lib/python3/cv2.cpython-37m-x86_64-linux-gnu.so
Then run the following command:
which creates the necessary links and cache to our freshly built shared library.
Put lib/python3/cv2.cpython-37m-x86_64-linux-gnu.so
into the virtual environment installed packages:
The last step is to put ~/opencv/build/lib/libopencv_java430.so
into the /usr/lib
directory:
For Windows and macOS OpenCV Java build, please, follow the steps described in Introduction to Java Development or Installing OpenCV for Java.
Executing Model Conversion and Test Script
The proposed for the experiments Mobilenetv2ToOnnx.py
script supports the --input_image
key to customize the model conversion pipeline. It defines the full input image path, including its name - "coffee.jpg"
by default.
To run MobileNetV2 conversion case, please, choose one of the described below scenarios:
for the custom input image and running evaluation of the converted model:
for the default input image:
Executing DNN OpenCV Java
To compile DnnOpenCV.java
run the following command setting the classpath
key, which value is the full path to the opencv-430.jar
:
javac -cp ":/home/$USER/opencv/build/bin/opencv-430.jar" DnnOpenCV.java
To run the code, please, execute the following line:
java -cp ":/home/$USER/opencv/build/bin/opencv-430.jar" DnnOpenCV
AI Courses by OpenCV
Want to become an expert in AI? AI Courses by OpenCV is a great place to start.