Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- import sys
- from PyQt4 import QtCore
- from PyQt4 import QtGui
- from picamera.array import PiRGBArray
- from picamera import PiCamera
- from PyQt4.QtGui import QWidget
- from PyQt4.QtCore import pyqtSignal
- from imutils.video import VideoStream
- from imutils import face_utils
- import imutils
- import io
- camera_port = 0
- class ShowVideo(QtCore.QObject):
- #initiating the built in camera
- VideoSignal = QtCore.pyqtSignal(QtGui.QImage)
- def __init__(self, parent = None):
- super(ShowVideo, self).__init__(parent)
- @QtCore.pyqtSlot()
- def startVideo(self):
- camera = PiCamera()
- camera.resolution=(640,480)
- rawCapture = PiRGBArray(camera,size=(640, 480))
- for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
- image = frame.array
- image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
- qt_image = QtGui.QImage(image, image.shape[1], image.shape[0], QtGui.QImage.Format_RGB888)
- ## pixmap = QtGui.QPixmap.fromImage(qt_image)
- ## qt_image = pixmap.scaled(320, 240, QtCore.Qt.KeepAspectRatio)
- qt_image = QtGui.QImage(qt_image)
- self.VideoSignal.emit(qt_image)
- rawCapture.truncate(0)
- class ImageViewer(QWidget):
- def __init__(self, parent = None):
- super(ImageViewer, self).__init__(parent)
- self.image = QtGui.QImage()
- self.setAttribute(QtCore.Qt.WA_OpaquePaintEvent)
- def paintEvent(self, event):
- painter = QtGui.QPainter(self)
- painter.drawImage(0,0, self.image)
- self.image = QtGui.QImage()
- def initUI(self):
- self.setWindowTitle('Test')
- @QtCore.pyqtSlot(QtGui.QImage)
- def setImage(self, image):
- if image.isNull():
- print("viewer dropped frame!")
- self.image = image
- if image.size() != self.size():
- self.setFixedSize(image.size())
- self.update()
- if __name__ == '__main__':
- app = QtGui.QApplication(sys.argv)
- thread = QtCore.QThread()
- thread.start()
- vid = ShowVideo()
- vid.moveToThread(thread)
- image_viewer = ImageViewer()
- #image_viewer.resize(200,400)
- vid.VideoSignal.connect(image_viewer.setImage)
- #Button to start the videocapture:
- push_button = QtGui.QPushButton('Start')
- push_button.clicked.connect(vid.startVideo)
- vertical_layout = QtGui.QVBoxLayout()
- vertical_layout.addWidget(image_viewer)
- vertical_layout.addWidget(push_button)
- layout_widget = QWidget()
- layout_widget.setLayout(vertical_layout)
- main_window = QtGui.QMainWindow()
- main_window.setCentralWidget(layout_widget)
- main_window.resize(720,480)
- main_window.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement