Unable to install OpenCV Python on M1 Mac - python

I've been following this thread (https://trayansh.medium.com/setting-machine-learning-environment-on-m1-mac-apple-silicon-959836bf494d), successfully installed TensorFlow but while making the config file(Cmake Command) for openCV it is giving me an error:-
CMake Error at cmake/OpenCVModule.cmake:354 (message):
Duplicated modules LOCATIONS has been found
Call Stack (most recent call first):
cmake/OpenCVModule.cmake:371 (_assert_uniqueness)
modules/CMakeLists.txt:7 (ocv_glob_modules)
I've been trying to install it for two days now and no progress. :(
The CMake command I used was
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/yashdhingra/Documents/Python/opencompile/opencv-4.5.0/modules \
-D PYTHON3_EXECUTABLE=/opt/homebrew/Caskroom/miniforge/base/envs/MLenv/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 ..
I did change the path of the modules and the Virtual Env_python as instructed in the guide.

This CMake error (Duplicated modules LOCATIONS has been found) is because you point the extra modules path to the main opencv module you are trying to build from.
You need to set the extra modules path to opencv-contrib and not opencv.
OPENCV_EXTRA_MODULES_PATH=/Users/yashdhingra/Documents/Python/opencompile/opencv_contrib-4.5.0/modules

You have perhaps copied the extra modules into the main module directory, and then are using -D OPENCV_EXTRA_MODULES_PATH=/opt/OpenCV3.4/modules to locate this. But the module locator looks at ALL the modules that are in a directory; and then it also requires each module to be unique and seen only once. Since the cmake looks at all the modules in the main /module directory by default, and then looks at all the modules in the EXTRA_MODULES path again when you specify this cmake option, it gets confused, and thinks it is seeing the exact same modules twice, and then barfs.
Solution 1: Simply don't specify -D OPENCV_EXTRA_MODULES_PATH=/opt/OpenCV3.4/modules in your cmake instructions if you have included all extra modules into the default system module file by hand. It will only review this directory once, and will happily compile all of them.
Solution 2: Keep the extra modules in their own separate directory, and specify cmake option -D OPENCV_EXTRA_MODULES_PATH=/my_extra_modules_directory which is separate from the main /modules directory.

Related

Airflow without sudo access?

in my virtual env on azure VM, i ran
pip3 install apache-airflow
when i started airflow db init i received
File "/home/shivamanand/myenv/lib/python3.7/site-packages/airflow/configuration.py", line 398, in _validate_config_dependencies
f"error: sqlite C library version too old (< {min_sqlite_version_str}). "
airflow.exceptions.AirflowConfigException: error: sqlite C library version too old (< 3.15.0)
Next, i followed the docs to upgrade sqllite (i cant install docker w postgres etc as i dont have sudo access)
https://airflow.apache.org/docs/apache-airflow/stable/howto/set-up-database.html
THESE WORKED ->
wget https://www.sqlite.org/src/tarball/sqlite.tar.gz
tar xzf sqlite.tar.gz
cd sqlite/
export CFLAGS="-DSQLITE_ENABLE_FTS3 \
-DSQLITE_ENABLE_FTS3_PARENTHESIS \
-DSQLITE_ENABLE_FTS4 \
-DSQLITE_ENABLE_FTS5 \
-DSQLITE_ENABLE_JSON1 \
-DSQLITE_ENABLE_LOAD_EXTENSION \
-DSQLITE_ENABLE_RTREE \
-DSQLITE_ENABLE_STAT4 \
-DSQLITE_ENABLE_UPDATE_DELETE_LIMIT \
-DSQLITE_SOUNDEX \
-DSQLITE_TEMP_STORE=3 \
-DSQLITE_USE_URI \
-O2 \
-fPIC"
export PREFIX="/usr/local"
LIBS="-lm" ./configure --disable-tcl --enable-shared --enable-tempstore=always --prefix="$PREFIX"
then
for
make
make install
i am getting
gcc -DSQLITE_SOUNDEX -DSQLITE_TEMP_STORE=3 -DSQLITE_USE_URI -O2 -fPIC -o mksourceid /home/shivamanand/sqlite/tool/mksourceid.c
tclsh /home/shivamanand/sqlite/tool/mksqlite3h.tcl /home/shivamanand/sqlite >sqlite3.h
/bin/sh: 1: tclsh: not found
Makefile:1075: recipe for target 'sqlite3.h' failed
make: *** [sqlite3.h] Error 127
How do i correct this?
This issue is due to the unavailability of the tcl in your system. To solve this problem first install tcl.
sudo apt install tcl
You can see other errors that is because the sqlite3.h file. It is possible that an earlier configure / make issue (e.g. missing tclsh) caused the file to be empty, which would certainly lead to many build errors like this. Even if the underlying issue is resolved (e.g. by fixing missing dependencies), the make file will not regenerate it if it is already present, in which case the sqlite3.h file would need to be deleted before a rebuild.

Building OpenCV for Python

I'm using this: http://docs.opencv.org/3.1.0/d7/d9f/tutorial_linux_install.html
to build opencv on ununtu 16LTS for Python 3.
On step 4 in Building OpenCV from Source Using CMake i'm stuck with typing in the right parameters. Could you, please check my variants, because i could only find folders and no files with suggested extensions. Also, shall i type a flag -D before each param?
-D PYTHON3_EXECUTABLE=$HOME/anaconda3/bin
-D PYTHON_INCLUDE_DIR=/usr/include/python3.5
-D PYTHON_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python3.5m
-D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.a
-D PYTHON3_NUMPY_INCLUDE_DIRS= I dont have anything similar like path in the tutorial, but i have a "numpy" folder in /usr/include/python3.5!
So what should i do?
Also, what shall i write in:
-D CMAKE_INSTALL_PREFIX
OPENCV_EXTRA_MODULES_PATH (in this tutrial(previus step), o downloaded opencv-contrib. So should i type in: OPENCV_EXTRA_MODULES_PATH=/home/vladislav/opencv/opencv_contrib
BUILD_DOCS
BUILD_EXAMPLES
ERROR: When i run as it is told in the tutorial, i got mistakes like this: CMake Error: The source directory "/home/vladislav/opencv/build/PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so" does not exist.
That's an example of my working cmake params (Ubuntu 16.04, Latest OpenCV, Python 3.5), but you need to adjust the paths by your own.
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D PYTHON3_EXECUTABLE=/usr/bin/python3.5 \
-D PYTHON3_INCLUDE_DIR=/usr/include/python3.5 \
-D PYTHON3_LIBRARY=/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu/libpython3.5.so \
-D PYTHON3_NUMPY_INCLUDE_DIR=/usr/local/lib/python3.5/dist-packages/numpy/core/include \
-D PYTHON3_PACKAGES_PATH=/usr/local/lib/python3.5/dist-packages \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
in my case opencv_contrib is in the same folder like opencv your cmake command starts from your build folder (mkdir build in opencv folder) so in my case I need to jump to dirs back and important: refer to the opencv_contrib/modules folder
Sometimes it's a little bit clearer to use the cmake-gui
sudo apt install cmake-gui

Building OpenCV cmake error: could NOT find PythonInterp

To build OpenCV, I ran this in the terminal in my ~/opencv/build directory :
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/path/to/opencv-3.0.0/build -D PYTHON2_LIBRARY=/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/bin
-D PYTHON2_INCLUDE_DIR=/usr/local/Frameworks/Python.framework/Headers -D PYTHON2_PACKAGES_PATH=/usr/local/lib/python2.7/site-packages -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=/path/to/opencv_contrib-3.0.0/modules ../
But no matter what happens, I see this error in the Traceback : (Could NOT find PythonInterp:)
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named numpy.distutils
-- Could NOT find PythonInterp: Found unsuitable version "2.7.11",
but required is at least "3.4" (found /usr/local/bin/python)
-- Could NOT find PythonInterp: Found unsuitable version "2.7.11",
but required is at least "3.2" (found /usr/local/bin/python)
Further down the line, this shows up
--
-- Python 2:
-- Interpreter: /usr/local/bin/python2.7 (ver 2.7.11)
--
-- Python 3:
-- Interpreter: NO
--
-- Python (for build): /usr/local/bin/python2.7
--
-- Java:
-- ant: NO
-- JNI:
The correct output should look somewhat like this:
correct Python Interp output
I have tried everything, but this keeps coming up!
In essence, I cannot build OpenCV properly, and hence cannot install it properly.
Your error talks about not being able to find numpy, so install numpy:
pip install numpy
In addition, change your include directory to make sure you are actually including it. For the PYTHON2_INCLUDE_DIR:
-D PYTHON2_INCLUDE_DIR=/usr/local/Frameworks/Python.framework/Headers
Then, run make clean, and delete your /build directory, and run your cmake, then make again.
You can specify the path to each version of Python manually.

how to build opencv for python3 when both python2 and python3 are installed

I was trying to build opencv for python3. However, cmake always sets python build option to be python2.7.11 even after I manually specified include and lib option for python3:
-- Python 2:
-- Interpreter: /home/ryu/anaconda2/bin/python2.7 (ver 2.7.11)
-- Python 3:
-- Interpreter: /usr/bin/python3 (ver 3.4.3)
-- Libraries: /usr/lib/x86_64-linux-gnu/libpython3.4m (ver 3.4.3)
-- numpy: /home/ryu/.local/lib/python3.4/site-packages/numpy/core/include (ver 1.11.0)
-- packages path: lib/python3.4/dist-packages
--
-- **Python (for build): /home/ryu/anaconda2/bin/python2.7**
Did I miss some cmake option?
OS: Ubuntu 14,04
thanks
You can override the python executable to build to by appending the argument PYTHON_DEFAULT_EXECUTABLE with the python executable URI during the cmake invokation.
cmake {...} -DPYTHON_DEFAULT_EXECUTABLE=$(which python3) ..
I was struggling with this one for some hours and the answers mentioned above didn't solve the problem straightaway.
Adding to Ivan's answer, I had to include these flags in cmake to make this work:
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_opencv_python3=ON \
-D HAVE_opencv_python3=ON \
-D PYTHON_DEFAULT_EXECUTABLE=<path_to_python3>
I leave that here, so it is maybe useful for someone else in the future.
it was take some hours for me. I built Dockerfile with opencv for python3
the key string is
pip install numpy
Full Docker file:
FROM python:3.8
RUN apt-get update && apt-get -y install \
cmake \
qtbase5-dev \
libdc1394-22-dev \
libavcodec-dev \
libavformat-dev \
libswscale-dev
RUN cd /lib \
&& git clone --branch 4.1.1 --depth 1 https://github.com/opencv/opencv.git \
&& git clone --branch 4.1.1 --depth 1 https://github.com/opencv/opencv_contrib.git
RUN pip install numpy \
&& mkdir /lib/opencv/build \
&& cd /lib/opencv/build \
&& cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DWITH_TBB=ON -DWITH_V4L=ON -DWITH_QT=ON -DWITH_OPENGL=ON -DWITH_FFMPEG=ON -DOPENCV_ENABLE_NONFREE=ON -DOPENCV_EXTRA_MODULES_PATH=/lib/opencv_contrib/modules .. \
&& make -j8 \
&& make install
CMD ["bash"]
The main point is to force compiler to build cv2 module for python
To make it we need python3 should be included in line To be built in CMakeCache.txt file in build folder of opencv
ref https://breakthrough.github.io/Installing-OpenCV/
If there are any errors, ensure that you downloaded all the required packages - the output should help track down what is missing. To ensure the Python module will be built, you should see python2 in the list of configured modules after running cmake
(in my case python3)
I've been trying to install opencv on a Pi3 and this solution didn't work for me as python (for build) was always set to Python2.7 but I found that by changing the order of an elseif statement at the bottom of 'OpenCVDetectPython.cmake' fixed the problem. For me, this file is located at '~/opencv-3.3.1/cmake'.
The original code segment:
if(PYTHON_DEFAULT_EXECUTABLE)
set(PYTHON_DEFAULT_AVAILABLE "TRUE")
elseif(PYTHON2INTERP_FOUND) # Use Python 2 as default Python interpreter
set(PYTHON_DEFAULT_AVAILABLE "TRUE")
set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON2_EXECUTABLE}")
elseif(PYTHON3INTERP_FOUND) # Use Python 3 as fallback Python interpreter (if there is no Python 2)
set(PYTHON_DEFAULT_AVAILABLE "TRUE")
set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON3_EXECUTABLE}")
endif()
My re-ordered code segment:
if(PYTHON_DEFAULT_EXECUTABLE)
set(PYTHON_DEFAULT_AVAILABLE "TRUE")
elseif(PYTHON3INTERP_FOUND) # Use Python 3 as fallback Python interpreter (if there is no Python 2)
set(PYTHON_DEFAULT_AVAILABLE "TRUE")
set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON3_EXECUTABLE}")
elseif(PYTHON2INTERP_FOUND) # Use Python 2 as default Python interpreter
set(PYTHON_DEFAULT_AVAILABLE "TRUE")
set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON2_EXECUTABLE}")
endif()
I don't know the reasoning behind it, but cmake is set to default to python2 if python2 exists, swapping the order of these elseif statements switches it to default to python3 if it exists
** Disclaimer **
I was using the script found at https://gist.github.com/willprice/c216fcbeba8d14ad1138 to download, install and build everything
(script was modified to not create a virtual environment as I didn't want one and with j1 not j4 as it failed around 85% when running with multiple cores).
I don't think the relevant file exists until you have attempted a build.
Changing the options in cmake did nothing for me no matter what options I modified. The simpliest (hacky) solution for me was to
sudo mv /usr/bin/python2.7 /usr/bin/pythonNO-temp
Then you build and install opencv
then
sudo mv /usr/bin/pythonNO-temp /usr/bin/python2.7

Impossible to install OpenCV Ubuntu (libopencv_core error)

I am trying to install OpenCV 3.0 for Python on Ubuntu 14.04 but after many installations (and StackOverflow pages read) it still does not work.
In Python (Python 2.7) when importing typing import cv2 I obtain this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: libopencv_core.so.2.4: cannot open shared object file: No such file or directory
I have seen that this error may be due to the file /etc/ld.so.conf.d/opencv.conf and therefore I wrote inside it the line /usr/local/opencv/ but it did not help (actually there is no folder opencvin /usr/local/ on my computer...
For the installation I followed the instructions on this website: http://milq.github.io/install-opencv-ubuntu-debian/
I suspect errors occurred during the cmake phase because I got failures like -- Performing Test HAVE_CXX_WMISSING_PROTOTYPES - Failed. I don't know what that means and if it is important for the installation...
I have other errors like
-- checking for module 'gstreamer-base-1.0'
-- package 'gstreamer-base-1.0' not found
During the make phase I obtained this error
[ 31%] Building CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/qrc_window_QT.cpp.o
In file included from /home/xavier/OpenCV/build/modules/highgui/qrc_window_QT.cpp:9:0:
/home/xavier/OpenCV/build/modules/highgui/qrc_window_QT.cpp: In function ‘int qInitResources_window_QT()’:
/home/xavier/OpenCV/build/modules/highgui/qrc_window_QT.cpp:1749:25: warning: no previous declaration for ‘int qInitResources_window_QT()’ [-Wmissing-declarations]
int QT_MANGLE_NAMESPACE(qInitResources_window_QT)()
^
/usr/include/qt5/QtCore/qglobal.h:100:36: note: in definition of macro ‘QT_MANGLE_NAMESPACE’
# define QT_MANGLE_NAMESPACE(name) name
^
/home/xavier/OpenCV/build/modules/highgui/qrc_window_QT.cpp: In function ‘int qCleanupResources_window_QT()’:
/home/xavier/OpenCV/build/modules/highgui/qrc_window_QT.cpp:1758:25: warning: no previous declaration for ‘int qCleanupResources_window_QT()’ [-Wmissing-declarations]
int QT_MANGLE_NAMESPACE(qCleanupResources_window_QT)()
^
/usr/include/qt5/QtCore/qglobal.h:100:36: note: in definition of macro ‘QT_MANGLE_NAMESPACE’
# define QT_MANGLE_NAMESPACE(name) name
^
[ 31%] Generating opencl_kernels_superres.cpp, opencl_kernels_superres.hpp
Do you know what is wrong?
I finally managed to solve my problems. Since this may interested other people, here how I proceed.
I removed entirely opencv from my computer:
sudo make unistall in the repository where I wrote sudo make install
sudo find / -name "*opencv*" -exec rm -i {} \; to remove all files containing "opencv"
sudo find / -name "*cv2.so*" -exec rm -i {} \; to remove all files containing "cv2.so"
conda uninstall opencv (if needed)
I followed this link to install OpenCV2.4.10 for Python2.7: I did only the points 5,6,7 and 12 (the remaining ones are useless for what I wanted). I changed a bit the cmake command and I typed
cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_OPENGL=ON -D WITH_VTK=ON -D WITH_GTK=ON ..
Now if when typing import cv2 in Python you get an error like "No module named cv2", edit your .bashrc file with the following command
export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
You will have to reload the .bashrc file (source .bahsrc) to apply the change.
Finally I rebooted my computer. And it worked!
To conclude, I don't know why it did not work previously but it appears that with this version of OpenCV I did not encounter any problem!
Good luck!
You didn't install it correctly that's why you can't import it. Here is a guide on how to install it, (it is for an .older version but the commands are the same). OpenCV 3.0 Trouble with Installation

Categories