Cannot access camera with opencv on Mac Mojave
Asked Answered
A

2

6

I tried running some basic code from OpenCV that opens the webcam. I was able to run this code before I updated to Mac Mojave, but afterwards I get this error when I build and run.

`[access] This app has crashed because it attempted to access privacy- 
 sensitive data without a usage description.  The app's Info.plist must 
 contain an NSCameraUsageDescription key with a string value explaining 
 to the user how the app uses this data.`

I followed steps on other posts and created the Info.plist in the project(same directory as main.cpp) , however it says it has a problem parsing the contents on the Info.plist.

Info.plist:

   <?xml version="1.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>
</dict>

How can I fix this problem and have it read the Info.plist?

Ambrotype answered 31/1, 2019 at 4:41 Comment(0)
C
1

You're missing the closing </plist> tag. Just add that as the last line of the file and it should work.

Coruscate answered 31/1, 2019 at 5:44 Comment(0)
W
1

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

Wightman answered 22/6, 2019 at 0:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.