Display OpenCV Webcam in GTK 4 using PyGobject

62 Views Asked by At

I am attempting to display an image captured from a webcam on a GTK 4 Picture widget using PyGObject. In this case, a thread is capturing the image data and storing individual frames in a queue which the parent thread reads and is supposed to display. However, the display is always just a black square. This makes me think there's something I'm doing wrong when converting from the NDArray to a Pixbuff / texture.

I have verified that:

  • the image is being captured as expected by the child thread
  • the image is being placed in the queue
  • the image is being correctly retrieved at the prescribed intervals by the GTK timeout
  • the image is stored as RGB and not BGR

The closest thing to a function that works is:

def _on_camera_update(self) -> bool:
    try:
        self._current_frame = self._camera_handler.output_queue.get_nowait()
        self._current_pixbuff = GdkPixbuf.Pixbuf.new_from_data(
            self._current_frame.tobytes(),
            GdkPixbuf.Colorspace.RGB,
            False,
            8,
            self._current_frame.shape[1],
            self._current_frame.shape[0],
            self._current_frame.shape[2] * self._current_frame.shape[1],
        )
        self._image_container.set_paintable(Gdk.Texture.new_for_pixbuf(self._current_pixbuff))
    except queue.Empty:
        pass

For reference, I've also looked into:

0

There are 0 best solutions below