我无法从命令行 OpenCV 程序访问 iMac 摄像头。(我在 CodeRunner 下编译和运行程序,而不是 Xcode。) 我读到 Mojave 要求 Info.plist 中包含 NSCameraUsageDescription
,我认为我已经正确地将它嵌入到二进制文件中了。我添加了 -sectcreate __TEXT __info_plist Info.plist
(我在这里了解到) 到编译标志中,当我运行 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,以防它试图与相机一起打开麦克风。这是我运行程序时的输出结果: (I added NSMicrophoneUsageDescription in case it was trying to open the microphone along with the camera.)
当我运行程序时,这是输出结果:
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}"
我缺少的拼图是什么?
(我知道这与Mac Mojave上无法使用opencv访问摄像头类似,但该问题从未超出格式错误的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
,然后使用此命令检查相机是否被找到:https://stackoverflow.com/a/46768069/2836621 - Mark Setchellffmpeg
似乎找到了相机。它也可以在照片展位中使用。 (今日所学:不要在嘴里含食物时启动照片展位。) - SSteve