openCV in python on Linux bombs out at show image [duplicate] - python-3.x

I'm having an error running simple code using cv2 module.
It's just:
import cv2
img = cv2.imread('sudoku.png',0)
cv2.imshow('image',img)
And it fails with the following error:
QObject::moveToThread: Current thread (0x1b74720) is not the object's thread (0x1e57d70).
Cannot move to target thread (0x1b74720)
I googled this error and tried a lot of things but it doesn't help. I tried installing without pip, I tried using step-by-step installation (from official OpenCV) but nothing helps.
When I run:
cv2.__version__
It returns 3.4.3

As noted already, the basis for this problem is discussed in opencv-python issue 46, and results from the duplication of the following libraries both on the host and the opencv-python distro libQtDBus libQtCore and libQtGui.
It has been lately addressed in the newest release of opencv-python. It is not a fix to the source code, rather the fix is to force pip to compile the newly available source via
pip install --no-binary opencv-python opencv-python
This will cause opencv-python to use the same libraries as the host, so the conflict no longer exists.

According to this issue posted on the OpenCV GitHub, this is a known issue that the developer states is damn near impossible to fix. It is apparently caused by a conflict in any Qt installations on the system with the Qt that is shipped with OpenCV. There are some suggestions floating around to remove the libqt5x11extras5 package from the system. This may fix it for some but anyone running these libraries on a Linux distribution that uses a window manager based on Qt will render their desktop environment unusable by removing this package (having tried it myself).
You can try building OpenCV from source using the WITH_GTK=ON option when running cmake which will use GTK instead of Qt, circumventing the conflict. However, this is hard to make use of in Python when using virtual environments.

I haven't spent the time to fully appreciate this problem, but as I understand this is caused by multiple conflicting versions of some plugin in the environment. I tried installing building opencv-python but there were errors with that approach. Another suggestion is to change your import order, but I've had mixed success with that, and I couldn't get it to work on a project today.
But I found a workaround that worked for me. Install opencv-python-headless instead of opencv-python. This will avoid installing the conflicting plugins. It may not work for you depending on what features of opencv you need.
$ pip uninstall opencv-python
$ pip install opencv-python-headless

The error was fixed on my system, by simply updating one library.
To find out where it is coming from, assuming Linux, try the following,
LD_DEBUG=files python -c "import cv2"
or,
LD_DEBUG=files python -c "import cv2 ; img = cv2.imread('myimage.png',0) ; cv2.imshow('image',img) ; cv2.waitKey(0)"
On my machine, it failed in one of the blas libraries. I updated that library and the code now runs without error.
This is an old bug you can find it discussed in a number of online communities.
My test code is as follows. Notably the error has not reoccurred, and apparently has nothing to do with Qt.
import cv2
img = cv2.imread('sudoku.png',0)
cv2.imshow('image',img)
cv2.waitKey(0)

To me, the solution to this problem was removing Anaconda, then installing pip followed by installing OpenCV with a simple pip install command.

Related

Is there currently any way to install mypy for python 3.7 and use with PyDev?

I tried to install mypy with "phyton -m pip install mypy" but it fails with an error about typed_ast that says it needs Visual C++ compiler but after installing that the error remains. I found several similar reports and notes that it can't be fixed due to the typed_ast thing, I don't know any details about that.
Is there a way to install mypy manually? Just copy a bunch of files somewhere?
I use Windows 10, Python 3.7, I updated to the latest pip.
Any help or hint is welcome
Thanks a lot
Martin
I found an answer myself.
I have python 3.7-32 installed and there is no appropriate wheel for typed -ast for this. This video
https://www.youtube.com/watch?v=XZrThBBpFlo
explains the problem and also gives a solution. I installed a 32-bit wheel from the link given and then installed mypy again and it finished successfully.
Now I will try mypy.
Good luck

Getting Error When installing Kivy Modules

So to be honest I started to learn to code like two weeks ago. If anyone could help me with this problem that would be great!
so Basically when I try to install kivy-deps.angle, kivy-deps.glew, or kivy-deps.gstreamer I get a message like the one below.
ERROR: Could not find a version that satisfies the requirement kivy-deps.angle-dev==0.2.0 (from versions: none)
ERROR: No matching distribution found for kivy-deps.angle-dev==0.2.0
enter image description here
enter image description here
I Tried everything I could think of and of course the proposed solutions are a joke i get the same Error in the terminal, I have the latest version of pip
$ pip --version
pip 20.1.1 from /Users/colemanschnaak/Library/Python/3.7/lib/python/site-packages/pip (python 3.7)
Since Im using a mac I already have python 2.7 pre installed I tried switching the python3 to the default.(which wasted my time)
I saw online that some people just have to type
$ python3 -m pip install [kivy module name]
but that still didnt work.
since i just started getting in to coding thats all I can think of.
The current version kivy-deps.angle-dev 0.2.0 provides wheels for Python 3.6-3.8 for w32 and w64 only. No other wheels and no source code at PyPI.
If you need kivy-deps.angle-dev for a different Python or a different OS you have to compile from sources. If there is something to compile — I downloaded a wheel and didn't find any code inside it.

No module named 'System'

I have an issue that has been discussed in some GitHub blogs but the answers there are so confusing and complex that I thought to ask here as well.
Before asking, let me just say I'm not an expert programmer, so my apologizes if mine is a simple question.
I recently had to install Python 3.7
I used to have modules like clr and System working totally fine and now everything seems broker.
I use Anaconda/Spyder to simply load the packages clr and System and Python gives me the following error messages:
No module named 'clr'
No module named 'System'
I managed to fix (apparently) the clr issue by running
pip install clr
from Anaconda Prompt.
However, in order to fix the issue with the System module, it seems I need to install pythonnet (I don't knoww exactly what it is but I guess it doesn't matter).
Based on anaconda official website: https://anaconda.org/pythonnet/pythonnet I should simply run:
conda install -c pythonnet pythonnet
But that doesn't work.
My extremely limited understanding is that something is going wrong between Pythonnet and Python 3.7.
Does anybody have a clue of what I should do?
you must uninstall first clr and then do pip install pythonnet. pythonnet has clr and System.
check documentation: https://github.com/pythonnet/pythonnet
you should use this:
import clr
clr.AddReference('System')
from System import String
What eventually worked for me, after messing around with my packages, was to: 1. uninstall clr. 2. Uninstall pythonnet. 3. Only at this point, I had to reinstall pythonnet. This sequence fixed my problem.
pip uninstall clr
pip uninstall pythonnet
pip install pythonnet
In my Python3, System is not a module - it's a method of the os module.
so I use
from os import system
then
system("<<Put Your Command Here>>")
If you want it called System with a capital S (maybe for backward compatability with already-written programs?), use
from os import system as System

importing pygrib anaconda throws dependency issues

I have the following issue: I have installed anaconda 3 and installed a package called "pygrib" into my anaconda environment. Now when importing pygrib in a file in my environment, it will show me this error:
import pygrib
ImportError: libhdf5.so.10: cannot open shared object file: No such file or directory
As I am a noobie, I dont really know what to do with this information. I installed the h5py package and some other related ones, but it didnt resolve the issue. What to do?
This is a linking error with the HDF5 library. Are you building pygrib from source or using the conda-forge channel to install it via conda? When I use the conda-forge build of pygrib I get the same issue. The GRIB API from ECMWF (on conda-forge it is listed as ecmwf_grib) is what pygrib depends on and the HDF5 dependency comes from netCDF4 being used in the GRIB API library. Specifically, using the latest HDF5 (1.10.0 at this time) is what is causing a problem. Using HDF5 1.8.* instead allows pygrib to import properly.
To force conda to grab a specific version, just do:
conda install pygrib hdf5=1.8
This will get conda to solve the package specifications again with the older HDF5 library and likely clear up the issue. This assumes you are in the conda environment that you installed pygrib into. You could also create a new environment with conda create -n <env name> pygrib hdf5=1.8 if you wanted to.
In general, when you see these errors where a library is not found, it is often a matter of getting the right version of a library installed. With conda, this sort of thing happens when updating packages and a newer version of a library gets installed that a package you are using has not been properly linked with. As long as you can track down the package/library that is causing trouble, you can use the above procedure to start requiring certain versions of things get installed and conda should then update or downgrade things so that things work together again. Hopefully this makes sense and helps.
This part may or may not interest you, but what I cannot say for certain is where this problem originates. My guess is that it is something with ecmwf_grib and how it is built. That is where ldd shows the old HDF5 dependency showing up for my installation. If I can figure out the exact issue, I'll update this answer.

How to install VTK5 on Archlinux?

I need to run a program which use VTK5 on my Archlinux PC, but I found it really hard to install VTK5, there is only VTK6(not compatible with VTK5) in official repo, and when I try to install it from AUR, it returns "Makepg was unable to build vtk5", then I try to install through source code, the result is that I was unable to install the VTK Python module...
Is there anybody who has any experience or idea about it?
I have not installed on Archlinux specifically, but on different linux machines. If you compile from source and are interested in python, remember to select the option python wrapping when running cmake. Btw, once built, you will have to update both the pythonpath and the ldlibrarypath.
You can also have a try at enthought canopy, which distributes a complete installation with numpy, scipy, vtk http://docs.enthought.com/canopy/quick-start/install_linux.html

Resources