首页 文章

OpenCV捕获来自覆盆子pi相机

提问于
浏览
2

我如何从插入Raspberry Pi 3B的标准相机模块v2中捕获用于OpenCV的图像?我've been trying to get high frame-rate video working, but using OpenCV' s VideoCapture 总是给我一个空的 Mat 当我尝试索引设备 0 时,thisthis都产生每秒1帧或更糟糕的时候我尝试 . 我也考虑过生成一个 raspivid 进程,但是我没有't see a way to get OpenCV' s VideoCapture 来读取该进程的输出 .

如何在Java中获得高FPS帧捕获?有没有办法让OpenCV从_964679中读取我从另一个进程中获取的内容?

编辑:我的代码的简化版本如下 . 我想知道如何在这个类中填充 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() 仍然是假的,即使程序反复尝试打开文件 .

1 回答

相关问题