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

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

问题描述

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

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:

  • 在Project Navigator(在Xcode IDE的左侧)中,右键单击项目-> New File->属性文件
  • 调用文件"Info.plist",并将其保存在main.cpp所在的同一目录中(它也应该在上一级dir中起作用,但这对我来说是有效的),如图所示以下:
  • 选择Info.plist文件并根据上面问题中的解释进行编辑.
  • 现在我们需要将Info.plist链接到项目,因此在Project Navigator中左键单击该项目,选择常规"选项卡,然后在左窗格(项目和目标列表")上,单击目标"部分.您应该能够看到显示选择Info.plist文件"的按钮,请参见下图:
  • In Project Navigator (on the left side of Xcode IDE) right click on the project -> New File -> Property file
  • Call the file "Info.plist" and save it in the very same directory where the main.cpp is (it should work also in the upper-level dir, but this is what worked for me) as shown in the picture below:
  • Select the Info.plist file and edit it according to what explained in the question above.
  • Now we need to link the Info.plist to the project, so left click on the project in Project Navigator, select tab General and, on the left pane ("project and target list"), click on the executable under the "TARGET" section. You should be able to see a button which states "Choose Info.plist File", see the picture below:

由于我注意到该程序还没有直接从Xcode IDE启动,但是我能够(在Finder中)导航到可执行文件所在的目录并通过使用Terminal运行该程序,因此我复制粘贴了如建议在此处

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天全站免登陆