python - GUI 卡住了其余的 python 代码

我在 python 中使用 wx gui。和一个opencv对象检测。 GUI 在我运行它时卡住其余代码 GUI 出现,当我关闭窗口时代码启动我发现很多关于这个问题的提问者> 解决方案是创建一个线程来运行 gui 并在 main 方法中启动这个线程但是同样的问题仍然存在 这里是线程

class GuIthread(threading.Thread):

def __init__ (self):
    threading.Thread.__init__(self)

def run(self):
    gettext.install("app") # replace with the appropriate catalog name\
    global View

    app = MyApp(0)
    app.MainLoop()

那么这里是主要方法
if __name__ == '__main__':
parser = OptionParser(usage = "usage: %prog [options] [filename|camera_index]")
parser.add_option("-c", "--cascade", action="store", dest="cascade", type="str", help="Haar cascade file, default %default", default = "../data/haarcascades/haarcascade_frontalface_alt.xml")
(options, args) = parser.parse_args()

cascade = cv.Load(options.cascade)
global Count

if len(args) != 1:
    parser.print_help()
    sys.exit(1)

input_name = args[0]
if input_name.isdigit():
    capture = cv.CreateCameraCapture(int(input_name))
else:
    capture = None

cv.NamedWindow("video", 1)

#size of the video
width = 600
height = 500

if width is None:
    width = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH))
else:
    cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH,width)

if height is None:
    height = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT))
else:
    cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT,height)

if capture:
    frame_copy = None
    thread1 = myThread()
    thread1.start()
    threadGUI = GuIthread()
    threadGUI.start()
    threadGUI.join()
    frame_copy = None
    t0=time.time()
    while True:
        global frame 
        frame = cv.QueryFrame(capture)
      #  cv.SaveImage('pic.jpg', frame)
        t1=time.time()
        if (t1-t0) >= 10:
            thread1.run()
            t0=t1
            SendPic (frame)
            Count=0                

        if not frame:
            cv.WaitKey(0)
            break
        if not frame_copy:
            frame_copy = cv.CreateImage((frame.width,frame.height),
                                        cv.IPL_DEPTH_8U, frame.nChannels)

        if frame.origin == cv.IPL_ORIGIN_TL:
            cv.Copy(frame, frame_copy)
        else:
            cv.Flip(frame, frame_copy, 0)

        detect_and_draw(frame_copy, cascade)

        if cv.WaitKey(10) >= 0:
            break
else:
    image = cv.LoadImage(input_name, 1)
    detect_and_draw(image, cascade)
    cv.WaitKey(0)

cv.DestroyWindow("video")

出现 gui 和视频窗口,但代码不运行,除非我关闭 GUI 窗口视频开始

我尝试的是
我添加了 GUIthread.join() (添加之前段错误)
我试图让代码在一个方法中运行 GUI 并调用它,结果发生了相同的情况,但没有出现视频窗口。

最佳答案

首先,@Mark 是正确的,您是从常规约定向后执行此操作,其中 GUI 在主线程中运行。您的代码被阻止,因为您正在执行此操作

threadGUI = GuIthread()
threadGUI.start()
threadGUI.join()  # This blocks until threadGUI is complete.
threadGUI.join()调用将使您的程序阻塞,直到 app.MainLoop()完成,直到您关闭 GUI 才会发生。

https://stackoverflow.com/questions/23856672/

相关文章:

python - 质心计算程序中的时间滞后

opencv - 更改图像opencv的宽度步长

python - 使用OpenCV计数视频中的帧…(Python)

docker - depends_on不等待docker-compose 1.22.0中的其他服务

android - 使用JNI获取arrayList的元素

opencv - 如何从opencv中的上下文获取设备列表

python - 使用 slider 编辑颜色值

android - 如何使用静态OpenCV在Android中使用JavaCV的静态方法

docker - 无法在 Dockerfile 中运行 sysctl 命令

opencv - ANN-人工神经网络训练