2

The Python-programs function is to find the brightest light with a webcam and send X,Y-coordinates to a arduino program. This is to follow the sun with a camera.

The the problem is that the memory increases until the program freezes. I've tried to deleve all the variables after use. But it's still using too much memory.

os: Windows 7

How can I find the leak, and stop it?

Here's the python code:

import cv
import traceback
import serial



try:
    cv.NamedWindow("CameraFeed", 1)
    capture = cv.CaptureFromCAM(0)
    cv.NamedWindow("Altered", 1)


except:
    cv.DestroyWindow("CameraFeed")
    cv.DestroyWindow("Altered")
    traceback.print_exc()

ser = serial.Serial("COM15",9600)



def repeat():
    imga = cv.QueryFrame(capture)
    img = cv.CreateImage(cv.GetSize(imga),8,3)
    cv.Smooth(imga, img, cv.CV_BLUR, 3)     


    thresholded_img =  cv.CreateImage(cv.GetSize(img), 8, 1)
    del(img)


    minv = cv.Scalar(250,250,250, 0)
    maxv = cv.Scalar(255,255,255, 0)
    cv.InRangeS(imga, minv, maxv, thresholded_img) 
    del(minv)
    del(maxv)        
            #determine the objects  moments and check that the area is large  
            #enough to be our object
    moments = cv.Moments(cv.GetMat(thresholded_img,1), 0) 
    area = cv.GetCentralMoment(moments, 0, 0)

            #there can be noise in the video so ignore objects with small areas 


    if(area > 10000):

                #determine the x and y coordinates of the center of the object 
                #we are tracking by dividing the 1, 0 and 0, 1 moments by the area 
            x = cv.GetSpatialMoment(moments, 1, 0)/area 
            y = cv.GetSpatialMoment(moments, 0, 1)/area 
            del(moments)
            del(area)
                #print 'x: ' + str(x) + ' y: ' + str(y) + ' area: ' + str(area) 

                #create an overlay to mark the center of the tracked object 
            overlay = cv.CreateImage(cv.GetSize(imga), 8, 3) 

            cv.Circle(imga, (int(x), int(y)), 5, (255, 0, 0), 20) 
            cv.Add(imga, overlay, imga)
            del(overlay)
                #add the thresholded image back to the img so we can see what was  
                #left after it was applied 
            cv.Merge(thresholded_img, None, None, None, imga)

            print 'X: ',x
            print 'Y: ',y
            print ' '
            if x < 300:
                print "left"    
            if x > 340:
                print "Right"
            if y < 210:
                print "Up"
            if y > 250:
                print "Down"

            ser.write('%03i%03i\r\n'% (int(x),int(y))) 







    else:
            ser.write('%03i%03i\r\n'% (320,240)) 



    cv.ShowImage("CameraFeed", thresholded_img)
    cv.ShowImage("Altered", imga)
    del(imga)
    del(thresholded_img)


try:
    while True:
        repeat()
        if cv.WaitKey(10) == 13:
            break
except:
    cv.DestroyWindow("CameraFeed")
    cv.DestroyWindow("Altered")
    ser.close()
    traceback.print_exc()

cv.DestroyWindow("CameraFeed")
cv.DestroyWindow("Altered")
ser.close()
del(serial)
exit()

2 Answers2

3

It is a known bug of GetMat -> http://code.opencv.org/issues/1579

Nilson Jr
  • 31
  • 1
  • Do you have any idea how to fix the bug? – user1384392 May 19 '12 at 15:29
  • They don't seem to be working in this bug right now. For now, I think you'll have to find and fix the bug on the python wrapper code yourself. If you do it, let me know, cause this bug is annoying me too... – Nilson Jr May 23 '12 at 15:09
1

The code looks correct (example how to do it). It's not necessary to delete the variables; they will all be deleted when repeat() returns.

My guess is that there is a bug in the version of OpenCV that you're using. Try a different version. Consider to report the bug if the behavior happens in the latest version but not in an older one.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Thank you for responding! I tried to re-install a later version of openCV. But it was the same problem. I`ve done some research and it seems that I need to release the frame after cv.QueryFrame or else it leaks. but I don`t know how to do that. – user1384392 May 09 '12 at 14:22
  • Nope. The documentation says "The returned image should not be released or modified by the user." Your problem is that you create lots of images with `CreateImage` and you never free those. Create the image objects once and then reuse them. – Aaron Digulla May 09 '12 at 15:27
  • And you can check the methods that an image has with `println dir(img)`; maybe there is a free/destroy method. – Aaron Digulla May 09 '12 at 15:29
  • Now I`ve tried several versions of OpenCv, no difference. The problem is I constantly need new images, so I can`t reuse the image objects. I also checked the image methods, but I could not find anything I could use. – user1384392 May 19 '12 at 15:00