我有两个摄像头,这对于完全同时使用 OpenCV
读取帧很重要,我想到了类似 Lock
的东西,但我无法弄清楚我的方式可以实现这个。
我需要一些触发器来推送并使线程能够读取帧,然后等待另一个触发器命中,如下所示:
def get_frame(queue, cap):
while running:
if(read_frame):
queue.put(cap.read());
else:
# without this sleep this function just consumes unnecessary CPU time
time.sleep(some_time);
q = Queue.Queue()
# for every camera
for u in xrange(2):
t = threading.Thread(target=get_frame, args = (q, caps[u]))
t.daemon = True
t.start()
上述实现的问题是:
- 我需要定义 sleep 时间,因为我不知道每帧读取之间的
延迟
(即可能长或短,具体取决于计算)
- 这并不能让我在每次触发时都阅读一次。
所以这种方法行不通,有什么建议吗?
考虑从 VideoCapture 获取 FPS。另外,请注意 VideoCapture.grab 和 VideoCapture.retrieve frame 之间的区别。这用于相机同步。
首先为两个摄像头调用 VideoCapture#grab,然后检索帧。参见 docs .
我是一名优秀的程序员,十分优秀!