-1

Setup: Trying to do python development with Anaconda 3, PyCharm Community Edition (2023.3.2) on Ubuntu (22.04.3 LTS) and a lot of existing packages including opencv-python. I was having the problem described in question

Python OpenCV Error: Current thread is not the object's thread

but is really a new question because I apparently have a configuration problem of some sort. In short, CV2 calls in Python 3 result in an error that looks like this:

QObject::moveToThread: Current thread (<address>) is not the object's thread (0x1e57d70).
Cannot move to target thread (<address>).

The recommended solution that seemed to help other folks was to first uninstall the opencv-python package and then reinstall it having the package rebuilt on the target platform with this command:

pip install --no-binary opencv-python opencv-python

While trying this, the build gets to 100% when I get a somewhat implausible error that Python.h can't be found. The full error is pasted below but a little background might help. While I'm not new to Linux, I'm a little new to Ubuntu and newer still to development (more of a Systems than Software Engineer). If these installers (apt, apt-get, pip et al.) can't resolve the dependencies and paths, it will be difficult for me to figure it out. The PyCharm package view seems to be pretty good and its connection to conda seems to be okay (simple programs work okay) but something underneath is not right. The connection to gcc perhaps? I appreciate any and all suggestions and offers of assistance. Thanks in advance.

-Rocky714

      Note: Class cv::Feature2D has more than 1 base class (not supported by Python C extensions)
            Bases:  cv::Algorithm, cv::class, cv::Feature2D, cv::Algorithm
            Only the first base class will be used
      Note: Class cv::detail::GraphCutSeamFinder has more than 1 base class (not supported by Python C extensions)
            Bases:  cv::detail::GraphCutSeamFinderBase, cv::detail::SeamFinder
            Only the first base class will be used
      [100%] Built target gen_opencv_python_source
      [100%] Building CXX object modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o
      In file included from /tmp/pip-install-s65xv3yn/opencv-python_f0507c14ed7b488894517f39f468dff8/opencv/modules/python/src2/cv2.cpp:5:
      /tmp/pip-install-s65xv3yn/opencv-python_f0507c14ed7b488894517f39f468dff8/opencv/modules/python/src2/cv2.hpp:20:10: fatal error: Python.h: No such file or directory
         20 | #include <Python.h>
            |          ^~~~~~~~~~
      compilation terminated.
      gmake[2]: *** [modules/python3/CMakeFiles/opencv_python3.dir/build.make:76: modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o] Error 1
      gmake[1]: *** [CMakeFiles/Makefile2:2304: modules/python3/CMakeFiles/opencv_python3.dir/all] Error 2
      gmake: *** [Makefile:166: all] Error 2
      Traceback (most recent call last):
        File "/tmp/pip-build-env-deg9zvgy/overlay/lib/python3.10/site-packages/skbuild/setuptools_wrap.py", line 674, in setup
          cmkr.make(make_args, install_target=cmake_install_target, env=env)
        File "/tmp/pip-build-env-deg9zvgy/overlay/lib/python3.10/site-packages/skbuild/cmaker.py", line 697, in make
          self.make_impl(clargs=clargs, config=config, source_dir=source_dir, install_target=install_target, env=env)
        File "/tmp/pip-build-env-deg9zvgy/overlay/lib/python3.10/site-packages/skbuild/cmaker.py", line 742, in make_impl
          raise SKBuildError(msg)
      
      An error occurred while building with CMake.
        Command:
          /tmp/pip-build-env-deg9zvgy/overlay/lib/python3.10/site-packages/cmake/data/bin/cmake --build . --target install --config Release --
        Install target:
          install
        Source directory:
          /tmp/pip-install-s65xv3yn/opencv-python_f0507c14ed7b488894517f39f468dff8
        Working directory:
          /tmp/pip-install-s65xv3yn/opencv-python_f0507c14ed7b488894517f39f468dff8/_skbuild/linux-x86_64-3.10/cmake-build
      Please check the install target is valid and see CMake's output for more information.
      
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for opencv-python
Failed to build opencv-python
ERROR: Could not build wheels for opencv-python, which is required to install pyproject.toml-based projects

1 Answer 1

0

Try to run this commands

sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

then try to reinstall opencv using pip of apt

Sign up to request clarification or add additional context in comments.

4 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
(In your last line, I took it as "...using pip instead of apt ..."). I used the listed command then installed opencv with pip outside of PyCharm (PC) and PC acted as if opencv wasn't installed. I installed it inside of PC using pip on the PC CLI and got the same error. I installed opencv using PC's package manager (after uninstalling it) w/ and w/o the -nobinary options and it wouldn't build. Same error with the plain install. The PC CLI ignored the options so it didn't build and I still got the same error. Is there way to compile outside of PC and have PC recognize it? Or alternate?
Ohh yes you're right it's a typo I meant "pip or apt". You can try to create a virtual environment and install it in on a virtual environment. Then use that install opencv-python in the virtual env. You will be able to access the opencv by using this virtual env inside or outside PyCharm.
using these commands "conda create --name opencvenv" then "conda activate opencvenv" then "pip install opencv-python". Make sure you have conda added to your enviroment variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.