0

I tried to make access my webcam with opencv in java but it gives me this error

[ INFO:0@0.054] global videoio_registry.cpp:232 cv::`anonymous-namespace'::VideoBackendRegistry::VideoBackendRegistry VIDEOIO: Enabled backends(9, sorted by priority): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); MSMF(970); DSHOW(960); CV_IMAGES(950); CV_MJPEG(940); UEYE(930); OBSENSOR(920)
[ INFO:0@0.055] global backend_plugin.cpp:383 cv::impl::getPluginCandidates Found 3 plugin(s) for FFMPEG
[ INFO:0@0.056] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load C:\opencv\build\java\x64\opencv_videoio_ffmpeg470_64d.dll => FAILED
[ INFO:0@0.057] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load opencv_videoio_ffmpeg470_64d.dll => FAILED
[ INFO:0@0.059] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load opencv_videoio_ffmpeg470_64.dll => FAILED
[ INFO:0@0.059] global backend_plugin.cpp:383 cv::impl::getPluginCandidates Found 2 plugin(s) for GSTREAMER
[ INFO:0@0.060] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load C:\opencv\build\java\x64\opencv_videoio_gstreamer470_64d.dll => FAILED
[ INFO:0@0.061] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load opencv_videoio_gstreamer470_64d.dll => FAILED
[ INFO:0@0.061] global backend_plugin.cpp:383 cv::impl::getPluginCandidates Found 2 plugin(s) for INTEL_MFX
[ INFO:0@0.062] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load C:\opencv\build\java\x64\opencv_videoio_intel_mfx470_64d.dll => FAILED
[ INFO:0@0.063] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load opencv_videoio_intel_mfx470_64d.dll => FAILED
[ INFO:0@0.116] global cap_msmf.cpp:1027 CvCapture_MSMF::configureHW MSMF: Using D3D11 video acceleration on GPU device: Intel(R) UHD Graphics 620
OpenCV(4.7.0-dev) Error: Bad argument (CAP_IMAGES: can't find starting number (in the name of file): rtsp://172.30.75.213:8080/video) in cv::icvExtractPattern, file C:\GHA-OCV-2\_work\ci-gha-workflow\ci-gha-workflow\opencv\modules\videoio\src\cap_images.cpp, line 253
[ERROR:0@1.865] global cap.cpp:166 cv::VideoCapture::open VIDEOIO(CV_IMAGES): raised OpenCV exception:

OpenCV(4.7.0-dev) C:\GHA-OCV-2\_work\ci-gha-workflow\ci-gha-workflow\opencv\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): rtsp://172.30.75.213:8080/video in function 'cv::icvExtractPattern'

Here is the code that I worte:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.ByteArrayInputStream;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.videoio.VideoCapture;
import org.opencv.imgcodecs.Imgcodecs;

public class capture extends Application {

    // Make the imageView accessible to all the threads
    volatile ImageView imageView = new ImageView();

    class CameraThread extends Thread {
        VideoCapture capture = new VideoCapture();

        @Override
        public void run() {
            capture.open("http://172.30.75.213:8080/video");
            Mat frame = new Mat();
            while (true) {
                if (capture.read(frame)) {
                    // Convert the OpenCV frame to a JavaFX image
                    Image image = convertMatToImage(frame);

                    // Display the image in the ImageView
                    imageView.setImage(image);
                }
            }
        }

        private Image convertMatToImage(Mat mat) {
            // Convert the Mat object to a byte array
            MatOfByte buffer = new MatOfByte();
            Imgcodecs.imencode(".png", mat, buffer);
            byte[] imageData = buffer.toArray();

            // Convert the byte array to a JavaFX image
            ByteArrayInputStream inputStream = new ByteArrayInputStream(imageData);
            return new Image(inputStream);
        }

    }

    @Override
    public void start(Stage stage) {

        // Start the video capture thread
        Thread videoCaptureThread = new CameraThread();
        videoCaptureThread.setDaemon(true);
        videoCaptureThread.start();

        stage.setTitle("Video Capture");

        // Create a JavaFX VBox to hold the ImageView
        VBox vbox = new VBox();
        vbox.getChildren().add(imageView);

        // Create a JavaFX Scene with the VBox and set it on the Stage
        Scene scene = new Scene(vbox, 640, 480);
        stage.setScene(scene);
        stage.show();

    }

    public static void main(String args[]) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        launch(args);

    }
}

I tried running the same webcam on python using opencv and it seems to work fine but for some reason it does not work in java. Can you help me understand this because the error message makes no sense

I was expecting to access mobile camera through ipcamera in java using opencv but it did not work

  • Let me search for you: [link1](https://stackoverflow.com/questions/59168477/cant-find-starting-number-in-the-name-of-file-when-trying-to-read-frames-fr), [link2](https://stackoverflow.com/questions/60651352/cvvideocaptureopen-videoiocv-images-raised-opencv-exception), [link3](https://community.intel.com/t5/Intel-Distribution-of-OpenVINO/OpenCV-4-1-2-openvino-Unsupported-Extension-Error-Solved/m-p/1193849), [link4](https://answers.opencv.org/question/220468/video-from-ip-camera-cant-find-starting-number-cvicvextractpattern/) – Shark Mar 03 '23 at 10:51
  • Why is it failing to load all these libraries though? – Shark Mar 03 '23 at 10:54

2 Answers2

0

Try rebuilding OpenCV with FFMpeg and -DWITH_FFMPEG=ON. All the four links i pasted to you revolve around the identical problem and identical solution.

Shark
  • 6,513
  • 3
  • 28
  • 50
0

Update

So, I was able to resolve this issue basically all I had to do was compile the OpenCV source code with -DWITH_FFMPEG=ON.

Other than that some other issues I had were actually trying to build OpenCV itself in windows as it is a nightmare I highly recommend to include opencv-contrib as extra modules that will fix a lot of dependency issues.

Lastly if you want to actually use FFMPEG in your application make sure to Dynamically load the opencv_videoio_ffmpegxxx_64.dll in your application or it will not be able to decode the videos if they are in H.264 format.

Mina Abd El-Massih
  • 636
  • 1
  • 8
  • 13