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

cv2模块AttributeError:_registerMatType缺失(循环导入)求助

解决OpenCV导入报错:AttributeError: partially initialized module 'cv2' has no attribute '_registerMatType'

问题描述

运行基于cv2、mediapipe和pyautogui开发的眼控鼠标代码时,出现循环导入类错误:AttributeError: partially initialized module 'cv2' has no attribute '_registerMatType' (most likely due to a circular import)。已尝试卸载OpenCV并重装headless版本,但问题仍未解决。

相关源码

import cv2
import mediapipe as mp
import pyautogui
cam = cv2.VideoCapture(0)
face_mesh = mp.solutions.face_mesh.FaceMesh(refine_landmarks=True)
screen_w, screen_h = pyautogui.size()
while True:
    _, frame = cam.read()
    frame = cv2.flip(frame, 1)
    rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    output = face_mesh.process(rgb_frame)
    landmark_points = output.multi_face_landmarks
    frame_h, frame_w, _ = frame.shape
    if landmark_points:
        landmarks = landmark_points[0].landmark
        for id, landmark in enumerate(landmarks[474:478]):
            x = int(landmark.x * frame_w)
            y = int(landmark.y * frame_h)
            cv2.circle(frame, (x, y), 3, (0, 255, 0))
            if id == 1:
                screen_x = screen_w * landmark.x
                screen_y = screen_h * landmark.y
                pyautogui.moveTo(screen_x, screen_y)
        left = [landmarks[145], landmarks[159]]
        for landmark in left:
            x = int(landmark.x * frame_w)
            y = int(landmark.y * frame_h)
            cv2.circle(frame, (x, y), 3, (0, 255, 255))
        if (left[0].y - left[1].y) < 0.004:
            pyautogui.click()
            pyautogui.sleep(1)
    cv2.imshow('Eye Controlled Mouse', frame)
    cv2.waitKey(1)

错误日志

Traceback (most recent call last):
  File "/home/yusra/Documents/test_proj/main.py", line 1, in <module>
    import cv2
  File "/home/yusra/anaconda3/lib/python3.9/site-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/home/yusra/anaconda3/lib/python3.9/site-packages/cv2/__init__.py", line 175, in bootstrap
    if __load_extra_py_code_for_module("cv2", submodule, DEBUG):
  File "/home/yusra/anaconda3/lib/python3.9/site-packages/cv2/__init__.py", line 28, in __load_extra_py_code_for_module
    py_module = importlib.import_module(module_name)
  File "/home/yusra/anaconda3/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/yusra/anaconda3/lib/python3.9/site-packages/cv2/mat_wrapper/__init__.py", line 33, in <module>
    cv._registerMatType(Mat)
AttributeError: partially initialized module 'cv2' has no attribute '_registerMatType' (most likely due to a circular import)
(base) yusra@yusra-Vostro-3480:~/Documents/test_proj$ 

排查解决步骤

  • 检查命名冲突:确认项目目录下没有名为cv2.py的文件或cv2文件夹,避免覆盖官方OpenCV库导致循环导入。
  • 替换为标准OpenCV版本:代码中使用了cv2.imshow等GUI功能,headless版本不支持,需完全清理后重装标准版本:
    1. 卸载所有相关OpenCV包:
      pip uninstall opencv-python opencv-contrib-python opencv-python-headless -y
      
    2. 重新安装标准OpenCV包:
      pip install opencv-python opencv-contrib-python
      
  • 解决conda环境冲突:如果使用Anaconda环境,优先用conda安装避免版本不兼容:
    1. 卸载pip安装的OpenCV:
      pip uninstall opencv-python opencv-contrib-python -y
      
    2. 通过conda安装:
      conda install -c conda-forge opencv
      
  • 验证安装有效性:新建测试脚本test_cv2.py,运行确认无报错:
    import cv2
    print(cv2.__version__)
    

内容的提问来源于stack exchange,提问作者yusra ansari

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 18:55:30