openCv captureFromCam memory leak?

1.7k views Asked by At

Here is part of the code i'm having a problem with, memory usage being tracked by memory_profiler

27   48.500 MiB    0.000 MiB           logging.debug("Initialize camera...")
29   63.996 MiB   15.496 MiB           self.capture = cv.CaptureFromCAM(0)
30   78.102 MiB   14.105 MiB           cv.SetCaptureProperty(self.capture,cv.CV_CAP_PROP_EXPOSURE,self.config.trackExposure)
31   59.551 MiB  -18.551 MiB           del self.capture

creating the capture object and configuring the properties uses 29.601 MiB of memory.

But, when I delete self.capture it only reclaims 18.551 MiB.

Does it in cv2 as well...

28   48.480 MiB    0.000 MiB           logging.debug("Initialize camera...")
30   65.652 MiB   17.172 MiB           self.capture = cv2.VideoCapture(0)
31   77.883 MiB   12.230 MiB           self.capture.set(cv.CV_CAP_PROP_EXPOSURE,self.config.trackExposure)
32   59.332 MiB  -18.551 MiB           self.capture.release()
33   59.332 MiB    0.000 MiB           del self.capture
1

There are 1 answers

0
wookie4747 On

Follow up -

Couldn't find an answer to my question, so for now my work around is to not release/delete the camera until the entire script quits.

There appears to be a "memory tax" where opencv wont release some memory every time you open and close a camera.

If the buffer appears to be lagging or holding old images when you come back to it, my fix has been to "clean" the cache manually...

for x in range(3):
    garbage = self.capture.QueryFrame()

It's not elegant but it gets the job done.