2

I have this code:

class CamThread(threading.Thread):

    def __init__(self, previewname, camid):
        threading.Thread.__init__(self)
        self.previewname = previewname
        self.camid = camid

    def run(self):
        print("Starting " + self.previewname)
        previewcam(self.previewname, self.camid)

# Function to preview the camera.
def previewcam(previewname, camid):
    cv2.namedWindow(previewname)
    cam = cv2.VideoCapture(camid)
    if cam.isOpened():
        rval, frame = cam.read()
    else:
        rval = False

    while rval:
        cv2.imshow(previewname, frame)
        rval, frame = cam.read()
        key = cv2.waitKey(20)
        if key == 27:  # Press ESC to exit/close each window.
            break
    cv2.destroyWindow(previewname)

when I run my python file, I get this error:

    self.run()
  File "swann.py", line 17, in run
    previewcam(self.previewname, self.camid)
  File "swann.py", line 21, in previewcam
    cv2.namedWindow(previewname)
cv2.error: Unknown C++ exception from OpenCV code
Assertion failed: (NSViewIsCurrentlyBuildingLayerTreeForDisplay() != currentlyBuildingLayerTree), function NSViewSetCurrentlyBuildingLayerTreeForDisplay, file NSView.m, line 13477.
zsh: illegal hardware instruction

I've never used the cv2 package before, so I'm not sure if I am doing something wrong. Could someone help me out?

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
bobthebuilder
  • 75
  • 1
  • 6

1 Answers1

0

This is known issue, see here. This is macOS specific problem, when cv2 tries to interact with UI in newly spawned thread, it throws this error. Use UI interactions on main thread only.