fld to xml and xml to fld
#coding=utf-8
#将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'dataVOC2012JPEGImages*.jpg'): # # print(file) # print(os.path.split(file)[1].split('.')[0]) # exit() for file in glob.glob(r'dataVOC2012JPEGImages*.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'dataVOC2012Annotations'+ 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'dataVOC2012' annot_path = os.path.join(data_path,'Annotations') imgs_path = os.path.join(data_path,'JPEGImages') visualise=True for file in glob.glob(r'dataVOC2012Annotations*.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) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |