0

installed opencv--python on bookworm os

when using picamera2 i get this error:

QObject::moveToThread: Current thread (0x7f6802fd50) is not the object's thread (0x7f6822efb0). Cannot move to target thread (0x7f6802fd50)

here is the code:

import cv2
from picamera2 import Picamera2, Preview
import time

picam2 = Picamera2()
picam2.start_preview(Preview.QTGL)
picam2.start()
time.sleep(4)

as soon as i comment out the line "import cv2" it works

opencv-python ver 4.6.0+dfsg-12

picamera2 ver 0.3.16

python3

raspberry pi 4b

pi@raspberrypi:~ $ apt show python3-opencv

Package: python3-opencv Version: 4.6.0+dfsg-12 Priority: optional Section: python Source: opencv Maintainer: Debian Science Team [email protected]

there is a solution which disables the opencv gui but. this cripples the application. here is the code :

from picamera2 import Picamera2, Preview
import time
import cv2  
import os, sys
ci_build_and_not_headless = False
try:
    from cv2.version import ci_build, headless
    ci_and_not_headless = ci_build and not headless
except:
    pass
    
if sys.platform.startswith("linux") and ci_and_not_headless:
    os.environ.pop("QT_QPA_PLATFORM_PLUGIN_PATH")
if sys.platform.startswith("linux") and ci_and_not_headless:
    os.environ.pop("QT_QPA_FONTDIR")


picam2 = Picamera2()    
picam2.start_preview(Preview.QTGL)  
picam2.start()
time.sleep(4)

this will disable any show window from opencv

hope there is another solution

2
  • We don't know what you did - there is no opencv--python in apt
    – Milliways
    Dec 26, 2023 at 23:33
  • @Milliways - added the output from apt show. mind you this is bookworm
    – Oferb
    Dec 27, 2023 at 8:10

1 Answer 1

0

I'm not a profession, but just doing the same thing with my university project and get the same error. I managed to solve it by uninstall the opencv-python package in python 3 but remain the 'import cv2'. It will import the cv2 package from bookworm.

The main reason of this error might due to the incompatible packages for opencv between python and bookworm, so uninstall either will work - that's just my opinion.

Hope this solve your problem

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.