【发布时间】:2018-02-13 04:40:15
【问题描述】:
如何从插入 Raspberry Pi 3B 的标准相机模块 v2 捕获用于 OpenCV 的图像?我一直在尝试让高帧率视频工作,但是当我尝试索引设备 0 和 this 和 this 时,使用 OpenCV 的 VideoCapture 总是给我一个空的 Mat 和 this 都产生 1 帧当我尝试时,每秒或更糟。我还研究过生成一个 raspivid 进程,但我没有看到让 OpenCV 的 VideoCapture 从该进程的输出中读取的方法。
如何在 Java 中获得高 FPS 帧捕获?有没有办法让 OpenCV 从我从另一个进程中获取的 OutputStream 中读取?
编辑:我的代码的简化版本如下。我想知道如何在这个类中填充startRecording() 函数
abstract class Main {
public static Mat currentCapture = null;
public static final double FOV_HEIGHT = 48.8;
public static final int WIDTH = 480;
public static final int HEIGHT = 384;
public static final int VIDEO_WIDTH = WIDTH * 2;
public static final int VIDEO_HEIGHT = HEIGHT * 2;
static {
System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);
}
public static void main(String[] args) {
client = new VisionClientTable("10.55.6.4", 5506);
new Thread(new VisionThread(VisionThread.Mode.VIDEO)).start();
new Thread(new VisionThread(VisionThread.Mode.TARGETING)).start();
}
private static class VisionThread implements Runnable {
private final Mode mode;
enum Mode {
VIDEO,
TARGETING
}
public VisionThread(Mode mode) {
this.mode = mode;
}
@Override
public void run() {
if (mode == Mode.TARGETING)
startTracking();
else if (mode == Mode.VIDEO)
startRecording();
}
}
public static void startTracking() {
/* this thread repeatedly captures currentCapture and processes it */
}
// this is where I need help
public static void startRecording() {
try {
VideoWriter video = new VideoWriter("/home/pi/vision/videos/video-" + Files.list(Paths.get("/home/pi/vision/captures")).count() + ".mp4", VideoWriter.fourcc('X', '2', '6', '4'), 30, new Size(VIDEO_WIDTH, VIDEO_HEIGHT), true);
VideoCapture capture = new VideoCapture(0);
capture.set(Videoio.CAP_PROP_FRAME_WIDTH, VIDEO_WIDTH);
capture.set(Videoio.CAP_PROP_FRAME_HEIGHT, VIDEO_HEIGHT);
Thread.sleep(2000);
while (true) {
long start = System.currentTimeMillis();
Mat mat = new Mat();
capture.read(mat);
if (!mat.empty()) { // mat is always empty
Mat downscaled = new Mat();
Imgproc.resize(mat, downscaled, new Size(WIDTH, HEIGHT), 0, 0, Imgproc.INTER_NEAREST);
currentCapture = downscaled;
}
video.write(mat);
long end = System.currentTimeMillis();
if (end - start < 1000 / 60)
Thread.sleep(1000 / 60 - (end - start));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void log(String text) {
System.out.println(text);
}
}
我觉得最好的方法可能是将raspivid 设置为输出到文件,并以某种方式将其输出输入 OpenCV,但我不知道该怎么做。
编辑 2:到目前为止,我已经尝试使用 Runtime.getRuntime().exec() 运行 raspivid 命令,该命令输出到一个文件,该文件有效,但是当我尝试使用 OpenCV 打开该文件时, capture.isOpened() 仍然为 false,即使程序反复尝试打开文件。
【问题讨论】:
-
能否分享您的代码。
-
我编辑了我的问题以包含代码。
标签: java raspberry-pi3 opencv3.0