Introducing Opencv Library in a Java Application
At the heart of many machine vision solutions is a software package named OpenCV. Since I'm a Java developer, I've wondered if I can...
At the heart of many machine vision solutions is a software package named OpenCV. Since I'm a Java developer, I've wondered if I can take advantage of this Python library inside a Java application. And so I built this project.
In this project, I created a backend application that can be tested using CURL or Postman that will integrate a simple OpenCV application. The API endpoint or endpoints handle all the inputs required by the application. Link to the OpenCV application can be found here: https://www.geeksforgeeks.org/detect-an-object-with-opencv-python/
This exercise is done using Quarkus, a serverless framework for JavaEE. To use the OpenCV, it is compiled to generate a jar and so library that is used by the Java application.
--
An alternative would be to use GraalVM, but sadly it doesn't support the OpenCV library yet.
In case you wanted to test running a Python code using Java running on GraalVM, I have commented on endpoints in the PolyglotResource class. To deploy, go to the GraalVM section.
Currently, GraalVM supports the following language:
- 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
6. GraalVM
- you set GRAALVM_HOME to where you extracted GraalVM
- Set JAVA_HOME to GRAALVM_HOME
- 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
- https://github.com/czetsuya/opencv-haar-java
- https://advancedweb.hu/getting-started-with-opencv-for-java-on-ubuntu/




Post a Comment