具有多处理功能的 MediaPipe 姿态估计器依赖于其处理功能

问题描述 投票:0回答:3

我目前正在尝试使用 Python 的多处理库将 MediaPipe 姿态估计器实现为一个独立的基于事件的进程,但它挂在 MediaPipe 的

Pose.process()
函数上。

我用另一个进程(readFrames)输入帧。每当捕获一帧时,它就会被写入共享对象并告诉 MediaPipe 进程 (MediaPipeRunner) 开始处理当前图像:

def readFrames(ns, event):
  #initialize the video capture object
  cap = cv2.VideoCapture(0)
  while cap.isOpened():
    ret, frame = cap.read()
    if ret:
        ns.frame = frame
        event.set()
        cv2.imshow('Orijinal Frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            cap.release()
            cv2.destroyAllWindows()
            return -1
    else:
        return


class MediaPipeRunner(mproc.Process):

    def __init__(self, name, nsFrame, nsMediaPipe, eventWait, eventPublish):
        super(MediaPipeRunner, self).__init__()
        
        # Specify a name for the instance
        self.name = name
        
        # Input and output namespaces
        self.nsFrame = nsFrame
        self.nsMediaPipe = nsMediaPipe

        # Waiter and publisher events
        self.eventWait = eventWait
        self.eventPublish = eventPublish

        # Create a pose estimator from MediaPipe
        mp_pose = mp.solutions.pose

        # Specify pose estimator parameters (static)
        static_image_mode = True
        model_complexity = 1
        enable_segmentation = True  # DONT CHANGE
        min_detection_confidence = 0.5

        # Create a pose estimator here
        self.pose = mp_pose.Pose(
        static_image_mode=static_image_mode,
        model_complexity=model_complexity,
        enable_segmentation=enable_segmentation,
        min_detection_confidence=min_detection_confidence,
        smooth_landmarks=False,
        )

    def run(self):
        while True:
            eventFrame.wait()
            
            # This part is where it gets stuck:
            results = self.pose.process(cv2.cvtColor(self.nsFrame.frame, cv2.COLOR_BGR2RGB))

            if not results.pose_landmarks:
                continue

            self.nsMediaPipe.segmentation = results.segmentation_mask
            eventMP.set()

这就是我绑定进程、命名空间和事件的方式:

if __name__=="__main__":

    mgr = mproc.Manager()

    nsFrame = mgr.Namespace()
    nsMP = mgr.Namespace()

    eventFrame = mproc.Event()
    eventMP = mproc.Event()

    camCap = mproc.Process(name='camCap', target=readFrames, args=(nsFrame, eventFrame, ))
    camCap.daemon=True

    mpCap = MediaPipeRunner('mpCap', nsFrame, nsMP, eventFrame, eventMP, )
    mpCap.daemon=True

    camCap.start()
    mpCap.start()

    camCap.join()
    mpCap.join()

我是否在进程上采取了错误的步骤,或者 MediaPipe 与 Python 的多处理库不兼容?

任何帮助将不胜感激,提前致谢:)

P.S.:我通过 pip 安装了 MediaPipe,版本为 0.8.9.1。

python events multiprocessing python-multiprocessing mediapipe
3个回答
4
投票

我发现了问题:当在Python中使用

process
结构时,
with
函数表现正确(不知道为什么):

    with mp_pose.Pose(
        static_image_mode=static_image_mode,
        model_complexity=model_complexity,
        enable_segmentation=enable_segmentation,
        min_detection_confidence=min_detection_confidence,
        smooth_landmarks=False,
        ) as pose:

现在这部分可以工作了!

    results = self.pose.process(cv2.cvtColor(self.nsFrame.frame, cv2.COLOR_BGR2RGB))

希望对您有帮助。


0
投票

我也遇到了同样的问题,通过更改解决了

results = self.pose.process(cv2.cvtColor(self.nsFrame.frame, cv2.COLOR_BGR2RGB))

pose= self.pose
results = pose.process(cv2.cvtColor(self.nsFrame.frame, cv2.COLOR_BGR2RGB))

不太确定为什么会这样,如果有人理解这一点,我很想得到一些意见


0
投票

我认为 MediaPipe 模型只能在创建它们的线程中访问,因此一种可能的解决方法是为每个线程创建一个模型。开销并不大。

model_storage = threading.local()

# ...

        if not hasattr(model_storage, 'recogniser'):
            model_storage.recogniser = mp.tasks.vision.GestureRecognizer.create_from_options(
                mp.tasks.vision.GestureRecognizerOptions(
                    base_options=mp.tasks.BaseOptions(
                        model_asset_path=os.path.join(os.path.dirname(__file__),
                                                      'models',
                                                      'HandGesture')
                    )
                )
            )
        recogniser = model_storage.recogniser
© www.soinside.com 2019 - 2024. All rights reserved.