gpt4 book ai didi

linux - 使用 OpenCV 让 BeagleBone 捕获静止帧

转载 作者:IT王子 更新时间:2023-10-29 01:27:17 24 4
gpt4 key购买 nike

我有一个正在运行的 BeagleBone Ångström Linux 3.2.28,我正在尝试从我的相机中捕捉一帧画面。

所以我插入我的 USB 网络摄像头,并检查 /dev 以确保它显示。

确实如此,如 video0(右下)。我知道这是正确的,因为它会在我拔下相机后消失。

 (bo

现在我启动 Python 并运行以下命令:

root@beaglebone:/dev# python
Python 2.7.2 (default, Sep 11 2012, 16:15:43)
[GCC 4.5.4 20120305 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv
>>> capture=cv.CaptureFromCAM(-1)
>>> img=cv.QueryFrame(capture)
>>> type(capture)
<type 'cv2.Capture'>
>>> type(img)
<type 'NoneType'>

如您所见,我能够充分创建捕获对象,但无法从中提取框架。我也尝试过使用相机 ID 的不同(或没有)整数参数(上面代码中的 -1)无济于事。

作为引用,在我的笔记本电脑上运行相同的代码 IPython看起来像这样:

In [1]: import cv
In [2]: capture=cv.CaptureFromCAM(-1)
In [3]: img=cv.QueryFrame(capture)
In [4]: type(capture)
Out[4]: cv2.Capture
In [5]: type(img)
Out[5]: cv2.cv.iplimage

你可以看到这里我确实在捕捉图像。我不确定从这里到哪里去。

更新:

我玩过 FFmpeg并且能够通过发出以下命令让相机响应(即它的灯亮起):

root@beaglebone:/# ffmpeg -f video4linux2 -i /dev/video0

这很有趣,因为 apparently CaptureFromCAM 使用 V4L 接口(interface)...我不知道从这里去哪里。

最佳答案

非常first thing you need to do 确保 CaptureFromCAM() 成功:

import cv
capture = cv.CaptureFromCAM(-1)
if not capture:
print "Unable to open device #-1"
sys.exit(1)

发送 -1 作为参数告诉 OpenCV 打开默认相机设备。在某些系统上这不起作用,您需要增加数量。尝试传递 0,然后是 1,然后是 2

您需要做的第二件事是确保QueryFrame() 也返回有效的内容:

img = cv.QueryFrame(capture)
if not img:
print "Unable to retrieve frame from the device"
sys.exit(1)

我见过 OpenCV 的 Python API 和 C(甚至 C++)API 之间的奇怪行为。如果以上方法都不能帮助您解决问题,我建议您使用 OpenCV 编译一个 C 程序(它具有最可靠的 API)以从相机中检索数据。在某些情况下,OpenCV 的 C API 可以工作而 Python 不能。

This C program从相机中检索帧并将它们显示在窗口中:

#include <stdio.h>
#include <highgui.h>
#include <cv.h>

int main()
{
CvCapture* capture = NULL;
if ((capture = cvCaptureFromCAM(-1)) == NULL)
{
fprintf(stderr, "ERROR: capture is NULL \n");
return -1;
}

cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE);

cvQueryFrame(capture); // Sometimes needed to get correct data

while (1)
{
IplImage* frame = cvQueryFrame(capture); // check return
{
fprintf( stderr, "ERROR: cvQueryFrame failed\n");
break;
}

// At this point you already have the frame! There's no need to
// repeat the thing 10x with cvGrabFrame and cvRetrieveFrame.
// You are probably sabotaging yourself doing this multiple times.

cvShowImage("mywindow", frame); // Do not release the frame!

int key = cvWaitKey(10);
if (key == 0x1b)
break;
}

cvReleaseCapture(&capture);
cvDestroyWindow("mywindow");

return 0;
}

关于linux - 使用 OpenCV 让 BeagleBone 捕获静止帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15254397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com