{"id":209,"date":"2021-02-22T20:53:08","date_gmt":"2021-02-22T20:53:08","guid":{"rendered":"https:\/\/moeenv.blog\/?p=209"},"modified":"2021-02-23T01:16:28","modified_gmt":"2021-02-23T01:16:28","slug":"installing-opencv-on-m1-macbook","status":"publish","type":"post","link":"https:\/\/moeenv.blog\/?p=209&lang=en","title":{"rendered":"Installing OpenCV on M1 MacBook"},"content":{"rendered":"<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/x_kAkabk-5o\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen=\"\"><\/iframe><\/p>\n\n\n\n<p> I have explained all the steps in this video tutorial. So while watching it, you can use the needed commands.<\/p>\n\n\n\n<p>Open Terminal on mac and run the following commands:<\/p>\n\n\n\n<p>Apple command line tools installation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo xcodebuild -license<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo xcode-select --install<\/code><\/pre>\n\n\n\n<p>brew installation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/usr\/bin\/ruby -e \"%(curl -fsSL https:\/\/raw.githubusercontent.com\/Homebrew\/install\/master\/install)\"<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>nano ~\/.zshrc<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>export PATH=$PATH:\/opt\/homebrew\/bin<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>source ~\/.zshrc<\/code><\/pre>\n\n\n\n<p>Installing Miniforge:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>brew install miniforge<\/code><\/pre>\n\n\n\n<p>Create Python environment in Conda:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>conda create --name &lt;environment_name&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>conda activate &lt;environment_name&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>conda install -y python==3.8.6<\/code><\/pre>\n\n\n\n<p>Install Numpy:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>% wget https:\/\/github.com\/apple\/tensorflow_macos\/releases\/download\/v0.1alpha0\/tensorflow_macos-0.1alpha0.tar.gz\n% tar xvf tensorflow_macos-0.1alpha0.tar.gz\n% cd tensorflow_macos\/arm64\n% pip install --upgrade --no-dependencies --force numpy-1.18.5-cp38-cp38-macosx_11_0_arm64.whl \n% cd ~<\/code><\/pre>\n\n\n\n<p>Compile OpenCV:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>% wget -O opencv.zip https:\/\/github.com\/opencv\/opencv\/archive\/4.5.0.zip\n% wget -O opencv_contrib.zip https:\/\/github.com\/opencv\/opencv_contrib\/archive\/4.5.0.zip\n% unzip opencv.zip\n% unzip opencv_contrib.zip\n% cd opencv-4.5.0\n% mkdir build &amp;&amp; cd build<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>% cmake \\\n  -DCMAKE_SYSTEM_PROCESSOR=arm64 \\\n  -DCMAKE_OSX_ARCHITECTURES=arm64 \\\n  -DWITH_OPENJPEG=OFF \\\n  -DWITH_IPP=OFF \\\n  -D CMAKE_BUILD_TYPE=RELEASE \\\n  -D CMAKE_INSTALL_PREFIX=\/usr\/local \\\n  -D OPENCV_EXTRA_MODULES_PATH=...\/opencv_contrib-4.5.0\/modules \\\n  -D PYTHON3_EXECUTABLE=...\/miniforge\/base\/envs\/TestOpenCv\/bin\/python3 \\\n  -D BUILD_opencv_python2=OFF \\\n  -D BUILD_opencv_python3=ON \\\n  -D INSTALL_PYTHON_EXAMPLES=ON \\\n  -D INSTALL_C_EXAMPLES=OFF \\\n  -D OPENCV_ENABLE_NONFREE=ON \\\n  -D BUILD_EXAMPLES=ON ..<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>make -j8<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo make install<\/code><\/pre>\n\n\n\n<p>Sym-link OpenCV 4 on macOS to virtual environment site-packages:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>% mdfind cv2.cpython\n\/...\/opencv-4.5.0\/build\/lib\/python3\/cv2.cpython-38-darwin.so\n\/usr\/local\/lib\/python3.8\/site-packages\/cv2\/python-3.8\/cv2.cpython-38-darwin.so<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>% cd \/...\/miniforge3\/envs\/dev\/lib\/python3.8\/site-packages\n% ln -s \/usr\/local\/lib\/python3.8\/site-packages\/cv2\/python-3.8\/cv2.cpython-38-darwin.so cv2.so<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>% python\n&gt;&gt;&gt; import cv2\n&gt;&gt;&gt; cv2.__version__<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have explained all the steps in this video tutorial. So while watching it, you can use the needed commands. Open Terminal on mac and run the following commands: Apple command line tools installation: brew installation: Installing Miniforge: Create Python environment in Conda: Install Numpy: Compile OpenCV: Sym-link OpenCV 4 on macOS to virtual environment &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[62],"tags":[73,71,67,69,72,68,70],"_links":{"self":[{"href":"https:\/\/moeenv.blog\/index.php?rest_route=\/wp\/v2\/posts\/209"}],"collection":[{"href":"https:\/\/moeenv.blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moeenv.blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moeenv.blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/moeenv.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=209"}],"version-history":[{"count":8,"href":"https:\/\/moeenv.blog\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions"}],"predecessor-version":[{"id":244,"href":"https:\/\/moeenv.blog\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions\/244"}],"wp:attachment":[{"href":"https:\/\/moeenv.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moeenv.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moeenv.blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}