sayakpaul / portfolio

Personal site of Sayak Paul. Deployed here 👉
https://sayak.dev/
24 stars 16 forks source link

Building and Installing OpenCV 4.5.0 on an M1 Mac | Sayak Paul #7

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

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.

https://sayak.dev/install-opencv-m1/

Nadlada commented 2 years ago

/Users/nadlada/Desktop/Screen Shot 2564-12-13 at 11.54.08.png

Nadlada commented 2 years ago

Consolidate compiler generated dependencies of target opencv_calib3d [ 28%] Built target opencv_saliency Undefined symbols for architecture arm64: "_png_do_expand_palette_rgba8_neon", referenced from: _png_do_read_transformations in liblibpng.a(pngrtran.c.o) "_png_init_filter_functions_neon", referenced from: _png_read_filter_row in liblibpng.a(pngrutil.c.o) "_png_riffle_palette_neon", referenced from: _png_do_read_transformations in liblibpng.a(pngrtran.c.o) "_png_do_expand_palette_rgb8_neon", referenced from: _png_do_read_transformations in liblibpng.a(pngrtran.c.o) 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_imgcodecs.4.5.3.dylib] Error 1 make[1]: [modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/all] Error 2 make[1]: Waiting for unfinished jobs.... [ 31%] Built target opencv_dnn [ 32%] Built target opencv_calib3d make: [all] Error 2

Glebmaksimov commented 2 years ago

Hello guys, tell me please does this tutorial is suitable for mac m1 opencv with C++ installation?I am a freshman.

Glebmaksimov commented 2 years ago

If it is only for python opencv installation, please ,help me to install opencv(C++) on mac m1.

maerten commented 2 years ago

fwiw, CMAKE was not able to find numpy on my machine and did not generate the cv2.cpython file because of it.

adapted command is found below, with added paths to python folders, and disabled building of tests & examples:

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/maarten/Projects/python/installs/opencv_contrib/modules \
-D PYTHON3_EXECUTABLE=/opt/homebrew/Caskroom/miniforge/base/envs/py39/python3 \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D BUILD_ZLIB=OFF \
-D BUILD_opencv_wechat_qrcode=OFF \
-D BUILD_opencv_xfeatures2d=OFF \
-D BUILD_TESTS=OFF \
-D PYTHON3_NUMPY_INCLUDE_DIR=/opt/homebrew/Caskroom/miniforge/base/envs/py39/lib/python3.9/site-packages/numpy/core/include \
-D PYTHON3_PACKAGES_PATH=/opt/homebrew/Caskroom/miniforge/base/envs/py39/lib/python3.9/site-packages \
-D PYTHON3_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-D PYTHON3_LIBRARY=$(python3 -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))") \
-D PYTHON3_EXECUTABLE=$(which python3) \
-D BUILD_PERF_TESTS=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D BUILD_EXAMPLES=OFF ..
lynnwilliam commented 2 years ago

can someone Please post the M1 DYLIB files on the Net? so we can just use them without compiling?

cedricve commented 2 years ago

Disabling ZLIB did make it work

 cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local/ \
    -D OPENCV_GENERATE_PKGCONFIG=YES \
    -D BUILD_TESTS=OFF \
    -D OPENCV_ENABLE_NONFREE=ON \
    -D BUILD_opencv_ml=OFF \
    -D BUILD_opencv_stitching=OFF \
    -D BUILD_opencv_ts=OFF \
    -D BUILD_opencv_java_bindings_generator=OFF \
    -D BUILD_opencv_python_bindings_generator=OFF \
    -D INSTALL_PYTHON_EXAMPLES=OFF \
    -D BUILD_EXAMPLES=OFF -D BUILD_ZLIB=OFF ..
54WC commented 2 years ago

Thank you very much. Finally, I can one warning when run "make -j8". Is it possible to fix it? /Users/wc/opencv/opencv-4.5.0/modules/flann/include/opencv2/flann/index_testing.h:249:11: warning: variable 'p1' set but not used [-Wunused-but-set-variable] float p1; ^ 1 warning generated.

mdsrosa commented 1 year ago

thanks for sharing this, @sayakpaul.

I ran all steps successfully with a couple of changes:

opencv source code

updated Github archive URLs:

cmake command

updated cmake command with -S (source code dir path) and -B (build dir path):

❯ cmake \
-DCMAKE_SYSTEM_PROCESSOR=arm64 \
-DCMAKE_OSX_ARCHITECTURES=arm64 \
...
-D BUILD_EXAMPLES=ON -S .. -B .
MikaelX commented 1 year ago

I had some issues building this, but used the latest 4.8.1 version instead of opencv and the build was successful.

Here the part opencv part of the flow:

wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.8.1.zip wget -O opencv.zip https://github.com/opencv/opencv/archive/4.8.1.zip unzip opencv_contrib.zip unzip opencv.zip cd opencv-4.8.1 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=/Users/mikael/Downloads/source_code/opencv_contrib-4.8.1/modules \ -D PYTHON3_EXECUTABLE=/Users/mikael/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 .. make -j8 sudo make install

MikaelX commented 1 year ago

I had some issues building this, but used the latest 4.8.1 version instead of opencv and the build was successful.

Mac Mini M1: Ventura? 13.4.1

Here the part opencv part of the flow:

terminal commands

wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.8.1.zip
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.8.1.zip
unzip opencv_contrib.zip
unzip opencv.zip
cd opencv-4.8.1
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=/Users/mikael/Downloads/source_code/opencv_contrib-4.8.1/modules
-D PYTHON3_EXECUTABLE=/Users/mikael/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 ..
make -j8
sudo make install
karmarv commented 1 year ago

cmake \ -D CMAKE_BUILD_TYPE=Release \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \ -D PYTHON3_EXECUTABLE=/Users/karmax/dev/miniconda3/envs/cv/bin/python3 \ -D PYTHON3_INCLUDE_DIR=/Users/karmax/dev/miniconda3/envs/cv/include/python3.8 \ -D PYTHON3_NUMPY_INCLUDE_DIRS=/Users/karmax/dev/miniconda3/envs/cv/lib/python3.8/site-packages/numpy/core/include \ -D BUILD_opencv_python2=OFF \ -D BUILD_opencv_python3=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_ENABLE_NONFREE=ON \ -D BUILD_SHARED_LIBS=ON \ -D WITH_FFMPEG=ON \ -D BUILD_EXAMPLES=ON ../opencv

make -j8

sudo make install

Find and copy the libraries

mdfind cv2.cpython

ln -s /usr/local/lib/python3.8/site-packages/cv2/python-3.8/cv2.cpython-38-darwin.so /Users/karmax/dev/miniconda3/envs/cv/lib/python3.8/site-packages/cv2.so

cultist123 commented 6 months ago

Sayak,感谢您的精彩描述。老实说,我曾经使用过 venv 而不是 conda,这可能是我的问题。我更喜欢使用 venv,这样就不会混淆几个不同的环境管理。

现在 make 过程在 23% 处中断,并输出以下输出:

'ld:未找到适用于架构 arm64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) make[2]: [lib/libopencv_hdf.4.5.1.dylib] 错误 1​​make[1]: [modules/hdf/CMakeFiles/opencv_hdf.dir/all] 错误 2 make[1]: 等待未完成的作业.... [ 23%] 已构建目标 opencv_imgproc make: [all] 错误 2'

也许你知道该看什么。提前谢谢。

I also encountered the same problem as you. How did you solve it?

cultist123 commented 6 months ago

合并编译器生成的目标 opencv_calib3d 依赖项 [ 28%] 构建目标 opencv_saliency 未定义适用于 arm64 架构的符号: “_png_do_expand_palette_rgba8_neon”,引用自: liblibpng.a(pngrtran.co) 中的 _png_do_read_transformations “_png_init_filter_functions_neon”,引用自: liblibpng.a(pngrutil.co) 中的 _png_read_filter_row “_png_riffle_palette_neon”,引用自: liblibpng.a(pngrtran.co) 中的 _png_do_read_transformations “_png_do_expand_palette_rgb8_neon”,引用自: liblibpng.a(pngrtran.co) 中的 _png_do_read_transformations ld:未找到适用于 arm64 架构的符号 clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) make[2]: [lib/libopencv_imgcodecs.4.5.3.dylib] 错误 1​​make[1]: [modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/all] 错误 2 make[1]: 等待未完成的作业.... [ 31%] 已构建目标 opencv_dnn [ 32%] 已构建目标 opencv_calib3d make: [all] 错误 2

I also encountered the same problem as you. How did you solve it?