{"id":13,"date":"2025-11-06T19:04:08","date_gmt":"2025-11-06T11:04:08","guid":{"rendered":"http:\/\/lcnas.cn\/?p=13"},"modified":"2025-11-06T21:41:49","modified_gmt":"2025-11-06T13:41:49","slug":"yolo%e6%a0%87%e6%b3%a8%e6%96%87%e4%bb%b6txt%e8%bd%acxml%e6%a0%bc%e5%bc%8f-%e6%9c%80%e7%ae%80%e5%8d%95","status":"publish","type":"post","link":"https:\/\/lcnas.cn\/index.php\/2025\/11\/06\/yolo%e6%a0%87%e6%b3%a8%e6%96%87%e4%bb%b6txt%e8%bd%acxml%e6%a0%bc%e5%bc%8f-%e6%9c%80%e7%ae%80%e5%8d%95\/","title":{"rendered":"yolo\u6807\u6ce8\u6587\u4ef6txt\u8f6cxml\u683c\u5f0f\u2014\u2014\u6700\u7b80\u5355"},"content":{"rendered":"\n<p>\u53ea\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0txt\u6587\u4ef6\u5730\u5740\u548cxml\u4fdd\u5b58\u5730\u5740\u7136\u540e\u4e00\u952e\u8fd0\u884c\u5373\u53ef<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\nimport xml.etree.ElementTree as ET\nfrom xml.dom import minidom\n \ndef convert_yolo_to_xml(yolo_folder, xml_folder):\n    if not os.path.exists(xml_folder):\n        os.makedirs(xml_folder)\n \n    for txt_file in os.listdir(yolo_folder):\n        if txt_file.endswith('.txt'):\n            xml_file = os.path.splitext(txt_file)&#91;0] + '.xml'\n            txt_path = os.path.join(yolo_folder, txt_file)\n            xml_path = os.path.join(xml_folder, xml_file)\n \n            with open(txt_path, 'r') as f:\n                lines = f.readlines()\n \n            # Create XML structure\n            annotation = ET.Element('annotation')\n \n            # Create filename node\n            filename = ET.SubElement(annotation, 'filename')\n            filename.text = os.path.splitext(txt_file)&#91;0] + '.jpg'  # Assuming jpg images\n \n            # Create size node\n            size = ET.SubElement(annotation, 'size')\n            width = ET.SubElement(size, 'width')\n            height = ET.SubElement(size, 'height')\n            depth = ET.SubElement(size, 'depth')\n \n            # Set your image dimensions here\n            width.text = '1920'\n            height.text = '1080'\n            depth.text = '3'  # Assuming RGB images\n \n            for line in lines:\n                class_index, x_center, y_center, bbox_width, bbox_height = map(float, line.split())\n \n                # Convert YOLO format to VOC format\n                xmin = int((x_center - bbox_width \/ 2) * 1920)\n                ymin = int((y_center - bbox_height \/ 2) * 1080)\n                xmax = int((x_center + bbox_width \/ 2) * 1920)\n                ymax = int((y_center + bbox_height \/ 2) * 1080)\n \n                # Create object node\n                obj = ET.SubElement(annotation, 'object')\n                name = ET.SubElement(obj, 'name')\n                bbox = ET.SubElement(obj, 'bndbox')\n                xmin_elem = ET.SubElement(bbox, 'xmin')\n                ymin_elem = ET.SubElement(bbox, 'ymin')\n                xmax_elem = ET.SubElement(bbox, 'xmax')\n                ymax_elem = ET.SubElement(bbox, 'ymax')\n \n                name.text = str(int(class_index))  # Convert class index to class name if needed\n                xmin_elem.text = str(xmin)\n                ymin_elem.text = str(ymin)\n                xmax_elem.text = str(xmax)\n                ymax_elem.text = str(ymax)\n \n            # Create XML tree and write to file\n            xmlstr = minidom.parseString(ET.tostring(annotation)).toprettyxml(indent=\"   \")\n            with open(xml_path, \"w\") as f:\n                f.write(xmlstr)\n \n            print(f'Converted {txt_file} to {xml_file}')\n \nyolo_folder = \"yolo\u6807\u6ce8\u6587\u4ef6txt\u5730\u5740\"\nxml_folder = \"\u751f\u6210\u7684xml\u6587\u4ef6\u4fdd\u5b58\u5730\u5740\"\nconvert_yolo_to_xml(yolo_folder, xml_folder)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u53ea\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0txt\u6587\u4ef6\u5730\u5740\u548cxml\u4fdd\u5b58\u5730\u5740\u7136\u540e\u4e00\u952e\u8fd0\u884c\u5373\u53ef<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,1],"tags":[],"class_list":["post-13","post","type-post","status-publish","format-standard","hentry","category-fourfour","category-leam"],"_links":{"self":[{"href":"https:\/\/lcnas.cn\/index.php\/wp-json\/wp\/v2\/posts\/13","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lcnas.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lcnas.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lcnas.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lcnas.cn\/index.php\/wp-json\/wp\/v2\/comments?post=13"}],"version-history":[{"count":1,"href":"https:\/\/lcnas.cn\/index.php\/wp-json\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":14,"href":"https:\/\/lcnas.cn\/index.php\/wp-json\/wp\/v2\/posts\/13\/revisions\/14"}],"wp:attachment":[{"href":"https:\/\/lcnas.cn\/index.php\/wp-json\/wp\/v2\/media?parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lcnas.cn\/index.php\/wp-json\/wp\/v2\/categories?post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lcnas.cn\/index.php\/wp-json\/wp\/v2\/tags?post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}