gpt4 book ai didi

python - opencv python多线程视频采集

转载 作者:太空宇宙 更新时间:2023-11-03 15:20:32 24 4
gpt4 key购买 nike

我正在尝试读取 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 窗口正在显示线程 1 在尝试读取超过 ma​​x_retries 限制(在我的例子中为 10)之后退出。问题是虽然我创建了单独的 cvVideo 对象,但我似乎无法同时使用它们。可能是什么问题?

最佳答案

*edit:我将把代码留在下面,但我猜你遇到了编解码器问题?我安装了 xvid 编解码器(样本 Megamind.avi 就是用它编码的),程序在运行 megamind 视频的任一线程或两个线程上都能正常工作。你能让 megamind 视频在单线程版本中运行吗?

这里是 an SO post on opencv video codecs以防万一。这是 xvid download我用过的(k-lite 对我不起作用)。


你写的代码基本上对我有用。对于您和任何其他想要尝试的人,我做了以下事情:

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

VideoCapture.read 可能会出现其他错误,这会使 read_attempt 方法值得,但我只能找到 the two errors the docs mention .对于那些它只返回代码已经测试过的 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()

关于python - opencv python多线程视频采集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15788122/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com