我的问题是:
我需要一种更快的方法将读取帧传送到我的计算机上的图像处理(Ubuntu 18.048核i7 3.00GHz内存32GB)。这个
cv2.VideoCapture.read()
太慢了。
我的处理模块每次运行大约需要40毫秒。我们需要25~30帧/秒。
import cv2
from collections import deque
from time import sleep, time
import threading
class camCapture:
def __init__(self, camID, buffer_size):
self.Frame = deque(maxlen=buffer_size)
self.status = False
self.isstop = False
self.capture = cv2.VideoCapture(camID)
def start(self):
print('camera started!')
t1 = threading.Thread(target=self.queryframe, daemon=True, args=())
t1.start()
def stop(self):
self.isstop = True
print('camera stopped!')
def getframe(self):
print('current buffers : ', len(self.Frame))
return self.Frame.popleft()
def queryframe(self):
while (not self.isstop):
start = time()
self.status, tmp = self.capture.read()
print('read frame processed : ', (time() - start) *1000, 'ms')
self.Frame.append(tmp)
self.capture.release()
cam = camCapture(camID=0, buffer_size=50)
W, H = 1280, 720
cam.capture.set(cv2.CAP_PROP_FRAME_WIDTH, W)
cam.capture.set(cv2.CAP_PROP_FRAME_HEIGHT, H)
# start the reading frame thread
cam.start()
# filling frames
sleep(5)
while True:
frame = cam.getframe() # numpy array shape (720, 1280, 3)
cv2.imshow('video',frame)
sleep( 40 / 1000) # mimic the processing time
if cv2.waitKey(1) == 27:
cv2.destroyAllWindows()
cam.stop()
break
我试过的:
多线程-一个线程只是读取帧,另一个线程做图像处理的事情。
因为我可以设置一个缓冲区,例如保存50帧。但是读帧线程的工作速度是~frame/130ms,我的图像处理线程的工作速度是~frame/40ms,然后deque就快用完了。所以我已经试过解决办法了。但不是我需要的。
这
topic
是我发现的最接近我问题的讨论。但不幸的是,我尝试了公认的解决方案(下面讨论的两个)。
conda install -c conda-forge opencv
,
pip install opencv-python
(是的,我都试过了。)
使用ffmpeg解决方案的另一个解决方案(1个拇指向上)。但它似乎是用视频文件而不是摄像设备?
调整c2.waitKey():
然后,我知道我只需要一些关键字跟随。
上面的代码是我到目前为止的演示代码,我想要一些方法或指南使我的videoCapture.read()更快。也许是一种在视频捕获对象或其他摄像机读取模块中使用多线程的方法。
有什么建议吗?