You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何定位并替换MediaPipe代码中弃用的SymbolDatabase.GetPrototype()

问题描述

我写了一段用MediaPipe和OpenCV实现手指标注的Python代码,功能正常,但运行时弹出如下protobuf弃用警告,不知道代码里哪里调用了这个已弃用的函数,也不知道怎么替换成新方法,求解决:

手指标注代码

import cv2
import mediapipe as mp
from mediapipe import solutions

mp_hands = mp.solutions.hands
hands = mp_hands.Hands(
    static_image_mode=False,
    max_num_hands=2,
    min_detection_confidence=0.5,
    min_tracking_confidence=0.5,
)
mp_drawing = mp.solutions.drawing_utils

finger_indices = [
    mp_hands.HandLandmark.THUMB_TIP,
    mp_hands.HandLandmark.INDEX_FINGER_TIP,
    mp_hands.HandLandmark.MIDDLE_FINGER_TIP,
    mp_hands.HandLandmark.RING_FINGER_TIP,
    mp_hands.HandLandmark.PINKY_TIP,
]


def label_fingers(frame, fingers, hand_landmarks, frame_width, frame_height):
    font = cv2.FONT_HERSHEY_SIMPLEX
    font_scale = 1.0
    color = (255, 0, 0)
    thickness = 2
    line_type = cv2.LINE_AA

    for idx, finger in enumerate(finger_indices):
        landmark = hand_landmarks.landmark[finger]
        x = int(landmark.x * frame_width)
        y = int(landmark.y * frame_height)
        text = str(fingers[idx])
        org = (x, y)
        cv2.putText(frame, text, org, font, font_scale, color, thickness, line_type)


async def process_video_stream(frame, fingers):
    frame = cv2.resize(frame, (600, 400))
    frame = cv2.flip(frame, 1)
    rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    result = hands.process(rgb_frame)

    if result.multi_hand_landmarks:
        frame_height, frame_width = frame.shape[:2]
        for hand_landmarks in result.multi_hand_landmarks:
            mp_drawing.draw_landmarks(
                frame,
                hand_landmarks,
                mp_hands.HAND_CONNECTIONS,
                solutions.drawing_styles.get_default_hand_landmarks_style(),
                solutions.drawing_styles.get_default_hand_connections_style(),
            )
            label_fingers(frame, fingers, hand_landmarks, frame_width, frame_height)

    return frame

运行时警告信息

/home/khalid/.local/lib/python3.10/site-packages/google/protobuf/symbol_database.py:55: UserWarning: SymbolDatabase.GetPrototype() is deprecated. Please use message_factory.GetMessageClass() instead. SymbolDatabase.GetPrototype() will be removed soon.
  warnings.warn('SymbolDatabase.GetPrototype() is deprecated. Please '

解决方法

首先明确:这个警告不是你的代码直接触发的,是MediaPipe内部依赖的protobuf库或者MediaPipe自身代码里使用了已弃用的API,不用改你自己的业务代码,按以下步骤处理:

  • 优先升级MediaPipe到最新版本
    官方会持续修复这类依赖兼容性问题,执行命令升级:

    pip install --upgrade mediapipe
    
  • 如果升级后仍有警告,修复protobuf版本兼容性
    MediaPipe对protobuf有特定版本要求,可能是你本地的protobuf版本过高/过低导致的:

    1. 先卸载现有protobuf:
      pip uninstall -y protobuf
      
    2. 重新安装MediaPipe,它会自动安装适配的protobuf版本:
      pip install mediapipe
      
  • 临时屏蔽警告(应急用,不推荐长期使用)
    如果你暂时不想调整依赖,可以在代码开头添加屏蔽该警告的逻辑:

    import warnings
    warnings.filterwarnings("ignore", category=UserWarning, message="SymbolDatabase.GetPrototype() is deprecated.*")
    
    # 后面再导入mediapipe和cv2
    import cv2
    import mediapipe as mp
    # ... 其余代码不变
    

内容的提问来源于stack exchange,提问作者Khalid BOUSSAROUAL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 21:42:07