#将fld框文件生成xml
'''
<annotation>
<folder>VOC2012</folder>
<filename>2007_000027.jpg</filename>
<source>
<database>The VOC2007 Database</database>
<annotation>PASCAL VOC2007</annotation>
<image>flickr</image>
</source>
<size>
<width>486</width>
<height>500</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>person</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndBox>
<xmin>174</xmin>
<ymin>101</ymin>
<xmax>349</xmax>
<ymax>351</ymax>
</bndBox>
</object>
</annotation>
'''
import codecs
import glob,os,cv2
import numpy as np
from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import SubElement
from xml.etree.ElementTree import dump
from xml.etree.ElementTree import Comment
from xml.etree.ElementTree import tostring
# for file in glob.glob(r'data\VOC2012\JPEGImages\*.jpg'):
# # print(file)
# print(os.path.split(file)[1].split('.')[0])
# exit()
for file in glob.glob(r'data\VOC2012\JPEGImages\*.jpg'):
print(file)
xml_filepath=file.split('.')[0]+'.xml'
fld_filepath=file.split('.')[0]+'.fld'
img=cv2.imread(file)
height,width,depth=np.shape(img)
print(np.shape(img))
# f_xml=codecs.open(xml_filepath,'w',encoding='utf-8')
# f_xml.writelines()
# f_xml.close()
book = ElementTree()
purchaSEOrder = Element('annotation')
book._setroot(purchaSEOrder)
SubElement(purchaSEOrder,'folder').text ='VOC2012'
SubElement(purchaSEOrder,'filename').text =os.path.split(file)[1]
source = Element("source",)
SubElement(source,'database').text ='The VOC2007 Database'
SubElement(source,'annotation').text ='PASCAL VOC2007'
SubElement(source,'image').text ='flickr'
purchaSEOrder.append(source)#
size = Element("size",)
SubElement(size,'width').text =str(width)
SubElement(size,'height').text =str(height)
SubElement(size,'depth').text ='3'
purchaSEOrder.append(size)#
f_fld=codecs.open(fld_filepath,'r')
lines=f_fld.readlines()
f_fld.close()
for line in lines:
object = Element("object",)
SubElement(object,'name').text ='1'
SubElement(object,'difficult').text ='0'
bndBox = Element("bndBox",)
SubElement(bndBox,'xmin').text = str(line.split('\t')[1])
SubElement(bndBox,'ymin').text = str(line.split('\t')[2])
SubElement(bndBox,'xmax').text = str(line.split('\t')[3])
SubElement(bndBox,'ymax').text = str(line.split('\t')[4])
object.append(bndBox)#
purchaSEOrder.append(object)#
# print(book)
book.write(r'data\VOC2012\Annotations\\'+
os.path.split(xml_filepath)[1])
xml to fld:
import xml.etree.ElementTree as ET import codecs import glob,cv2 import numpy as np data_path=r'data\VOC2012\\' annot_path = os.path.join(data_path,'Annotations') imgs_path = os.path.join(data_path,'JPEGImages') visualise=True for file in glob.glob(r'data\VOC2012\Annotations\*.xml'): print(file) xml_filepath=file.split('.')[0]+'.xml' fld_filepath=file.split('.')[0]+'.fld' et = ET.parse(file) element = et.getroot() element_objs = element.findall('object') element_filename = element.find('filename').text element_width = int(element.find('size').find('width').text) element_height = int(element.find('size').find('height').text) if len(element_objs) > 0: # annotation format 封装后的注释格式 annotation_data = {'filepath': os.path.join(imgs_path,element_filename),'width': element_width,'height': element_height,'bBoxes': []} # if element_filename in trainval_files: # annotation_data['imageset'] = 'trainval' # elif element_filename in test_files: # annotation_data['imageset'] = 'test' # else: # annotation_data['imageset'] = 'trainval' for element_obj in element_objs: class_name = element_obj.find('name').text # if class_name not in classes_count: # classes_count[class_name] = 1 # else: # classes_count[class_name] += 1 # # if class_name not in class_mapping: # class_mapping[class_name] = len(class_mapping) # print('*****************************') obj_bBox = element_obj.find('bndBox') x1 = int(round(float(obj_bBox.find('xmin').text))) y1 = int(round(float(obj_bBox.find('ymin').text))) x2 = int(round(float(obj_bBox.find('xmax').text))) y2 = int(round(float(obj_bBox.find('ymax').text))) difficulty = int(element_obj.find('difficult').text) == 1 # annotation format of bounding Box 矩形框的封装格式 annotation_data['bBoxes'].append( {'class': class_name,'x1': x1,'x2': x2,'y1': y1,'y2': y2,'difficult': difficulty}) # all_imgs.append(annotation_data) # if len(all_imgs) > 2: # visualise = False if visualise: img = cv2.imread(annotation_data['filepath']) for bBox in annotation_data['bBoxes']: cv2.rectangle(img,(bBox['x1'],bBox['y1']),(bBox['x2'],bBox['y2']),(0,255)) textOrg = (bBox['x1'],bBox['y1']+10) cv2.putText(img,bBox['class'],textOrg,cv2.FONT_HERSHEY_PLAIN,1,200),1) cv2.imshow('img',img) cv2.waitKey(0)