只需修改读写地址即可
:
# -*- coding:utf-8 -*-
from lxml.etree import Element, SubElement, tostring
from xml.dom.minidom import parseString
import json
import os
import cv2
import codecs
import numpy as np
def save_xml(image_name, bbox, save_dir='/home/weidu/zhourixin/shijie/AdvSemiSeg-master/dataset/VOC2012/Annotations', width=256, height=256, channel=3):
# 将CSV中的一行
# 000000001.jpg [[1,2,3,4],...]
# 转化成
# 000000001.xml
#
# :param image_name:图片名
# :param bbox:对应的bbox
# :param save_dir:
# :param width:这个是图片的宽度,博主使用的数据集是固定的大小的,所以设置默认
# :param height:这个是图片的高度,博主使用的数据集是固定的大小的,所以设置默认
# :param channel:这个是图片的通道,博主使用的数据集是固定的大小的,所以设置默认
# :return:
#
node_root = Element('annotation')
node_folder = SubElement(node_root, 'folder')
node_folder.text = 'JPEGImages'
node_filename = SubElement(node_root, 'filename')
node_filename.text = image_name
node_size = SubElement(node_root, 'size')
node_width = SubElement(node_size, 'width')
node_width.text = '%s' % width
node_height = SubElement(node_size, 'height')
node_height.text = '%s' % height
node_depth = SubElement(node_size, 'depth')
node_depth.text = '%s' % channel
node_folder = SubElement(node_root, 'segmented')
node_folder.text = '1'
# for x, y, w, h in list(bbox):
x, y, w, h = 1, 2, 3, 4
left, top, right, bottom = x, y, x + w, y + h
node_object = SubElement(node_root, 'object')
node_name = SubElement(node_object, 'name')
node_name.text = 'brain'
node_difficult = SubElement(node_object, 'truncated')
node_difficult.text = '0'
node_difficult = SubElement(node_object, 'difficult')
node_difficult.text = '0'
node_bndbox = SubElement(node_object, 'bndbox')
node_xmin = SubElement(node_bndbox, 'xmin')
node_xmin.text = '%s' % left
node_ymin = SubElement(node_bndbox, 'ymin')
node_ymin.text = '%s' % top
node_xmax = SubElement(node_bndbox, 'xmax')
node_xmax.text = '%s' % right
node_ymax = SubElement(node_bndbox, 'ymax')
node_ymax.text = '%s' % bottom
xml = tostring(node_root, pretty_print=True)
dom = parseString(xml)
save_xml = os.path.join(save_dir, image_name.replace('png', 'xml'))
with open(save_xml, 'wb') as f:
f.write(xml)
return
def change2xml(label_dict={}):
for image in label_dict.keys():
image_name = os.path.split(image)[-1]
bbox = label_dict.get(image, [])
save_xml(image_name, bbox)
return
if __name__ == '__main__':
# step 2
# make_voc_dir()
# step 3
# label_dict = utils.read_csv(csv_path=r'./train_b.csv',
# pre_dir=r'/home/matthew/dataset')
# rename_image(label_dict)
# step 3
# label_dict = utils.read_csv(csv_path=r'./rename_train_b.csv',
# pre_dir=r'/home/matthew/VOC2007/JPEGImages')
root_path = '/home/weidu/zhourixin/shijie/images/'
imagelist = os.listdir(os.path.join(root_path))
bbox = [1, 2, 3, 4]
for image_name in imagelist:
save_xml(image_name, bbox)