Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何运行OpenCV VideoCapture';多线程类之外的读取函数?_Python 3.x_Multithreading_Opencv_Python Multithreading_Video Capture - Fatal编程技术网

Python 3.x 如何运行OpenCV VideoCapture';多线程类之外的读取函数?

Python 3.x 如何运行OpenCV VideoCapture';多线程类之外的读取函数?,python-3.x,multithreading,opencv,python-multithreading,video-capture,Python 3.x,Multithreading,Opencv,Python Multithreading,Video Capture,我使用多线程技术从磁盘并行读取两个静态视频。下面修改的代码对我来说非常好: from threading import Thread import cv2, time class VideoStreamWidget(object): def __init__(self, src=0): self.capture = cv2.VideoCapture(src) # Start the thread to read frames from the vide

我使用多线程技术从磁盘并行读取两个静态视频。下面修改的代码对我来说非常好:

from threading import Thread
import cv2, time


class VideoStreamWidget(object):
    def __init__(self, src=0):
        self.capture = cv2.VideoCapture(src)
        # Start the thread to read frames from the video stream
        self.thread = Thread(target=self.update, args=())
        self.thread.daemon = True
        self.thread.start()

    def update(self):
        # Read the next frame from the stream in a different thread
        while True:
            if self.capture.isOpened():
                (self.status, self.image) = self.capture.read()
                self.frame = cv2.resize(self.image, (640, 480))
            time.sleep(.01)

    def show_frame(self, win_name='frame_1'):
        # Display frames in main program
        cv2.imshow(win_name, self.frame)
        key = cv2.waitKey(1)
        if key == ord('q'):
            self.capture.release()
            cv2.destroyAllWindows()
            exit(1)

if __name__ == '__main__':
    video_stream_widget1 = VideoStreamWidget(src=r'C:\Users\ab\Documents\Video\ch6.asf')
    video_stream_widget2 = VideoStreamWidget(
        src=r'C:\Users\ab\Documents\Video\ch4.asf')
    while True:
        try:
            video_stream_widget1.show_frame('frame_1')
            video_stream_widget2.show_frame('frame_2')
        except AttributeError:
            pass
现在,我正在尝试调整上述代码,因为我想在类外创建一个while循环,并从capture.read()函数中获取状态和图像,以便循环中的其他代码逐帧使用。但是,如果出现以下错误(断言fctx->async_lock在libavcodec/pthread_frame.c:155处失败),它甚至不能用于单个视频流。代码如下:

from threading import Thread
import cv2, time


class VideoStreamWidget(object):
    def __init__(self, src=0):
        self.capture = cv2.VideoCapture(src)
        # Start the thread to read frames from the video stream
        self.thread = Thread(target=self.update, args=())
        self.thread.daemon = True
        self.thread.start()

    def update(self):
        # Read the next frame from the stream in a different thread
        while True:
            if self.capture.isOpened():
                (self.status, self.image) = self.capture.read()
                self.frame = cv2.resize(self.image, (640, 480))
            # time.sleep(.01)

    # def show_frame(self, win_name='frame_1'):
    #     # Display frames in main program
    #     cv2.imshow(win_name, self.frame)
    #     key = cv2.waitKey(1)
    #     if key == ord('q'):
    #         self.capture.release()
    #         cv2.destroyAllWindows()
    #         exit(1)

if __name__ == '__main__':
    video_stream_widget1 = VideoStreamWidget(src=r'C:\Users\ab\Documents\Video\ch6.asf')
    # video_stream_widget2 = VideoStreamWidget(
    #     src=r'C:\Users\ab\Documents\Video\ch4.asf')
    # while True:
    #     try:
    #         video_stream_widget1.show_frame('frame_1')
    #         video_stream_widget2.show_frame('frame_2')
    #     except AttributeError:
    #         pass
    while True:
        vflag, image_np = video_stream_widget1.status, video_stream_widget1.frame
        print(image_np)

这是一个二传手-接受者问题吗?你能帮我指出哪里出了问题吗?

在尝试了几种方法后,在init中添加self.status、self.image和self.frame解决了问题。由于它是多线程的,我们需要在构造函数中显式初始化这些变量