简体   繁体   中英

Raspberry pi, python, open cv memory leak?

Running this python code on the Raspery Pi will cause the pi to become unstable after a few hours. I think there is a memory leak or some resource not being freed. I'm very new to python.

#initialise pygame
pygame.init()
pygame.camera.init()
cam = pygame.camera.Camera("/dev/video0",(width,height))
cam.start()

....

# Read the image we have presaved as an alert image
# and convert it to greyscale and blur it
alertimage = cv2.imread('./alert/alert.jpg')
alertgray = cv2.cvtColor(alertimage, cv2.COLOR_RGBA2GRAY)
alertgray = cv2.GaussianBlur(alertgray, (21, 21), 0)    

# Compare a given image to the saved image to and return true if
# they are the same
def IsAlert( image ):
    global alertgray

    gray = cv2.cvtColor(image, cv2.COLOR_RGBA2GRAY)
    gray = cv2.GaussianBlur(gray, (21, 21), 0)

    frameDelta = cv2.absdiff(alertgray, gray)
    thresh = cv2.threshold(frameDelta, 40, 255, cv2.THRESH_BINARY)[1]   
    thresh = cv2.dilate(thresh, None, iterations=2)
    (_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    # loop over the contours
    for c in cnts:
        # if the contour is large enough
        if cv2.contourArea(c) > 1600:
            return 0
    return 1

# Main loop ####################################################
while True:

    # Get an image from tpygame and save it to ram disk
    # im doing this beacuse I can't figure our how to convert
    # pygame image to cv2 image so I save it and read it back
    imageS = cam.get_image()    
    pygame.image.save(imageS,'/var/ramdsk/picture.jpg')

    # Read the image I just saved
    image = cv2.imread('/var/ramdsk/picture.jpg')

    # Compare the image to a standard image that I have presaved
    alert = IsAlert( image )

    # Convert the image to grey and blur it
    gray = cv2.cvtColor(image, cv2.COLOR_RGBA2GRAY)
    gray = cv2.GaussianBlur(gray, (21, 21), 0)  

    if lastgray is None:
        lastgray = gray

    # See what has changed...
    frameDelta = cv2.absdiff(lastgray, gray)
    thresh = cv2.threshold(frameDelta, 40, 255, cv2.THRESH_BINARY)[1]   
    thresh = cv2.dilate(thresh, None, iterations=2)
    (_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    # loop over the contours
    waschange = change
    for c in cnts:
        # if the contour is large enough
        if cv2.contourArea(c) > 1600:
            print "Area: ",cv2.contourArea(c)
            change = change + 1
            same = 0
            break

    # If the image is 
    if change == waschange:
        same = same + 1

    # If the image has settled after changing then it's time to 
    # capture it by moving the saved version to another directory
    if (change > 0 and same > 3) or init == 0:
        fileout = '/home/pi/Desktop/CamManager/clips_new/0x{}L-{}-{}.jpg'.format(mac,t,alert)
        shutil.move('/var/ramdsk/picture.jpg',fileout)
        change = 0
        same = 0
        init = 1
        print "Saving New Still",fileout

    lastgray = gray

cam.stop()

In a similar script I solved a problem with frame.truncate(0)

camera = picamera.PiCamera()
....
camera.capture(frame, format='bgr', use_video_port=True)
....
frame.truncate(0)

Sorry for posting so much code but I am not exactly sure where the resource leak is. After a few hours I can not open a new shell on the pi and I think it's because there aren't enough resources.

You can pinpoint/check memory leaks yourself , using the following tools:

  • The first tool to use is guppy/heapy - which will track all objects inside Python's memory

  • For long-running systems, use dowser - which allows live objects introspection

  • RAM usage is demystified with memory_profiler

See my presentation .

BTW, SO has quite a few entries explaining how to use above tools to track memory leaks (GIYF).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM