我目前正在从一个网络摄像头拍摄的图像,应该尽快处理他们被捕获。在提取图像之后,代码使用cv2.videocapture()函数。在
想法:一旦图像被捕获,我想利用系统需要的时间来捕获一个新的图像,以便在之前的图像上进行计算。在
为了完成这个任务,我使用asyncio库。在
首先,我定义了一个函数,该函数生成n个图像,这些图像应该存储在队列中
`在async def produce(queue, n):
for x in range(n):
# produce an item
print('producing {}/{}'.format(x, n))
# simulate i/o operation using sleep
await asyncio.sleep(random.random())
item = obtainImage()
z.append(x)
# put the item in the queue
await queue.put(item)`
之后,我实现了一个producer方法,它等待队列中的条目并计算一些图像特征(这里是一个简单的阈值):
^{pr2}$
为了启动函数,我创建了一个run函数,并将其添加到事件循环中(run方法包含一个参数,该参数指示我要处理多少图像):async def run(n):
queue = asyncio.Queue()
# schedule the consumer
consumer = asyncio.ensure_future(c