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>
(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;
}
This is the compiler invocation:
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}"
What piece of the puzzle am I missing?
(I know this is similar to Cannot access camera with opencv on Mac Mojave but that question never went beyond a malformed plist file.)
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
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.
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.