【发布时间】:2019-09-28 18:12:33
【问题描述】:
我无法从命令行 OpenCV 程序访问 iMac 相机。 (我在 CodeRunner 下编译和运行程序,而不是 Xcode。)我读过 Mojave 在 Info.plist 中需要NSCameraUsageDescription,我认为我将它正确地嵌入到二进制文件中。我在编译标志中添加了-sectcreate __TEXT __info_plist Info.plist (which I learned about here),当我运行otool -X -s __TEXT __info_plist videotest | xxd -r(来自同一篇博文)时,它输出:
-?<?xml ve.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSCameraUsageDescription</key>
<string>Uses camera to see vision targets</string>
<key>NSMicrophoneUsageDescription</key>
<string>This app requires to access your microphone in order to access the camera</string>
</dict>
</plist>
(我添加了 NSMicrophoneUsageDescription 以防它试图打开麦克风和摄像头。)
这是我运行程序时的输出:
OpenCV version 4.1.0-dev
[ INFO:0] global /Users/steve/Documents/GitHub/ssteve-opencv/modules/videoio/src/videoio_registry.cpp (185) VideoBackendRegistry VIDEOIO: Enabled backends(5, sorted by priority): FFMPEG(1000); GSTREAMER(990); AVFOUNDATION(980); CV_IMAGES(970); CV_MJPEG(960)
[ INFO:0] global /Users/steve/Documents/GitHub/ssteve-opencv/modules/videoio/src/backend_plugin.cpp (248) getPluginCandidates VideoIO pluigin (GSTREAMER): glob is 'libopencv_videoio_gstreamer*.dylib', 1 location(s)
[ INFO:0] global /Users/steve/Documents/GitHub/ssteve-opencv/modules/videoio/src/backend_plugin.cpp (256) getPluginCandidates - /usr/local/lib: 0
[ INFO:0] global /Users/steve/Documents/GitHub/ssteve-opencv/modules/videoio/src/backend_plugin.cpp (259) getPluginCandidates Found 0 plugin(s) for GSTREAMER
OpenCV: not authorized to capture video (status 0), requesting...
OpenCV: camera failed to properly initialize!
Unable to open camera
这意味着它正在请求访问,但我从来没有得到一个对话框,并且系统偏好设置 > 安全和隐私 > 相机下没有列出任何应用程序。
这是我正在运行的程序:
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
using namespace std;
using namespace cv;
int main(int argc, char *argv[]) {
cout << "OpenCV version " << CV_VERSION << endl;
VideoCapture cap;
cap.open(0);
if (!cap.isOpened()) {
cerr << "Unable to open camera\n";
return -1;
}
Mat frame;
for (;;) {
cap >> frame;
if (frame.empty()) {
cerr << "Got blank frame\n";
return -1;
}
imshow("Live", frame);
if (waitKey(5) >= 0)
break;
}
return 0;
}
这是编译器调用:
xcrun clang++ -x c++ -lc++ -o "$out" -std=c++11 -I/usr/local/include/opencv4 -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs -lopencv_videoio -lopencv_calib3d -lopencv_aruco -lopencv_xfeatures2d -lopencv_features2d -sectcreate __TEXT __info_plist Info.plist "${files[@]}" "${@:1}"
我错过了哪一块拼图?
(我知道这类似于Cannot access camera with opencv on Mac Mojave,但这个问题从未超出格式错误的 plist 文件。)
响应确保 ffmpeg 看到设备的建议:
$ ffmpeg -hide_banner -f avfoundation -list_devices true -i ""
[AVFoundation input device @ 0x7fed77d16dc0] AVFoundation video devices:
[AVFoundation input device @ 0x7fed77d16dc0] [0] FaceTime HD Camera (Built-in)
[AVFoundation input device @ 0x7fed77d16dc0] [1] Capture screen 0
[AVFoundation input device @ 0x7fed77d16dc0] [2] Capture screen 1
[AVFoundation input device @ 0x7fed77d16dc0] [3] Capture screen 2
[AVFoundation input device @ 0x7fed77d16dc0] AVFoundation audio devices:
[AVFoundation input device @ 0x7fed77d16dc0] [0] Built-in Microphone
【问题讨论】:
-
不知道它是否有帮助,但如果你想抓住一根稻草......你可以尝试使用
brew install ffmpeg安装ffmpeg和homebrew,然后检查可以使用此命令找到相机stackoverflow.com/a/46768069/2836621 -
@MarkSetchell 感谢您的建议。
ffmpeg似乎正在寻找相机。它确实适用于 Photo Booth。 (TIL:千万不要在咀嚼食物时启动 Photo Booth。) -
你可以试试 sudo 吗?值得一试。 AFAIK 你根本不需要包含 plist,但我不确定。你也可以试试 Python 吗?
-
sudo 是个好主意。我没有尝试过。但它没有用。但后来我尝试了 Python 的建议。第一次运行它时,我得到一个对话框,说终端正在请求访问相机,所以我授予了访问权限。之后,Python 程序开始工作。然后我尝试了命令行程序,它也工作了! 成功!!
标签: macos opencv macos-mojave