Symptom
When I ran my OpenCV code and the following exception was raised.
Unknown C++ exception from OpenCV code
File …, line …, in … keyboard = cv2.waitKey(1) & 0xFF
Unknown C++ exception from OpenCV code
File File …, line …, in …cv2.imshow(‘frame’, frame)
Version
macOS Monterey 12.3.1
python: 3.9.12
OpenCV: 4.5.5
opencv-python: opencv_python-4.5.5.64-cp37-abi3-macosx_11_0_arm64
Cause
The code was not invoked in the main thread. Moving the code to the main thread and then it worked. So, I’m assuming that OpenCV on macOS shall run on the main thread.
Please note that the code worked perfectly on Windows 10. So, it is only on macOS.
Solution
On macOS, run OpenCV code in the main thread.