python cv2.错误:OpenCV代码中出现未知的C++异常

xzv2uavs  于 2022-11-24  发布在  Python
关注(0)|答案(1)|浏览(79)

我有这样的代码:

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)

当我运行python文件时,我收到以下错误:

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

我从来没有用过cv2软件包,所以我不确定我是否做错了什么。有人能帮我吗?

2w2cym1i

2w2cym1i1#

这是已知问题,请参阅here。这是macOS特有的问题,当cv2尝试在新生成的线程中与UI交互时,它会抛出此错误。仅在主线程上使用UI交互。

相关问题