Skip to content Skip to sidebar Skip to footer

Multithreaded Cv2.imshow() In Python Does Not Work

I have two cameras (using OpenNI, I have two streams per camera, handled by the same instance of the driver API) and would like to have two threads, each capturing data from each c

Solution 1:

I solved the issue by using mutables, passing a dictionary cam_disp = {} to the thread and reading the value in the main thread. cv2.imshow() works best when kept in the main thread, so this worked perfectly. I am not sure if this is the "right" way to do this, so all suggestions are welcome.

Solution 2:

Try moving cv2.namedWindow('RGB' + str(cam)) inside your thread target capture_and_save

Solution 3:

The cv2.imshow function is not thread safe. Just move cv2.namedWindow to the threading.Thread that calls cv2.imshow.

import cv2
import threading

def run():
    cap = cv2.VideoCapture('test.mp4')
    cv2.namedWindow("preview", cv2.WINDOW_NORMAL)
    while True:
        ret, frame = cap.read()
        if frame is None:
            print("Video is over")
            break
        cv2.imshow('preview', frame)
        cv2.waitKey(1)
    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    thread = threading.Thread(target=run)
    thread.start()
    thread.join()
    print("Bye :)")

Post a Comment for "Multithreaded Cv2.imshow() In Python Does Not Work"