700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > yolo数据集txt标注转voc数据集xml标注格式

yolo数据集txt标注转voc数据集xml标注格式

时间:2019-04-22 16:13:38

相关推荐

yolo数据集txt标注转voc数据集xml标注格式

yolo数据集txt标注格式为:

0 0.159375 0.552083 0.121875 0.381944

0 0.776953 0.747222 0.099219 0.361111

代码如下:

import cv2import osxml_head = '''<annotation><folder>VOC</folder><!--文件名--><filename>{}</filename><source><database>The VOC Database</database><annotation>PASCAL VOC</annotation><image>flickr</image><flickrid>325991873</flickrid></source><owner><flickrid>null</flickrid><name>null</name></owner> <size><width>{}</width><height>{}</height><depth>{}</depth></size><segmented>0</segmented>'''xml_obj = '''<object> <name>{}</name><pose>Rear</pose><!--是否被裁减,0表示完整,1表示不完整--><truncated>0</truncated><!--是否容易识别,0表示容易,1表示困难--><difficult>0</difficult><!--bounding box的四个坐标--><bndbox><xmin>{}</xmin><ymin>{}</ymin><xmax>{}</xmax><ymax>{}</ymax></bndbox></object>'''xml_end = '''</annotation>'''labels = ['phone']#label for datasetscnt = 0txt_path=os.path.join('/home/xiaobumi/dataset/imagestxt/')#yolo存放txt的文件目录image_path=os.path.join('/home/xiaobumi/dataset/images/')#存放图片的文件目录path=os.path.join('/home/xiaobumi/dataset/Annotations/')#存放生成xml的文件目录for(root,dirname,files) in os.walk(image_path):#遍历图片文件夹for ft in files:ftxt=ft.replace('jpg','txt')#ft是图片名字+扩展名,将jpg和txt替换fxml=ft.replace('jpg','xml')xml_path=path+fxmlobj = ''img = cv2.imread(root+ft)img_h,img_w = img.shape[0],img.shape[1]head = xml_head.format(str(fxml),str(img_w),str(img_h),3)with open(txt_path+ftxt,'r') as f:#读取对应txt文件内容for line in f.readlines():yolo_datas = line.strip().split(' ')label = int(float(yolo_datas[0].strip()))center_x = round(float(str(yolo_datas[1]).strip()) * img_w)center_y = round(float(str(yolo_datas[2]).strip()) * img_h)bbox_width = round(float(str(yolo_datas[3]).strip()) * img_w)bbox_height = round(float(str(yolo_datas[4]).strip()) * img_h)xmin = str(int(center_x - bbox_width / 2 ))ymin = str(int(center_y - bbox_height / 2))xmax = str(int(center_x + bbox_width / 2))ymax = str(int(center_y + bbox_height / 2))obj += xml_obj.format(labels[label],xmin,ymin,xmax,ymax)with open(xml_path,'w') as f_xml:f_xml.write(head+obj+xml_end)cnt += 1print(cnt)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。