【发布时间】:2020-04-13 13:15:38
【问题描述】:
我有一个使用 MTCNN 人脸检测库的脚本,它遍历大量目录,总共有数千张图像。我在使用此脚本时遇到的一个问题是在处理所有这些图像时内存使用过多,这最终会导致我的 MacBook(16gb 的 RAM)内存不足。我要做的是逐个文件夹实现批处理,而不是特定的批处理限制,因为没有一个文件夹单独包含足够的图像,这会使系统内存不足。
# open up the csv file
with open(csv_path, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Index', 'Threshhold', 'Path'])
for path, subdirs, files in os.walk(path):
for name in files:
if name == '.DS_Store':
print("Skipping .DS_Store")
continue
else:
try:
image = os.path.join(path, name)
pixels = pyplot.imread(image)
print("Processing " + image)
print("Count: " + str(inc))
# calculate the area of the image
total_height = pixels.shape[0]
total_width = pixels.shape[1]
total_area = total_height * total_width
# create the detector, using default weights
detector = MTCNN()
faces = detector.detect_faces(pixels)
ax = pyplot.gca()
face_total_area = 0
if faces == []:
print("No faces detected.")
# pass in 0 for the threshold becuase there's no faces
#write_to_csv(inc, 0, image)
print()
else:
for face in faces:
# get dimensions from the face
x, y, width, height = face['box']
# calculate the area of the face
face_area = width * height
face_total_area += face_area
threshold = face_total_area / total_area
# write to csv only if the threshold is less than the limit
# change back to this eventually ^^^^^^^^^
if threshold > threshhold_limit:
print("Facial area is over the threshold - writing file path to csv.")
write_to_csv(inc, threshold, image)
else:
print("Image threshold is under the limit - good")
print(threshold)
print()
inc += 1
except:
print("Processing error - skipping image")
这样的事情可以做吗?还是应该以不同的方式完成?这个想法是,像这样的批处理将允许 mtcnn 在处理完该文件夹时释放它所持有的内存。
【问题讨论】:
标签: python-3.x batching