def image_dataset_to_lmdb(image_folder_path,lmdb_path,posAttributeList):
db = lmdb.open(lmdb_path, map_size=2147483648)
with db.begin(write=True) as db_txn:
for parent, dirnames, filenames in os.walk(image_folder_path):
if (parent == image_folder_path):
for index, filename in enumerate(filenames, start=0):
image_path = image_folder_path + filename
# read image into numpy.ndarray
imageData=Image.open(image_path)
image_array = numpy.array(imageData)
# image type recognition
if type(image_array[0][0]) is numpy.uint8:
print(image_path + ' => image type : grayscale, this function can not handle this case, skip.')
continue
elif type(image_array[0][0]) is numpy.ndarray:
print(image_path + ' => image type : color, go.')
# RGB format to BGR format
image_array = image_array[:, :, ::-1]
#channel split by transpose matrix operation
image_array = image_array.transpose((2, 0, 1))
print image_array.shape
datum = caffe.Datum()
datum.channels=3
datum.width=imageData.size[0]
datum.height=imageData.size[1]
datum.data=image_array.tobytes()
if filename in posAttributeList:
datum.label = 1
else:
datum.label=0
db_txn.put('{:0>10d}'.format(index), datum.SerializeToString())
print "%d has been writted!"% index
if index%1000==0:
db_txn.commit()
db_txn=db.begin(write=True)
db.close()
caffe:图像数据 lmdb的写入
最新推荐文章于 2022-04-04 05:38:42 发布