如何在Raspbian Jessie中正确安装cv2?python3-opencv安装报错求助
Hey there, let's sort out this OpenCV installation problem you're facing on Raspbian Jessie with Python 3.4.2. The errors you're getting happen because the package names you tried aren't quite right, and Raspbian Jessie's default repositories don't include a pre-built Python 3 OpenCV package. Here are the most reliable ways to get cv2 installed:
Sometimes the missing package issue is just because your repo list is outdated or missing the right sources. Give this a shot:
- Update your package index and upgrade existing packages:
sudo apt-get update && sudo apt-get upgrade -y - Open your sources list file to add a more complete repo:
sudo nano /etc/apt/sources.list - Make sure the file includes this line (if it's missing, add it at the end):
deb http://mirrordirector.raspbian.org/raspbian/ jessie main contrib non-free rpi - Save and exit the editor (press
Ctrl+O, hit Enter, thenCtrl+X), then update again:sudo apt-get update - Now try installing the correct package:
sudo apt-get install python3-opencv
If this still fails (which is common for older Jessie builds), move on to the more reliable source compilation method below.
Since Raspbian Jessie is pretty outdated, pre-built packages are hard to come by. Compiling from source ensures compatibility with your Python 3.4.2 environment. It takes a bit of time, but it works every time:
Install all required dependencies
Run this command to grab every tool and library OpenCV needs:sudo apt-get install build-essential cmake pkg-config libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libgtk2.0-dev libgtk-3-dev libatlas-base-dev gfortran python3-dev python3-pipInstall NumPy (required for OpenCV)
pip3 install numpyDownload compatible OpenCV source code
We'll use OpenCV 3.4.16, the last stable 3.x release that works with Python 3.4:cd ~ git clone https://github.com/opencv/opencv.git cd opencv git checkout 3.4.16 cd .. git clone https://github.com/opencv/opencv_contrib.git cd opencv_contrib git checkout 3.4.16 cd ..Configure the build environment
Create a build directory and set up compile options tailored to your system:cd opencv mkdir build cd build cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D INSTALL_C_EXAMPLES=OFF \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ -D PYTHON_EXECUTABLE=/usr/bin/python3 \ -D BUILD_EXAMPLES=ON ..Start compiling
This will take 1-2 hours on a typical Raspberry Pi. Use-j2to use 2 CPU cores (adjust to-j4if you have a Pi 3/4 with more cores):make -j2Install the compiled files
sudo make install sudo ldconfigVerify the installation
Open a Python 3 shell and test importing cv2:import cv2 print(cv2.__version__)If you see
3.4.16printed out, you're good to go!
Quick note on your original errors: The pip3 install python3-opencv command was incorrect—PyPI's package name for OpenCV is opencv-python, but even that won't work for Python 3.4 on Jessie since newer PyPI packages drop support for older Python versions. Compiling from source is the safest bet here.
内容的提问来源于stack exchange,提问作者Nishajith .A

