[PyQt] PyQt5, QtQuick and QCamera

Robert Kent rob at gulon.co.uk
Wed Jul 1 15:50:17 BST 2015


Hi Guys,

I wonder if anyone can point out a mistake here or if I have encountered a bug. If I try to use a VideOutput element in QML to display the live output from QCamera, via setViewfinder, I get a set fault. This works fine from C++, but I’m not sure if something is different in Python. Example follows (also attached):

camerasource.py

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

class CameraSource(QObject):
    @pyqtProperty(QAbstractVideoSurface)
    def videoSurface(self): return self._surface

    @videoSurface.setter
    def videoSurface(self, surface):
        self._surface=surface
        if self._surface:
            self._surface.start(self._format)
            self.camera.setViewfinder(self._surface)
            self.camera.start()

    def __init__(self, parent=None, **kwargs):
        super().__init__(parent, **kwargs)

        self.camera=QCamera(QCamera.FrontFace)
        self.capture=QCameraImageCapture(self.camera, self)
        self.recorder=QMediaRecorder(self.camera, self)
        self._format=QVideoSurfaceFormat()
        self._surface=None

    @pyqtSlot(QVideoFrame)
    def onNewVideoContentReceived(self, frame):
        if self._surface: self._surface.present(frame)      

main.qml

import QtQuick 2.2
import QtQuick.Controls 1.1
import QtMultimedia 5.2

import CameraSource 1.0

ApplicationWindow {
  visible: true
  width: 1280
  height: 720
  color: "black"

  VideoOutput {
    id: video
    source: camera
    anchors.fill: parent
  }

  CameraSource {
    id: camera
  }
}

video_example.py

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtQuick import *
from PyQt5.QtQml import *

from camerasource import *


if __name__=="__main__":
    from sys import argv, exit

    a=QApplication(argv)

    qmlRegisterType(CameraSource, "CameraSource", 1, 0, "CameraSource")

    engine=QQmlApplicationEngine()
    engine.load(QUrl("./main.qml"))

    exit(a.exec_())

Run ‘python video_example.py’ to test. If the ‘setViewfinder’ line in camerasource.py is commented out then the problem goes away, but obviously I don’t get any video displayed either. My setup is as follows:

OSX Yosemite
Python 3.4.3
Qt5.4
PyQt5.4.2

All help gratefully received,

Rob

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20150701/a8d4517f/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: video_example.zip
Type: application/zip
Size: 2592 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20150701/a8d4517f/attachment.zip>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20150701/a8d4517f/attachment-0001.html>


More information about the PyQt mailing list