I was finally able to resolve this by following a chain of recommendations across Stackoverflow and GitHub. It was a painful bug that burnt my day trying to get my code to work again even though it was working fine pre MacOS Mojave.
Solution
Put the Info.plist
file with the NSCameraUsageDescription
field as suggested in the Products/Build directory of your Target (Right click Product in left pane in Xcode project and click "Show in Finder").
- Automate this process of copy/pasting
Info.plist
to your Build directory (following this suggestion) by adding it to the list of Copy Files
under Build Phases
of your "Target" and changing the Destination to "Products Directory" and Subpath to "."
Outcome
- The Target's Unix executable binary will then ask for permission to access the Camera and upon consenting, the binary will be added to the list of applications permitted to access the Camera available in
System Preferences > Privacy > Camera
.
- FYI: To force clear this list, type
tccutil reset Camera
in Terminal
- You might need to run the Target a couple times before you are prompted for permission / the Camera is accessed.
Issue
Instantiating the cv::VideoCapture(0)
object to access the camera video stream throws the follwoing error even though the code was running fine in MacOS version before Mojave
OpenCV: not authorized to capture video (status 0), requesting...
OpenCV: camera failed to properly initialize!
Cause
MacOS Mojave has tightened the privacy protection, which now requires applications to explicitly prompt and seek permission from the before accessing the camera as explained here.
Suggestions that didn't work
Below suggestions as given in various Stackoverflow posts did not successfully force the built binary to prompt for permission to access the camera:
- Adding the Info.plist
to your Project directory
- Setting the path to Info.plist
under Build Settings > Packaging > Info.plist File
or
- Choosing it in General > Identity > Choose Info.plist File...
of your Target
Suggestions that might have helped
As indicated in the opencv
closed GitHub issue, some change was made in the libopencv
around April '19 which could've also possibly facilitated the usage of available Info.plist
in the build directory to prompt the user for permission to access camera. So I also upgraded my opencv
to latest stable 4.1.0
release using brew upgrade
.
P.S. I'm running MacOS Mojave 10.14.5
, Xcode 10.2.1
and OpenCV 4.1.0