【发布时间】:2020-04-06 03:07:15
【问题描述】:
我正在从事一个图像处理项目,我希望 3 个摄像头的图片在按下按钮时获取最新帧,为此,我使用了 multiprocessing.process 和 multiprocessing.queue,如下面的代码所示。完成了要求的任务,但现在有两个问题:
任务管理器中的 1.cpu-100%(这会减慢程序速度)
2.代码中有cam.set(cv2.CAP_PROP_BUFFERSIZE, 0) 的解决方法,这是导致50% cpu 使用率的主要原因
实际上我想要一种快速获取帧的方法,所以我也尝试了 multiprocessing.pipe 而不是队列,但由于第二次按下按钮时它没有获得最新的帧,所以我不得不使用队列通信方法。任何有关代码的帮助将不胜感激
def camera_func(queue,cam_indx):
cam = cv2.VideoCapture(cam_indx,cv2.CAP_DSHOW) #current camera
cam.set(cv2.CAP_PROP_BUFFERSIZE, 0)
if cam.isOpened(): # Check success if the object is created and opened
while True:
try:
flag, frame=cam.read()
if flag==0:
break
# used to remove buffer size problem because it gets next frame
# rather latest frame
if not queue.empty():
try:
queue.get_nowait() # discard previous (unprocessed) frame
except Queue.Empty:
pass
queue.put(frame,False)
except:
continue
else:
cam.open()
raise Exception("Could not open camera")
queues=[]
for pip in range(0,a_cams):
queu = Queue(maxsize=1)
queues.append(queu)
processes = [Process(target=camera_func, args=(queues[x],x)) for x in range(a_cams)] # Setup a list of processes that we want to run
for p in processes:
p.start() # Run processes
更新:
我已经按照 nathancy 的建议使用了线程,现在由于来自here 的 fps 同步,它正在执行相同的任务而没有解决方法,CPU 使用率仍然是 80%,但这次启动时有 40 秒的延迟程序太多了。
【问题讨论】:
-
你做过任何基准测试/分析吗?
-
它的行为是线性的吗?例如1 个摄像头占用 40% 的 vcpu,2 个摄像头占用 80%,3 个摄像头占用 100%?你有一个多核系统,如果使用多线程可以超过 100%?
-
如何进行基准分析?
-
不,当没有解决方法时,它不会呈现线性 50% 的使用率,即按下按钮时我得到的连续帧不是最新的。当我做解决方法时,50% 会跳进去。我有i7 8gen 12核系统
标签: python multithreading opencv image-processing multiprocessing