Ask Your Question
0

Memory leak while using cv2.VideoCapture()

asked 2017-03-27 10:47:16 -0600

raghavendra gravatar image

I am trying to video stream from a 1920x1080 HD camera with 60fps,using opencv. I am losing 6GB of my memory within the first minute of streaming. Any guidelines on how to fix this issue. I am using Python 3.4.4, Opencv 3.1.0, Pycharm Community edition and Pyside to generate the GUI

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 retag flag offensive close merge delete

Comments

try without any gui, i bet, there's no memleaks ..

berak gravatar imageberak ( 2017-03-27 11:06:07 -0600 )edit

But I need to embed the video in a GUI. So any suggestions to correct the code to prevent the memory leak would be helpful

raghavendra gravatar imageraghavendra ( 2017-03-27 11:07:42 -0600 )edit

I did find out where the memory leak is , its in the timer.start(30) if I increase the number , the memory leak is slow, if the number is zero, the memory leak increases.

raghavendra gravatar imageraghavendra ( 2017-03-27 11:22:30 -0600 )edit

unlikely. if you increase the sleep time, the whole thing will just run slower.

berak gravatar imageberak ( 2017-03-27 11:24:56 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-05-24 17:05:37 -0600

raghavendra gravatar image

If anyone having the same issue, please import qimage2ndarray this library and add the following changes to the function 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))
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-03-27 10:47:16 -0600

Seen: 3,455 times

Last updated: May 24 '17