Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import sys
  4. from PyQt4 import QtCore
  5. from PyQt4 import QtGui
  6. from picamera.array import PiRGBArray
  7. from picamera import PiCamera
  8. from PyQt4.QtGui import QWidget
  9. from PyQt4.QtCore import pyqtSignal
  10. from imutils.video import VideoStream
  11. from imutils import face_utils
  12. import imutils
  13. import io
  14. camera_port = 0
  15.  
  16.  
  17. class ShowVideo(QtCore.QObject):
  18.  
  19. #initiating the built in camera
  20. VideoSignal = QtCore.pyqtSignal(QtGui.QImage)
  21.  
  22. def __init__(self, parent = None):
  23. super(ShowVideo, self).__init__(parent)
  24.  
  25. @QtCore.pyqtSlot()
  26. def startVideo(self):
  27. camera = PiCamera()
  28. camera.resolution=(640,480)
  29.  
  30. rawCapture = PiRGBArray(camera,size=(640, 480))
  31. for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
  32. image = frame.array
  33. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  34. qt_image = QtGui.QImage(image, image.shape[1], image.shape[0], QtGui.QImage.Format_RGB888)
  35.  
  36.  
  37.  
  38. ## pixmap = QtGui.QPixmap.fromImage(qt_image)
  39. ## qt_image = pixmap.scaled(320, 240, QtCore.Qt.KeepAspectRatio)
  40. qt_image = QtGui.QImage(qt_image)
  41.  
  42. self.VideoSignal.emit(qt_image)
  43. rawCapture.truncate(0)
  44.  
  45.  
  46.  
  47. class ImageViewer(QWidget):
  48. def __init__(self, parent = None):
  49. super(ImageViewer, self).__init__(parent)
  50. self.image = QtGui.QImage()
  51. self.setAttribute(QtCore.Qt.WA_OpaquePaintEvent)
  52.  
  53.  
  54.  
  55. def paintEvent(self, event):
  56. painter = QtGui.QPainter(self)
  57. painter.drawImage(0,0, self.image)
  58. self.image = QtGui.QImage()
  59.  
  60. def initUI(self):
  61. self.setWindowTitle('Test')
  62.  
  63.  
  64. @QtCore.pyqtSlot(QtGui.QImage)
  65. def setImage(self, image):
  66. if image.isNull():
  67. print("viewer dropped frame!")
  68.  
  69. self.image = image
  70. if image.size() != self.size():
  71. self.setFixedSize(image.size())
  72. self.update()
  73.  
  74.  
  75. if __name__ == '__main__':
  76.  
  77. app = QtGui.QApplication(sys.argv)
  78. thread = QtCore.QThread()
  79. thread.start()
  80.  
  81. vid = ShowVideo()
  82. vid.moveToThread(thread)
  83. image_viewer = ImageViewer()
  84. #image_viewer.resize(200,400)
  85.  
  86.  
  87. vid.VideoSignal.connect(image_viewer.setImage)
  88.  
  89. #Button to start the videocapture:
  90.  
  91. push_button = QtGui.QPushButton('Start')
  92. push_button.clicked.connect(vid.startVideo)
  93. vertical_layout = QtGui.QVBoxLayout()
  94.  
  95. vertical_layout.addWidget(image_viewer)
  96. vertical_layout.addWidget(push_button)
  97.  
  98. layout_widget = QWidget()
  99. layout_widget.setLayout(vertical_layout)
  100.  
  101. main_window = QtGui.QMainWindow()
  102. main_window.setCentralWidget(layout_widget)
  103. main_window.resize(720,480)
  104. main_window.show()
  105. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement