我一直在使用cv2& python中的多处理,我终于有了一个工作脚本,一旦它们已经在输入队列中就会对各个帧进行处理。但是,我想通过使用多个核心来加速将帧放入队列,因此我尝试使用相同的多处理方法将每个图像读入队列。我似乎无法让这个工作,但我不知道为什么。我想也许是因为我试图写入一个队列,所以我把它们分开了,但现在我想知道它是不是因为我试图从同一个视频文件中读取同时。
以下是我希望以伪代码完成的事情:
for process in range(processCount):
start a process that does this:
for frame in range(startFrame,endFrame):
set next frame to startFrame
read frame
add frame to queue
这是我目前的代码。我尝试过使用pool&单独的流程,但是现在我坚持不同的流程,因为我不确定问题是否与队列管理有关。如果我手动调用getFrame,我会将正确的东西放入队列中,所以我认为该函数本身可以正常工作。
我确定我做的事情非常愚蠢(或者很奇怪)。有人可以提出解决方案吗?只有一个队列也很棒......我只有两个试图解决这个问题。
提前致谢。
import numpy as np
import cv2
import multiprocessing as mp
import time
def getFrame(queue, startFrame, endFrame):
for frame in range(startFrame, endFrame):
cap.set(1,frame)
frameNo = int(cap.get(0))
ret, frame = cap.read()
queue.put((frameNo,frame))
file = 'video.mov'
cap = cv2.VideoCapture(file)
fileLen = int(cap.get(7))
# get cpuCount for processCount
processCount = mp.cpu_count()/3
inQ1 = mp.JoinableQueue() # not sure if this is right queue type, but I also tried mp.Queue()
inQ2 = mp.JoinableQueue()
qList = [inQ1,inQ2]
# set up bunches
bunches = []
for startFrame in range(0,fileLen,fileLen/processCount):
endFrame = startFrame + fileLen/processCount
bunches.append((startFrame,endFrame))
getFrames = []
for i in range(processCount):
getFrames.append(mp.Process(target=getFrame, args=(qList[i], bunches[i][0],bunches[i][1],)))
for process in getFrames:
process.start()
results1 = [inQ1.get() for p in range(bunches[0][0],bunches[0][1])]
results2 = [inQ2.get() for p in range(bunches[1][0],bunches[1][1])]
inQ1.close()
inQ2.close()
cap.release()
for process in getFrames:
process.terminate()
process.join()
答案 0 :(得分:2)
代码中确实存在错误:跨进程使用相同的VideoCapture
对象。显然,文件中当前正在读取的位置存在冲突。
这就是说,当我尝试为每个进程实例化一个VideoCapture时,我的解释器崩溃了(使用python3.4.2
+ opencv3.0.0-beta
和python2.7.6
+ opencv2.4.8
进行了测试)。到目前为止,这是我的尝试,如果你想检查它/进一步。
import cv2
import multiprocessing as mp
def getFrame(queue, startFrame, endFrame):
cap = cv2.VideoCapture(file) # crashes here
print("opened capture {}".format(mp.current_process()))
for frame in range(startFrame, endFrame):
# cap.set(cv2.CAP_PROP_POS_FRAMES, frame) # opencv3
cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frame)
# frameNo = int(cap.get(cv2.CAP_PROP_POS_FRAMES)) # opencv3
frameNo = int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES))
ret, f = cap.read()
if ret:
print("{} - put ({})".format(mp.current_process(), frameNo))
queue.put((frameNo, f))
cap.release()
file = "video.mov"
capture_temp = cv2.VideoCapture(file)
# fileLen = int((capture_temp).get(cv2.CAP_PROP_FRAME_COUNT)) # opencv3
fileLen = int((capture_temp).get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
capture_temp.release()
# get cpuCount for processCount
# processCount = mp.cpu_count() / 3
processCount = 2
inQ1 = mp.JoinableQueue() # not sure if this is right queue type, but I also tried mp.Queue()
inQ2 = mp.JoinableQueue()
qList = [inQ1, inQ2]
# set up bunches
bunches = []
for startFrame in range(0, fileLen, int(fileLen / processCount)):
endFrame = startFrame + int(fileLen / processCount)
bunches.append((startFrame, endFrame))
getFrames = []
for i in range(processCount):
getFrames.append(mp.Process(target=getFrame, args=(qList[i], bunches[i][0], bunches[i][1])))
for process in getFrames:
process.start()
results1 = [inQ1.get() for p in range(bunches[0][0], bunches[0][1])]
results2 = [inQ2.get() for p in range(bunches[1][0], bunches[1][1])]
inQ1.close()
inQ2.close()
for process in getFrames:
process.terminate()
process.join()