搬运总结一下国外的教程,原地址https://sayak.dev/install-opencv-m1/#Install-conda
1、安装brew
/bin/bash -c “$(curl -fsSLhttps://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”
2、安装wget
brew install wget
3、安装miniforge,这是Anaconda在M1上的替代品,这是什么东西?不懂别问,自行百度
安装miniforge
brew install miniforge
创建conda虚拟环境
conda create --name (自定义名称)
激活环境
conda activate (刚才创建的自定义名称)
会为你安装一些常用的Python包,这是我安装的版本号,你可以随意改,出问题不许问
conda install -y python==3.8.6
4、安装cmake
https://cmake.org/download/官网下载,自行选择对应的系统版本
安装完毕cmake后,命令行执行:
sudo “/Applications/CMake.app/Contents/bin/cmake-gui” --install
5、安装opencv前置依赖NumPy,这里在pip install的时候,必须激活虚拟环境!
wget https://github.com/apple/tensorflow_macos/releases/download/v0.1alpha0/tensorflow_macos 0.1alpha0.tar.gz
tar xvf tensorflow_macos-0.1alpha0.tar.gz cd tensorflow_macos/arm64
pip install --upgrade --no-dependencies --force
numpy-1.18.5-cp38-cp38-macosx_11_0_arm64.whl cd ~
6、git拉取最新opencv和opencv_contrib,不会clone的直接下载zip包然后解压
https://github.com/opencv/opencv
https://github.com/opencv/opencv_contrib
cd opencv
mkdir build && cd build
PYTHON3_EXECUTABLE 这里另开cmd窗口,同样要激活conda环境,执行:which python,把结果替换
OPENCV_EXTRA_MODULES_PATH 是opencv_contrib文件夹里的models路径
arch -arm64 cmake \
-DCMAKE_SYSTEM_PROCESSOR=arm64 \
-DCMAKE_OSX_ARCHITECTURES=arm64 \
-DWITH_OPENJPEG=OFF \
-DWITH_IPP=OFF \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=/Users/sayakpaul/Downloads/opencv_contrib/modules \
-D PYTHON3_EXECUTABLE=/Users/sayakpaul/miniforge3/envs/dev/bin/python3 \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D BUILD_EXAMPLES=ON …
这里大约三四分钟
arch -arm64
这里大约8-10分钟,会把M1的8个核拉满
arch -arm64 make -j8
最后一步
arch -arm64 sudo make install
6、将 macOS 上的 OpenCV 4 符号链接到虚拟环境
mdfind cv2.cpython
会出现几行路径,复制类似下面这行
/usr/local/lib/python3.8/site-packages/cv2/python-3.8/cv2.cpython-38-darwin.so
cd /usr/local/lib/python3.8/site-packages
sudo ln -s /usr/local/lib/python3.8/site-packages/cv2/python-3.8/cv2.cpython-38-darwin.so cv2.so
7、最后的验证是否安装成功
python
import cv2
cv2.version
End…