使用 OpenCV (C++) 在 Xcode 中访问网络摄像头 [英] Accessing webcam in Xcode with OpenCV (C++)

查看:52
本文介绍了使用 OpenCV (C++) 在 Xcode 中访问网络摄像头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打开网络摄像头并使用 OpenCV 显示一个简短的捕获.我目前正在 Xcode 上工作,使用 C++ 语言.

I am trying to open the webcam and display a short capture with OpenCV. I am currently working on Xcode, with C++ language.

代码非常简单:

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(int argc, const char * argv[]) {

    // variable initialization
    Mat frame;
    VideoCapture cap;            // 0 is the webcam

    // try to open camera
    cap.open(0);
    if (!cap.isOpened()) return -1;  // check if there is no video or no way to display it

    // create a window and display the video capture
    namedWindow("Video Capture", CV_WINDOW_AUTOSIZE);
    for (int i=0; i<100; i++) {
        cap >> frame;
        imshow("Video Capture", frame);
        waitKey(1);
    }

    return 0;
}

当我运行代码时,返回以下错误:

As I run the code the following error is returned:

[access] This app has crashed because it attempted to access privacy-sensitive 
data without a usage description.  The app's Info.plist must contain an 
NSCameraUsageDescription key with a string value explaining to the 
user how the app uses this data.

因此,我在项目中添加了一个 Info.plist 文件(当前在 main.cpp 所在的目录中)并添加了编译器建议的描述:

Thus, I added an Info.plist file to the project (currently in the same directory where the main.cpp is) and added the description the compiler suggested:

Key: Privacy - Camera Usage Description
Value: $(PRODUCT_NAME) camera use

然后,在项目的构建设置中,我使用完整路径引用了我刚刚编写的文件,如下图所示:

Then, in the project's Build Settings I referenced the file I had just written, by using the full path, as you can see in the following image:

我确定路径是正确的,因为我拖放了文件本身,但编译器一直显示相同的错误并退出执行.

I am sure that the path is correct, as I drag and dropped the file itself, but the compiler keeps on showing the same error and quits the execution.

推荐答案

我最后找到了解决方案;这些是我遵循的步骤:

I found a solution in the end; these are the steps that I followed:

  • 在项目导航器(Xcode IDE 的左侧)中右键单击项目 -> 新建文件 -> 属性文件
  • 调用文件Info.plist"并将其保存在 main.cpp 所在的目录中(它也应该在上层目录中工作,但这对我有用)如图所示以下:
  • 选择 Info.plist 文件并根据上述问题中的说明进行编辑.
  • 现在我们需要将 Info.plist 链接到项目,因此在 Project Navigator 中左键单击该项目,选择选项卡 General,然后在左侧窗格(项目和目标列表")上单击目标"部分.您应该能够看到一个按钮,上面写着选择 Info.plist 文件",见下图:

因为我注意到该程序尚未直接从 Xcode IDE 启动,但我能够(在 Finder 中)导航到可执行文件所在的目录并使用终端运行该程序,所以我复制粘贴了按照此处的建议将Info.plist 放入该文件夹中

As I noticed that the program did not start yet from the Xcode IDE directly, but I was able to navigate (in Finder) to the directory where the executable was and run the program by using the Terminal, I copy-pasted the Info.plist into that folder, as suggested here

这篇关于使用 OpenCV (C++) 在 Xcode 中访问网络摄像头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆