python-imagesearch
python-imagesearch copied to clipboard
Leak of memory
After many 'call' to "imagesearch" i got the error message :
pos = imagesearch(nom_xxx, PREC_yyy) File "......", line nn, in imagesearch res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED) cv2.error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\core\src\alloc.cpp:72: error: (-4:Insufficient memory) Failed to allocate 14133848 bytes in function 'cv::OutOfMemoryError'
From my point of view, this memory leak is due to opencv cv2. Then this problem is solved by adding the following lines of Python code in "imagesearch" :
import gc # import the garbage collector, at the beginning .................... def imagesearch(image, precision=0.8): .................... ....................min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) del img_gray # free memory del template del res gc.collect() del gc.garbage[:]
Then , there is also about the same problem with "click_image".
del img_gray # free memory del template del res gc.collect() del gc.garbage[:]
Thank you for this!