OpenCV命令行应用程序无法在macOS Mojave下访问相机 [英] OpenCV command line app can't access camera under macOS Mojave
问题描述
我无法从命令行OpenCV程序访问iMac相机. (我正在CodeRunner而不是Xcode上编译和运行程序.)我读到Mojave在Info.plist中需要NSCameraUsageDescription
,我认为我已经将它正确地嵌入了二进制文件中.我添加了-sectcreate __TEXT __info_plist Info.plist
(我在这里了解到的)编译标志,当我运行otool -X -s __TEXT __info_plist videotest | xxd -r
(来自同一博客文章)时,其输出:
I'm unable to access the iMac camera from a command line OpenCV program. (I'm compiling and running the program under CodeRunner, not Xcode.) I've read that Mojave requires NSCameraUsageDescription
in Info.plist and I think I'm embedding it correctly in the binary. I added -sectcreate __TEXT __info_plist Info.plist
(which I learned about here) to the compile flags and when I run otool -X -s __TEXT __info_plist videotest | xxd -r
(from the same blog post) it outputs:
-?<?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.)
这是我运行程序时的输出:
This is the output when I run the program:
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
这意味着它正在请求访问权限,但我没有收到对话框,并且在系统偏好设置">安全性"和隐私>相机.
It implies it's requesting access, but I never get a dialog and no apps are listed under System Preferences > Security & Privacy > Camera.
这是我正在运行的程序:
Here's the program I'm running:
#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}"
我错过了哪一个难题?
(我知道这类似于无法通过opencv访问摄像机Mac Mojave ,但这个问题从未超出格式错误的plist文件的范围.)
(I know this is similar to Cannot access camera with opencv on Mac Mojave but that question never went beyond a malformed plist file.)
作为对确保ffmpeg看到设备的建议的回应:
In response to the suggestion to make sure ffmpeg see the device:
$ 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
推荐答案
问题是,无论出于何种原因,C ++程序均未请求访问相机.我在评论中采纳了@gerwin的建议,以尝试使用Python.从Terminal运行该程序会导致Terminal要求访问摄像机.一旦我同意,当从Terminal运行时,c ++程序便能够访问摄像机.
The problem was that the c++ program, for whatever reason, wasn't requesting camera access. I took the advice of @gerwin in the comments to give it a try with Python. Running that program from Terminal resulted in Terminal asking for camera access. Once I granted that, the c++ program was able to access the camera when run from Terminal.
就CodeRunner而言,我不确定如何使CodeRunner在虚拟环境下运行Python程序,因此我无法运行Python OpenCV程序来要求其访问相机.因此,目前我无法使用CodeRunner运行访问相机的c ++程序.
As far as CodeRunner, I'm not sure how to get CodeRunner to run Python programs under a virtual environment so I haven't been able to run a Python OpenCV program to get it to ask for camera access. So at the moment I can't use CodeRunner to run a c++ program that accesses the camera.
这篇关于OpenCV命令行应用程序无法在macOS Mojave下访问相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!