Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenCV and PyQt5 incompatibility workaround #184

Open
krishauser opened this issue May 8, 2024 · 1 comment
Open

OpenCV and PyQt5 incompatibility workaround #184

krishauser opened this issue May 8, 2024 · 1 comment

Comments

@krishauser
Copy link
Owner

If you are using the opencv-python library on Linux, there will be a problem with PyQt5. The workaround is as follows:

import cv2
from PyQt5.QtCore import QLibraryInfo
import os

#hack needed to get opencv to work with PyQt5
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = QLibraryInfo.location(QLibraryInfo.PluginsPath)

from klampt import vis
from klampt import WorldModel
import time

w = WorldModel()
w.readFile("../../data/athlete_plane.xml")
vis.add("world",w)
vis.show()
while vis.shown():
    time.sleep(0.1)
vis.kill()
@krishauser
Copy link
Owner Author

If you do want to use OpenCV windows, you need to

  • show them before the above workaround,
  • apply the above workaround after the window is shown
  • run the Klampt GUI in single-threaded mode

For example:

import cv2
from PyQt5.QtCore import QLibraryInfo
import os

cap = cv2.VideoCapture(0)
if not cap.isOpened():
    print("Cannot open camera")
    exit()

ret, frame = cap.read()

# if frame is read correctly ret is True
if not ret:
    print("Can't receive frame (stream end?). Exiting ...")
    exit()
# Display the resulting frame
cv2.imshow('frame', frame)

#hack needed to get opencv to work with PyQt5
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = QLibraryInfo.location(QLibraryInfo.PluginsPath)

from klampt import vis
from klampt.vis import GLPluginInterface
from klampt import WorldModel
import time

def capture():
    ret, frame = cap.read()
    
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        vis.show(False)
        return
    # Display the resulting frame
    cv2.imshow('frame', frame)

w = WorldModel()
w.readFile("../../data/athlete_plane.xml")
vis.add("world",w)

#can only run single-threaded mode
vis.loop(callback=capture)

#vis.show()
#while vis.shown():
#    capture()
#    time.sleep(0.01)

vis.kill()

cap.release()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant