当前位置:网站首页>VOC format label to YOLO format
VOC format label to YOLO format
2022-08-09 07:32:00 【qq_52217283】
VOC to Yolo format code:
import osimport xml.etree.ElementTree as ET# Convert x1, y1, x2, y2 to the x, y, w, h format required by yolodef voc_to_yolo_wh(size, box):dw = 1. / size[0]dh = 1. / size[1]x = (box[0] + box[2]) / 2 * dwy = (box[1] + box[3]) / 2 * dhw = (box[2] - box[0]) * dwh = (box[3] - box[1]) * dhreturn x, y, w, h # return the normalized values# Get the classes category in all xml, return (list/array)def get_cls_list(xml_path):result_list = []for file in os.listdir(xml_path):tree = ET.parse(xml_path + file)root = tree.getroot()for obj in root.iter('object'):cls = obj.find('name').textif cls not in result_list:result_list.append(cls)return result_listdef voc_to_yolo(xml_path, save_path, classes):# can print to see if the path is correctif not os.path.exists(xml_path):print("XML path does not exist")return []if len(os.listdir(xml_path)) == 0:print("XML file directory is empty")return []if not os.path.exists(save_path):os.makedirs(save_path)if not classes:classes = get_cls_list(xml_path)# loop through each xml filefor file in os.listdir(xml_path):label_file = xml_path + filesave_file = save_path + file.replace('xml', 'txt')out_file = open(save_file, 'w')# print(label_file)# Start parsing the xml filetree = ET.parse(label_file)root = tree.getroot()size = root.find('size') # The shape value of the imagew = int(size.find('width').text)h = int(size.find('height').text)for obj in root.iter('object'):difficult = obj.find('difficult').textcls = obj.find('name').textif cls not in classes or int(difficult) == 1:continue# Convert name to id subscriptcls_id = classes.index(cls)# Get the entire bounding boxbnd_box = obj.find('bndbox')# xml gives x1, y1, x2, y2box = [float(bnd_box.find('xmin').text), float(bnd_box.find('ymin').text), float(bnd_box.find('xmax').text),float(bnd_box.find('ymax').text)]# Convert x1, y1, x2, y2 to x_center, y_center, w, h format required by yolobbox = voc_to_yolo_wh((w, h), box)# print(save_file)# Write to the target file, the format is id x y w hout_file.write(str(cls_id) + " " + " ".join(str(x) for x in bbox) + '\n')return classesif __name__ == '__main__':# here to change to your own data set pathxml_path_ = 'Annotations/xml/'# This is to be changed to the data set path saved after conversionsave_path_ = 'Annotations/labels/'# Data set label, if it is empty, it will automatically get the label, just get the label in the function return result.Class_Name = []result_classes = voc_to_yolo(xml_path_, save_path_, Class_Name)print("Dataset label:", result_classes)
边栏推荐
- 【MySQL】update mysql.user set authentication_string=password(“123456“) where User=‘root‘; 报错
- Codeforces Round #359 (Div. 2) C. Robbers' watch Violent Enumeration
- Unity first lesson
- 2019南昌网络赛 C题,Hello 2019
- 【模板】树链剖分 P3384
- 毕业我选择了保家卫国,退伍我选择了华为外包
- 字节也开始缩招了...
- TCP段重组PDU
- Learning Notes---Machine Learning
- list and string conversion
猜你喜欢
随机推荐
2022 年全球十大最佳自动化测试工具
力扣第 305 场周赛复盘
TCP段重组PDU
redis学习笔记
MUV LUV EXTRA 2019CCPC秦皇岛站J题 KMP
SA-Siam:用于实时目标跟踪的双重连体网络A Twofold Siamese Network for Real-Time Object Tracking
【Docker】Docker安装MySQL
Anaconda 更换默认虚拟环境
定时任务组件Quartz
分布式理论
Sklearn data preprocessing
eyb:Redis学习(2)
洛谷P1110 报表统计 multiset stl好题
Anaconda 使用代理
排序第二节——选择排序(选择排序+堆排序)(两个视频讲解)
Forest Program dfs+tanjar仙人掌
sklearn数据预处理
car-price-deeplearning-0411
MUV LUV EXTRA 2019CCPC Qinhuangdao Station J Question KMP
postgresql Window Functions