Wie zeige ich OpenCV -Videoprahmen in der Python GTK -GUI?

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Wie zeige ich OpenCV -Videoprahmen in der Python GTK -GUI?

by Anonymous » 02 Apr 2025, 11:03

Ich habe versucht, dies von Thread zu einer Bildbox von GTK zu tun. Die Rahmen zeigen einige Male erfolgreich, aber nach 5 Sekunden friert die GUI ein und das Bild scheint leer zu sein, aber der Thread läuft immer noch < /p>

Code: Select all

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()

Top