Error building wheel for opencv-python from source

I am trying to build opencv-python with the following environment:

OS: MacOS Monterey 12.5
uname -a: Darwin Darwin Kernel Version 21.6.0 root:xnu-8020.140.41~1/RELEASE_ARM64_T6000 arm64

Python version: Python 3.10.3
Pip version: pip 22.2.2 from .../.pyenv/versions/3.10.3/lib/python3.10/site-packages/pip (python 3.10)

After running pipdeptree -fl:

cmake==3.24.0
meson==0.63.0
numpy==1.21.2
pipdeptree==2.2.1
  pip==22.2.2
scikit-build==0.15.0
  distro==1.7.0
  packaging==21.3
    pyparsing==3.0.9
  setuptools==59.2.0
  wheel==0.37.0

I also removed all previous imports:

pip show opencv-python
WARNING: Package(s) not found: opencv-python

pip show scikit-build -> Version: 0.15.0
pip show numpy -> Version: 1.21.2
pip show cmake -> Version: 3.24.0
pip show wheel -> Version: 0.37.0
pip show setuptools -> Version: 59.2.0

I set python to 3.10.3 after running pyevn global 3.10.3
Following that I did the following steps:

git clone --recursive https://github.com/skvark/opencv-python.git
cd opencv-python
export CMAKE_ARGS="-DWITH_GSTREAMER=ON"
pip install --upgrade pip wheel
pip wheel . --verbose &> build.out

This failed with the following error:

  creating _skbuild/macosx-12.0-arm64-3.10/setuptools/bdist.macosx-12.0-arm64/wheel/opencv_python-4.6.0.60630d1.dist-info/WHEEL
  creating '/private/var/folders/9b/gpqz3hy13vg_s90qbfxtb8pc0000gn/T/pip-wheel-9eyrsscn/tmpnkbqsfp1/opencv_python-4.6.0.60630d1-cp310-cp310-macosx_12_0_arm64.whl' and adding '_skbuild/macosx-12.0-arm64-3.10/setuptools/bdist.macosx-12.0-arm64/wheel' to it
  adding 'cv2/LICENSE-3RD-PARTY.txt'
  adding 'cv2/LICENSE.txt'
  adding 'cv2/__init__.py'
  adding 'cv2/config-3.py'
  adding 'cv2/config.py'
  adding 'cv2/cv2.abi3.so'
  adding 'cv2/load_config_py2.py'
  adding 'cv2/load_config_py3.py'
  adding 'cv2/version.py'
  adding 'cv2/data/__init__.py'
  adding 'cv2/data/haarcascade_eye.xml'
  adding 'cv2/data/haarcascade_eye_tree_eyeglasses.xml'
  adding 'cv2/data/haarcascade_frontalcatface.xml'
  adding 'cv2/data/haarcascade_frontalcatface_extended.xml'
  adding 'cv2/data/haarcascade_frontalface_alt.xml'
  adding 'cv2/data/haarcascade_frontalface_alt2.xml'
  adding 'cv2/data/haarcascade_frontalface_alt_tree.xml'
  adding 'cv2/data/haarcascade_frontalface_default.xml'
  adding 'cv2/data/haarcascade_fullbody.xml'
  adding 'cv2/data/haarcascade_lefteye_2splits.xml'
  adding 'cv2/data/haarcascade_licence_plate_rus_16stages.xml'
  adding 'cv2/data/haarcascade_lowerbody.xml'
  adding 'cv2/data/haarcascade_profileface.xml'
  adding 'cv2/data/haarcascade_righteye_2splits.xml'
  adding 'cv2/data/haarcascade_russian_plate_number.xml'
  adding 'cv2/data/haarcascade_smile.xml'
  adding 'cv2/data/haarcascade_upperbody.xml'
  adding 'cv2/gapi/__init__.py'
  adding 'cv2/mat_wrapper/__init__.py'
  adding 'cv2/misc/__init__.py'
  adding 'cv2/misc/version.py'
  adding 'cv2/utils/__init__.py'
  adding 'opencv_python-4.6.0.60630d1.dist-info/LICENSE-3RD-PARTY.txt'
  adding 'opencv_python-4.6.0.60630d1.dist-info/LICENSE.txt'
  adding 'opencv_python-4.6.0.60630d1.dist-info/METADATA'
  adding 'opencv_python-4.6.0.60630d1.dist-info/WHEEL'
  adding 'opencv_python-4.6.0.60630d1.dist-info/top_level.txt'
  adding 'opencv_python-4.6.0.60630d1.dist-info/RECORD'
  removing _skbuild/macosx-12.0-arm64-3.10/setuptools/bdist.macosx-12.0-arm64/wheel
  Building wheel for opencv-python (pyproject.toml): finished with status 'done'
  Created wheel for opencv-python: filename=opencv_python-4.6.0.60630d1-cp310-cp310-macosx_12_0_arm64.whl size=11504833 sha256=e18841f81ce452659982d1e171a799c9145a815c8de8b0b7a9809b80ac2d8167
  Stored in directory: .../Library/Caches/pip/wheels/24/50/27/aed596df8de2a2cbab5140a7a5774936bc1eee156ce1120fd4
  WARNING: Built wheel for opencv-python is invalid: Metadata 1.2 mandates PEP 440 version, but '4.6.0.60630d1' is not
Failed to build opencv-python
ERROR: Failed to build one or more wheels

After checking out branch 5.x the build produces the following error:

  creating _skbuild/macosx-12.0-arm64-3.10/setuptools/bdist.macosx-12.0-arm64/wheel/opencv_python-5.0.0.27cca18.dist-info/WHEEL
  creating '/private/var/folders/9b/gpqz3hy13vg_s90qbfxtb8pc0000gn/T/pip-wheel-fm0tq_ze/tmpi5qsqgb_/opencv_python-5.0.0.27cca18-cp310-cp310-macosx_12_0_arm64.whl' and adding '_skbuild/macosx-12.0-arm64-3.10/setuptools/bdist.macosx-12.0-arm64/wheel' to it
  adding 'cv2/LICENSE-3RD-PARTY.txt'
  adding 'cv2/LICENSE.txt'
  adding 'cv2/__init__.py'
  adding 'cv2/config-3.py'
  adding 'cv2/config.py'
  adding 'cv2/cv2.abi3.so'
  adding 'cv2/load_config_py2.py'
  adding 'cv2/load_config_py3.py'
  adding 'cv2/version.py'
  adding 'cv2/data/__init__.py'
  adding 'cv2/data/haarcascade_eye.xml'
  adding 'cv2/data/haarcascade_eye_tree_eyeglasses.xml'
  adding 'cv2/data/haarcascade_frontalcatface.xml'
  adding 'cv2/data/haarcascade_frontalcatface_extended.xml'
  adding 'cv2/data/haarcascade_frontalface_alt.xml'
  adding 'cv2/data/haarcascade_frontalface_alt2.xml'
  adding 'cv2/data/haarcascade_frontalface_alt_tree.xml'
  adding 'cv2/data/haarcascade_frontalface_default.xml'
  adding 'cv2/data/haarcascade_fullbody.xml'
  adding 'cv2/data/haarcascade_lefteye_2splits.xml'
  adding 'cv2/data/haarcascade_licence_plate_rus_16stages.xml'
  adding 'cv2/data/haarcascade_lowerbody.xml'
  adding 'cv2/data/haarcascade_profileface.xml'
  adding 'cv2/data/haarcascade_righteye_2splits.xml'
  adding 'cv2/data/haarcascade_russian_plate_number.xml'
  adding 'cv2/data/haarcascade_smile.xml'
  adding 'cv2/data/haarcascade_upperbody.xml'
  adding 'cv2/gapi/__init__.py'
  adding 'cv2/mat_wrapper/__init__.py'
  adding 'cv2/misc/__init__.py'
  adding 'cv2/misc/version.py'
  adding 'cv2/utils/__init__.py'
  adding 'opencv_python-5.0.0.27cca18.dist-info/LICENSE-3RD-PARTY.txt'
  adding 'opencv_python-5.0.0.27cca18.dist-info/LICENSE.txt'
  adding 'opencv_python-5.0.0.27cca18.dist-info/METADATA'
  adding 'opencv_python-5.0.0.27cca18.dist-info/WHEEL'
  adding 'opencv_python-5.0.0.27cca18.dist-info/top_level.txt'
  adding 'opencv_python-5.0.0.27cca18.dist-info/RECORD'
  removing _skbuild/macosx-12.0-arm64-3.10/setuptools/bdist.macosx-12.0-arm64/wheel
  Building wheel for opencv-python (pyproject.toml): finished with status 'done'
  Created wheel for opencv-python: filename=opencv_python-5.0.0.27cca18-cp310-cp310-macosx_12_0_arm64.whl size=13781263 sha256=ad073c827fd1919f8b2d9d30c1076af7937975ada5e548917448ea99417b75a0
  Stored in directory: .../Library/Caches/pip/wheels/24/50/27/aed596df8de2a2cbab5140a7a5774936bc1eee156ce1120fd4
  WARNING: Built wheel for opencv-python is invalid: Metadata 1.2 mandates PEP 440 version, but '5.0.0.27cca18' is not
Failed to build opencv-python
ERROR: Failed to build one or more wheels

I can build on branch 3.4 without a issue, but any subsequent tagged versions seem to fail without an explicit error in the output. What is causing the build error or how can I fix it?

Thanks!

Was able to look through find_version.py and found out that if I checked out a specific tag then then wheel build would actually comply with PEP 440. So I fixed this with:

git checkout tags/66 -b 4.6.0.66
 rm -rf _skbuild
pip wheel . --verbose &> build.out

After which I was able to install with pip install opencv_python*.whl.

A follow up question, should this be using PEP 440, instead of a later version? Or is this something wrong with my setup?

Thanks!

just out of curiosity – why do you try to build a wheel ?
what’s the use-case here ?
(lots of overhead, compared to just building the cv2 module locally)

According to the readme opencv-python does not come bundled with GStreamer, and it is recommended that we have to build with WITH_GSTREAMER=ON option. In my case I am testing capturing frames with gstreamer instead of ffmpeg. By building the wheel locally and installing it via pip I can then import the package in my requirements.txt file.

Is there a better way here?

Additionally I notice that even with_gstreamer=on it does not install plugins_good/bad/ugly. This causes errors for rtsp streams: opencv-python/opencv/modules/videoio/src/cap_gstreamer.cpp (1127) open OpenCV | GStreamer warning: Error opening bin: no element "rtspsrc".

Is there a way to include these plugins in macos? Or circling back, is there a better way to import in opencv-python with gstreamer?

1 Like