Issue with Getting All Zeros from the Camera Using OpenCV in Python

Reading images from the camera (on macOS, for example) using Python with OpenCV may return vectors full of zero values even though the camera turns on correctly.

This may be due to the fact that we are not waiting for the data properly.

The solution is to make sure to read the data in a loop, i.e. to wait for the data to arrive instead of expecting it to be available instantly.
One single read may not be enough. The code below illustrates the situation.

The following will most likely return all zeros. Note that even though the isOpened() property returns True and the success value from captureObject.read() is also True, the camera data will not be ready.

import cv2

captureObject = cv2.VideoCapture(0)

if captureObject.isOpened():
  success, testImage = captureObject.read()

  # This may be True despite all-zeros images.
  print(success)

  # Numeric values of the image.
  print(testImage)

The following code shows how to properly wait for the camera data to be available in a loop. Note that the first few reads will output zeros as well.
Afterward, a stream of numeric values should be output until we exit the script.

import cv2

captureObject = cv2.VideoCapture(0)

while captureObject.isOpened():
  success, testImage = captureObject.read()

  # Numeric values of the image.
  print(testImage)

  # Use CTRL-C to exit.