Robot code won't load after writing camera code (please help)

We got some help from a previous programmer on the team. He commented all of his code from last year to guide us through programming a dual-camera system that can switch between cameras by pressing a button. Here’s what we’ve typed out:

package frc.robot;

import edu.wpi.first.wpilibj.TimedRobot;

import org.opencv.core.Mat;
import edu.wpi.cscore.CvSink;
import edu.wpi.cscore.CvSource;
import edu.wpi.cscore.UsbCamera;
import edu.wpi.first.cameraserver.CameraServer;

import frc.robot.OI.Controller;

public class Camera extends TimedRobot {

private CvSink[] cameras = {null, null};
private CvSource imgOut;
private int currCamera = 0;
private Mat img;
private final int[] res = {240, 160};

public Camera() {
    img = new Mat(res[0], res[1], 0);

    UsbCamera cam0 = CameraServer.getInstance().startAutomaticCapture(0);
    UsbCamera cam1 = CameraServer.getInstance().startAutomaticCapture(1);

    cameras[0] = CameraServer.getInstance().getVideo(cam0);
    cameras[1] = CameraServer.getInstance().getVideo(cam1);

    imgOut = CameraServer.getInstance().putVideo("Camera", res[0], res[1]);
}

public void run() {
    viewCam();
    SwitchCam();
}

private void SwitchCam() {
    if(Robot.oi.getButtonDown(Controller.Pilot, 8)) {
        if(currCamera == 0) {
            currCamera = 1;
        } else {
            currCamera = 0;
        }
    }
}

private void viewCam() {
    cameras[currCamera].grabFrame(img);
    imgOut.putFrame(img);
}

public void robotInit() {
//CameraServer.getInstance().startAutomaticCapture(0);
//CameraServer.getInstance().startAutomaticCapture(1);
 }
}

The “robotInit” function isn’t being used, as it just runs both cameras at the same time and sucks up bandwidth. The system uses an int id for each camera and switches between them using the Start button on an Xbox 360 controller.

The code throws an exception when we try to deploy it:
Unhandled exception: java.lang.UnsatisfiedLinkError: ‘long org.opencv.core.Mat.n_Mat(int, int, int)’

Can anybody help?

The problem is the OpenCV native libraries don’t load themselves. WPILib classes such as CameraServer take care of this loading when they are first used, but you’re creating the Mat before you call any CameraServer functions. Moving the new Mat() line below the camera creation will fix this.

There’s also an easier way to do switched cameras, using CameraServer.addSwitchedCamera():

UsbCamera cam0 = CameraServer.getInstance().startAutomaticCapture(0);
UsbCamera cam1 = CameraServer.getInstance().startAutomaticCapture(1);
MjpegServer switchCam = CameraServer.getInstance().addSwitchedCamera("Camera");

...

// set camera 0
switchCam.setSource(cam0);
// set camera 1
switchCam.setSource(cam1);

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.