drov0 / python-imagesearch

A wrapper around opencv2 and pyautogui to do image searching easily.
MIT License
279 stars 98 forks source link

Leak of memory #12

Open Aramis7609 opened 4 years ago

Aramis7609 commented 4 years ago

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 imggray # free memory del template del res gc.collect() del gc.garbage[:]

Then , there is also about the same problem with "click_image".

kadusalles commented 4 years ago

del img_gray # free memory del template del res gc.collect() del gc.garbage[:]

Thank you for this!