【精华】OpenCV常见报错信息及解决方法汇总

目录

        • 1 报错信息:安装opencv-python报超时
          • 解决方法
        • 2 报错信息:'cvWaitKey'
          • 原因分析
          • 解决办法
        • 3 报错信息:'_registerMatType'
          • 原因分析
          • 解决方法
        • 4 报错信息:protobuf
          • 原因分析
          • 解决方法

1 报错信息:安装opencv-python报超时

>>> pip install opencv-python
Collecting opencv-python
  Downloading opencv_python-4.5.5.64-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (60.5 MB)
     || 307 kB 8.7 kB/s eta 1:55:00ERROR: Exception:
Traceback (most recent call last):
  File "/home/leezhao/anaconda3/lib/python3.9/site-packages/pip/_vendor/urllib3/response.py", line 438, in _error_catcher
    yield
  File "/home/leezhao/anaconda3/lib/python3.9/site-packages/pip/_vendor/urllib3/response.py", line 519, in read
    data = self._fp.read(amt) if not fp_closed else b""
  File "/home/leezhao/anaconda3/lib/python3.9/site-packages/pip/_vendor/cachecontrol/filewrapper.py", line 62, in read
    data = self.__fp.read(amt)
  File "/home/leezhao/anaconda3/lib/python3.9/http/client.py", line 463, in read
    n = self.readinto(b)
  File "/home/leezhao/anaconda3/lib/python3.9/http/client.py", line 507, in readinto
    n = self.fp.readinto(b)
  File "/home/leezhao/anaconda3/lib/python3.9/socket.py", line 704, in readinto
    return self._sock.recv_into(b)
  File "/home/leezhao/anaconda3/lib/python3.9/ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)
  File "/home/leezhao/anaconda3/lib/python3.9/ssl.py", line 1099, in read
    return self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out
解决方法

使用豆瓣源下载opencv-python

pip install opencv-python -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
或者
pip install opencv-python -i https://pypi.doubanio.com/simple

2 报错信息:‘cvWaitKey’

cv2.error: OpenCV(4.5.5) /io/opencv/modules/highgui/src/window.cpp:1334: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvWaitKey'
或
cv2.error: OpenCV(4.5.5) /io/opencv/modules/highgui/src/window.cpp:651: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'
原因分析

opencv-python需要配套扩展库opencv-contrib-python使用

解决办法
pip install --upgrade pip
pip install opencv-contrib-python

3 报错信息:‘_registerMatType’

ImportError: cannot import name '_registerMatType' from 'cv2.cv2'
原因分析

opencv-python-headless版本过高

解决方法
pip uninstall opencv-python-headless
pip install "opencv-python-headless<4.3"

4 报错信息:protobuf

TypeError: Descriptors cannot not be created directly.
If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
If you cannot immediately regenerate your protos, some other possible workarounds are:
 1. Downgrade the protobuf package to 3.20.x or lower.
 2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).
原因分析

protobuf 版本过高

解决方法
pip uninstall protobuf
pip install protobuf==3.20

你可能感兴趣的:(OpenCV,opencv,python,计算机视觉)