在 PyQt4 中显示视频流的最佳方式是什么?
What is the best way to display a videostream in PyQt4?
我有一台不断发送视频流的服务器。实际上它只是源源不断的图像流。
我可以通过加载以下 html 页面在我的浏览器中显示此流:
<!DOCTYPE html>
<html><body>
<h2>Video:</h2>
<img src='http://192.168.1.100:8081/' style='width:304px;height:228px;'>
</body></html>
现在我想在我正在构建的 PyQt 应用程序中加载这个流。
当我在 QWebView 中尝试此操作时,它不会加载图像。我也试过将它加载到 QPixmap 中。没有任何效果。
所以现在我想知道,有没有一种简单的方法可以在 QT 小部件中显示此流?
过了一会儿我找到了解决办法。在此示例中,我首先加载了一个 .ui 模板。在这个 window 中有一个名为 'label' 的标签,我想在其中加载我的流。为此你需要 openCV。
class videoThread(QThread):
def __init__(self,address):
super(videoThread,self).__init__()
self.ip = address
def run(self):
cap = cv2.VideoCapture("http://"+ str(self.ip) +
":8081/?action=stream?dummy=param.mjpg")
while cap.isOpened():
_,frame = cap.read()
# adjust width en height to the preferred values
image = QImage(frame.tostring(),640,480,QImage.Format_RGB888)
.rgbSwapped()
self.emit(SIGNAL('newImage(QImage)'), image)
class MyGui(QMainWindow):
"""
My gui implementation
"""
def __init__(self,template):
super(MyGui,self).__init__()
uic.loadUi(template,self)
#video stream
self.video = videoThread("192.168.1.100")
self.video.start()
#my label is named label
self.label.connect(self.video,SIGNAL('newImage(QImage)'),self.setFrame)
def setFrame(self,frame):
pixmap = QPixmap.fromImage(frame)
self.label.setPixmap(pixmap)
我有一台不断发送视频流的服务器。实际上它只是源源不断的图像流。
我可以通过加载以下 html 页面在我的浏览器中显示此流:
<!DOCTYPE html>
<html><body>
<h2>Video:</h2>
<img src='http://192.168.1.100:8081/' style='width:304px;height:228px;'>
</body></html>
现在我想在我正在构建的 PyQt 应用程序中加载这个流。 当我在 QWebView 中尝试此操作时,它不会加载图像。我也试过将它加载到 QPixmap 中。没有任何效果。
所以现在我想知道,有没有一种简单的方法可以在 QT 小部件中显示此流?
过了一会儿我找到了解决办法。在此示例中,我首先加载了一个 .ui 模板。在这个 window 中有一个名为 'label' 的标签,我想在其中加载我的流。为此你需要 openCV。
class videoThread(QThread):
def __init__(self,address):
super(videoThread,self).__init__()
self.ip = address
def run(self):
cap = cv2.VideoCapture("http://"+ str(self.ip) +
":8081/?action=stream?dummy=param.mjpg")
while cap.isOpened():
_,frame = cap.read()
# adjust width en height to the preferred values
image = QImage(frame.tostring(),640,480,QImage.Format_RGB888)
.rgbSwapped()
self.emit(SIGNAL('newImage(QImage)'), image)
class MyGui(QMainWindow):
"""
My gui implementation
"""
def __init__(self,template):
super(MyGui,self).__init__()
uic.loadUi(template,self)
#video stream
self.video = videoThread("192.168.1.100")
self.video.start()
#my label is named label
self.label.connect(self.video,SIGNAL('newImage(QImage)'),self.setFrame)
def setFrame(self,frame):
pixmap = QPixmap.fromImage(frame)
self.label.setPixmap(pixmap)