OpenCV在Java中无法与IP摄像头一起使用。

huangapple go评论86阅读模式
英文:

OpenCV is not working with ip webcam in Java

问题

我尝试在Java中使用OpenCV访问我的网络摄像头,但出现以下错误:

[ 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'

以下是我编写的代码:

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 {

    // 让imageView对所有线程可访问
    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)) {
                    // 将OpenCV帧转换为JavaFX图像
                    Image image = convertMatToImage(frame);

                    // 在ImageView中显示图像
                    imageView.setImage(image);
                }
            }
        }

        private Image convertMatToImage(Mat mat) {
            // 将Mat对象转换为字节数组
            MatOfByte buffer = new MatOfByte();
            Imgcodecs.imencode(".png", mat, buffer);
            byte[] imageData = buffer.toArray();

            // 将字节数组转换为JavaFX图像
            ByteArrayInputStream inputStream = new ByteArrayInputStream(imageData);
            return new Image(inputStream);
        }

    }

    @Override
    public void start(Stage stage) {

        // 启动视频捕捉线程
        Thread videoCaptureThread = new CameraThread();
        videoCaptureThread.setDaemon(true);
        videoCaptureThread.start();

        stage.setTitle("视频捕捉");

        // 创建一个包含ImageView的JavaFX VBox
        VBox vbox = new VBox();
        vbox.getChildren().add(imageView);

        // 使用VBox创建一个JavaFX场景并将其设置在舞台上
        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);

    }
}

我尝试在Python中使用OpenCV运行相同的网络摄像头,似乎可以正常工作,但出于某种原因,它在Java中无法正常工作。您能帮助我理解这个问题吗?因为错误消息没有意义。

我希望能够通过IP摄像头在Java中访问移动摄像头,但却无法正常工作。

英文:

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

答案1

得分: 0

尝试使用FFMpeg重新构建OpenCV,加上-DWITH_FFMPEG=ON。我贴给你的四个链接都与相同的问题和相同的解决方案有关。

英文:

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.

答案2

得分: 0

更新

所以,我成功解决了这个问题,基本上我只需要使用-DWITH_FFMPEG=ON编译OpenCV源代码。

除此之外,我还遇到了一些其他问题,实际上在Windows上构建OpenCV本身是一场噩梦,我强烈建议将opencv-contrib作为额外模块包含进来,这将解决很多依赖性问题。

最后,如果你想在你的应用程序中实际使用FFMPEG,请确保在你的应用程序中动态加载opencv_videoio_ffmpegxxx_64.dll,否则它将无法解码H.264格式的视频。

英文:

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.

huangapple
  • 本文由 发表于 2023年3月3日 18:08:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75625686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定