How to use OpenCV Library in a Java Application

  • language:nfi
  • language:js
  • language:python
  • language:regex
  • language:llvm

1. Requirements

  • Ubuntu OS
  • OpenJDK 11
  • GraalVM 20.3.0
  • Python
  • PIP
  • CMake

2. Running the Application with Docker

./mvnw package

3. Build the dockerfile

# Install java and the run-java script
# Also set up permissions for user `1001`
RUN apt-get update \
&& apt-get -y install curl ca-certificates cmake git ant ${JAVA_PACKAGE} python3-pip python3-opencv \
&& pip3 install opencv-python \
&& export JAVA_HOME \
&& mkdir /deployments \
&& chown 1001 /deployments \
&& chmod "g+rwX" /deployments \
&& chown 1001:root /deployments \
&& curl https://repo1.maven.org/maven2/io/fabric8/run-java-sh/${RUN_JAVA_VERSION}/run-java-sh-${RUN_JAVA_VERSION}-sh.sh -o /deployments/run-java.sh \
&& chown 1001 /deployments/run-java.sh \
&& chmod 540 /deployments/run-java.sh

# Build the OpenCV libraries
RUN git clone https://github.com/opencv/opencv.git \
&& cd opencv \
&& git checkout tags/4.5.1 -b 4.5.1 \
&& mkdir build \
&& cd build \
&& cmake -D BUILD_SHARED_LIBS=OFF .. \
&& make -j8
  • OpenJDK 11
  • Python3 (OpenCV)
  • PIP
  • Cmake
docker build -f src/main/docker/Dockerfile.jvm -t quarkus/opencv-haar-java-jvm .
docker run -i --rm -p 8080:8080 quarkus/opencv-haar-java-jvm

4. API

5. Testing

  • Detect object - process a hardcoded image in code
  • Detect object - accepts an image parameter and a filename use to cache the processed image. Make sure to change the attached image as it is lost during transfer.

6. GraalVM

  1. you set GRAALVM_HOME to where you extracted GraalVM
  2. Set JAVA_HOME to GRAALVM_HOME
  3. Add JAVA_HOME\bin to the path
sudo update-alternatives --install /usr/bin/javac javac /opt/graalvm-ce-java11-21.0.0/bin/javac 1
sudo update-alternatives --install /usr/bin/java java /opt/graalvm-ce-java11-21.0.0/bin/java 1
sudo apt-get install python3-opencv libopencv-dev
pip3 install opencv-python
python3 -c "import cv2; print(cv2.__version__)"
sudo  ./gu install python
./graalpython -m ginstall install -h
./graalpython -m ginstall install numpy
./graalpython -m ginstall install opencv
@Path("/js")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String helloJs() {
context.eval(Source.create("js", "console.log('hello js')"));
return "Running JS script console.log('hello js'). Check the logs.";
}

@Path("/py")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String helloPy() {
final URL pyTestPy = getClass().getClassLoader().getResource("pytest.py");
System.out.println("" + pyTestPy);
Value effectValue = context.eval(Source.newBuilder("python", pyTestPy).build());
return "Result read from pytest.py=" + effectValue.toString();
}
./mvnw package -Dquarkus.package.type=uber-jar --this command will take some time
java -jar target/opencv-haar-java-1.0.0-SNAPSHOT-runner.jar .

7. Bonus: Build OpenCV Locally

mkdir build
cd build
cmake -D BUILD_SHARED_LIBS=OFF ..
make -j8

8. References

--

--

--

Senior Java Developer with 15 years of professional experience | Startup enthusiast.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

THE FIVE-LETTER WORD GAME

JavaScript and Rails Portfolio project

A Basic Approach to Game Design and Gamification

Ship Application logs from EC2 instance to Cloudwatch

Profiling using CProfiler

Deciphering Ruan Pethiyagoda’s N-Queens Bitwise Solution

Home Automation Using Arduino and IR

Arduino Uno, relay Module and TSOP38238 IR reciever

Using ElasticSearch with Python Hosted on a Virtual Machine

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
czetsuya

czetsuya

Senior Java Developer with 15 years of professional experience | Startup enthusiast.

More from Medium

Computer Vision Project Using OpenCV & Java

Springboot 2.6.0 릴리즈

Decode Base64 String to File in Java

Add, Verify or Remove Digital Signature in PowerPoint in Java