Why did multiprocessing giving me EOFError : Ran out of Input Error?
Why did multiprocessing giving me EOFError : Ran out of Input Error?
videoPlayerThreading
是我自己制作的库,基本上制作了 2 个 class,每个都使用线程来获取和显示帧。 objDect
也是我自己的库,在对象检测后基本上 return 帧。我收到 EOFError : Ran out of Input
错误,从回溯中我认为这是由多处理本身引起的,因此我没有 post 我的库,因为它太长了。谁能帮我解决它的问题?谢谢
from multiprocessing import Process
import sys
import videoPlayerThreading as vpt
from objDect import objDect as od
def main(videoSource):
obd = od( videoSources = videoSource )
getFrame = vpt.getFrames(videoSource).start()
showFrame = vpt.showFrames(videoSource).start()
while True:
frame = getFrame.frame
frame=Process(target=obd.predictYolo, args=(frame,)).start()
showFrame.frame = frame
if getFrame.doVideo == False or showFrame.doVideo == False:
getFrame.stop()
showFrame.stop()
sys.exit()
if __name__=="__main__":
main(0)
编辑:
这是显示帧和获取帧 class 它基本上只使用线程获取和显示帧。
class getFrames():
def __init__(self,
videoSource:Union[int,str]=0):
self.stream = self.videoInit(videoSource)
self.hasFrame, self.frame = self.stream.read()
self.doVideo = True
def videoInit(self,
videoSource:Union[int,str]):
try:
cap = cv2.VideoCapture(videoSource)
except Exception as e:
raise Exception(f"Video source error: {e}")
return cap
def start(self):
Thread(target=self.getFrames, args=()).start()
return self
def getFrames(self):
while self.doVideo:
if not self.hasFrame:
self.stop()
else:
(self.hasFrame, self.frame) = self.stream.read()
def stop(self):
self.doVideo = False
self.stream.release()
class showFrames():
def __init__(self,
frame:cv2=None):
self.frame = frame
self.doVideo = True
def start(self):
Thread(target=self.showFrame, args=()).start()
return self
def showFrame(self):
while self.doVideo:
cv2.imshow("Video", self.frame)
if cv2.waitKey(1) == ord("q"):
self.doVideo = False
def stop(self):
self.doVideo = False
尽我所能理解您的程序逻辑,您需要如下内容。生成器函数 read_frames
(可能需要也可能不需要更正)逐帧读取每一帧。主进程创建一个多处理池,并将每个输入帧传递给多处理池以供 obd.predictYolo
处理,并使用返回的帧设置 vpt.frame
。这一直持续到没有更多的帧要处理或 showFrame.doVideo
为 False
。总之,我把你的getFrames
class废掉了,在这里没用
我没有安装 OpenCV,也不知道这个包,也没有你的视频文件,所以把这作为你进一步调查的起点。
from multiprocessing.pool import Pool
import sys
import videoPlayerThreading as vpt
from objDect import objDect as od
def read_frames(videoSource:Union[int,str]=0):
try:
stream = cv2.VideoCapture(videoSource)
except Exception as e:
raise Exception(f"Video source error: {e}")
while True:
hasFrame, frame = stream.read()
if not hasFrame:
break
yield frame
def main(videoSource):
obd = od( videoSources = videoSource )
showFrame = vpt.showFrames(videoSource).start()
with Pool() as pool:
for frame in pool.imap(obd.predictYolo, read_frames(videoSource)):
showFrame.frame = frame
if showFrame.doVideo is False:
showFrame.stop()
break
if __name__=="__main__":
main(0)
videoPlayerThreading
是我自己制作的库,基本上制作了 2 个 class,每个都使用线程来获取和显示帧。 objDect
也是我自己的库,在对象检测后基本上 return 帧。我收到 EOFError : Ran out of Input
错误,从回溯中我认为这是由多处理本身引起的,因此我没有 post 我的库,因为它太长了。谁能帮我解决它的问题?谢谢
from multiprocessing import Process
import sys
import videoPlayerThreading as vpt
from objDect import objDect as od
def main(videoSource):
obd = od( videoSources = videoSource )
getFrame = vpt.getFrames(videoSource).start()
showFrame = vpt.showFrames(videoSource).start()
while True:
frame = getFrame.frame
frame=Process(target=obd.predictYolo, args=(frame,)).start()
showFrame.frame = frame
if getFrame.doVideo == False or showFrame.doVideo == False:
getFrame.stop()
showFrame.stop()
sys.exit()
if __name__=="__main__":
main(0)
编辑:
这是显示帧和获取帧 class 它基本上只使用线程获取和显示帧。
class getFrames():
def __init__(self,
videoSource:Union[int,str]=0):
self.stream = self.videoInit(videoSource)
self.hasFrame, self.frame = self.stream.read()
self.doVideo = True
def videoInit(self,
videoSource:Union[int,str]):
try:
cap = cv2.VideoCapture(videoSource)
except Exception as e:
raise Exception(f"Video source error: {e}")
return cap
def start(self):
Thread(target=self.getFrames, args=()).start()
return self
def getFrames(self):
while self.doVideo:
if not self.hasFrame:
self.stop()
else:
(self.hasFrame, self.frame) = self.stream.read()
def stop(self):
self.doVideo = False
self.stream.release()
class showFrames():
def __init__(self,
frame:cv2=None):
self.frame = frame
self.doVideo = True
def start(self):
Thread(target=self.showFrame, args=()).start()
return self
def showFrame(self):
while self.doVideo:
cv2.imshow("Video", self.frame)
if cv2.waitKey(1) == ord("q"):
self.doVideo = False
def stop(self):
self.doVideo = False
尽我所能理解您的程序逻辑,您需要如下内容。生成器函数 read_frames
(可能需要也可能不需要更正)逐帧读取每一帧。主进程创建一个多处理池,并将每个输入帧传递给多处理池以供 obd.predictYolo
处理,并使用返回的帧设置 vpt.frame
。这一直持续到没有更多的帧要处理或 showFrame.doVideo
为 False
。总之,我把你的getFrames
class废掉了,在这里没用
我没有安装 OpenCV,也不知道这个包,也没有你的视频文件,所以把这作为你进一步调查的起点。
from multiprocessing.pool import Pool
import sys
import videoPlayerThreading as vpt
from objDect import objDect as od
def read_frames(videoSource:Union[int,str]=0):
try:
stream = cv2.VideoCapture(videoSource)
except Exception as e:
raise Exception(f"Video source error: {e}")
while True:
hasFrame, frame = stream.read()
if not hasFrame:
break
yield frame
def main(videoSource):
obd = od( videoSources = videoSource )
showFrame = vpt.showFrames(videoSource).start()
with Pool() as pool:
for frame in pool.imap(obd.predictYolo, read_frames(videoSource)):
showFrame.frame = frame
if showFrame.doVideo is False:
showFrame.stop()
break
if __name__=="__main__":
main(0)