# 扫描图像和标注文件
# image_files = sorted(os.listdir(image_dir))
# label_files = sorted(os.listdir(label_dir))
# 创建LMDB数据库
lmdb_path = os.path.join(lmdb_dir, lmdb_name)
env = lmdb.open(lmdb_path, map_size=1099511627776)
# 将所有图像和标注数据插入到LMDB数据库中
with env.begin(write=True) as txn:
for label_file in jjson_file:
image_file = label_file.replace('json', 'jpg')
# 读取图像
image_path = os.path.join(image_dir, image_file)
image = cv2.imread(image_path)
if image is None:
# 图像读取失败的情况
continue
# 将图像转换为JPEG格式
_, image_jpeg_buffer = cv2.imencode('.jpg', image)
# 读取标注文件
label_path = os.path.join(label_dir, label_file)
with open(label_path, 'r') as f:
label_data = json.load(f)
# {shape:[{},{}],[{},{}]}
# 将图像和标注数据转换为LMDB格式
lut = {
'image_jpeg_buffer': base64.b64encode(image_jpeg_buffer).decode(),
'source_contour': [],
'target_contour': [],
'transform_matrix': '',
'image_height': image.shape[0],
'image_width': image.shape[1]
}
for i in label_data['shape']:
lut["source_contour"].append(i['cp1'])
lut["target_contour"].append(i['cp2'])
lut_str = json.dumps(lut)
# 将转换后的数据插入到LMDB数据库中
txn.put(image_file.encode(), lut_str.encode())
# 关闭LMDB数据库
env.close()
将jpg和label 转化为lmdb格式
最新推荐文章于 2024-07-21 22:27:18 发布