Skip to content

cv2.imshow() throwing "Unknown C++ exception from OpenCV code" only when threaded #22602

@kentmchenry

Description

@kentmchenry

System Information

OpenCV Python Version: 4.6.0.66
Operating System / Platform: macOS Monterey Version 12.6
Chip: Apple M1 Max
Python Version: 3.10.6

Also using NumPy for storing image data
numpy 1.23.2

Detailed description

When using cv.imshow() to display an image it works fine when using a single thread. When one thread invokes a second thread to show the image the following error occurs:

Exception in thread Thread-2:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/Users/spock/workspace/enterprise/src/opencvbug.py", line 28, in run
    self.render_callback(random_image)
  File "/Users/spock/workspace/enterprise/src/opencvbug.py", line 15, in render_image
    cv2.imshow('CV2 C++ Bug', image)
cv2.error: Unknown C++ exception from OpenCV code
Assertion failed: (NSViewIsCurrentlyBuildingLayerTreeForDisplay() != currentlyBuildingLayerTree), function 
NSViewSetCurrentlyBuildingLayerTreeForDisplay, file NSView.m, line 13477.

This steps to reproduce below demonstrate the code working from a main method and then demonstrates the same code failing when it's threaded.

Steps to reproduce

import cv2
import numpy as np
import threading
import time


class Renderer(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)

    def render_image(self, image):
        print('Showing image in renderer thread... kaboom!')
        cv2.imshow('CV2 C++ Bug', image)
        cv2.waitKey(1)
        
            
class ImageStream(threading.Thread):

    def __init__(self, render_callback):
        threading.Thread.__init__(self)
        self.render_callback = render_callback

    def run(self):
        for i in range(7):
            random_image = np.random.randint(255, size=(420, 666, 3),dtype=np.uint8)
            self.render_callback(random_image)
            time.sleep(1)


if __name__ == '__main__':

    print('Showing random image in main thread ...')
    for i in range(7):
        rand_img = np.random.randint(255, size=(420, 666, 3),dtype=np.uint8)
        cv2.imshow('CV2 Single Thread Works', rand_img)
        cv2.waitKey(1)
        time.sleep(1)
    
    cv2.destroyAllWindows()

    render_thread = Renderer()
    render_thread.start()

    im_stream_thread = ImageStream(render_callback=render_thread.render_image)
    im_stream_thread.start()
    
    im_stream_thread.join()
    render_thread.join()

    cv2.destroyAllWindows()

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy