1

I'm trying to save video files on a raspberry pi with python2.7 and opencv. The code shown below consistently saves several video files (size 16-18 Mb) to a usb but after the first few files the file sizes drop to 6 kb and appear to be empty since they won't open.

I opened task manager to monitor the memory usage by python during the saving and noticed that the RSS memory continually increases until roughly 200 MB which is when the video files start showing up blank.

Is that a sure indicator of a memory leak, or should I run other tests?

Is there something wrong in the below code that isn't releasing variables properly?

import cv2
import numpy as np
import datetime

dispWidth = 640
dispHeight = 480
FPS = 6 

SetupNewVideoFile = True # state variable
VidCaptureDurationMinutes = 3 


filepath = '/media/pi/9CEE-5383/Videos/'

i = 1 # counter for the video file names

fourcc = cv2.cv.CV_FOURCC('X','V','I','D') 

while True:
    # timer section that ends running file saves and triggers a new file save
    Now = datetime.datetime.now() # refresh current time
    delta = Now - VidCaptureStartTime
    print('delta: ',delta.seconds,delta.days)
    if ((delta.seconds/60) >= VidCaptureDurationMinutes) and delta.days >= 0:
        print('delta: ',delta.seconds,delta.days)
        SetupNewVideoFile = True
        Vidoutput.release()
        cap.release()

    # setting up new file saves
    if SetupNewVideoFile:
        SetupNewVideoFile = False

        title = "Video_"+str(i)+".avi"
        i += 1
        fullpath = filepath + title
        print(fullpath)

        Vidoutput  = cv2.VideoWriter(fullpath, fourcc, FPS,(dispWidth,dispHeight))
        VidCaptureStartTime = datetime.datetime.now()  # updating video start time

        cap = cv2.VideoCapture(-1) # start video capture




    ret, frame = cap.read() 
    if ret:  # display and save if a frame was successfully read
        cv2.imshow('webcam',frame)
        Vidoutput.write(frame) # save the frames

    key = cv2.waitKey(1) & 0xFF
    if key == ord('q'): # quits program
        break


# clean up 
cap.release()
Vidoutput.release()
cv2.destroyAllWindows()
cv2.waitKey(1) # these seem to be needed to flush the cv actions
cv2.waitKey(1)
cv2.waitKey(1)
cv2.waitKey(1)

1 Answers1

0

After some more trouble shooting and help from the pympler module functions and tutorials (https://pythonhosted.org/Pympler/muppy.html) my problems still looked like a memory leak but I was unable to solve the specific error.

This other S.O. post (Releasing memory in Python) mentioned improvements in memory management made in version 3.3 of Python:

"In Python 3.3 the small object allocator was switched to using anonymous memory maps instead of the heap, so it should perform better at releasing memory."

So I switched over to Python 3.3 and now the code saves valid video files well past the error out time I saw previously. This isn't an answer as to why the blank files were occurring, but at least it's a solution.

Community
  • 1
  • 1