opencv python多线程视频捕获

发布于 2021-01-29 15:56:33

我正在尝试读取2个视频文件,并同时在单独的Windows中显示它们。这是我的代码:

import threading
import cv2
threadLock=threading.Lock()
class myThread (threading.Thread):
    maxRetries=20
    def __init__(self, threadID, name,video_url):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.video_url=video_url

    def attemptRead(self,cvVideo):
        threadLock.acquire()
        (isRead,cvImage)=cvVideo.read()
        threadLock.release()
        if isRead==False:
            count=1
            while isRead==False and count<myThread.maxRetries:
                threadLock.acquire()
                (isRead,cvImage)=cvVideo.read()
                threadLock.release()
                print self.name+' try no: ',count
                count+=1
        return (isRead,cvImage)

    def run(self):
        print "Starting " + self.name
        windowName = self.name
        cv2.namedWindow(windowName)
        cvVideo = cv2.VideoCapture(self.video_url)


        while True:
            (isRead,cvImage)=self.attemptRead(cvVideo)
            if isRead==False:
                break
            cv2.imshow(windowName,cvImage)
            key=cv2.waitKey(50)
            if key==27:
                break

        cv2.destroyWindow(windowName)
        print self.name + "Exiting"

def main():
    thread1 = myThread(1, "Thread1",'C:/Traffic Pics/Videos/Panjim Capture.mp4')
    thread2 = myThread(2, "Thread2",'C:/Traffic Pics/Videos/Miramar Capture.mp4')

    thread1.start()
    thread2.start()

print "Exiting Main Thread"

if __name__ == '__main__':
    main()

发生了什么事是, 只有线程2 窗口 中显示 。尝试读取超出 max_retries限制
(在我的情况下为10)的视频后, 线程1退出 。问题是,尽管我创建了单独的 cvVideo 对象,但似乎无法同时使用它们。可能是什么问题呢?


关注者
0
被浏览
139
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    *编辑:我将在下面保留代码,但是我想您有编解码器问题吗?我安装了xvid编解码器(这是示例Megamind.avi的编码方式),并且该程序可以在运行megamind视频的一个或两个线程中正常运行。您能使megamind视频在单线程版本中运行吗?

    如果有帮助,这是有关opencv视频编解码器的文章。这是我使用的xvid下载(k-lite不适用于我)。


    您编写的代码基本上对我有用。对于您和其他想要尝试的人,我做了以下工作:

    • 应用的PEP 8建议
    • 删除了不必要的读取尝试代码(也删除了工作视频最后一帧之后的误导性线程重新读取警告)
    • 使用了一个视频文件,也许每个使用opencv的人都拥有
    • 至少在我的系统上删除了对opencv似乎无关紧要的线程锁定
    • 移动了其他一些小东西

    VideoCapture.read可能还会出现其他错误,这些错误使值得使用read_attempt方法,但我只能找到文档中提到的两个错误。对于那些,它只返回false,代码已经对其进行了测试。

    import os
    import threading
    
    import cv2
    
    my_opencv_path = "C:/opencv2.4.3"
    video_path_1 = os.path.join(my_opencv_path, "samples", "cpp", "tutorial_code",
                                "HighGUI", "video-input-psnr-ssim", "video",
                                "Megamind.avi")
    video_path_2 = os.path.join(my_opencv_path, "samples", "c", "tree.avi")
    assert os.path.isfile(video_path_1)
    assert os.path.isfile(video_path_2)
    
    
    class MyThread (threading.Thread):
        maxRetries = 20
    
        def __init__(self, thread_id, name, video_url, thread_lock):
            threading.Thread.__init__(self)
            self.thread_id = thread_id
            self.name = name
            self.video_url = video_url
            self.thread_lock = thread_lock
    
        def run(self):
            print "Starting " + self.name
            window_name = self.name
            cv2.namedWindow(window_name)
            video = cv2.VideoCapture(self.video_url)
            while True:
                # self.thread_lock.acquire()  # These didn't seem necessary
                got_a_frame, image = video.read()
                # self.thread_lock.release()
                if not got_a_frame:  # error on video source or last frame finished
                    break
                cv2.imshow(window_name, image)
                key = cv2.waitKey(50)
                if key == 27:
                    break
            cv2.destroyWindow(window_name)
            print self.name + " Exiting"
    
    
    def main():
        thread_lock = threading.Lock()
        thread1 = MyThread(1, "Thread 1", video_path_1, thread_lock)
        thread2 = MyThread(2, "Thread 2", video_path_2, thread_lock)
        thread1.start()
        thread2.start()
        print "Exiting Main Thread"
    
    if __name__ == '__main__':
        main()
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看