最近使用yolov5做项目,但是在安装pycocotools和opencv的时候却遇到了麻烦,始终报错。尝试了网上的各种解决办法都无解,历时一下午终于解决了这个头疼的问题。
首先建立了yolov5的虚拟环境,然后切换到yolov5环境下,根据官方源码的requirements.txt文件安装依赖关系。
(yolov5) C:\Research\landslide\code\yolov5\yolov5-master>pip install -r C:\Research\landslide\code\yolov5\yolov5-master\requirements.txt
一杯茶结束后,发现报错,然后 conda list列出所有包和requirements.txt中的要求对比,发现少了pycocotools和opencv-python两个包。然后再次执行下面的代码都报错。
pip install pycocotools
pip install opencv-python
分别报错信息如下:
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)'))': /simple/pycocotools/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)'))': /simple/pycocotools/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)'))': /simple/pycocotools/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)'))': /simple/pycocotools/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)'))': /simple/pycocotools/
Could not fetch URL https://pypi.org/simple/pycocotools/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pycocotools/ (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)'))) - skipping
ERROR: Could not find a version that satisfies the requirement pycocotools
ERROR: No matching distribution found for pycocotools
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)'))': /simple/opencv-python/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)'))': /simple/opencv-python/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)'))': /simple/opencv-python/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)'))': /simple/opencv-python/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)'))': /simple/opencv-python/
Could not fetch URL https://pypi.org/simple/opencv-python/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/opencv-python/ (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)'))) - skipping
ERROR: Could not find a version that satisfies the requirement opencv-python
ERROR: No matching distribution found for opencv-python
最后找到的解决办法就是,到对应的官网去下载.whl文件包,然后指定路径安装就可以
到oencv官网下载对应的.whl文件(opencv官网下载地址)
根据自己的python和电脑操作系统版本下载对应的包,我下载的是:
opencv_python-4.4.0-cp38-cp38-win_amd64.whl
opencv_python-4.4.0:opencv版本号
cp38-cp38:环境中python的版本号
win_amd64:windows64位操作系统
将下载好的.whl放置到anaconda目录下的yolov5环境中的site-packages文件下下,比如我放置在
最后再次安装opencv
(yolov5) C:\Research\landslide\code\yolov5\yolov5-master>pip install C:\Users\40809\anaconda3\envs\yolov5\Lib\site-packages\opencv_python-4.4.0-cp38-cp38-win_amd64.whl
提示如下即为安装成功
Processing c:\users\40809\anaconda3\envs\yolov5\lib\site-packages\opencv_python-4.4.0-cp38-cp38-win_amd64.whl
Installing collected packages: opencv-python
Successfully installed opencv-python-4.4.0
安装方法和安装opencv一样,包下载地址(pycocotools下载地址)。
安装完成后运行 conda list即可看到已经全部安装成功。
昨天安装完后,今天运行yolov5时候还是报错了,报错如下:
pkg_resources.DistributionNotFound: The 'pycocotools>=2.0' distribution was not found and is required by the application
我去查看,发现我的pycocotools的名字叫做pycocotools-windows。如下:正确的应该是第一个,但是我当时只有第二个。
然后仔细去cocoapi网站仔细读了一下(cocoapi的github网址),发现官网说法如下:
因为我的电脑之前已经安装了Visual C++ 2015和git,然后切换到虚拟环境直接运行了这句代码,成!
pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI
至于昨天装出来的是pycocotools-windows,今天还没找到解决办法。