OpenCV (packaged by OpenPnP) library not loading

485 Views Asked by At

I am trying to use OpenVC and encounter an erorr that says that it is not in java.library.path. I have followed the github page for this version of OpenCV (https://github.com/openpnp/opencv) but still the probelm persisits, I tried using maven but it would still not locate the file.

I tried to use maven but the dependency would not work, I then used the inbuilt library system in IntelliJ but I am constantly getting this error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java460 in java.library.path: /Users/ranveerbehl/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
    at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2434)
    at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:848)
    at java.base/java.lang.System.loadLibrary(System.java:2015)
    at Main.main(Main.java:13)

I am trying to compress video files as my previous library (IVCompressor) just plain stopped working one day and after days of troubleshooting, I was not able to fix it. Here is the code I wrote for reference:

public static void main(String[] args){

        // Load the OpenCV library
        nu.pattern.OpenCV.loadLocally();
        System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);
        // Define the input and output file paths
        String inputFile = "input.mov";
        String outputFile = "output.mov";

        // Create a VideoCapture object to read the input video
        VideoCapture capture = new VideoCapture(inputFile);

        // Get the video frames per second
        double fps = capture.get(Videoio.CAP_PROP_FPS);

        // Get the video frame size
        int frameWidth = (int) capture.get(Videoio.CAP_PROP_FRAME_WIDTH);
        int frameHeight = (int) capture.get(Videoio.CAP_PROP_FRAME_HEIGHT);

        // Create a VideoWriter object to write the output video
        VideoWriter writer = new VideoWriter(outputFile, VideoWriter.fourcc('M', 'P', '4', 'V'), fps, new Size(frameWidth, frameHeight));

        // Read and write the video frames
        Mat frame = new Mat();
        while (capture.read(frame)) {
            writer.write(frame);
        }

        // Release the resources
        capture.release();
        writer.release();
    }

All imports are imported.

I feel the main probelm lies in these two lines of code if not with the library installation itself:

// Load the OpenCV library
        nu.pattern.OpenCV.loadLocally();
        System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);

OpenCV version 4.6.0.

Thank you.

1

There are 1 best solutions below

0
On

I am on openpnp 4.7.0-0 and had the same error.

Instead of

nu.pattern.OpenCV.loadLocally();
System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);

I used

nu.pattern.OpenCV.loadShared();
System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);

And it solved it.