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:
sudo xcodebuild -license
sudo xcode-select --install
brew installation:
/usr/bin/ruby -e "%(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
nano ~/.zshrc
export PATH=$PATH:/opt/homebrew/bin
source ~/.zshrc
Installing Miniforge:
brew install miniforge
Create Python environment in Conda:
conda create --name <environment_name>
conda activate <environment_name>
conda install -y python==3.8.6
Install Numpy:
% 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 ~
Compile OpenCV:
% wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.0.zip
% wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.0.zip
% unzip opencv.zip
% unzip opencv_contrib.zip
% cd opencv-4.5.0
% mkdir build && cd build
% 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=.../opencv_contrib-4.5.0/modules \
-D PYTHON3_EXECUTABLE=.../miniforge/base/envs/TestOpenCv/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 ..
make -j8
sudo make install
Sym-link OpenCV 4 on macOS to virtual environment site-packages:
% mdfind cv2.cpython
/.../opencv-4.5.0/build/lib/python3/cv2.cpython-38-darwin.so
/usr/local/lib/python3.8/site-packages/cv2/python-3.8/cv2.cpython-38-darwin.so
% cd /.../miniforge3/envs/dev/lib/python3.8/site-packages
% ln -s /usr/local/lib/python3.8/site-packages/cv2/python-3.8/cv2.cpython-38-darwin.so cv2.so
% python
>>> import cv2
>>> cv2.__version__
Hello,
I was just wondering if you could perhaps show what your .zshrc file looks like? I might have removed a couple of lines from the file and added the new line (export PATH…) in the wrong location.
Thanks!
Kind regards,
Tin
Hello,
This is my .zshrc filr:
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/opt/homebrew/Caskroom/miniforge/base/bin/conda' 'shell.zsh' $
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/opt/homebrew/Caskroom/miniforge/base/etc/profile.d/conda.sh" ]; t$
. "/opt/homebrew/Caskroom/miniforge/base/etc/profile.d/conda.sh"
else
export PATH="/opt/homebrew/Caskroom/miniforge/base/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
Hi,
I tried /usr/bin/ruby -e “%(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)” but it didn’t do anything and when I typed brew install miniforge, it said cannot find brew command?
Thank you
Hi,
My .zshrc file looks like:
# >>> conda initialize >>>
# !! Contents within this block are managed by ‘conda init’ !!
__conda_setup=”$(‘/Users/seankoh/opt/anaconda3/bin/conda’ ‘shell.zsh’ ‘hook’ 2$
if [ $? -eq 0 ]; then
eval “$__conda_setup”
else
if [ -f “/Users/seankoh/opt/anaconda3/etc/profile.d/conda.sh” ]; then
. “/Users/seankoh/opt/anaconda3/etc/profile.d/conda.sh”
else
export PATH=”/Users/seankoh/opt/anaconda3/bin:$PATH”
fi
fi
unset __conda_setup
# <<< conda initialize <<<
How do I implement this code (export PATH=$PATH:/opt/homebrew/bin) as you said?
Thank you for your help in advance.
Hi there,
I really appreciate this guide. Helped me out with all the concerns.
Thank you.
Hello,
I have followed your tutorial (thank you) and everything went smoothly and uneventful until I try to run an application.
I get this (see below) error (my application is called ‘Camera’ and is supposed to take continuous shots from the computer camera and show them in a JFrame on the screen).
I use java 17 on a MacBook Pro (M1)
=====================================
java -Djava.library.path=/Users/liviu/Documents/Java/Libraries/opencv_build/lib Camera 0
—————————————————————
Exception in thread “main” java.lang.UnsatisfiedLinkError: /Users/liviu/Documents/Java/Libraries/opencv_build/lib/libopencv_java454.dylib: dlopen(/Users/liviu/Documents/Java/Libraries/opencv_build/lib/libopencv_java454.dylib, 1): no suitable image found. Did find:
/Users/liviu/Documents/Java/Libraries/opencv_build/lib/libopencv_java454.dylib: mach-o, but wrong architecture
/Users/liviu/Documents/Java/Libraries/opencv_build/lib/libopencv_java454.dylib: mach-o, but wrong architecture
at java.base/jdk.internal.loader.NativeLibraries.load(Native Method)
at java.base/jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:384)
at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:228)
at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:170)
at java.base/jdk.internal.loader.NativeLibraries.findFromPaths(NativeLibraries.java:311)
at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:283)
at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2422)
at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:818)
at java.base/java.lang.System.loadLibrary(System.java:1989)
at Camera.main(Camera.java:28)
(test_opencv) liviu@Livius-MacBook-Pro Camera %
Hi, thanks a lot for the tutorial!
I’m trying to follow your instructions, but when running the cmake command I immediately get the error
CMake Error: The source directory “/Users/cdemasi/projects/opencv-4.5.0/build/
even if I’m literally inside the opencv-4.5.0/build folder.
Any clues why?
Hmm it looks like your site ate my first comment (it was super long) so I guess I’ll just sum it up what I had written and say,
I’m thoroughly enjoying your blog. I as well am an aspiring
blog writer but I’m still new to everything.
Do you have any points for inexperienced blog writers? I’d definitely appreciate it.
Hey, I am so sorry about that. Thanks for your comments. Just keep writing and you will find out your way. Nothing is better than learning by practice. 😉
Wow, this post is nice, my younger sister is analyzing such things, so I am going to let know her.
the I am installing the pip force numpy it is saying that the it is not supported on this platform
can you please tell