user4740258
user4740258

Reputation: 41

PyQt5 + Gstreamer

i am trying to stream a video from webcam to GUI made using PyQt5 and Gstreamer. so far i get a video with this code :

import sys
import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject, Gst
GObject.threads_init()
Gst.init(None)

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class WebCam(QMainWindow):
"""Form for Streaming a WebCam"""
def __init__(self, parent = None):
    super(WebCam, self).__init__(parent)
    self.display = QWidget()
    self.windowId = self.display.winId()
    self.setGeometry(300,300,640,480)
    self.setWindowTitle("WebCam Streaming")

def setUpGst(self):
    portUDP_RTP = 5800
    self.WebCamPipe = Gst.Pipeline() # define the GStreamer Pipeline
    self.UDP_RTP = Gst.ElementFactory.make('udpsrc', None) 
    self.UDP_RTP.set_property('port', portUDP_RTP)
    self.WebCamPipe.add(self.UDP_RTP) # Add Elements to Pipeline
    cameraCaps = Gst.Caps.from_string('application/x-rtp, encoding-name=JPEG,payload=26')
    self.capsFilter = Gst.ElementFactory.make('capsfilter', None)
    self.capsFilter.set_property('caps', cameraCaps)        
    self.WebCamPipe.add(self.capsFilter) # Add Elements to Pipeline
    self.UDP_RTP.link(self.capsFilter)  # Link Elements of Pipeline
    self.rtpjpegdepay = Gst.ElementFactory.make('rtpjpegdepay', None) 
    self.WebCamPipe.add(self.rtpjpegdepay) # Add Elements to Pipeline
    self.capsFilter.link(self.rtpjpegdepay)  
    self.jpegdec = Gst.ElementFactory.make('jpegdec', None) 
    self.WebCamPipe.add(self.jpegdec) 
    self.rtpjpegdepay.link(self.jpegdec)        
    self.autovideosink = Gst.ElementFactory.make('autovideosink', None)
    self.WebCamPipe.add(self.autovideosink) 
    self.jpegdec.link(self.autovideosink)
    bus =  self.WebCamPipe.get_bus()
    bus.add_signal_watch()
    bus.enable_sync_message_emission()
    bus.connect('sync-message::element', self.on_sync_message)

def on_sync_message(self, bus, msg):        
    if msg.get_structure().get_name() == 'prepare-window-handle':
        msg.src.set_window_handle(self.windowId)    

def startPrev(self):
    self.WebCamPipe.set_state(Gst.State.PLAYING)
    print("should be playing")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    screen = WebCam()
    screen.setUpGst()
    screen.startPrev()
    screen.show
    sys.exit(app.exec_()) 

what I need is to get the overlay working so it's displayed within a widget on my GUI. i think the problem is with this part of code

bus.connect('sync-message::element', self.on_sync_message)

it's also give me this message of error:

Traceback (most recent call last):
  File "WebCamWidget.py", line 66, in on_sync_message
    msg.src.set_window_handle(self.windowId)    
AttributeError: '__main__.GstXvImageSink' object has no attribute 'set_window_handle'

Upvotes: 1

Views: 2935

Answers (1)

user4740258
user4740258

Reputation: 41

I have found the solution, it was to import GstVideo from gi.repository, the correct line of code should become :

    from gi.repository import GObject, Gst, GstVideo   

for more information you can visit these two links:

1 - https://wiki.ubuntu.com/Novacut/GStreamer1.0#Using_GStreamer_1.0_from_Python

2 - http://bazaar.launchpad.net/~jderose/+junk/gst-examples/view/head:/webcam-1.0

Upvotes: 3

Related Questions