cv2.imshow没有显示视频和冻结程序

时间:2019-11-20 23:37:47

标签: python opencv cv2

我有一些代码,其中在Tkinter GUI内的单独线程中调用cv2.imshow()。它在while循环中检查线程是否已终止。 while循环显示来自摄像机的视频。第一次运行没问题,但是一旦线程终止并且启动了新线程以显示视频,该程序似乎在cv2.imshow()调用中丢失了。具体来说,不会在cv2.imshow()调用中引发任何错误,但是不会显示任何图像,并且程序不会继续经过cv2.imshow()调用。我的问题是,我如何打印以查看cv2.imshow()中正在发生什么?另外,如果您发现可能导致问题的原因,将不胜感激。

功能如下:


def startVideo(self):

        #camera settings
        self.cam.set_exposure(9000)
        self.cam.set_width(400) 
        self.cam.set_height(300)

        #start acquisition and create Image object and clear data
        self.cam.start_acquisition()
        self.img = xiapi.Image()
        self.data = None

        print('Starting video. Press CTRL+C to exit.')

        #problem persists with or without this call
        cv2.startWindowThread()

        while True:


            if(self.system.control.stop_threads.is_set()):
                break

            #get data and pass them from camera to img
            self.cam.get_image(self.img,100000)


            if(self.system.control.stop_threads.is_set()):
                break

            #create numpy array with data from camera. Dimensions of the array are 
            #determined by imgdataformat
            self.data = self.img.get_image_data_numpy()


            if(self.system.control.stop_threads.is_set()):
                break

            #calculate variance for image sharpness and display on the frame
            var = self.system.focus.varianceofLap(self.data)
            var = '{:5.2f}'.format(var)
            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(
                    self.data, var, (0,100), font, 4, (255, 255, 255), 2
                    )

            #this is the call that the program hangs on
            cv2.imshow('XiCAM Video', self.data)
            cv2.waitKey(1)


            #position window and set it to stay on top    
            cv2.moveWindow("XiCAM Video", 1100,0)
            os.system("wmctrl -r 'XiCAM Video' -b add,above")

        #destroy window and stop camera acquisition when thread is terminated
        cv2.destroyWindow('XiCAM Video')
        self.cam.stop_acquisition()

0 个答案:

没有答案