Building OpenCV for Python - 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

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.

Unable to install OpenCV Python on M1 Mac

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.

CMake not being able to find Python Library

I am following this tutorial to install OpenCV 3.0 and Python 2.7 but however, I am getting Cmake errors at step 8.
-- Could NOT find PythonLibs: Found unsuitable version "2.7.11", but required is exact version "2.7.13" (found
/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/bin)
-- Could NOT find PythonInterp: Found unsuitable version "2.7.13", but required is at least "3.4" (found
/Users/SabrinaZuraimi/.virtualenvs/cv/bin/python)
-- Could NOT find PythonInterp: Found unsuitable version "2.7.13", but required is at least "3.2" (found
/Users/SabrinaZuraimi/.virtualenvs/cv/bin/python)
There is 2 versions of Python(2.7.11 and 2.7.13) in the usr/local/Cellar/Python but I don't understand why there is an error saying that i can't find 2.7.13
The command that I typed into the terminal was
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local \
-D PYTHON2_PACKAGES_PATH=~/.virtualenvs/cv/lib/python2.7/site-packages \
-D PYTHON2_LIBRARY=/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/bin \
-D PYTHON2_INCLUDE_DIR=/usr/local/Frameworks/Python.framework/Headers \
-D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON \
-D BUILD_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF\
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules ..
I am not very familiar with cmake, and I appreciate any help that I can get.
that tutorial is out-of-date. you can try this new tutorial

'libavformat/avformat.h' file not found #include <libavformat/avformat.h>

When following the below tutorial:
brew install python3 pip3 install numpy brew install cmake git clone
--depth=1 https://github.com/Itseez/opencv.git cd opencv mkdir build cd build
note: in the next line, adjust paths to point to the correct python version cmake -DBUILD_opencv_python3=YES -DBUILD_opencv_python2=NO
-DINSTALL_PYTHON_EXAMPLES=YES -DPYTHON3_EXECUTABLE=/usr/local/bin/python3 -DPYTHON3_INCLUDE_DIR=/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/include/python3.5m/
-DPYTHON3_LIBRARY=/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/libpython3.5.dylib
-DPYTHON3_NUMPY_INCLUDE_DIRS=/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/numpy/core/include/
-DPYTHON3_PACKAGES_PATH=/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/
.. make -j8 make install python3 -c "import cv2;
print(cv2.version)"
I get this error for line 10 $ make -j8:
[ 39%] Built target opencv_shape
In file included from /Users/mona/Downloads/opencv/modules/videoio/src/cap_ffmpeg.cpp:47:
In file included from /Users/mona/Downloads/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:65:
/Users/mona/Downloads/opencv/modules/videoio/src/ffmpeg_codecs.hpp:77:12: fatal error:
'libavformat/avformat.h' file not found
#include <libavformat/avformat.h>
^
1 error generated.
make[2]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
[ 40%] Linking CXX shared library ../../lib/libopencv_photo.dylib
[ 40%] Built target opencv_photo
make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
make: *** [all] Error 2
Monas-MacBook-Pro:build mona$
Why is this tutorial not passing on my OSX?
So this is how I solved the problem. Please let me know if you might have further questions:
~  c   master ⭑  Face_Recognition  ls  10:03:38 PM
3.1.0.zip 3.1.0.zip.1 opencv-3.1.0 opencv_contrib-3.1.0 papers
-------------------------------------------------------
In opencv-3.1.0/modules/viz/src/vtk/vtkCocoaInteractorFix.mm
#if VTK_MAJOR_VERSION >= 6 && VTK_MINOR_VERSION >=2
If we use VTK 7.0.0, although it is a newer version, the minor version is less than two, so the vtkCocoaInteractorFix.mm will handle this as older VTK versions and that causes the problem.
So to fix this we just need to add the version number 7 to the line.
#if VTK_MAJOR_VERSION >= 6 && VTK_MINOR_VERSION >=2 || VTK_MAJOR_VERSION >=7
---------------------------------------------------------------------------
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D BUILD_opencv_java=OFF \
-D WITH_IPP=OFF -D WITH_1394=OFF \
-D WITH_FFMPEG=OFF\
-D BUILD_EXAMPLES=ON \
-D BUILD_TESTS=ON \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_DOCS=ON \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_video=ON \
-D BUILD_opencv_videoio=ON \
-D BUILD_opencv_videostab=ON \
-D PYTHON_EXECUTABLE=$(which python) \
-D OPENCV_EXTRA_MODULES_PATH=/Users/mona/computer_vision/Face_Recognition/opencv_contrib-3.1.0/modules ..
-------------------------------------------------------
make -j4
-------------------------------------------------------
sudo make install
Main points are -D WITH_FFMPEG=OFF and also giving the absolute path to opencv modules in -D OPENCV_EXTRA_MODULES_PATH=/Users/mona/computer_vision/Face_Recognition/opencv_contrib-3.1.0/modules ..

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

Categories