我一直在用Python从OpenCV相机阅读图像,并从主程序读取最新的图像。这是因为硬件有问题。
在混乱的线程和得到一个非常低的效率(废话!),我想切换到多处理。
以下是线程版本:
class WebcamStream:
# initialization method
def __init__(self, stream_id=0):
self.stream_id = stream_id # default is 0 for main camera
# opening video capture stream
self.camera = cv2.VideoCapture(self.stream_id)
self.camera.set(cv2.CAP_PROP_FRAME_WIDTH, 3840)
self.camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 2880)
if self.camera.isOpened() is False:
print("[Exiting]: Error accessing webcam stream.")
exit(0)
# reading a single frame from camera stream for initializing
_, self.frame = self.camera.read()
# self.stopped is initialized to False
self.stopped = True
# thread instantiation
self.t = Thread(target=self.update, args=())
self.t.daemon = True # daemon threads run in background
# method to start thread
def start(self):
self.stopped = False
self.t.start()
# method passed to thread to read next available frame
def update(self):
while True:
if self.stopped is True:
break
_, self.frame = self.camera.read()
self.camera.release()
# method to return latest read frame
def read(self):
return self.frame
# method to stop reading frames
def stop(self):
self.stopped = True
还有...
if __name__ == "__main__":
main_camera_stream = WebcamStream(stream_id=0)
main_camera_stream.start()
frame = main_camera_stream.read()
有人能帮我把这个翻译成多进程吗?
谢谢你!
1条答案
按热度按时间z9smfwbn1#
我已经写了几个类似问题的解决方案,但已经有一段时间了,所以我们开始:
我将使用
shared_memory
作为一个缓冲区来读取帧,然后由另一个进程读取。我的第一个倾向是初始化相机并在子进程中读取帧,因为这看起来像是“设置它并忘记它”一类的事情。