Guest User

Untitled

a guest
Jun 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. # imports
  2. import sys
  3. import cv2
  4. import datetime
  5. from PyQt5.QtWidgets import *
  6. from PyQt5.QtGui import *
  7. from PyQt5.QtCore import *
  8.  
  9.  
  10. # custom imports
  11. import arconfig
  12.  
  13. cam = cv2.VideoCapture(arconfig.cap1)
  14.  
  15. app = QApplication([])
  16. screen_resolution = app.desktop().screenGeometry()
  17. winwidth, winheight = screen_resolution.width(), screen_resolution.height()
  18. vidwith = winwidth * 0.84
  19. vidheight = vidwith * 0.5625
  20.  
  21. class Ui_MainWindow(object):
  22. def setupUi(self, MainWindow):
  23. MainWindow.setObjectName("MainWindow")
  24. MainWindow.setFixedSize(winwidth, winheight)
  25. MainWindow.setWindowTitle("ARCV")
  26.  
  27. self.centralWidget = QWidget(MainWindow)
  28. self.centralWidget.setObjectName("Video")
  29. self.label = QLabel(self.centralWidget)
  30. self.label.setGeometry(winwidth*0.01, winwidth*0.01, vidwith, vidheight)
  31. self.label.setFixedSize(vidwith, vidheight)
  32. self.label.setScaledContents(True)
  33. self.label.setObjectName("Video")
  34. MainWindow.setCentralWidget(self.centralWidget)
  35.  
  36. self.dockWidget = QDockWidget(MainWindow)
  37. self.dockWidget.setFixedSize(winwidth * 0.14, winheight)
  38. self.dockWidget.setLayoutDirection(Qt.LeftToRight)
  39. self.dockWidget.setFeatures(QDockWidget.DockWidgetMovable)
  40. self.dockWidget.setAllowedAreas(Qt.LeftDockWidgetArea|Qt.RightDockWidgetArea)
  41. self.dockWidget.setObjectName("dockWidget")
  42.  
  43. self.dockWidgetContents = QWidget()
  44. self.dockWidgetContents.setObjectName("dockWidgetContents")
  45.  
  46. self.groupBox = QGroupBox(self.dockWidgetContents)
  47. self.groupBox.setGeometry(QRect(winwidth * 0.01, winwidth * 0.01, winwidth * 0.12, winheight * 0.7))
  48. self.groupBox.setObjectName("groupBox")
  49. self.groupBox.setTitle("Options")
  50.  
  51. self.startRecButton = QPushButton(self.groupBox)
  52. self.startRecButton.setGeometry(QRect(winwidth * 0.01, winwidth * 0.02, winwidth * 0.10, winwidth * 0.025))
  53. self.startRecButton.setObjectName("startRecButton")
  54. self.startRecButton.setText("Start Recording")
  55.  
  56. self.stopRecButton = QPushButton(self.groupBox)
  57. self.stopRecButton.setGeometry(QRect(winwidth * 0.01, winwidth * 0.05, winwidth * 0.10, winwidth * 0.025))
  58. self.stopRecButton.setObjectName("stopRecButton")
  59. self.stopRecButton.setText("Stop Recording")
  60.  
  61. self.settingsButton = QPushButton(self.groupBox)
  62. self.settingsButton.setGeometry(QRect(winwidth * 0.01, winwidth * 0.08, winwidth * 0.10, winwidth * 0.025))
  63. self.settingsButton.setObjectName("settingsButton")
  64. self.settingsButton.setText("Settings")
  65.  
  66. self.cam1radioButton = QRadioButton(self.groupBox) ### Here the radiobuttons start
  67. self.cam1radioButton.setGeometry(QRect(winwidth * 0.02, winwidth * 0.12, winwidth * 0.10, winwidth * 0.02))
  68. self.cam1radioButton.setObjectName("cam1radioButton")
  69. self.cam1radioButton.toggle()
  70. self.cam1radioButton.clicked.connect(self.switchCam)
  71. self.cam1radioButton.setText("Camera 1")
  72.  
  73. self.cam2radioButton = QRadioButton(self.groupBox)
  74. self.cam2radioButton.setGeometry(QRect(winwidth * 0.02, winwidth * 0.15, winwidth * 0.10, winwidth * 0.02))
  75. self.cam2radioButton.setObjectName("cam2radioButton")
  76. self.cam2radioButton.clicked.connect(self.switchCam)
  77. self.cam2radioButton.setText("Camera 2")
  78.  
  79. self.cam3radioButton = QRadioButton(self.groupBox)
  80. self.cam3radioButton.setGeometry(QRect(winwidth * 0.02, winwidth * 0.18, winwidth * 0.10, winwidth * 0.02))
  81. self.cam3radioButton.setObjectName("cam3radioButton")
  82. self.cam3radioButton.clicked.connect(self.switchCam)
  83. self.cam3radioButton.setText("Camera 3")
  84.  
  85. self.dockWidget.setWidget(self.dockWidgetContents)
  86. MainWindow.addDockWidget(Qt.DockWidgetArea(2), self.dockWidget)
  87.  
  88.  
  89.  
  90. def switchCam(self): ###Here is where I try to switch cams
  91. global cam
  92. if self.cam1radioButton.isChecked():
  93. cam = cv2.VideoCapture(arconfig.cap1)
  94. elif self.cam2radioButton.isChecked():
  95. cam = cv2.VideoCapture(arconfig.cap2)
  96. elif self.cam3radioButton.isChecked():
  97. cam = cv2.VideoCapture(arconfig.cap3)
  98.  
  99. class Thread(QThread):
  100. changePixmap = pyqtSignal(QImage)
  101.  
  102. def __init__(self, *args, **kwargs):
  103. QThread.__init__(self, *args, **kwargs)
  104. self.flag = False
  105.  
  106. def run(self):
  107. self.flag = True
  108. while self.flag:
  109. ret, frame = cam.read()
  110. if ret:
  111. cv2.putText(frame, datetime.datetime.now().strftime("%d-%m-%Y--%H-%M-%S"),
  112. (30, 60), arconfig.font, 3, (0, 0, 0), 4, cv2.LINE_AA)
  113. rgb_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  114. cvt2qt = QImage(rgb_image.data, rgb_image.shape[1], rgb_image.shape[0], QImage.Format_RGB888)
  115. self.changePixmap.emit(cvt2qt)
  116.  
  117. def stop(self):
  118. cam.release()
  119. self.flag = False
  120.  
  121.  
  122. class Prog(QMainWindow, Ui_MainWindow):
  123. def __init__(self):
  124. super().__init__()
  125. self.setupUi(self)
  126. self.th = Thread(self)
  127. self.th.changePixmap.connect(self.setImage)
  128. self.th.start()
  129.  
  130. @pyqtSlot(QImage)
  131. def setImage(self, image):
  132. self.label.setPixmap(QPixmap.fromImage(image))
  133.  
  134. def closeEvent(self, event):
  135. reply = QMessageBox.question(self, "Close ARCV", "Are you sure to quit the ARCV user interface?",
  136. QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
  137. if reply == QMessageBox.Yes:
  138. event.accept()
  139. self.th.stop()
  140. quit()
  141. super().closeEvent(event)
  142. else:
  143. event.ignore()
  144.  
  145.  
  146. if __name__=='__main__':
  147. Program = QApplication(sys.argv)
  148. MyProg = Prog()
  149. MyProg.show()
  150. sys.exit(Program.exec_())
  151.  
  152. # define video streams ### These are the file paths which are interchanged
  153. cap1 = "single.mp4"
  154. cap2 = "test1.mp4"
  155. cap3 = "test2.mp4"
  156.  
  157. # define file destination for automatically saved videos
  158. auto_dest = "./captured/"
  159.  
  160. # define file destination for manually recorded videos
  161. man_dest = "./records/"
  162.  
  163. # define OpenCV system font
  164. font = cv2.FONT_HERSHEY_PLAIN
  165.  
  166. # define output format and name format
  167. fourcc = cv2.VideoWriter_fourcc('a', 'v', 'c', '1')
  168.  
  169. # pre-defined variables
  170. # in case of movement add (n) frames at beginning and end of video output
  171. lenPrequel = 90
  172. lenSequel = 120
  173.  
  174. # minimum picture-area changing to detect movement
  175. min_area = 500
  176.  
  177. # minimal difference between average and gray frame
  178. delta_thresh = 5
Add Comment
Please, Sign In to add comment