英文:
Java open CV hangs on VideoCapture for file if openCV is loaded with nu.pattern.OpenCV.loadShared();
问题
以下是您提供的代码的翻译部分:
我从 https://ratiler.wordpress.com/2014/09/08/detection-de-mouvement-avec-javacv/ 获取了一个示例代码,与在静态块中使用 `nu.pattern.OpenCV.loadShared();` 加载 open cv 库有小小的差异:
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.videoio.VideoCapture;
import org.opencv.imgproc.Imgproc;
public class JavaCVPrjt01 {
static {
//System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
nu.pattern.OpenCV.loadShared();
}
public static void main(String[] args) {
JFrame jframe = new JFrame("HUMAN MOTION DETECTOR FPS");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel vidpanel = new JLabel();
jframe.setContentPane(vidpanel);
jframe.setSize(1280,720) ;
jframe.setVisible(true);
Mat frame = new Mat();
Mat outerBox = new Mat();
Size sz = new Size(1280,720);
VideoCapture camera = new VideoCapture("D:/Downloads/video4.mp4");
while (true) {
if (camera.read(frame)) {
Imgproc.resize(frame,frame, sz );
outerBox = new Mat(frame.size(), CvType.CV_8UC1);
Imgproc.cvtColor(frame, outerBox, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur(outerBox, outerBox, new Size(3, 3), 0);
ImageIcon image = new ImageIcon(Mat2bufferedImage(outerBox));
vidpanel.setIcon(image);
vidpanel.repaint();
}
}
}
public static BufferedImage Mat2bufferedImage(Mat image) {
MatOfByte bytemat = new MatOfByte();
Imgcodecs.imencode(".jpg", image, bytemat);
byte[] bytes = bytemat.toArray();
InputStream in = new ByteArrayInputStream(bytes);
BufferedImage img = null;
try {
img = ImageIO.read(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return img;
}
}
还有必要的依赖项:
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv</artifactId>
<version>3.2.0-1.3</version>
</dependency>
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>3.2.0-1</version>
</dependency>
因此,在以下代码之前执行:
VideoCapture camera = new VideoCapture("D:/Downloads/video4.mp4");
代码会在那里挂起,我没有线索可以检查 JNI 级别的问题。
英文:
I have a sample code from https://ratiler.wordpress.com/2014/09/08/detection-de-mouvement-avec-javacv/ with small difference in loading the open cv library in the static block using nu.pattern.OpenCV.loadShared();
:
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.videoio.VideoCapture;
import org.opencv.imgproc.Imgproc;
public class JavaCVPrjt01 {
static {
//System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
nu.pattern.OpenCV.loadShared();
}
public static void main(String[] args) {
JFrame jframe = new JFrame("HUMAN MOTION DETECTOR FPS");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel vidpanel = new JLabel();
jframe.setContentPane(vidpanel);
jframe.setSize(1280,720) ;
jframe.setVisible(true);
Mat frame = new Mat();
Mat outerBox = new Mat();
Size sz = new Size(1280,720);
VideoCapture camera = new VideoCapture("D:/Downloads/video4.mp4");
while (true) {
if (camera.read(frame)) {
Imgproc.resize(frame,frame, sz );
outerBox = new Mat(frame.size(), CvType.CV_8UC1);
Imgproc.cvtColor(frame, outerBox, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur(outerBox, outerBox, new Size(3, 3), 0);
ImageIcon image = new ImageIcon(Mat2bufferedImage(outerBox));
vidpanel.setIcon(image);
vidpanel.repaint();
}
}
}
public static BufferedImage Mat2bufferedImage(Mat image) {
MatOfByte bytemat = new MatOfByte();
Imgcodecs.imencode(".jpg", image, bytemat);
byte[] bytes = bytemat.toArray();
InputStream in = new ByteArrayInputStream(bytes);
BufferedImage img = null;
try {
img = ImageIO.read(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return img;
}
}
and of course, the needed dependencies were added:
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv</artifactId>
<version>3.2.0-1.3</version>
</dependency>
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>3.2.0-1</version>
</dependency>
so, code is executed right before
VideoCapture camera = new VideoCapture("D:/Downloads/video4.mp4");
where it hangs and I have no clues to check what is wrong on the JNI level there.
答案1
得分: 0
以下是翻译好的内容:
这是一个许可问题,导致无法将ffmpeg包含到以下依赖中:
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>3.2.0-1</version>
</dependency>
(看起来在所有版本中都出现了这个问题,我已经尝试了Maven中所有可用的版本)
链接:https://github.com/openpnp/opencv/issues/55
更新:
因此仍然需要显式加载DLL。在我的情况下:
static {
//System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.load("D:\\Downloads\\opencv\\build\\java\\x64\\opencv_java2411.dll");
System.load("D:\\Downloads\\opencv\\build\\x64\\vc12\\bin\\opencv_ffmpeg2411_64.dll");
}
显式加载 opencv_java
和 opencv_ffmpeg
即可解决问题。如果不显式加载 opencv_ffmpeg
,VideoCapture
的 .read(frame)
始终会返回false,而且没有任何提及缺少本机 ffmpeg
依赖的问题。
在 pom.xml
中所需的Java依赖项是:
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv</artifactId>
<version>2.4.11-0.11</version>
</dependency>
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>2.4.11-2</version>
</dependency>
(请注意我已经降级了OpenCV的版本)
英文:
It was a licensing issue that made impossible to include ffmpeg to
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>3.2.0-1</version>
</dependency>
(and it looks like it occurs in all versions, I have tried all available in maven)
https://github.com/openpnp/opencv/issues/55
UPD.
so it is still required to load dlls explicitly. In my case
static {
//System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.load("D:\\Downloads\\opencv\\build\\java\\x64\\opencv_java2411.dll");
System.load("D:\\Downloads\\opencv\\build\\x64\\vc12\\bin\\opencv_ffmpeg2411_64.dll");
}
explicit load of opencv_java
and opencv_ffmpeg
made the trick. Without loading opencv_ffmpeg
explicitly the VideoCapture's
.read(frame)
was just always returning false with no any mention on any missing native ffmpeg
dependency problem.
And the required java dependencies in pom.xml
are:
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv</artifactId>
<version>2.4.11-0.11</version>
</dependency>
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>2.4.11-2</version>
</dependency>
(please not i have downgraded the opencv version)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论