虚拟机上的opencv视频捕获和摄像头访问

时间:2015-11-04 04:37:39

标签: c++ opencv ubuntu virtual-machine video-capture

我正在尝试使用计算机上的网络摄像头实时捕捉图像。 我正在使用虚拟盒运行Ubuntu,我知道我需要设置USB设置才能使用网络摄像头,但我是否还需要安装网络摄像头驱动程序?如果是的话我该怎么做!!

我安装了 virtualbox 5.0.6 ubuntu 14.04.3

我正在运行Windows 10计算机

这是我正在运行的代码,我收到“错误:无法访问相机!” ..

你可以帮忙吗?

// Get access to the webcam.
void initWebcam(VideoCapture &videoCapture, int cameraNumber)
{
    // Get access to the default camera.
    try {   
        videoCapture.open(cameraNumber);
    } catch (Exception &e) {}
    if ( !videoCapture.isOpened() ) {
        cerr << "ERROR: Could not access the camera!" << endl;
        exit(1);
    }
    cout << "Loaded camera " << cameraNumber << "." << endl;
}

int main(int argc, char** argv)
{

    const int DESIRED_CAMERA_WIDTH = 640;
    const int DESIRED_CAMERA_HEIGHT = 480;    
    int cameraNumber = 0;  

    // Get access to the camera.
    VideoCapture camera;
    initWebcam(camera, cameraNumber);

    camera.set(CV_CAP_PROP_FRAME_WIDTH, DESIRED_CAMERA_WIDTH);
    camera.set(CV_CAP_PROP_FRAME_HEIGHT, DESIRED_CAMERA_HEIGHT);

    while (true) {

        // Grab the next camera frame. Note that you can't modify camera frames.
        Mat cameraFrame;
        camera >> cameraFrame;
        if( cameraFrame.empty() ) {
            cerr << "ERROR: Couldn't grab the next camera frame." << endl;
            exit(1);
        }

        Mat displayedFrame = Mat(cameraFrame.size(), CV_8UC3);
        // DO SOME PROCESSING

return 0;
}

3 个答案:

答案 0 :(得分:0)

我得到了同样的错误。这发生在我身上,因为我的虚拟机没有检测到我的网络摄像头。我安装了奶酪(使用网络摄像头的程序)并确认了这件事。所以,我打开了虚拟机配置上的USB控制器。然后,运行虚拟机,我在设备菜单上检查了我的网络摄像头,一切都开始工作! 希望它有所帮助。

答案 1 :(得分:0)

这似乎是授权问题。 尝试从命令行运行sudo。 另一个问题可能是您选择了错误的相机(例如,您有一台笔记本电脑,并且还有集成的相机),您可以使用'dmesg |在连接到vm的设备上获得一些提示。 grep usb'

答案 2 :(得分:0)

我知道这不是你想要的解决方案,但这可以解决你的问题。

https://medium.com/@ageitgey/try-deep-learning-in-python-now-with-a-fully-pre-configured-vm-1d97d4c3e9b

按照上面的链接创建一个新的虚拟机,其中包含所有需要的包,它也可以使摄像头捕获工作。

希望它有所帮助。