Install and use OpenCV from source - python

I have tried this to install opencv. The terminal output after installing was:
OpenCV 2.4.13 ready to be used
But when I try to import the module, it says the module doesn't exist.
When I tried:
pkg-config opencv --cflags
to check if opencv was installed, I got:
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
How can I fix this? Is there any other way to install opencv?

Since you are using Python3, You can only choose to build your own cv2.pyd. There is no pre-built version for you to download.
I found another link of installing the Package, which is installing from the source, means that you need to compile the source code.
The general idea is to download the source code you want first. After that, yor will use CMake to build the libraries and the Python wrapper (based on the specific Python version you are using). After build successful, you will then get the cv2.pyd and several OpenCV libraries, you just copy them into your Python site-package. Finally you should be able to import cv2.
I did the same thing in Window for Python3.3, 3.4, and 3.5 by following this tutorial, the general idea is the same but only on different platform. I think it should be helpful as a reference for you.

Related

OpenCV won't install to python

I am trying to install OpenCV 4.5.5 with CUDA support. I installed the source from their GitHub and the additional OpenCV contrib and built it with CMAKE. I checked BUILD_opencv_python3 when it was done configuring and generating it showed that it had detected python3 and had installed cv2 in site-packages.
But cv2 isn't in the site-packages.
So it doesn't load in python. But it does give an interesting error
What should I do?
I already tried doing a fresh build with CMAKE but it didn't work.
After building OpenCV it generates a PYD file in the lib folder. I don't know if that is helpful or not.
Thanks
You must have missed a step in the process.
"Configure" step in cmake-gui, investigates the environment (various paths, e.g. where python wants packages to go) and prepares information for the build
"Generate" step in cmake-gui, generates the actual build files (VS Solution)
open the .sln in Visual Studio
build the ALL_BUILD target
build the INSTALL target, and this installs the files, also the python package

django OSError: no library called "cairo" was found on windows

When I run the Django server, I see this problem !!
OSError: no library called "cairo" was found
no library called "libcairo-2" was found
cannot load library 'libcairo.so': error 0x7e
cannot load library 'libcairo.2.dylib': error 0x
cannot load library 'libcairo-2.dll': error 0x7e
Installing GTK+ didn't work for me.
I solved this problem using UniConverter2.0.
My environments is
Python 3.7
Windows 10 x64
Install uniconvertor-2.0rc4-win64_headless.msi,
Find the "dll" sub-directory under the UniConverter installation path.(In my case, C:\Program Files\UniConvertor-2.0rc4\dlls)
Add this "dll" path to the system path.
Close VSCode and reopen the project.
Try to run the server again.
Enjoy!
WeasyPrint needs the Pango, cairo and GDK-PixBuf libraries. They are part of GTK+ (formerly known as GIMP Toolkit), and must be installed separately.
After installing GTK+ libraries, do :
python -m weasyprint http://weasyprint.org weasyprint.pdf
Starting from Python 3.8, dll's need to be added separately.
Added GTK+, MSYS2, Visual Studio C Compiler and Uniconverter. But, nothing seemed to work.
Finally, got it working after putting the script for calling add_dll_directory.
import os
def set_dll_search_path():
# Python 3.8 no longer searches for DLLs in PATH, so we have to add
# everything in PATH manually. Note that unlike PATH add_dll_directory
# has no defined order, so if there are two cairo DLLs in PATH we
# might get a random one.
if os.name != "nt" or not hasattr(os, "add_dll_directory"):
return
for p in os.environ.get("PATH", "").split(os.pathsep):
try:
os.add_dll_directory(p)
except OSError:
pass
set_dll_search_path()
Source: PyCairo Windows Python3.8 Import Issue
See the solution here:
https://www.programmersought.com/article/47674569357/
You will need to add a path if not added after installations:
C:\Program Files\GTK3-Runtime Win64\bin
I have been solving it many times this way.
If you are using a lightweight Linux Docker Image, it may not include GTK as said above, then, you can include it by adding in your Dockerfile
RUN apt-get update -y
RUN apt-get install python3-cffi python3-brotli libpango-1.0-0 libharfbuzz0b libpangoft2-1.0-0 libgtk-3-dev gcc -y
Where all those packages are recommended to be installed by weasyprint and GCC has the GTK.
Ok I figure this out. you can have a 64 bit version of python that doesn't work. What I have found to work and this could change is the installing the 64 bit version of python from python's website, not from the Microsoft store!
remove any version of python you have installed
download https://www.python.org/ftp/python/3.8.10/python-3.8.10-amd64.exe
install
download and install https://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer/releases
exact link is https://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer/releases/download/2021-04-29/gtk3-runtime-3.24.29-2021-04-29-ts-win64.exe
note that link is from https://doc.courtbouillon.org/weasyprint/stable/first_steps.html#windows
add bin folder from that (sorry it was somebody else's machine, I use Ubuntu) in your environment path.
run through the first steps on weasyprint and you should be good to go!
Might be a bit late, but I just encoutered the same issue and:
Start here: https://weasyprint.readthedocs.io/en/stable/install.html#windows. Where you will find the links for the GTK Pack that contains the required DLLs.
In my case, I have a 64bit Python, so i use: "Download and run the latest gtk3-runtime-x.x.x-x-x-x-ts-win64.exe"
I did not change the installation directory
Once the installation is completed. I added the path to my variable paths.
I restarted the terminal, made sure I could locate the DLLs with : WHERE libcairo-2.dll. This retured C:\Program Files\GTK3-Runtime Win64\bin\libcairo-2.dll
Then I run python -m weasyprint http://weasyprint.org weasyprint.pdf
and got a few WARNINGs but they are just warnings :)
I have faced same error as well
I haved installed gtk as followed as gtk install in windows
Nothing worked
After that :
python -m pip install pycairo
solved the problem for me
Try this: https://cairocffi.readthedocs.io/en/stable/overview.html#installing-cairo-on-windows
And maybe this will work to: https://www.lfd.uci.edu/~gohlke/pythonlibs/#pycairo
Best option is instal cairocffi trough pipwin
pip install pipwin
pipwin install cairocffi

Installing Matplotlib on Windows ERROR - cannot open include file 'ft2build.h'

I am trying to install matplotlib using:
pip install matplotlib
However, I keep getting this error:
- cannot open include file 'ft2build.h' no such file or directory
Error Image
You are using python 3.8 for which no pre-built whl files are avialable on pypi, therefore your pip is attempting to build matplotlib from source. For this, you need to have downloaded and installed the dependencies of matplotlib. In this case, ft2build.h is missing which is part of the freetype library.
If you really want to build from source, there is a script based solution from the matplotlib developers to download and install all neccessary dependencies.
Since you probably don't care about building from source (which can be quite some work on Windows), you should instead consider one of these options:
Install a python version other than 3.8, it is very new and therefore there is not always an official pre-built whl file for modules you want to use
As MrFuppes suggested, download matplotlib‑3.2.0rc1‑cp38‑cp38‑win_amd64.whl from this unofficial collection of whl files and do pip install matplotlib‑3.2.0rc1‑cp38‑cp38‑win_amd64.whl in your cmd
It is a bit unclear what your problem is. It may just be that you have some packages that are not compatible. To avoid this sort of issue I can recommend the use of Anaconda: https://www.anaconda.com/
It will come with python and most basic libraries, including matplotlib. I can also recommend seaborn for professional-looking plots that do not require much code.

Installing openCV with contrib modules

I was running python3.6 (32 bit) with opencv3.3.1.
I saw this tutorial so I was trying out the brute force matching with SIFT descriptors: http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_feature2d/py_matcher/py_matcher.html
I read that some modules including SIFT were no longer included in the default installation and needed to be installed separately. I found on the official python website a download for opencv3 with the contrib modules (which I assumed contains SIFT). Specifically I downloaded opencv_contrib_python-3.3.0.10-cp36-cp36m-win32.whl (md5) from here: https://pypi.python.org/pypi/opencv-contrib-python#downloads
I have uninstalled my previous opencv and installed and imported this new version of opencv and verified that I am running the new version.
However, still when I try to use cv2.SIFT() I am still getting the error that module cv2.cv2 has no attribute 'SIFT'.
Did I download the correct package? Are all opencv modules including SIFT supposed to be included in the package I installed?
For enabling opencv-contrib modules, you have to rebuild your opencv lib from source. In CMake config, you have to enable it, specifying variable OPENCV_EXTRA_MODULES_PATH with path where you have opencv_contrib modules such as ...wherever_you_downloaded_it/opencv_contrib/opencv_contrib-master/modules.
Then, building opencv with make will build modules, and make install will install these libs with other opencv libs.

I cannot install dipy library for python, why ? In the description I explain what I did

I went to http://nipy.org/dipy/installation.html and install nibabel, then I when I wanted to install dipy, there where 2 problems:
Wheel was not built
and vcvarshall.bat not found.
What I did ?
Install Setuptools in site-pakcages
download Setuptools-34.3.1-py2.py3-none-any.whl (md5) and save in site-packages
I also try
python setup.py install --compiler=mingw32ç
and
If you get an error saying unable to find vcvarsall.bat then you need to create a file called pydistutils.cfg in notepad and give it the contents
[build]
compiler=mingw32
But setup.py de system it did not find, and I still have vcvarshall.bat not found.
what I need to do?
I am using, Windows 7, Python 3.5.1 and Anaconda 2.5.0 (64 bit)
You will almost certainly find it easier to install third-party packages if you adopt virtual environments. When done correctly you will then not need admin privileges to install packages into virtualenvs. The HitchHikers' Guide to Python contains more information about this.
The vcvarsall.bat is, I believe, a part of the Visual Studio (the Express version is available at no cost) environment. It's required when you are trying to build a compiled Python extension as described in this article. I'm not sure how that will play with mingw.
So, I installed via ANACONDA but , when I go to python, and I want to import dipy it says: No modle named dipy
Solved ! Well I had python 3.5 and dipy has some issues with that version, so I installed Anaconda with python 2.7 , installed visual c++9 and follow the steps on the web !

Categories