VOC2012 分割数据 转 lmdb 格式 python 代码

参考
caffe 将三通道或四通道图片转换为lmdb格式,将标签(单通道灰度图)转换为lmdb格式
http://blog.csdn.net/c_qianbo/article/details/53375476

import numpy as np
import sys
from PIL import Image
import lmdb
import random
import os

sys.path.append('/home/guest/caffe/python/')

import caffe

if __name__ == '__main__' :
    train_list_file = '/home/guest/caffe/examples\
    /VOC2012ext/VOCdevkit/VOC2012/ImageSets/Segmentation/val.txt'
    train_images_root = '/home/guest/caffe/examples\
    /VOC2012ext/VOCdevkit/VOC2012/JPEGImages/'

    f = open(train_list_file, 'r')
    trainlist = f.readlines()
    f.close()

    random.shuffle(trainlist)

    # creating images lmdb
    in_db = lmdb.open('/home/guest/caffe/VOC2012ext_val_img_lmdb',\
     map_size=int(1e12))
    with in_db.begin(write=True) as in_txn :
        for in_idx, in_ in enumerate(trainlist) :
            fid = in_.strip()+'.jpg'
            fn = os.path.join(train_images_root, fid)
            im = np.array(Image.open(fn))
            Dtype = im.dtype


            im = im[:,:,::-1]
            im = Image.fromarray(im)  
            im = np.array(im, Dtype)  
            im = im.transpose((2, 0, 1)) 
            im_dat = caffe.io.array_to_datum(im)
            in_txn.put('{:0>10d}'.format(in_idx), im_dat.SerializeToString())
    in_db.close()


 # creating label lmdb
    in_db = lmdb.open('/home/guest/caffe/VOC2012ext_val_label_lmdb',\
     map_size=int(1e12))
    train_images_root = '/home/guest/caffe/examples\
    /VOC2012ext/VOCdevkit/VOC2012/SegmentationClass/'
    with in_db.begin(write=True) as in_txn :
        for in_idx, in_ in enumerate(trainlist) :
            fid = in_.strip()+'.png'
            fn = os.path.join(train_images_root, fid)
        Dtype = 'uint8'  
            L = np.array(Image.open(fn), Dtype)  
            Limg = Image.fromarray(L)  
            L = np.array(Limg,Dtype)  
            L = L.reshape(L.shape[0],L.shape[1],1)  
            L = L.transpose((2,0,1))  
            L_dat = caffe.io.array_to_datum(L)  
            in_txn.put('{:0>10d}'.format(in_idx),L_dat.SerializeToString())  
    in_db.close()
以下是将YOLO格式数据换为VOC格式数据集的Python代码示例: ```python import os import xml.etree.ElementTree as ET # Define the classes present in your dataset classes = ["class1", "class2", "class3"] def convert_yolo_to_voc(yolo_path, voc_path): """ Convert YOLO format dataset to VOC format dataset :param yolo_path: path to the YOLO dataset directory :param voc_path: path to the VOC dataset directory """ # Create the VOC dataset directory if it doesn't exist if not os.path.exists(voc_path): os.makedirs(voc_path) # Loop through all the files in the YOLO dataset directory for file_name in os.listdir(yolo_path): if file_name.endswith(".txt"): # Open the YOLO annotation file with open(os.path.join(yolo_path, file_name), "r") as f: lines = f.readlines() # Extract the image dimensions from the corresponding image file img_path = os.path.join(yolo_path, file_name.replace(".txt", ".jpg")) img = Image.open(img_path) img_width, img_height = img.size # Create a new VOC annotation file with the same name as the YOLO annotation file xml_file_name = file_name.replace(".txt", ".xml") xml_file_path = os.path.join(voc_path, xml_file_name) root = ET.Element("annotation") ET.SubElement(root, "filename").text = file_name.replace(".txt", ".jpg") size = ET.SubElement(root, "size") ET.SubElement(size, "width").text = str(img_width) ET.SubElement(size, "height").text = str(img_height) # Loop through each line in the YOLO annotation file for line in lines: class_id, x_center, y_center, box_width, box_height = map(float, line.split()) # Convert the YOLO format coordinates to VOC format coordinates x_min = int((x_center - box_width/2) * img_width) y_min = int((y_center - box_height/2) * img_height) x_max = int((x_center + box_width/2) * img_width) y_max = int((y_center + box_height/2) * img_height) # Create a new object element in the VOC annotation file object_tag = ET.SubElement(root, "object") ET.SubElement(object_tag, "name").text = classes[int(class_id)] bndbox = ET.SubElement(object_tag, "bndbox") ET.SubElement(bndbox, "xmin").text = str(x_min) ET.SubElement(bndbox, "ymin").text = str(y_min) ET.SubElement(bndbox, "xmax").text = str(x_max) ET.SubElement(bndbox, "ymax").text = str(y_max) # Write the VOC annotation file to disk tree = ET.ElementTree(root) tree.write(xml_file_path) # Call the function with the YOLO and VOC dataset directory paths yolo_path = "/path/to/yolo/dataset" voc_path = "/path/to/voc/dataset" convert_yolo_to_voc(yolo_path, voc_path) ``` 这段代码将YOLO格式数据集目录中的所有文本文件换为VOC格式的XML文件,并将它们保存到指定的目录中。每个XML文件对应于一个图像文件,其中包含图像的名称、尺寸和每个对象的边界框。如果您需要更改类别名称,请修改代码中的classes列表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值