最近在Jetson Nano上遇到个怪事,考虑到懒得自己编译安装arm64平台上的pytorch,直接拉取NVIDIA的用于JetPack4.5的镜像l4t-pytorch来使用:

docker pull nvcr.io/nvidia/l4t-pytorch:r32.5.0-pth1.7-py3

sudo docker run -it -d --runtime nvidia --network host --ipc host -e DISPLAY=$DISPLAY -v /home/ubuntu:/workspace --name torch1.7-torch2trt nvcr.io/nvidia/l4t-pytorch:r32.5.0-pth1.7-py3

后执行 pip install opencv-python下载源码包自动进行编译build出whl文件并安装都成功了,可是执行代码时遇到问题:

cap = VideoCapture("./videos/palace.mp4")

这个最简单不过的代码都始终不成功,检查cap.isOpened()的值始终为false!安装网上有人说的用apt-get install ffmpeg或者安装python版ffmpeg什么的都没用! 反复折腾后想想应该还是安装的opencv-python有问题,于是卸载了再执行pip installl opencv-python时始终提示没有skbuild:

root@JP45:/workspace/ByteTrack/# pip install opencv-python -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host  mirrors.aliyun.com
Collecting opencv-python
  Downloading http://mirrors.aliyun.com/pypi/packages/b6/82/0519fdbbcaddc0fa8c2568327a8311477315a91b4513738ee1d35f0ce715/opencv-python-4.5.4.58.tar.gz (89.8MB)
    100% |████████████████████████████████| 89.8MB 3.5MB/s
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-kesxp0gq/opencv-python/setup.py", line 10, in <module>
        import skbuild
    ModuleNotFoundError: No module named 'skbuild'

没有那就安装吧:pip install scikit-build,安装scikit-build 0.12版后再装opencv-python,结果遇到更莫名其妙的错误No such file or directory: 'scripts/__init__.py',我估计是因为scikit-build最新版有问题:

pip install opencv-python -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host  mirrors.aliyun.com

Collecting opencv-python
  Downloading http://mirrors.aliyun.com/pypi/packages/b6/82/0519fdbbcaddc0fa8c2568327a8311477315a91b4513738ee1d35f0ce715/opencv-python-4.5.4.58.tar.gz (89.8MB)
  ...
 Copying files from CMake output
  Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "/tmp/pip-build-_2qe__rg/opencv-python/setup.py", line 496, in <module>
      main()
    File "/tmp/pip-build-_2qe__rg/opencv-python/setup.py", line 289, in main
      cmake_source_dir=cmake_source_dir,
    File "/usr/local/lib/python3.6/dist-packages/skbuild/setuptools_wrap.py", line 629, in setup
      cmake_source_dir, skbuild_kw['cmake_install_dir'])
    File "/tmp/pip-build-_2qe__rg/opencv-python/setup.py", line 381, in _classify_installed_files_override
      with open('scripts/__init__.py', 'r') as custom_init:
  FileNotFoundError: [Errno 2] No such file or directory: 'scripts/__init__.py'

反正进退维谷的感觉,急着赶活却在环境安装上浪费了不少时间,最后抱着试试看的心理先升级一下pip,然后再安装opencv-python看能否找到合适的版本自动安装,结果奇迹还出现了:

先升级pip到最新版:

root@JP45:/workspace/ByteTrack# pip install --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/90/a9/1ea3a69a51dcc679724e3512fc2aa1668999eed59976f749134eb02229c8/pip-21.3-py3-none-any.whl (1.7MB)
    100% |████████████████████████████████| 1.7MB 218kB/s
Installing collected packages: pip
  Found existing installation: pip 9.0.1
    Not uninstalling pip at /usr/lib/python3/dist-packages, outside environment /usr
Successfully installed pip-21.3

再安装opencv-python,结果最新版的pip还真的能自动去发现用于arm64平台的opencv-python的whl安装包opencv_python-4.5.4.58-cp36-cp36m-manylinux2014_aarch64.whl  !而不是去下载opencv-python-4.5.4.58.tar.gz源码包然后费劲地编译出whl安装包,这样就绕开了scikit-build的问题!

###升级后再安装opencv-python就没问题了!而且根本不需要scikit-build,因为不需要编译whl安装文件了!
root@JP45:/workspace/ByteTrack# pip install opencv-python -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host  mirrors.aliyun.com
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting opencv-python
  Downloading http://mirrors.aliyun.com/pypi/packages/2b/11/2fa2b7577a9dc1721fe1c9b67d25b1a9a5b139835962f59d9d4964e7801a/opencv_python-4.5.4.58-cp36-cp36m-manylinux2014_aarch64.whl (39.0 MB)
     |████████████████████████████████| 39.0 MB 18 kB/s
Requirement already satisfied: numpy>=1.19.3 in /usr/local/lib/python3.6/dist-packages (from opencv-python) (1.19.4)
Installing collected packages: opencv-python
Successfully installed opencv-python-4.5.4.58

安装完感觉瞬间轻松了,然后再执行代码,果然cap = VideoCapture("./videos/palace.mp4")可以正常打开视频文件了!

     有时环境安装因为版本不对或者冲突(不同软件包要求不同版本的numpy的问题最要命!)真的很浪费时间可以折腾si人!记录下来这个教训,以后安装新环境时得经常记得,最好先更新pip后再进行后面的其他python版本的各种支持包的安装步骤!

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐