如何在python gtk gui中显示opencv视频帧?

时间:2020-06-08 16:32:49

标签: python opencv image-processing gtk

我尝试从线程到gtk的图像框执行此操作。帧已成功显示一段时间,但5秒钟后gui冻结,图像似乎为空白,但线程仍在运行

import gi
import threading
import cv2
import time
gi.require_version("Gtk" , "3.0")
from gi.repository import Gtk , GLib , GObject , GdkPixbuf
run = True
cam = cv2.VideoCapture(0)
ret , frame = cam.read()
frame  = cv2.cvtColor(frame , cv2.COLOR_BGR2RGB)
cam.release()
class MyWindow(Gtk.Window):

    def __init__(self):

        global cam, frame , run

        Gtk.Window.__init__(self,title="Hello")
        self.set_border_width(10)

        image = Gtk.Image()

        h, w, d = frame.shape
        pixbuf = GdkPixbuf.Pixbuf.new_from_data(frame.tostring(),GdkPixbuf.Colorspace.RGB, False, 8, w, h, w*d)
        image.set_from_pixbuf (pixbuf.copy())
        self.add(image)
    def thread_running_example():
        cam = cv2.VideoCapture(0)
        while run:
            ret , frame = cam.read()
            frame  = cv2.cvtColor(frame , cv2.COLOR_BGR2RGB)
            h, w, d = frame.shape
            pixbuf = GdkPixbuf.Pixbuf.new_from_data(frame.tostring(), GdkPixbuf.Colorspace.RGB, False, 8, w, h, w*d)                
            image.set_from_pixbuf (pixbuf.copy())



    thread = threading.Thread( target = thread_running_example )
    thread.daemon=True 
    thread.start()

win = MyWindow()
win.connect("destroy" , Gtk.main_quit )
win.show_all()
Gtk.main()

0 个答案:

没有答案