Closed bharatkumarreddy closed 6 years ago
@bharatkumarreddy You can use "xml.dom.minidom" in Python to create xml files. Here is an example:
import xml.dom.minidom as Dom
def create_xml_rotated_icdar13_train(root_dir, save_dir, generate_file):
generate_list=open(generate_file,'wt')
count=0
# for i in range(1000):
for root, dirs, files in os.walk(os.path.join(root_dir,'train_images/')):
for file in files:
image_name=file
label_name='gt_'+file.split('.')[0]+'.txt'
image_path=os.path.join(root_dir,'train_images/',image_name)
sub_dir='train_images'
print(image_path)
I=Image.open(image_path)
image_width,image_height=I.size
label_path=os.path.join(root_dir,'train_gts/',label_name)
label=open(label_path,'r')
doc = Dom.Document()
root_node=doc.createElement('annotation')
doc.appendChild(root_node)
folder_node=doc.createElement('folder')
folder_node_value=doc.createTextNode(sub_dir)
folder_node.appendChild(folder_node_value)
filename_node=doc.createElement('filename')
filename_value=doc.createTextNode(image_name)
filename_node.appendChild(filename_value)
size_node=doc.createElement('size')
root_node.appendChild(size_node)
width_node=doc.createElement('width')
width_value=doc.createTextNode(str(image_width))
width_node.appendChild(width_value)
height_node=doc.createElement('height')
height_value=doc.createTextNode(str(image_height))
height_node.appendChild(height_value)
depth_node=doc.createElement('depth')
depth_value=doc.createTextNode('3')
depth_node.appendChild(depth_value)
size_node.appendChild(width_node)
size_node.appendChild(height_node)
size_node.appendChild(depth_node)
first_line=True
count=0
for line in label.readlines():
if first_line:
line=line.decode('utf-8-sig').strip()
else:
line=line.strip()
print(line)
bbs=line.split(' ')
xmin=int(bbs[0])
ymin=int(bbs[1])
xmax=int(bbs[2])
ymax=int(bbs[3])
x1=xmin
x2=xmax
x3=xmax
x4=xmin
y1=ymin
y2=ymin
y3=ymax
y4=ymax
xmin=str(min(x1,x2,x3,x4))
ymin=str(min(y1,y2,y3,y4))
xmax=str(max(x1,x2,x3,x4))
ymax=str(max(y1,y2,y3,y4))
x1=str(x1)
x2=str(x2)
x3=str(x3)
x4=str(x4)
y1=str(y1)
y2=str(y2)
y3=str(y3)
y4=str(y4)
content=bbs[4]
content=''.join(ch for ch in content if ch.isalnum())
content=content.upper()
if content=='':
content='###'
difficult='1'
else:
difficult='0'
object_node=doc.createElement('object')
root_node.appendChild(object_node)
name_node=doc.createElement('name')
name_value=doc.createTextNode('text')
name_node.appendChild(name_value)
content_node=doc.createElement('content')
content_value=doc.createTextNode(content)
content_node.appendChild(content_value)
difficult_node=doc.createElement('difficult')
difficult_value=doc.createTextNode(difficult)
difficult_node.appendChild(difficult_value)
bndbox_node=doc.createElement('bndbox')
object_node.appendChild(bndbox_node)
x1_node=doc.createElement('x1')
x1_value=doc.createTextNode(x1)
x1_node.appendChild(x1_value)
y1_node=doc.createElement('y1')
y1_value=doc.createTextNode(y1)
y1_node.appendChild(y1_value)
x2_node=doc.createElement('x2')
x2_value=doc.createTextNode(x2)
x2_node.appendChild(x2_value)
y2_node=doc.createElement('y2')
y2_value=doc.createTextNode(y2)
y2_node.appendChild(y2_value)
x3_node=doc.createElement('x3')
x3_value=doc.createTextNode(x3)
x3_node.appendChild(x3_value)
y3_node=doc.createElement('y3')
y3_value=doc.createTextNode(y3)
y3_node.appendChild(y3_value)
x4_node=doc.createElement('x4')
x4_value=doc.createTextNode(x4)
x4_node.appendChild(x4_value)
y4_node=doc.createElement('y4')
y4_value=doc.createTextNode(y4)
y4_node.appendChild(y4_value)
xmin_node=doc.createElement('xmin')
xmin_value=doc.createTextNode(xmin)
xmin_node.appendChild(xmin_value)
ymin_node=doc.createElement('ymin')
ymin_value=doc.createTextNode(ymin)
ymin_node.appendChild(ymin_value)
xmax_node=doc.createElement('xmax')
xmax_value=doc.createTextNode(xmax)
xmax_node.appendChild(xmax_value)
ymax_node=doc.createElement('ymax')
ymax_value=doc.createTextNode(ymax)
ymax_node.appendChild(ymax_value)
bndbox_node.appendChild(x1_node)
bndbox_node.appendChild(y1_node)
bndbox_node.appendChild(x2_node)
bndbox_node.appendChild(y2_node)
bndbox_node.appendChild(x3_node)
bndbox_node.appendChild(y3_node)
bndbox_node.appendChild(x4_node)
bndbox_node.appendChild(y4_node)
bndbox_node.appendChild(xmin_node)
bndbox_node.appendChild(ymin_node)
bndbox_node.appendChild(xmax_node)
bndbox_node.appendChild(ymax_node)
object_node.appendChild(difficult_node)
object_node.appendChild(content_node)
object_node.appendChild(name_node)
object_node.appendChild(bndbox_node)
count=count+1
root_node.appendChild(folder_node)
root_node.appendChild(filename_node)
root_node.appendChild(size_node)
for i in range(0,count):
root_node.appendChild(object_node)
if count>0:
xml_file_path=save_dir+image_name[0:len(image_name)-4]+'.xml'
f = open(xml_file_path, 'w+')
f.write(doc.toprettyxml(indent = "\t", newl = "\n", encoding = "utf-8"))
f.close()
generate_list.write(image_path+' '+xml_file_path+'\n')
label.close()
generate_list.close()
Do you have recreate the new XML? I hope that there is a chance to ask you , and my qq is 1323369151, looking forward for your adding my qq ! Thank you very much!@ bharatkumarreddy
Ground truth can be directly converted to xml by the code ? @MhLiao
@19931991 Yes, it is suitable for the ground truth of ICDAR 2013. For other formats such as ICDAR 2015, you should modify the code to get the corresponding coordinates.
Thanks for your reply ! I change the input path to recreate the xml . but it creates nothing , the file is null , this is my code of xml , I want to know where is the problem ?@MhLiao
I have solved the problem, the net is running , thank you very much!@MhLiao
I also find nothing as a result. @19931991 how did you solve the problem ?
Hi,
Is there any tool to recreate the XML in Pascal VOC format. I have found few on github but they are for Rectangles. Can u suggest any ?