2012-05-09 15 views
2

Pythonプログラムの機能は、ウェブカメラで最も明るい光を見つけ、ArduinoプログラムにX、Y座標を送信することです。これはカメラで太陽を追跡することです。Cライブラリを使ってPythonプログラムでメモリをフラッシュするにはどうしたらいいですか?

問題は、プログラムがフリーズするまでメモリが増加することです。 私は使用後にすべての変数を削除しようとしました。しかし、まだメモリが多すぎます。

OS:Windows 7の

にはどうすれば漏れを見つけ、それを止めることができますか?

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() 
+0

"cv"とは何ですか? ... –

+0

cv(openCV)は、リアルタイム画像処理用のCライブラリです。私はウェブカメラから画像を処理するためにそれを使用しています。 – user1384392

答えて

1

コードが正しい(example how to do itを)になります。

はここでPythonのコードです。変数を削除する必要はありません。彼らはすべてrepeat()が返されたときに削除されます。

私の推測では、使用しているOpenCVのバージョンにバグがあります。別のバージョンをお試しください。最新のバージョンで動作が発生する場合、バグを報告することを検討してください。古いバージョンでは発生しません。

+0

ありがとうございました! openCVの新しいバージョンを再インストールしようとしました。しかしそれは同じ問題でした。 私はいくつかの研究を行っており、cv.QueryFrameの後にフレームをリリースする必要があると思われます。しかし私はそれをする方法を知らない。 – user1384392

+0

ドキュメンテーションには、「返された画像は、ユーザによって解放または変更されるべきではありません」と書かれています。あなたの問題は、 'CreateImage'でたくさんの画像を作成し、それらを解放しないことです。イメージオブジェクトを一度作成してから再利用してください。 –

+0

そして 'println dir(img)'でイメージの持つメソッドをチェックすることができます。おそらく、フリー/破壊の方法があります。 –

3

GetMatの既知のバグです - >http://code.opencv.org/issues/1579

+0

バグを修正する方法はありますか? – user1384392

+0

現在、このバグでは動作していないようです。 今のところ、Pythonラッパーコードのバグを見つけて修正する必要があると思います。 もしそうなら、私に知らせてください、このバグが私にも迷惑をかける原因になります... –

関連する問題