Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert xml format to txt format by Python

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to convert Python from xml format to txt format". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Preface

Recently learning Yolo v5 is encountered a problem, looking for data sets are all xml files, VOC tags are xml format, and YOLO is .txt format, then the problem comes, manual extraction is certainly impossible, then can only borrow the program to solve it.

2. Analyze xml and txt data

This is a xml tree structure.

This is in txt format.

Summary:

1. Extract object- > name, bndbox- > xmin,ymin,xmax,ymin

two。 Format conversion requires formula conversion

YOLO dataset txt format:

X_center: normalized center point x coordinate

Y_center: normalized center point y coordinate

W: the width of the normalized target box

H: the height of the normalized target condition

(normalization here refers to dividing the picture by width and height.)

VOC dataset xml format

Yolo's four data xml- > txt formula x_center ((x_min+x_max) / 2-1) / w_imagey_center ((y_min+y_max) / 2-1) / h_imagew (x_max-x_min) / w_imageh (y_max-y_min) / h_image3, conversion process

Define two folders, train for xml data and labels for txt data.

Code parsing:

Import osimport xml.etree.ElementTree as ETimport iofind_path ='. / train/' # xml savepath='./labels/' # Save the file class Voc_Yolo (object): def _ init__ (self, find_path): self.find_path = find_path def Make_txt (self, outfile): out = open (outfile) 'w') print ("created successfully: {}" .format (outfile)) return out def Work (self, count): # find the file path for root, dirs Files in os.walk (self.find_path): # find every xml file in the file directory for file in files: # record the processed file count + = 1 # input, Output file definition input_file = find_path + file outfile = savepath+file [:-4] + '.txt' # New txt file Make sure the file is saved properly out = self.Make_txt (outfile) # analyze the xml tree Remove w_image, H_image tree=ET.parse (input_file) root=tree.getroot () size=root.find ('size') w_image=float (size.find (' width'). Text) h_image=float (size.find ('height'). Text) # continue to extract valid information to calculate the four data in txt For obj in root.iter ('object'): # extract the type Different target types are different. There is only one category in this article-> 0 classname=obj.find ('name'). Text cls_id = classname xmlbox=obj.find (' bndbox') x_min=float (xmlbox.find ('xmin'). Text) x_max=float (xmlbox.find (' xmax'). Text) Y_min=float (xmlbox.find ('ymin'). Text) y_max=float (xmlbox.find (' ymax'). Text) # calculation formula x = ((x_min+x_max) / 2-1) / w_image yearly center = ((y_min+y_max) / 2-1) / h_image W = (x_max-x_min) / w_image h = (y_max-y_min) / h_image # file writes out.write (str (cls_id) + "+ str (x_center) +"+ str (y_center) +" + str (w) + "" + str (h) +'\ n') Out.close () return countif _ _ name__ = "_ _ main__": data = Voc_Yolo (find_path) number = data.Work (0) print (number) 4, Comparison of the final results

Created successfully

The error is very small compared with the real data.

This is the end of the introduction of "how to convert Python from xml format to txt format". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report