Created
May 11, 2018 21:42
-
-
Save eyllanesc/b82683fd946940b2f575ffac716bc696 to your computer and use it in GitHub Desktop.
50295870
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import cv2 | |
from PyQt4 import QtGui, QtCore | |
class QtCapture(QtGui.QWidget): | |
def __init__(self, *args): | |
self.app = QtGui.QApplication(sys.argv) | |
super(QtGui.QWidget, self).__init__() | |
self.layout = QtGui.QGridLayout() | |
self.w = QtGui.QWidget() # Main gui object | |
self.w.setMinimumHeight(480) | |
self.capture = None | |
self.frame = None | |
self.video_frame = QtGui.QLabel() | |
self.button_start = QtGui.QPushButton('Camera Connect') | |
self.button_start.setFixedWidth(300) | |
self.button_start.clicked.connect(lambda: self.start()) | |
self.w.setLayout(self.layout) | |
self.layout.addWidget(self.button_start, 0, 0) | |
self.layout.addWidget(self.video_frame, 0, 1, 16, 1) | |
self.w.show() | |
print("Init") | |
def nextFrameSlot(self): | |
previous_frame = self.current_frame | |
ret, self.frame = self.cap.read() | |
if ret: | |
self.current_frame = self.frame | |
frame_diff = cv2.absdiff(self.current_frame, previous_frame) | |
qformat= QtGui.QImage.Format_Indexed8 | |
if len(self.frame.shape) == 3: | |
if self.frame.shape[2] == 4: | |
qformat = QtGui.QImage.Format_RGBA8888 | |
else: | |
qformat = QtGui.QImage.Format_RGB888 | |
img = QtGui.QImage(self.frame, self.frame.shape[1], self.frame.shape[0], self.frame.strides[0], qformat) | |
self.video_frame.setPixmap(QtGui.QPixmap.fromImage(img)) | |
self.video_frame.setScaledContents(True) | |
def start(self): | |
print("button_start pressed. ") | |
self.cap = cv2.VideoCapture(0) | |
if self.cap.isOpened(): | |
self.timer = QtCore.QTimer() | |
ret, self.current_frame = self.cap.read() | |
self.timer.timeout.connect(self.nextFrameSlot) | |
self.timer.start(1./10.) | |
def closeEvent(self): | |
print('closed') | |
self.cap.release() | |
super(QtGui.QWidget, self).closeEvent() | |
def main(): | |
#calibration_pts = getCalibrationCoordinates(cameraChoice) | |
sozen = QtCapture() | |
sys.exit(sozen.app.exec_()) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment