简体   繁体   中英

Unable to read video in MacBook Pro Catalina OS using OpenCV 4.1.1 VideoCapture

I am unable to open the video camera and read the frames. Below is the Python code.

import cv2 as cv

cap = cv.VideoCapture()
while True:
    success, frame = cap.read()
    ... do something with the frame ...
    ... break on a certain key ...

cap.release()

I noticed that success is False and no frame is read. I am not sure if there are specific permissions I need to set on my MacBook Pro. I explored the security settings and also the accessibility settings but could not find anything other than applications that typically expect to use the video camera. I also don't know if I need to and if there is some way to switch on the video camera in the Mac. I am using PyCharm and my macOS version is 10.15 (upgraded only last week).

My current workaround is to play the video into a file and read the file. That works. If you have any suggestions for me to try, I will do that right away. Thank you.

I found a solution to this. Skip the next 3-4 lines to see the solution without getting the logic.

Info:

Every app on Mac OS/iOS/watches OS needs privacy permission from the user before accessing features like camera/ mic/ gallery etc. These permissions are stored in a property list file generally "info.plist" . Now as this app will run on the mac itself you need to specify permissions before running.

Solution:

Go to the following on your mac: Finder -> Applications -> pycharm (right-click to show package contents) -> Contents -> You'll see a file called info.plist (if not, search for it)

Now open "info.plist" file either with any plain text editor and paste the following code right after the opening tag which must be most probably in 4th or 5th line.

<key>Privacy - Camera Usage Description</key>
<string>py requires access to your camera.</string>

Note : If you open the plist file with Xcode, it might open in a GUI unknown to you. Right-click on the file and select open as -> source code.

Try this worked for me [downgrade or upgrade to this specific version of opencv]: pip install opencv-python==4.1.2.30

what's your error message? I cannot open video camera on MacOS using python-opencv and the error message is:

error: (-215:Assertion failed).info.backendFactory.empty() in function 'open'

I add a parameter cv2.CAP_AVFOUNDATION when initialize the camera and it works.

cap = cv.VideoCapture(0, cv2.CAP_AVFOUNDATION)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM