Search code examples
javaweb-servicesopencvvideo-capturertsp

Capture rtsp video stream using OpenCV 3.1 in Java


I am trying to create an app in java using OpenCV to grab videostream from web service which is a camera system with couple of cameras and a recording device.

I have found the address "rtsp://login:pass@IP address:Port/cam/realmonitor?channel=1&subtype=0" for accessing the camera on channel 1.

For opening camera stream I have used this code (curently it catches a local usb camera):

VideoCapture cap; Mat2Image mat2Img = new Mat2Image();

public VideoGrabber(){
    cap = new VideoCapture(0);

    try {
        System.out.println("Sleeping..");
        Thread.sleep(4000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Camera on..");
    cap.open("0");
    if(!cap.isOpened()){
        System.out.println("Camera Error");
    }
    else{
        System.out.println("Camera OK?");
    }
}

After grabbing the video stream I put it into a JFrame.

I think I should put the video streaming service address in cap.open( ... ) but using rtsp://login:pass@http://192.168.1.14:8006/cam/realmonitor?channel=1&subtype=0 gave me "Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Width (0) and height (0) must be > 0".

Please help,

EDIT I have found out that rtsp://login:pass@http://192.168.1.14:554/cam/realmonitor?channel=1&subtype=0 works in vlc but still no luck in opencv.

EDIT #2 Ok. After playing with vlcl, gstreamer and most of the popular solutions it just started working. I don't know if it wasn't bad rtsp address after all. Code:

static {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        //load the library of opencv
    }

    VideoCapture cap;
    Mat2Image mat2Img = new Mat2Image();
    Mat matFilter = new Mat();

    public VideoGrabber(){
        cap = new VideoCapture();

        try {
            System.out.println("Sleeping..");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Camera on..");
        cap.open("rtsp://login:[email protected]:554/cam/realmonitor?channel=1&subtype=0");
        if(!cap.isOpened()){
            System.out.println("Camera Error");
        }
        else{
            System.out.println("Camera OK?");
        }


    }

Solution

  • Answering my question and for Fouad I post the working code: I am guessing the answer was loading ffmpeg dll.

    //all the imports
    public class App {
    static {
        String path = null;
        try {
            //I have copied dlls from opencv folder to my project folder
            path = "E:\\JAVA Projects\\OpenCv\\RTSP Example\\libraries";
            System.load(path+"\\opencv_java310.dll");
            System.load(path+"\\opencv_ffmpeg310_64.dll");
        } catch (UnsatisfiedLinkError e) {
            System.out.println("Error loading libs");
        }
    }
    
    public static void main(String[] args) {
    
        App app = new App();
        //Address can be different. Check your cameras manual. :554 a standard RTSP port for cameras but it can be different
        String addressString = "rtsp://login:[email protected]:554/cam/realmonitor?channel=11&subtype=0";
        Mat mat = new Mat();
        VideoCapture capturedVideo = new VideoCapture();
    
        boolean isOpened = capturedVideo.open(addressString); 
        app.openRTSP(isOpened, capturedVideo, mat);
    
    }
    
    public void openRTSP(boolean isOpened, VideoCapture capturedVideo, Mat cameraMat) {
        if (isOpened) {
            boolean tempBool = capturedVideo.read(cameraMat);
            System.out.println("VideoCapture returned mat? "+tempBool);
    
            if (!cameraMat.empty()) {
                System.out.println("Print image size: "+cameraMat.size());
                //processing image captured in cameraMat object
    
            } else {
                System.out.println("Mat is empty.");
            }
        } else {
            System.out.println("Camera connection problem. Check addressString");
        }
    }
    }