简体   繁体   English

该代码在Windows上有效,但在Linux上显示错误“未在主线程中创建QApplication”,如何解决,

[英]this code works on windows but giving error on Linux “QApplication was not created in main thread” ,how to resolve it,

import cv2
import threading

class camThread(threading.Thread):
    def __init__(self, previewName, camID):

        threading.Thread.__init__(self)
        self.previewName = previewName
        self.camID = camID

    def run(self):

        print "Starting " + self.previewName
        camPreview(self.previewName, self.camID)

def camPreview(previewName, camID):
    cv2.namedWindow(previewName)
    cam = cv2.VideoCapture(camID)
    if cam.isOpened():  # try to get the first frame

        rval, frame = cam.read()

    else:

        rval = False
    while rval:

        cv2.imshow(previewName, frame)
        rval, frame = cam.read()
        key = cv2.waitKey(20)
        if key == 27:  

            break
    cv2.destroyWindow(previewName)

# Create two threads as follows

thread1 = camThread("Camera 1", 1)
thread2 = camThread("Camera 2", 2)

thread1.start()
thread2.start()

Don't create threads, namedWindow , imshow and destroyWindow must not be called in a thread. 不要创建线程,不能在线程中调用namedWindowimshowdestroyWindow

def preview(camera_ids):
    cameras = {}
    for name, id in camera_ids.items():
        cv2.namedWindow(name)
        cameras[name] = cv2.VideoCapture(id)
    while True:
        for name, cam in cameras.items():
            ok, frame = cam.read()
            if ok: cv2.imshow(name, frame)
        key = cv2.waitKey(20)
        if key == 27:  
            break
    for name in cameras:
        cv2.destroyWindow(name)

preview({"Camera 1": 1, "Camera 2": 2})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Django中使用QT刮取一次,在main()线程中未创建使用QApplication的下一次运行时崩溃 - Scraping with QT in Django works once, crashes on next run with QApplication was not created in the main() thread 如何避免PyCharm控制台崩溃“警告:在使用matplotlib进行绘图时,未在main()线程中创建QApplication? - How to avoid PyCharm console crash “WARNING: QApplication was not created in the main() thread” when plotting with matplotlib? QApplication 在 pyQt4 的第二个进程的非主线程中:这段代码是否合法,如果不合法,为什么它有效? - QApplication in a non-main thread of second process with pyQt4: is this code legal, and if not, why does it work? Linux上的ViewDoesNotExist错误但是在Windows上使用相同的代码,为什么? - ViewDoesNotExist error on linux but same code works on windows, why? Websocket代码可在Windows上运行,但不适用于Linux - Websocket code works on Windows but not Linux 我们如何改变 flet 中的视图? 我收到一个错误 -> 信号仅在主解释器的主线程中有效 - how do we change views in flet ? I get an error -> signal only works in main thread of the main interpreter 尝试在 Heroku 上部署 flask 应用程序,部署工作正常,直到我在我的代码中添加会话,然后它给出 500 内部错误。 如何解决这个问题? - Trying to deploy flask app on Heroku, deployment works fine until I add sessions in my code, then it's giving 500 internal error. how to resolve this? 如何在我创建的网络搜寻器上解决python线程错误“参数过多”? - How to resolve python thread error 'too many arguments' on my created web crawler? 脚本可在Windows上运行,但不能在Linux上运行(语法错误) - Script works on Windows, but not on Linux (Syntax Error) 如何使用 python 或 Shell 代码退出 Qapplication (PyQt) - How to Quit Qapplication (PyQt) with python or Shell code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM