how i run opencv and javacv on linux with maven, java and ...€¦ · how i run opencv and javacv...

Post on 23-May-2020

35 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

anderssonfredrik233@gmail.com

How I run OpenCV and JavaCV onLinux with Maven, Java and Eclipse

(version 1.0.0)Description Version

First version 1.0.0

Table of ContentsAbout the author...................................................................................................................................1Preface..................................................................................................................................................1Prerequisites.........................................................................................................................................2

Git....................................................................................................................................................2Java..................................................................................................................................................2Maven..............................................................................................................................................3Cmake..............................................................................................................................................4Eclipse..............................................................................................................................................6

My goal with this..................................................................................................................................6OpenCV - example...............................................................................................................................6

Step 1 – get the code........................................................................................................................6Step 2 – run cmake...........................................................................................................................9Step 3 – run make..........................................................................................................................10Step 4 – install the jar-file..............................................................................................................11Step 5 – install the so-file...............................................................................................................11Step 6 – create a Maven project.....................................................................................................12Step 7 – add some code..................................................................................................................22

JavaCV – example..............................................................................................................................23Step 1 – create a new Maven project.............................................................................................23Step 2 – add some code..................................................................................................................29

About the authorMy name is Fredrik Andersson, and I do some coding, mainly Java. I live in Sweden out of Stockholm with my family. I appreciate feedback, and comments to improve this.

PrefaceI tried to follow the guide at:https://docs.opencv.org/4.0.1/d9/d52/tutorial_java_dev_intro.htmlBut I never got it to work without to tweak the guide a bit. I are also more familiar with Maven and Eclipse, so I thought that this paper might help some one else as well.

anderssonfredrik233@gmail.com

Prerequisites

GitI think Git was preinstalled with my Linux Mint 19. How ever I use:

JavaYou will need to install Java. I use:

I also put these lines in my “.bashrc” for my Java installation to my PATH (else you will not get the needed files for Java when you build the project)

JAVA_HOME=/opt/java/jdk1.8.0_181

export JAVA_HOME

PATH=$PATH:$JAVA_HOME

anderssonfredrik233@gmail.com

MavenI have also installed Maven, from the Software Manager in Linux Mint:

So I got this:

anderssonfredrik233@gmail.com

CmakeI installed cmake, from the Software Manager in Linux Mint:

So I got this:

anderssonfredrik233@gmail.com

EclipseI use Eclipse Photon and downloaded it from:https://www.eclipse.org

My goal with thisMy main goal was to be able to use OpenCV from Java on my Linux. I will also attempt to run the same Java example but with dependency to JavaCV that I guess will be equal. The tricky bit is to get and place the file libopencv_javaXXX.so at the right location since it is needed.

OpenCV - example

Step 1 – get the codeAs in the guide I refereed to above I started to clone the opencv-repo.

git clone git://github.com/opencv/opencv.git

I took a look at the available branches, and as you see there are 3 branches:

anderssonfredrik233@gmail.com

I also took a look at what was the last commit in the log:

In opposite to the guide mentioned above I choose not to checkout the branch 2.4. When I did that I did not got it to work. (I tried with branch 3.4 and that worked fine with some tweak of the code to test it.) But I would like to get the latest so I stayed on the master branch. So I did:

cd opencvmkdir buildcd build

Step 2 – run cmakeI run the following command:

cmake -DBUILD_SHARED_LIBS=OFF ..

That gave me a long output and when I examine that I found that Java-libs will be built. If your Java installation is not on the PATH “java” will not be present in the following output and the important jar-file will not be built.

anderssonfredrik233@gmail.com

Step 3 – run makeNow it is time to build what we are looking for, the jar file and the native dynamic lib:

• opencv-XXX.jar• libopencv_javaXXX.so•

I run the following command:

make -j8

After a long while, like 10 minutes, it is done and I could for e.g. see this in the output:

anderssonfredrik233@gmail.com

And I find the files I need at:

I do not know why the opencv-410.jar is duplicated, but it do not bother me. At this time we also see what version, 4.1.0, that was built.

Step 4 – install the jar-fileInstall the newly build jar-file in your local maven repo. I run the following command:

cd bin

mvn install:install-file -Dfile=opencv-410.jar -DgroupId=org.opencv -DartifactId=opencv -Dversion=4.1.0 -Dpackaging=jar

Step 5 – install the so-fileWhen I print out the java.library.path on my machine (the path where nativie libs will be loaded from) with this code.

String nativeLibPath = System.getProperty( "java.library.path" );System.out.println( "java.library.path: " + nativeLibPath );

I get:

/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib

I first thought I could add my so-file to that path during runtime with a call to:

public static void addNativeLib(){

String nativeLib = System.getProperty( "java.library.path" );String opencvNativeLib = "/home/fredrik/cpp-projects/opencv/build/lib";String combinedNativeLib = nativeLib + ":" +opencvNativeLib;System.setProperty( "java.library.path", combinedNativeLib);

}

But it does not seem to work, I guess the libs from the path is loaded only at start time. But I bet you could load more libs during runtime. But right now I have not figure it out.

anderssonfredrik233@gmail.com

So instead I put my so-file in one of the “libs” that already is at the path.

Oh! So I need to be root to put it in that folder, okay.

Step 6 – create a Maven project

Let’s create a Maven project and import it into Eclipse. First create a dir for the project, I use to do like this:

anderssonfredrik233@gmail.com

Second I run this neat command to create the project structure:

mvn archetype:generate -DgroupId=se.albinoni.opencv -DartifactId=opencv-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Running that gave me this project structure:

anderssonfredrik233@gmail.com

Third I open the pom.xml and add the dependency to the jar we installed locally.

<dependency>

<groupId>org.opencv</groupId>

<artifactId>opencv</artifactId>

<version>4.1.0</version>

</dependency>

Now enter the dir opencv-app and run the maven-eclipse-command to set up the project for eclipse.

mvn eclipse:eclipse

anderssonfredrik233@gmail.com

After a while it is done.

Now import the project into Eclipse.

anderssonfredrik233@gmail.com

Create the resources folder (this step might be better to do before mvn eclipse:eclipse. then I guess you do not need to add it to the build path described further down):

In this folder we need to insert the file lbpcascade_frontalface.xml from the build of opencv. I found it at: /home/fredrik/cpp-projects/opencv/data/lbpcascades

I copy the file like this:

I also download the test image lena.png into the resources folder from:

https://docs.opencv.org/4.0.1/d9/d52/tutorial_java_dev_intro.html

anderssonfredrik233@gmail.com

I also set the resources folder to be on the build path in Eclipse. Right click the folder resources and select “Build Path > Use as Source Folder”.

anderssonfredrik233@gmail.com

Step 7 – add some codeI rip the example code from the tutorial and put it in my class App.java.

package se.albinoni.opencv;

import org.opencv.core.Core;import org.opencv.core.Mat;import org.opencv.core.MatOfRect;import org.opencv.core.Point;import org.opencv.core.Rect;import org.opencv.core.Scalar;import org.opencv.imgcodecs.Imgcodecs;import org.opencv.imgproc.Imgproc;import org.opencv.objdetect.CascadeClassifier;

/** * Hello world! * */public class App {

public App(){

// Create a face detector from the cascade file in the resources // directory. CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("/lbpcascade_frontalface.xml").getPath()); Mat image = Imgcodecs.imread(getClass().getResource("/lena.png").getPath()); // Detect faces in the image. // MatOfRect is a special container class for Rect. MatOfRect faceDetections = new MatOfRect(); faceDetector.detectMultiScale(image, faceDetections); System.out.println(String.format("Detected %s faces", faceDetections.toArray().length)); // Draw a bounding box around each face. for (Rect rect : faceDetections.toArray()) { Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0)); } // Save the visualized detection. String filename = "faceDetection.png"; System.out.println(String.format("Writing %s", filename)); Imgcodecs.imwrite(filename, image);

}

public static void main( String[] args ) { //Print out the paths to where to find native libaries String nativeLibPath = System.getProperty( "java.library.path" ); System.out.println( "java.library.path: " + nativeLibPath );

//Print out the name of the native libary System.out.println( "Name of the native libary to be loaded: " + Core.NATIVE_LIBRARY_NAME );

// Load the native library . System.loadLibrary(Core.NATIVE_LIBRARY_NAME); //Do the magic App app = new App(); }}

Run it!Then open the created the file faceDetection.png and see if it detected a face. Bingo?

anderssonfredrik233@gmail.com

JavaCV – exampleMy next goal is to do the “same” but with JavaCV that is available at the maven central repository. The idea with JavaCV is that you do not need to build the jar-file your self.

Step 1 – create a new Maven projectI create a new project as above with this neat command (remark the root-dir):

mvn archetype:generate -DgroupId=se.albinoni.javacv -DartifactId=javacv-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

That gave me, similar as before, this project structure:

anderssonfredrik233@gmail.com

Second I open up the pom.xml and add the dependecy for javacv. (I found the dependency at https://mvnrepository.com/artifact/org.bytedeco/javacv/1.4.4, it seems to be the latest at this time)

<dependency>

<groupId>org.bytedeco</groupId>

<artifactId>javacv</artifactId>

<version>1.4.4</version>

</dependency>

anderssonfredrik233@gmail.com

Now enter the dir javacv-app and run the maven-eclipse-command to set up the project for eclipse.

mvn eclipse:eclipse

After a while it is done:

anderssonfredrik233@gmail.com

Now import the project into Eclipse:

anderssonfredrik233@gmail.com

Create the resources folder (this step might be better to do before mvn eclipse:eclipse. then I guess you do not need to add it to the build path described further down):

In this folder we need to insert the file lbpcascade_frontalface.xml from the build of opencv. I found it at: /home/fredrik/cpp-projects/opencv/data/lbpcascades

I copy the file like this:

I also download the test image lena.png into the resources folder from https://docs.opencv.org/4.0.1/d9/d52/tutorial_java_dev_intro.html

anderssonfredrik233@gmail.com

I also set the resources folder to be on the build path in Eclipse. Right click the folder resources and select “Build Path > Use as Source Folder”

anderssonfredrik233@gmail.com

Step 2 – add some codeI rip the example code from the tutorial and put it in my class App.java.

package se.albinoni.javacv;

import org.opencv.core.Core;import org.opencv.core.Mat;import org.opencv.core.MatOfRect;import org.opencv.core.Point;import org.opencv.core.Rect;import org.opencv.core.Scalar;import org.opencv.imgcodecs.Imgcodecs;import org.opencv.imgproc.Imgproc;import org.opencv.objdetect.CascadeClassifier;

/** * Hello world! * */public class App {

public App(){

// Create a face detector from the cascade file in the resources // directory. CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("/lbpcascade_frontalface.xml").getPath()); Mat image = Imgcodecs.imread(getClass().getResource("/lena.png").getPath()); // Detect faces in the image. // MatOfRect is a special container class for Rect. MatOfRect faceDetections = new MatOfRect(); faceDetector.detectMultiScale(image, faceDetections); System.out.println(String.format("Detected %s faces", faceDetections.toArray().length)); // Draw a bounding box around each face. for (Rect rect : faceDetections.toArray()) { Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0)); } // Save the visualized detection. String filename = "faceDetection.png"; System.out.println(String.format("Writing %s", filename)); Imgcodecs.imwrite(filename, image);

}

public static void main( String[] args ) { //Print out the paths to where to find native libaries String nativeLibPath = System.getProperty( "java.library.path" ); System.out.println( "java.library.path: " + nativeLibPath );

//Print out the name of the native libary System.out.println( "Name of the native libary to be loaded: " + Core.NATIVE_LIBRARY_NAME );

// Load the native library . System.loadLibrary(Core.NATIVE_LIBRARY_NAME); //Do the magic App app = new App(); }}

Run it.

anderssonfredrik233@gmail.com

This time it will give a error, The error we see this time is because of a other name of the so-file. In the opencv-4.1.0.jar (the one we built and installed in our mvn-repo) the static member of the class Core.NATIVE_LIBRARY_NAME has the value for the so-file: opencv_java410But in the javacv-1.4.4.jar (the maven dependency and downloaded by maven from maven central) the class Core.NATIVE_LIBRARY_NAME has the value for the so-file: opencv_java401. We have not build this file.

The following solution to the problem is not right! But I can not find out a way to get my hand ofa so-file for 4.0.1. I do not seem to be able to build it since I do not find a branch for it in the git-repo we cloned. Nor I found it for download. But all this might depend on my lack of knowledgeof Git.If I tweak the code to use the so-file we built and change the code to:

// Load the native library . //System.loadLibrary(Core.NATIVE_LIBRARY_NAME); System.loadLibrary( "opencv_java410" );

(The “4” in the file name say’s to me that it is backwards compatible.)When I run the example this time it works like above.

top related