From d5aee7b93618613e96e82a8eb7b7cc74404c1be9 Mon Sep 17 00:00:00 2001 From: lee <770918727@qq.com> Date: Tue, 5 Aug 2025 14:27:30 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A2=84=E8=AE=AD=E7=BB=83=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo.py | 2 +- ultralytics/utils/txt2xml.py | 77 ++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 ultralytics/utils/txt2xml.py diff --git a/demo.py b/demo.py index c17f78a..829e78a 100644 --- a/demo.py +++ b/demo.py @@ -3,7 +3,7 @@ import numpy as np # model = YOLOv10.from_pretrained('jameslahm/yolov10{n/s/m/b/l/x}') # or # wget https://github.com/THU-MIG/yolov10/releases/download/v1.1/yolov10{n/s/m/b/l/x}.pt -model = YOLOv10('ckpts/20250630/best_gift_v10n.pt') +model = YOLOv10('ckpts/20250701/best_gift_v10n.pt') # result = model.predict('./data/bandage.jpg', save=True, imgsz=[224, 224], conf=0.1) result = model.predict('/home/lc/data_center/gift/trace_subimgs/predict_actual_test/test', save=True, imgsz=[224, 224], conf=0.1) diff --git a/ultralytics/utils/txt2xml.py b/ultralytics/utils/txt2xml.py new file mode 100644 index 0000000..ab10dee --- /dev/null +++ b/ultralytics/utils/txt2xml.py @@ -0,0 +1,77 @@ +# # ## # # # # # # # # # #using: 将yolo txt label 转换成xml标签 +# #-*- coding: utf-8 -*- + +import cv2 +import os + +xml_head = ''' + VOC2007 + {}. + + + + null + null + + + {} + {} + {} + + 0 + ''' +xml_obj = ''' + + {} + Rear + 0 + 0 + + {} + {} + {} + {} + + + ''' +xml_end = ''' +''' + +# labels = ['mouse','snake','lizard'] # label for datasets +# labels = ['mouse','snake','lizard','fire','smoke','open','close','R_0','R_1','G_0','G_1','Y_0','Y_1','mask','person','hat','reflect'] # 需要训练的所有类,label for datasets +# labels = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30'] +labels = ['1', '2', '3', '4'] +cnt = 0 +txt_path = 'txt2xml/labels/' +xml_path = 'txt2xml/xmls/' +img_path = 'txt2xml/images/' +os.makedirs(xml_path, exist_ok=True) +for yolo_file in os.listdir(txt_path): + jpg = img_path + yolo_file.replace('.txt', '.png') # image path + txt = txt_path + yolo_file # yolo label txt path + xml = xml_path + yolo_file.replace('.txt', '.xml') # xml save path + print(jpg) + obj = '' + + img = cv2.imread(jpg) + img_h, img_w = img.shape[0], img.shape[1] + head = xml_head.format(str(jpg), str(img_w), str(img_h), str(3)) + with open(txt, 'r') as f: + 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, 'w') as f_xml: + f_xml.write(head + obj + xml_end) + cnt += 1 + print(cnt)