将jpg和label 转化为lmdb格式



# 扫描图像和标注文件
# 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()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将LaSOT数据集转换为LMDB格式的步骤如下: 1. 首先,需要下载并解压缩LaSOT数据集。您可以从以下链接下载数据集:http://vision.cs.stonybrook.edu/~lasot/download.php 2. 然后,您需要安装LMDB库。您可以从以下链接下载并安装LMDB库:https://github.com/LMDB/lmdb 3. 接下来,您需要使用脚本将LaSOT数据集转换为LMDB格式。以下是一个示例脚本: ``` import lmdb import os import cv2 import numpy as np def prepare_lmdb_data(data_dir, lmdb_file): env = lmdb.Environment(lmdb_file, map_size=int(1e12)) txn = env.begin(write=True) for root, dirs, files in os.walk(data_dir): for file in files: if file.endswith(".jpg"): video_name = os.path.basename(root) frame_name = os.path.splitext(file)[0] img_path = os.path.join(root, file) img = cv2.imread(img_path) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 将图像转换为LMDB格式 datum = caffe.proto.caffe_pb2.Datum() datum.channels = 3 datum.height = img.shape[0] datum.width = img.shape[1] datum.data = img.tobytes() datum.label = -1 key = (video_name + "/" + frame_name).encode("ascii") txn.put(key, datum.SerializeToString()) txn.commit() env.close() if __name__ == "__main__": data_dir = "/path/to/LaSOT/dataset" lmdb_file = "/path/to/LaSOT/lmdb" prepare_lmdb_data(data_dir, lmdb_file) ``` 4. 运行脚本,将LaSOT数据集转换为LMDB格式。请注意,此过程需要一些时间和计算资源,具体取决于数据集的大小和您的计算机性能。 5. 转换完成后,您可以使用LMDB库读取和处理数据集。以下是一个示例代码片段: ``` import lmdb import caffe env = lmdb.open('/path/to/LaSOT/lmdb', readonly=True) with env.begin() as txn: cursor = txn.cursor() for key, value in cursor: datum = caffe.proto.caffe_pb2.Datum() datum.ParseFromString(value) img = np.fromstring(datum.data, dtype=np.uint8).reshape(datum.channels, datum.height, datum.width) img = img.transpose((1, 2, 0)) # 转换为RGB格式 label = datum.label # 处理图像和标签 env.close() ``` 希望这可以帮助您将LaSOT数据集转换为LMDB格式

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值