error: Unable to allocate 141. MiB for an array with shape (6000, 8192, 3) and data type bool

最近在捣腾瓷砖表检的代码,在把缺陷画到图片上来认识缺陷的时候,出现了以下问题,按照网上的教程没有解决问题,后来想起来内存不够的时候可以清楚图片缓存,于是找到了解决问题的思路,分享给大家~
 
环境:Window10,  Visual Studio Code
 
error:Exception has occurred: _ArrayMemoryError Unable to allocate 141. MiB for an array with shape (6000, 8192, 3) and data type bool

 

 本人总结了三种解决方案:

 

方案一:修改内存配置的方法(这种方法看上去挺有道理,但是我试了没有成功)

参考资料:https://blog.csdn.net/get_py/article/details/124187372

 

方案二:清除 plt 图片缓存的方法(试验过,可以解决问题)

1 from matplotlib import pyplot as plt
2 fig = plt.figure()
3 plt.figure().clear()  # It is the same as clf
4 plt.close()  # Close a figure window
5 plt.cla()    # Clear the current axes
6 plt.clf()    # Clear the current figure

添加后的代码如图所示:

参考资料:https://www.tutorialspoint.com/how-to-clear-the-memory-completely-of-all-matplotlib-plots

 

方案三:不使用画图 plt 函数 ,而是使用保存图片的函数,如 cv2.imwrite 函数将保存图片到本地,再查看结果。

这种方案就不会产生内存不足问题,但是要依照情况选择,如果不能通过保存图片的方法来查看你想要的结果,那就不能用这种方法。在本代码中,其实 plt 函数主要用在前期的调试,因为图片数量有5000多张,所以最终的结果查看还是要通过保存图片的方式,所以在用 plt 函数调式了之后,完全可以用 cv2.imwrite 函数把图片保存到本地查看运行结果,即可以把 plt.imshow(image) 注释即可解决这个问题。 

 

posted @ 2022-08-03 10:03  ttweixiao9999  阅读(1978)  评论(0编辑  收藏  举报