VSCode Testing Classes Using Opencv

Putting in this note to myself and others that may be having trouble running tests in VSCode test classes by pressing the “Run Test” helper above the method or class.

If the code you are testing depends on opencv (for image processing) and you get an UnsatisfiedLinkError like:


java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_Mat()J
	at org.opencv.core.Mat.n_Mat(Native Method)
	at org.opencv.core.Mat.<init>(Unknown Source)
	at ImageProcessor.<init>(ImageProcessor.java:10)
	...

then you need to add


System.loadLibrary("opencv_java310");

in the constructor of your test class.

Next, you need to tell the VSCode JUnit test runner where the opencv_java310.dll file is. To do this, edit the ./vscode/launch.test.json file and add a PATH environment variable like so (see env parameter - your directory may vary):


{
    "run": {
        "default": "CameraVision",
        "items": 
            {
                "name": "CameraVision",
                "projectName": "CameraVision",
                "workingDirectory": "C:\\Users\\chuck\\Documents\\BB2018BallFindingVision\\CameraVision",
                "args": ],
                "vmargs": ],
                "env": {"PATH": "C:\\Users\\chuck\\Documents\\BB2018BallFindingVision\\CameraVision\\build\\opencv"},
                "preLaunchTask": ""
            }
        ]
    },
    "debug": {
        "default": "CameraVision",
        "items": 
            {
                "name": "CameraVision",
                "projectName": "CameraVision",
                "workingDirectory": "C:\\Users\\chuck\\Documents\\BB2018BallFindingVision\\CameraVision",
                "args": ],
                "vmargs": ],
                "env": {"PATH": "C:\\Users\\chuck\\Documents\\BB2018BallFindingVision\\CameraVision\\build\\opencv"},
                "preLaunchTask": ""
            }
        ]
    }
}

Finally, the helpers need to know which configuration to use by default, and that is the purpose of the default parameter. It should match the name of each of the run and debug configurations (as above).

Took me a while to figure out…

Looks like we are no longer supposed to set env in launch.json.

@SamCarlberg @Thad_House @itsmechuckb
Do you know what the current recommend approach is to load OpenCV in Java for use in Unit Tests on Desktop (I am particularly interested in OSX). Ideally, I could reference the libraries included inside the frc2019 folder, but I don’t see the JNI libraries in here anywhere.

~/frc2019/maven/edu/wpi/first/thirdparty/frc2019/opencv  -> tree .
.
├── opencv-cpp
│   └── 3.4.4-4
│       ├── LICENSE
│       ├── opencv-cpp-3.4.4-4-headers.zip
│       ├── opencv-cpp-3.4.4-4-linuxathenadebug.zip
│       ├── opencv-cpp-3.4.4-4-linuxathenastaticdebug.zip
│       ├── opencv-cpp-3.4.4-4-linuxraspbian.zip
│       ├── opencv-cpp-3.4.4-4-linuxraspbiandebug.zip
│       ├── opencv-cpp-3.4.4-4-linuxraspbianstatic.zip
│       ├── opencv-cpp-3.4.4-4-linuxraspbianstaticdebug.zip
│       ├── opencv-cpp-3.4.4-4-osxx86-64.zip
│       ├── opencv-cpp-3.4.4-4-osxx86-64debug.zip
│       ├── opencv-cpp-3.4.4-4-osxx86-64static.zip
│       ├── opencv-cpp-3.4.4-4-osxx86-64staticdebug.zip
│       ├── opencv-cpp-3.4.4-4-sources.zip
│       └── opencv-cpp-3.4.4-4.pom
└── opencv-java
    └── 3.4.4-4
        ├── opencv-java-3.4.4-4-sources.jar
        ├── opencv-java-3.4.4-4.jar
        └── opencv-java-3.4.4-4.pom

Edit:
A workable solution I have found is to use the following project OpenPNP.

To use, simple add:
testCompile group: 'org.openpnp', name: 'opencv', version: '3.2.0-0
to your build.gradle file.

and, load the library in the constructor of the relevant tests. E.g.

public VisionProcessorTests() {
    System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);
}

If you run the Test Robot Code command in vscode, it will run the tests in Gradle. We do not have a good way of running the tests in debug mode because of the issue with the launch files being weird.

We also include opencv in the simulation setup, so you don’t have to add anything extra. Just a stock 2019 project will work and have all the desktop tools in it.