Skip to content

Instantly share code, notes, and snippets.

@TimSC
Created May 29, 2013 13:01
Show Gist options
  • Save TimSC/5670099 to your computer and use it in GitHub Desktop.
Save TimSC/5670099 to your computer and use it in GitHub Desktop.

Revisions

  1. TimSC created this gist May 29, 2013.
    85 changes: 85 additions & 0 deletions qt4-webcam.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,85 @@
    '''
    QT4 Webcam demo in python
    Copyright (c) 2013, Tim Sheerman-Chase
    All rights reserved.
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
    1. Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
    ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    The views and conclusions contained in the software and documentation are those
    of the authors and should not be interpreted as representing official policies,
    either expressed or implied, of the FreeBSD Project.
    '''

    import sys, time, cv
    from PyQt4 import QtGui, QtCore

    class CamWorker(QtCore.QThread):
    def __init__(self):
    super(CamWorker, self).__init__()
    self.cap = cv.CaptureFromCAM(-1)
    capture_size = (640,480)
    cv.SetCaptureProperty(self.cap, cv.CV_CAP_PROP_FRAME_WIDTH, capture_size[0])
    cv.SetCaptureProperty(self.cap, cv.CV_CAP_PROP_FRAME_HEIGHT, capture_size[1])

    def run(self):
    while 1:
    time.sleep(0.01)
    frame = cv.QueryFrame(self.cap)
    im = QtGui.QImage(frame.tostring(), frame.width, frame.height, QtGui.QImage.Format_RGB888).rgbSwapped()
    self.emit(QtCore.SIGNAL('webcam_frame(QImage)'), im)

    class MainWindow(QtGui.QMainWindow):
    def __init__(self):
    super(MainWindow, self).__init__()
    self.resize(700, 550)
    self.move(300, 300)
    self.setWindowTitle('Qt Webcam Demo')

    self.scene = QtGui.QGraphicsScene(self)
    self.view = QtGui.QGraphicsView(self.scene)

    self.vbox = QtGui.QVBoxLayout()
    self.vbox.addWidget(self.view)

    centralWidget = QtGui.QWidget()
    centralWidget.setLayout(self.vbox)
    self.setCentralWidget(centralWidget)
    self.show()


    def ProcessFrame(self, im):
    print "Frame update", im
    pix = QtGui.QPixmap(im)
    self.scene.clear()
    self.scene.addPixmap(pix)


    if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)

    mainWindow = MainWindow()

    camWorker = CamWorker()
    QtCore.QObject.connect(camWorker, QtCore.SIGNAL("webcam_frame(QImage)"), mainWindow.ProcessFrame)
    camWorker.start()

    sys.exit(app.exec_())