2017-02-28 12 views
0

opencvを使用して、1920x1080 HDカメラから60fpsでビデオストリームを再生しようとしています。問題はストリーミングではなくメモリリークで、ストリーミングの最初の1分間で私のメモリは約6GBも失われています。これをやめる方法を教えてください。OpenCV:QPixmapを使用中にメモリリークが発生する

from PySide.QtCore import * 
    from PySide.QtGui import * 
    import cv2 
    import sys 

    class MainApp(QWidget): 

     def __init__(self): 
     QWidget.__init__(self) 
     self.video_size = QSize(1920, 1080) 
     self.setup_ui() 
     self.setup_camera() 

    def setup_ui(self): 
     """Initialize widgets. 
     """ 
     self.image_label = QLabel() 
     self.image_label.setFixedSize(self.video_size) 

     self.quit_button = QPushButton("Quit") 
     self.quit_button.clicked.connect(self.close) 

     self.main_layout = QVBoxLayout() 
     self.main_layout.addWidget(self.image_label) 
     self.main_layout.addWidget(self.quit_button) 

     self.setLayout(self.main_layout) 

    def setup_camera(self): 
     """Initialize camera. 
     """ 
     self.capture = cv2.VideoCapture(0) 
     self.capture.set(cv2.CAP_PROP_FRAME_WIDTH, self.video_size.width()) 
     self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT, self.video_size.height()) 

     self.timer = QTimer() 
     self.timer.timeout.connect(self.display_video_stream) 
     self.timer.start(30) 

    def display_video_stream(self): 
     """Read frame from camera and repaint QLabel widget. 
     """ 
     _, frame = self.capture.read() 
     frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 
     image = QImage(frame, frame.shape[1], frame.shape[0], 
        frame.strides[0], QImage.Format_RGB888) 
     self.image_label.setPixmap(QPixmap.fromImage(image)) 

if __name__ == "__main__": 
    app = QApplication(sys.argv) 
    win = MainApp() 
    win.show() 
    sys.exit(app.exec_()) 

EDIT

Iは、メモリリークがself.timer.start(30)であるIが時間呼び出しを増やす場合、メモリリークが遅い発見しました。どのようにメモリリークを停止するための任意の提案?

答えて

1

誰もが同じ問題を抱えている場合、このライブラリをインポートし、qimage2ndarray機能display_video_stream

def display_video_stream(self): 
    """Read frame from camera and repaint QLabel widget. 
    """ 

    _, frame = self.capture.read() 

    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 
    # frame = cv2.flip(frame, 1) 
    image = qimage2ndarray.array2qimage(frame) #Solution for memory leak 
    self.image_label.setPixmap(QPixmap.fromImage(image)) 
へ 次の変更を追加してください
関連する問題