杰瑞科技汇

yum python opencv

Of course. Here’s a comprehensive guide on how to install OpenCV for Python using yum, which is the package manager for RPM-based distributions like CentOS, RHEL, and Fedora.

Important Note: yum vs. dnf

  • yum: The older package manager. Used on CentOS 7 and RHEL 7.
  • dnf: The newer, default package manager for CentOS 8+, RHEL 8+, and modern Fedora. It is a successor to yum with the same command-line syntax and is generally recommended.

For the best results, you should use the package manager your system provides. If you are on a modern system (CentOS 8+, RHEL 8+), replace yum with dnf in all the commands below.


Option 1: The Recommended Way (Using EPEL Repository)

The standard yum repositories do not include the Python 3 bindings for OpenCV. The best and most reliable method is to use the EPEL (Extra Packages for Enterprise Linux) repository.

Step 1: Enable the EPEL Repository

First, you need to add the EPEL repository to your system.

# For CentOS 7, RHEL 7
sudo yum install epel-release
# For CentOS 8, RHEL 8 (using dnf)
sudo dnf install epel-release

Step 2: Install OpenCV

Now you can install OpenCV using yum or dnf. This will typically install the library for both Python 2 and Python 3.

# Using yum (CentOS 7, RHEL 7)
sudo yum install opencv-python
# Using dnf (CentOS 8, RHEL 8, Fedora)
sudo dnf install opencv-python

Step 3: Verify the Installation

You can verify the installation by running a simple Python script.

python3 -c "import cv2; print(cv2.__version__)"

If the installation was successful, this command will print the installed OpenCV version (e.g., 5.5).


Option 2: The Manual Way (Compiling from Source)

If the version available in EPEL is too old for your needs, you can compile OpenCV from source. This gives you full control over the version and features but is more complex.

Step 1: Install Dependencies

You'll need a lot of development tools and libraries.

# Install development tools and basic dependencies
sudo yum groupinstall "Development Tools"
sudo yum install cmake git
# Install Python 3 development headers
sudo yum install python3-devel
# Install OpenCV's required libraries
sudo yum install \
    gtk2-devel \
    libjpeg-turbo-devel \
    libpng-devel \
    libtiff-devel \
    jasper-devel \
    openexr-devel \
    libwebp-devel \
    tbb-devel \
    libdc1394-devel \
    numpy-devel

Step 2: Download OpenCV Source Code

Use git to clone the OpenCV repository. You can choose a specific version or the latest.

# Clone the latest 4.x version
git clone --depth 1 https://github.com/opencv/opencv.git
git clone --depth 1 https://github.com/opencv/opencv_contrib.git

Step 3: Create a Build Directory and Configure with CMake

It's a best practice to build in a separate directory.

cd opencv
mkdir build && cd build
# Configure the build with CMake
# This command enables Python 3, specifies the path to your Python interpreter,
# and links against the numpy library.
cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D WITH_PYTHON=ON \
      -D PYTHON_EXECUTABLE=/usr/bin/python3 \
      -D PYTHON_INCLUDE_DIR=$(python3 -c "from sysconfig import get_paths; print(get_paths()['include'])") \
      -D PYTHON_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
      -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
      -D BUILD_EXAMPLES=ON \
      -D BUILD_TESTS=OFF \
      -D BUILD_PERF_TESTS=OFF \
      ..

Step 4: Compile and Install

This step will take a significant amount of time, depending on your CPU speed.

# Compile the code (use -j to speed up with multiple cores)
# Replace N with the number of CPU cores you have (e.g., -j 4)
make -j$(nproc)
# Install the compiled files
sudo make install

Step 5: Verify the Installation

After the installation, the shared libraries are in /usr/local/lib. You may need to update the library cache.

sudo ldconfig

Now, run the verification command again:

python3 -c "import cv2; print(cv2.__version__)"

This should now print the version you just compiled (e.g., 7.0).


Option 3: The Modern Python Way (Using pip)

For most Python projects, the recommended way to manage packages is using pip. This installs OpenCV in a user-specific or virtual environment, avoiding conflicts with system packages.

Step 1: Install pip for Python 3

If you don't have pip installed:

sudo yum install python3-pip

Step 2: Install OpenCV using pip

This is the simplest command.

pip3 install opencv-python

This will download and install the latest compatible binary wheel of OpenCV for your system.

Step 3: Verify the Installation

The verification command is the same:

python3 -c "import cv2; print(cv2.__version__)"

Summary and Recommendation

Method Pros Cons Best For
EPEL (yum/dnf) - Easy, one command
- Integrates with system package manager
- Handles dependencies automatically
- May have an older version of OpenCV - Users on stable servers (like RHEL/CentOS) who need a reliable, system-wide installation.
Compile from Source - Full control over version and features (like contrib modules)
- Can be optimized for your system
- Very complex and time-consuming
- Prone to build errors
- Many manual dependencies
- Developers/researchers who need the absolute latest features or specific build options.
pip - Extremely easy
- Always gets the latest version from PyPI
- Standard for Python projects
- Installs to a virtual environment or user directory, not system-wide
- Can conflict with system packages if not managed carefully
- Most Python developers. The standard and recommended approach for almost all use cases.

Final Recommendation:

For most use cases, using pip (Option 3) is the best choice. It's the modern standard for Python package management and is the simplest way to get a recent version of OpenCV.

If you are managing a system where you need a package managed by the system package manager (e.g., for a dependency of another RPM package), then using the EPEL repository (Option 1) is the correct approach.

分享:
扫描分享到社交APP
上一篇
下一篇