大佬们,有人碰到下面 这样的情况吗?
def cv2_image(path): img = cv2.imread(path) return img
for i in file_path: img = cv2_img(i)
重复的调用 cv2_img()读图,然后内存爆掉。
1
chesterzzy 2020-10-14 19:30:50 +08:00
不该爆吗?
|
2
allenwuli OP @chesterzzy 不该爆啊
|
3
xiaoming1992 2020-10-14 23:26:37 +08:00 via Android
不停往内存读,垃圾不能回收,肯定要爆啊
|
4
allenwuli OP @xiaoming1992 一批一批的读图的,一批处理完结束。再读下一批,变量有释放,引用计数为 0 了,但是还是占着内存。用 gc.collect(),都不能释放。这应该是 opencv 的 bug,python 的 opencv 不知道怎么解决,c 语言的可以使用 release()。
|
5
tkmiles 2020-10-15 10:22:09 +08:00
@allenwuli
import cv2 def main(): def cv2_image(path): img = cv2.imread(path) return img p = r"c:\miyou.jpg" file_path = [p] * 1000 for i in file_path: img = cv2_image(i) print("done") return if __name__ == "__main__": main() 单独这么测内存没泄露呀 windows python3.6 opencv-python-3.3.3 |
6
czdpzc 2020-10-15 11:07:50 +08:00
同测没爆内存
Python 3.7.7 opencv-python 4.3.0 |
8
czdpzc 2020-10-15 15:29:18 +08:00
@allenwuli
5 楼那么测应该是对的。但我也用 CelebA 数据库试了一下,202599 张人脸图像,一切正常。 ``` import cv2 as cv import os def img_read(path): img = cv.imread(path) return img def main(): path = "celebA_dataset/img_align_celeba/" imgs = os.listdir(path) print(len(imgs)) count=0 for i in imgs: img = img_read(os.path.join(path,i)) count+=1 print(count) print("done") if __name__ == "__main__": main() ``` |