下のpyqt5で書かれたwebカメラから映像を取得し保存するアプリで,最初にendボタンを押すと
'NoneType' object has no attribute 'deleteLater'
とエラーが出てしまいます.これを解決する方法を教えてくださるとありがたいです.
python3
1import sys 2from PyQt5 import QtCore, QtGui, QtWidgets 3import cv2 4import os, zipfile, datetime 5now = datetime.datetime.now() 6dirname = 'test_{0:%Y%m%d}'.format(now) 7 8class QtCapture(QtWidgets.QWidget): 9 def __init__(self, *args): 10 super(QtWidgets.QWidget, self).__init__() 11 12 self.fps = 24 13 self.cap = cv2.VideoCapture(*args) 14 15 self.video_frame = QtWidgets.QLabel() 16 lay = QtWidgets.QVBoxLayout() 17 lay.setContentsMargins(0,0,0,0) 18 lay.addWidget(self.video_frame) 19 self.setLayout(lay) 20 21 # ------ Modification ------ # 22 self.isCapturing = False 23 self.ith_frame = 1 24 # ------ Modification ------ # 25 26 def setFPS(self, fps): 27 self.fps = fps 28 29 def nextFrameSlot(self): 30 ret, frame = self.cap.read() 31 32 # ------ Modification ------ # 33 # Save images if isCapturing 34 if self.isCapturing: 35 if not(os.path.isdir(dirname)): 36 os.mkdir(dirname) 37 cv2.imwrite('./'+dirname+'/img_%05d.jpg'%self.ith_frame, frame) 38 self.ith_frame += 1 39 # ------ Modification ------ # 40 41 # My webcam yields frames in BGR format 42 frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 43 img = QtGui.QImage(frame, frame.shape[1], frame.shape[0], QtGui.QImage.Format_RGB888) 44 pix = QtGui.QPixmap.fromImage(img) 45 self.video_frame.setPixmap(pix) 46 47 def start(self): 48 self.timer = QtCore.QTimer() 49 self.timer.timeout.connect(self.nextFrameSlot) 50 self.timer.start(1000./self.fps) 51 52 def stop(self): 53 self.timer.stop() 54 55 # ------ Modification ------ # 56 def capture(self): 57 if not self.isCapturing: 58 self.isCapturing = True 59 else: 60 self.isCapturing = False 61 # ------ Modification ------ # 62 63 def deleteLater(self): 64 self.cap.release() 65 super(QtWidgets.QWidget, self).deleteLater() 66 67 68class ControlWindow(QtWidgets.QWidget): 69 def __init__(self): 70 QtWidgets.QWidget.__init__(self) 71 self.capture = None 72 73 self.start_button = QtWidgets.QPushButton('Start') 74 self.start_button.clicked.connect(self.startCapture) 75 self.quit_button = QtWidgets.QPushButton('End') 76 self.quit_button.clicked.connect(self.endCapture) 77 self.end_button = QtWidgets.QPushButton('Stop') 78 79 # ------ Modification ------ # 80 self.capture_button = QtWidgets.QPushButton('Capture') 81 self.capture_button.clicked.connect(self.saveCapture) 82 # ------ Modification ------ # 83 84 vbox = QtWidgets.QVBoxLayout(self) 85 vbox.addWidget(self.start_button) 86 vbox.addWidget(self.end_button) 87 vbox.addWidget(self.quit_button) 88 89 # ------ Modification ------ # 90 vbox.addWidget(self.capture_button) 91 # ------ Modification ------ # 92 93 self.setLayout(vbox) 94 self.setWindowTitle('Control Panel') 95 self.setGeometry(100,100,200,200) 96 self.show() 97 98 def startCapture(self): 99 if not self.capture: 100 self.capture = QtCapture(0) 101 self.end_button.clicked.connect(self.capture.stop) 102 # self.capture.setFPS(1) 103 self.capture.setParent(self) 104 self.capture.setWindowFlags(QtCore.Qt.Tool) 105 self.capture.start() 106 self.capture.show() 107 108 def endCapture(self): 109 self.capture.deleteLater() 110 self.capture = None 111 112 # ------ Modification ------ # 113 def saveCapture(self): 114 if self.capture: 115 self.capture.capture() 116 # ------ Modification ------ # 117 118 119 120if __name__ == '__main__': 121 122 import sys 123 app = QtWidgets.QApplication(sys.argv) 124 window = ControlWindow() 125 sys.exit(app.exec_()) 126
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。