gpt4 book ai didi

multithreading - 同步线程以同时读取不同的资源

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

我有两个摄像头,这对于完全同时使用 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()

上述实现的问题是:

  1. 我需要定义 sleep 时间,因为我不知道每帧读取之间的延迟(即可能长或短,具体取决于计算)
  2. 这并不能让我在每次触发时都阅读一次。

所以这种方法行不通,有什么建议吗?

最佳答案

考虑从 VideoCapture 获取 FPS。另外,请注意 VideoCapture.grab 和 VideoCapture.retrieve frame 之间的区别。这用于相机同步。

首先为两个摄像头调用 VideoCapture#grab,然后检索帧。参见 docs .

关于multithreading - 同步线程以同时读取不同的资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39974310/

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