Python Tensorflow and OpenCV on Apple Silicon M1

Viewed 1542

I try to use OpenCV and Tensorflow with Python on Apple silicon M1. I'm able to use Tensorflow, but install OpenCV in my environment fails

pip3 install opencv-python

Even build opencv-python by my own locally fails on numpy

git clone https://github.com/skvark/opencv-python.git
cd opencv-python
ENABLE_CONTRIB=0 
ENABLE_HEADLESS=1
pip wheel . --verbose

with

ERROR: Command errored out with exit status 1: /Users/hannes/git/city-simulator/tensorflow_venv2/bin/python3 /Users/hannes/git/city-simulator/tensorflow_venv2/lib/python3.8/site-packages/pip install --ignore-installed --no-user --prefix /private/var/folders/np/7ckckk8s5fd169kvnnfhjkk40000gr/T/pip-build-env-3zg4n104/overlay --no-warn-script-location -v --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- setuptools wheel scikit-build cmake pip 'numpy==1.13.3; python_version=='"'"'3.6'"'"' and platform_machine != '"'"'aarch64'"'"'' 'numpy==1.19.3; python_version>='"'"'3.6'"'"' and sys_platform == '"'"'linux'"'"' and platform_machine == '"'"'aarch64'"'"'' 'numpy==1.14.5; python_version=='"'"'3.7'"'"' and platform_machine != '"'"'aarch64'"'"'' 'numpy==1.17.3; python_version=='"'"'3.8'"'"' and platform_machine != '"'"'aarch64'"'"'' 'numpy==1.19.3; python_version>='"'"'3.9'"'"' and platform_machine != '"'"'aarch64'"'"'' Check the logs for full command output.

Even build numpy by my own fails

git clone git@github.com:numpy/numpy.git
cd numpy
OPENBLAS="$(brew --prefix openblas)" MACOSX_DEPLOYMENT_TARGET=11.1 python3 -m pip install cython --no-use-pep517
OPENBLAS="$(brew --prefix openblas)" MACOSX_DEPLOYMENT_TARGET=11.1 python3 -m pip install numpy --no-use-pep517
OPENBLAS="$(brew --prefix openblas)" MACOSX_DEPLOYMENT_TARGET=11.1 python3 -m pip install pandas --no-use-pep517
OPENBLAS="$(brew --prefix openblas)" MACOSX_DEPLOYMENT_TARGET=11.1 python3 -m pip install pybind11 --no-use-pep517
OPENBLAS="$(brew --prefix openblas)" MACOSX_DEPLOYMENT_TARGET=11.1 python3 -m pip install scipy --no-use-pep517
brew install libjpeg zlib
python3 -m pip install pillow
python3 -m pip install matplotlib
OPENBLAS="$(brew --prefix openblas)" MACOSX_DEPLOYMENT_TARGET=11.1 python3 -m pip install scikit-learn --no-use-pep517
OPENBLAS="$(brew --prefix openblas)" MACOSX_DEPLOYMENT_TARGET=11.1 python3 -m pip install statsmodels --no-use-pep517
cd ../opencv-python
pip wheel . --verbose
pip install numpy --no-index --find-links file:///Users/hannes/git/city-simulator/numpy/numpy

all without errors, bun on last I see this

Looking in links: file:///Users/hannes/git/city-simulator/numpy/numpy
Requirement already satisfied: numpy in /Users/hannes/git/city-simulator/tensorflow_venv2/lib/python3.8/site-packages (1.18.5)

and pip wheel . --verbose still runs into same error

1 Answers

I made it work with conda !

cd ~/Downloads
wget https://github.com/conda-forge/miniforge/releases/download/4.9.2-7/Mambaforge-4.9.2-7-MacOSX-arm64.sh 
chmod +x Mambaforge-4.9.2-7-MacOSX-arm64.sh
./Mambaforge-4.9.2-7-MacOSX-arm64.sh
which python
file $(which python)
wget https://raw.githubusercontent.com/mwidjaja1/DSOnMacARM/main/environment.yml
conda env create --file=/Users/hannes/Downloads/environment.yml --name=condaVenv
conda activate condaVenv
pip3 install --upgrade --force --no-dependencies \
  https://github.com/apple/tensorflow_macos/releases/download/v0.1alpha3/tensorflow_macos-0.1a3-cp38-cp38-macosx_11_0_$(uname -m).whl \
  https://github.com/apple/tensorflow_macos/releases/download/v0.1alpha3/tensorflow_addons_macos-0.1a3-cp38-cp38-macosx_11_0_$(uname -m).whl
python -c 'import tensorflow as tf; print(tf.__version__)' 
conda install opencv
python -c 'import tensorflow as tf; import cv2; print(cv2.__version__); print(tf.__version__)'

shows now

4.5.1
2.4.0-rc0

ATTENTION during copy&paste this command !

When you copy it, you will see

enter image description here

Please remove \ after paste

Related