portfolio
portfolio copied to clipboard
Building and Installing OpenCV 4.5.0 on an M1 Mac | Sayak Paul
Building and Installing OpenCV 4.5.0 on an M1 Mac | Sayak Paul
This post shows how to build and install OpenCV 4.5.0 on a MacBook Pro that comes with an M1 chip.
Hi, thanks for the tutorial. I followed your steps and everything went smooth untill I ran mdfind cv2.cpython
which was returned empty. Any ideas as to why would that be happening?
That is very strange. If the compilation was successful thre process should have generated the .so files. Maybe you could look into the paths manually? You can get an idea of the paths from the post itself.
Thanks a lot! This really helps.
@ntlex it might be that your numpy and python3 lib path may not be configured, at least this was the case for me.. Even after the fact, mfind wasn't working, but the objects were generated at the paths provided opencv-4.5.0/build/lib/python3/cv2.cpython-38-darwin.so
Thanks for the tutorial @sayakpaul. But after a while after typing make -j8 command I get the following error. Do you have an idea?
[ 71%] Linking CXX shared library ../../lib/libopencv_sfm.dylib ld: warning: ignoring file /usr/local/lib/libglog.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64 ld: warning: ignoring file /usr/local/lib/libglog.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64 ld: warning: ignoring file /usr/local/lib/libceres.1.14.0.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64 ld: warning: ignoring file /usr/local/lib/libgflags.2.2.2.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64 [ 71%] Building CXX object modules/optflow/CMakeFiles/opencv_optflow.dir/src/sparsetodenseflow.cpp.o Undefined symbols for architecture arm64: ... ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make[2]: *** [lib/libopencv_sfm.4.5.0.dylib] Error 1 make[1]: *** [modules/sfm/CMakeFiles/opencv_sfm.dir/all] Error 2
ld: warning: ignoring file /usr/local/lib/libglog.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
This tells me that the architecture you are building on is not based on ARM6.
I did not understand. I do these operations on Macbook Air M1. The cmake code I ran is as follows
/opt/homebrew/Cellar/cmake/3.19.3/bin/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/eminarvas/deeplearning/opencv_contrib-4.5.0/modules
-D PYTHON3_EXECUTABLE=/Users/eminarvas/miniforge3/envs/deeplearning/bin/python3
-D PYTHON_LIBRARY=/Users/eminarvas/miniforge3/envs/deeplearning/lib/libpython3.8.dylib
-D PYTHON_NUMPY_INCLUDE_DIR=/Users/eminarvas/miniforge3/envs/deeplearning/lib/python3.8/site-packages/numpy/core/include
-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 ..
From your comment here https://github.com/sayakpaul/portfolio/issues/7#issuecomment-767517870, it appears that CMAKE is picking up the architecture as x86_64 and not as ARM64. This, I believe, is the primary cause of why your build is failing.
Thanks @sayakpaul. I solve the problem encoutered by Memina by:
- Use the latest OpenCV (current build is 4.5.1-dev): Just download directly OpenCV and unzip from the links
https://github.com/opencv/opencv https://github.com/opencv/opencv_contrib
- Make sure you install Cmake properly. Maybe the easiest way is to follow instruction to download Universal Cmake version
https://cmake.org/download/
and then configure
sudo "/Applications/CMake.app/Contents/bin/cmake-gui" --install
- When compiling, don't forget to do it under ARM architecture (instead of Resetta) Adding "arch -arm64" to all statements
arch -arm64 cmake ... arch -arm64 make -j8 arch -arm64 sudo make install
Thanks for passing this along @luuqh.
@sayakpaul Everything worked fine and installed as you mentioned, except on typing
mdfind cv2.cpython
this is not returning anything, please help me out.
You will probably need to do a bit of trial and error for that. Look for the path I have mentioned in my blog post and start your search from there. I am not exactly sure why mdfind
behaves weirdly sometimes. Probably there's an updatedb
like command which I am not aware of.
Sayak, thanks for the great description. Honestly, I have used venv instead of conda and that might be my problem. I would prefer to use venv in order to not mix several different environment manages.
Now the make process breaks at 23% with the following output:
'ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make[2]: *** [lib/libopencv_hdf.4.5.1.dylib] Error 1 make[1]: *** [modules/hdf/CMakeFiles/opencv_hdf.dir/all] Error 2 make[1]: *** Waiting for unfinished jobs.... [ 23%] Built target opencv_imgproc make: *** [all] Error 2'
Maybe you have an idea what to look at. Thanks in advance.
ld: symbol(s) not found for architecture arm64
The above line from the logs might suggest that it's an architecture mismatch but I am not sure.
@sayakpaul I resolved my issue of mdfind cv2.cpython
returning empty string by deleting the build folder and compiling opencv again.
Thanks for the help.
Hi @sayakpaul I'm a newcomer in terms of python, conda, numpy and have zero knowledge in environment programming other than C/C++. On my m1 mac, I've installed conda and python 3.8.6 but when I'm trying to install NumPy I got several errors.
1st problem, when I execute :
pip install --upgrade --no-dependencies --force numpy-1.18.5-cp38-cp38-macosx_11_0_arm64.whl
it returns as zsh: command not found:pip
After some googling, people on stackoverflow suggest to use pip3 instead of pip.
Hence when I execute: pip3 install --upgrade --no-dependencies --force numpy-1.18.5-cp38-cp38-macosx_11_0_arm64.whl
it returns as WARNING: Requirement 'numpy-1.18.5-cp38-cp38-macosx_11_0_arm64.whl' looks like a filename, but the file does not exist ERROR: numpy-1.18.5-cp38-cp38-macosx_11_0_arm64.whl is not a supported wheel on this platform.
I'm extremely new in this installation (literally installed homebrew a few hours ago). Any help would be highly appreciate. Thanks !
@muhmdakml seems like you have not downloaded this file - numpy-1.18.5-cp38-cp38-macosx_11_0_arm64.whl
. If you untar tensorflow_macos-0.1alpha0.tar.gz
you will find it inside tensorflow_macos/arm64
directory.
@sayakpaul thank you for your feedback. I deleted the original tensorflow_macos folder and re-download it from github. Extracting the newly downloaded .tar.gz now runs smoothly.
The only issue I face now is the same as others, where running
mdfind cv2.python
returns nothing.
I'm trying to find the paths manually as per suggested. But manually path searching (enabled show hidden folders) into
/Users/xxx/opencv-4.5.0/build/lib/
/usr/local/lib
only files with ".dylib" extensions are shown. No other folders inside this lib and no 'python3' folder are shown. However, I did install python 3.8.6 as per the tutorial.
Is there anything more installation that I could've missed? Or am I looking in the wrong path direction ? Thank you so much.
Edit : Upon further inspection when running CMake (just before running make), I noticed my :
Python 3:
> Interpreter : NO
> Libraries : NO
> numpy : NO (Python3 wrappers can not be generated)
> Install Path : -
Could this be the issue? When I execute python3 --version
it does return as python 3.8.6.
I would suggest activating your conda environment first and then try redoing all the installations. You probably forgot to activate conda environment before starting all the other installations which is not recommended for this blog post.
I think if you follow through this tutorial sequentially you would be successful installing OpenCV on your M1 Mac.
Now that conda install opencv
is available, what's the difference between that one and this one?
@louy2 when I wrote this blog post it was not available. I believe with conda install opencv
the process is easier. So, there won't likely be any difference.
I am still getting the error of module not found when I import cv2.
When I ran "ls -l cv2.so" I got the exact same output as "/usr/local/lib/python3.8/site-packages/cv2/python-3.8/cv2.cpython-38-darwin.so". But when I went to look at the "venv/lib/python3.8/site-packages/cv2.so" file from finder, it showed that the original item for cv2.so could not be found.
Please help me resolve this issue.
You probably forgot to sym-link the .so file.
Thank you for the tutorial, @sayakpaul! I was wondering if after doing the above steps (which worked perfectly for me) it is possible for me to use opencv in pycharm, or only in the terminal?
Sorry for the noob question :D
As long as the Python interpreter inside your PyCharm is pointed to the one where you installed OpenCV, you should be able to use it inside PyCharm. In fact, this is exactly what I do as well.
I did every step successfully but the command - "mdfind cv2.cpython" is not giving location of the .so file . and I not able to symlink I looked myself manually in many folders but nothing could be found . can u help me ?
Try looking here - /usr/local/lib/python3.8/site-packages/cv2/python-3.8
(of course, the path might be different in your case).
Thanks, it worked! For future people asking this you must check the 'previously created interpreter' when creating a new project and fill in the location of the environment configured with @sayakpaul 's tutorial.
If mdfind doesn't work for you, you could also try to do:
python -c "import cv2; print(cv2.__file__)"
thanks @sayakpaul @gerardsimons I just removed everything and did the process again and it worked