C# 編寫一個XmlToDota的轉換工具

以下代碼可以將Labelme標注的旋轉框Xml格式文件轉換為Yolo標注格式的txt文件,以便用Yolo OBB訓練自己的數據集:

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Linq;
using System.Globalization;

namespace XmlToDotaConverter
{
class Program
{
static readonly List<string> clsList = new List<string> { "Hole1", "Hole2" };

? ? ? ? static void Main(string[] args)
{
string roxmlPath = @"C:\BAN\orgin-xml";
string dotaxmlPath = @"C:\BAN\new-xml";
string outPath = @"C:\BAN\out-txt";

? ? ? ? ? ? // 創建輸出目錄
Directory.CreateDirectory(dotaxmlPath);
Directory.CreateDirectory(outPath);

? ? ? ? ? ? // 第一步:轉換XML格式
var xmlFiles = Directory.GetFiles(roxmlPath, "*.xml");
foreach (var xmlFile in xmlFiles)
{
string outputXml = Path.Combine(dotaxmlPath, Path.GetFileName(xmlFile));
EditXml(xmlFile, outputXml);
}

? ? ? ? ? ? // 第二步:轉換為TXT格式
ToTxt(dotaxmlPath, outPath);
}

? ? ? ? static void EditXml(string xmlFile, string dotaxmlFile)
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);

? ? ? ? ? ? XmlNodeList objectNodes = doc.SelectNodes("//object");
foreach (XmlNode objNode in objectNodes)
{
XmlNode bndbox = objNode.SelectSingleNode("bndbox");
XmlNode robndbox = objNode.SelectSingleNode("robndbox");

? ? ? ? ? ? ? ? // 處理普通矩形框
if (robndbox == null && bndbox != null)
{
ConvertRectangleBox(bndbox);
}
// 處理旋轉矩形框
else if (robndbox != null)
{
ConvertRotatedBox(objNode, robndbox);
}
}

? ? ? ? ? ? // 保存修改后的XML
doc.Save(dotaxmlFile);
}

? ? ? ? static void ConvertRectangleBox(XmlNode bndbox)
{
double xmin = Math.Max(GetDoubleValue(bndbox, "xmin"), 0);
double ymin = Math.Max(GetDoubleValue(bndbox, "ymin"), 0);
double xmax = Math.Max(GetDoubleValue(bndbox, "xmax"), 0);
double ymax = Math.Max(GetDoubleValue(bndbox, "ymax"), 0);

? ? ? ? ? ? // 移除舊節點
RemoveChildNodes(bndbox);

? ? ? ? ? ? // 添加四個角點坐標
AddPoint(bndbox, "x0", xmin.ToString());
AddPoint(bndbox, "y0", ymax.ToString());
AddPoint(bndbox, "x1", xmax.ToString());
AddPoint(bndbox, "y1", ymax.ToString());
AddPoint(bndbox, "x2", xmax.ToString());
AddPoint(bndbox, "y2", ymin.ToString());
AddPoint(bndbox, "x3", xmin.ToString());
AddPoint(bndbox, "y3", ymin.ToString());
}

? ? ? ? static void ConvertRotatedBox(XmlNode objNode, XmlNode robndbox)
{
// 將robndbox重命名為bndbox
XmlNode bndbox = objNode.OwnerDocument.CreateElement("bndbox");
objNode.ReplaceChild(bndbox, robndbox);

? ? ? ? ? ? double cx = GetDoubleValue(robndbox, "cx");
double cy = GetDoubleValue(robndbox, "cy");
double w = GetDoubleValue(robndbox, "w");
double h = GetDoubleValue(robndbox, "h");
double angle = GetDoubleValue(robndbox, "angle");

? ? ? ? ? ? // 計算旋轉后的四個角點
var p0 = RotatePoint(cx, cy, cx - w / 2, cy - h / 2, -angle);
var p1 = RotatePoint(cx, cy, cx + w / 2, cy - h / 2, -angle);
var p2 = RotatePoint(cx, cy, cx + w / 2, cy + h / 2, -angle);
var p3 = RotatePoint(cx, cy, cx - w / 2, cy + h / 2, -angle);

? ? ? ? ? ? // 添加四個角點坐標
AddPoint(bndbox, "x0", p0.X.ToString());
AddPoint(bndbox, "y0", p0.Y.ToString());
AddPoint(bndbox, "x1", p1.X.ToString());
AddPoint(bndbox, "y1", p1.Y.ToString());
AddPoint(bndbox, "x2", p2.X.ToString());
AddPoint(bndbox, "y2", p2.Y.ToString());
AddPoint(bndbox, "x3", p3.X.ToString());
AddPoint(bndbox, "y3", p3.Y.ToString());
}

? ? ? ? static (int X, int Y) RotatePoint(double cx, double cy, double xp, double yp, double theta)
{
double xoff = xp - cx;
double yoff = yp - cy;
double cosTheta = Math.Cos(theta);
double sinTheta = Math.Sin(theta);
double pResx = cosTheta * xoff + sinTheta * yoff;
double pResy = -sinTheta * xoff + cosTheta * yoff;
return ((int)(cx + pResx), (int)(cy + pResy));
}

? ? ? ? static void ToTxt(string xmlPath, string outPath)
{
foreach (string xmlFile in Directory.GetFiles(xmlPath, "*.xml"))
{
string fileName = Path.GetFileNameWithoutExtension(xmlFile);
string txtPath = Path.Combine(outPath, fileName + ".txt");

? ? ? ? ? ? ? ? XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);

? ? ? ? ? ? ? ? using StreamWriter writer = new StreamWriter(txtPath);
foreach (XmlNode objNode in doc.SelectNodes("//object"))
{
string cls = objNode.SelectSingleNode("name").InnerText;
XmlNode bndbox = objNode.SelectSingleNode("bndbox");

? ? ? ? ? ? ? ? ? ? int[] points = new int[8];
points[0] = Math.Max((int)GetDoubleValue(bndbox, "x0"), 0);
points[1] = Math.Max((int)GetDoubleValue(bndbox, "y0"), 0);
points[2] = Math.Max((int)GetDoubleValue(bndbox, "x1"), 0);
points[3] = Math.Max((int)GetDoubleValue(bndbox, "y1"), 0);
points[4] = Math.Max((int)GetDoubleValue(bndbox, "x2"), 0);
points[5] = Math.Max((int)GetDoubleValue(bndbox, "y2"), 0);
points[6] = Math.Max((int)GetDoubleValue(bndbox, "x3"), 0);
points[7] = Math.Max((int)GetDoubleValue(bndbox, "y3"), 0);

? ? ? ? ? ? ? ? ? ? int clsIndex = clsList.IndexOf(cls);
if (clsIndex == -1) continue;

? ? ? ? ? ? ? ? ? ? writer.WriteLine($"{points[0]} {points[1]} {points[2]} {points[3]} " +
$"{points[4]} {points[5]} {points[6]} {points[7]} " +
$"{cls} {clsIndex}");
}
}
}

? ? ? ? #region Helper Methods
static double GetDoubleValue(XmlNode parent, string nodeName)
{
return double.Parse(parent.SelectSingleNode(nodeName).InnerText,
CultureInfo.InvariantCulture);
}

? ? ? ? static void RemoveChildNodes(XmlNode node)
{
while (node.HasChildNodes)
{
node.RemoveChild(node.FirstChild);
}
}

? ? ? ? static void AddPoint(XmlNode parent, string name, string value)
{
XmlElement elem = parent.OwnerDocument.CreateElement(name);
elem.InnerText = value;
parent.AppendChild(elem);
}
#endregion
}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/94474.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/94474.shtml
英文地址,請注明出處:http://en.pswp.cn/web/94474.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

[Android] 人體細胞模擬器1.5

[Android] 人體細胞模擬器1.5 鏈接&#xff1a;https://pan.xunlei.com/s/VOYVUieTpjNVJq-bMys4EEDGA1?pwdm7m6# 省流:這個軟件的開發者有點逆天&#xff0c;一個模擬人體器官的軟件&#xff0c;細致到有血液報告&#xff0c;還縫合了生理學和病理學&#xff0c;甚至還能做切…

【Linux基礎知識系列】第一百一十篇 - 使用Nmap進行網絡安全掃描

在網絡安全管理中&#xff0c;了解網絡中的設備、開放的端口以及運行的服務是至關重要的。Nmap&#xff08;Network Mapper&#xff09;是一個功能強大的開源工具&#xff0c;用于網絡發現和安全審計。它可以掃描網絡中的設備&#xff0c;識別開放的端口和運行的服務&#xff0…

【Linux倉庫】進程的“奪舍”與“飛升”:exec 驅動的應用現代化部署流水線

&#x1f31f; 各位看官好&#xff0c;我是egoist2023&#xff01; &#x1f30d; Linux Linux is not Unix &#xff01; &#x1f680; 今天來學習exec系列的進程程序替換,從"fork"的"克隆"到"exec"的"重生"。 &#x1f44d; 如果覺…

Reachability Query

題目分析 該代碼實現了一個動態集合管理系統&#xff0c;支持三種操作&#xff1a;合并集合、切換元素狀態、查詢集合中是否- 存在活躍元素。核心數據結構為并查集&#xff0c;結合狀態標記數組和計數器。關鍵數據結構與函數 初始化 fa[N]&#xff1a;并查集父節點數組&#xf…

SSL移動接入方案和移動資源發布

一、SSL VPN概述SSL VPN是一種基于SSL/TLS協議的遠程安全接入技術&#xff0c;因其廣泛兼容Web瀏覽器&#xff0c;支持“無客戶端”部署&#xff0c;具備易于使用和維護的特點。它通過插件系統支持非Web類TCP/UDP應用&#xff0c;并且支持對用戶的訪問可以做出限制&#xff0c;…

C++STL---count() 統計容器中特定元素出現次數

在 C 標準庫中&#xff0c;count 是一個用于統計容器中特定元素出現次數的函數&#xff0c;定義在 <algorithm> 頭文件中。它可以快速計算某個值在容器&#xff08;如數組、vector、list 等&#xff09;中出現的次數&#xff0c;避免手動編寫循環計數的麻煩。 一、函數原…

Tesla自動駕駛域控制器(AutoPilot HW)的系統化梳理

目前網絡上對Tesla自動駕駛硬件&#xff08;AP1-AP4、HW1.0-HW4.0&#xff09;迭代的相關介紹比較混亂&#xff0c;本文這里進行系統化梳理并澄清&#xff0c;并對一些錯誤進行更正。1、AutoPilot HW迭代圖圖1 AutoPilot HWMCU迭代圖圖2 AutoPilot HW 散熱設計迭代圖&#xff0…

C 語言:第 20 天筆記:typedef(類型重命名規則、應用場景與實戰案例)

C語言&#xff1a;第20天筆記 內容提要 構造類型枚舉類型typedef綜合案例:斗地主預處理 構造類型&#xff1a;枚舉類型 使用建議 如果定義不相干的常量&#xff0c;使用宏定義&#xff08;符號常量&#xff09;&#xff1b;如果需要定義一組相關聯的常量&#xff08;如月份011、…

在 vue3 和 vue2 中,v-for 和 v-if 可以一起用嗎,區別是什么

在 Vue 2 和 Vue 3 中&#xff0c;v-for 和 v-if 可以一起使用&#xff0c;但兩者在處理順序和推薦用法上存在明顯區別&#xff0c;主要體現在優先級和最佳實踐上&#xff1a; 1. Vue 2 中的 v-for 與 v-if優先級&#xff1a;v-for 的優先級高于 v-if。 這意味著 Vue 會先循環渲…

Linux-進程相關函數

文章目錄Linux-進程相關函數父子進程關系父子進程地址空間getpid函數 獲取本進程號getppid函數 獲取當前進程的進程的父進程號getpgid函數 獲取進程組號示例fork函數 創建進程區分父子進程exit函數 進程退出wait函數 等待子進程退出waitpid函數Linux-進程相關函數 每個進程都由…

數據挖掘 6.1 其他降維方法(不是很重要)

6.1 Other dimensionality reduction methods 6.1 其他降維方法 其他降維方法前言問題答案流形3 降維大綱3.1 線性方法3.2 非線性方法3.2.1 流形學習方法&#xff08;Manifold Learning&#xff09;3.2.2 概率方法&#xff08;Probabilistic Approaches&#xff09;3.2.3 拓撲數…

Unity中的特殊文件夾

一.工程路徑獲取print(Application.dataPath);只用于游戲開發編輯器模式下&#xff0c;游戲發布后此路徑就不存在了二.Resources 資源文件夾//路徑獲取: //一般不獲取 //只能使用Resources相關API進行加載 //如果硬要獲取 可以用工程路徑拼接print(Application.dataPath "…

Seaborn數據可視化實戰:Seaborn高級使用與性能優化教程

Seaborn最佳實踐與技巧 學習目標 本課程將深入探討Seaborn庫的高級使用技巧&#xff0c;包括性能優化、常見問題解決方法等&#xff0c;旨在幫助學員掌握如何高效地使用Seaborn進行數據可視化&#xff0c;提升圖表的美觀度和信息傳達效率。 相關知識點 Seaborn最佳實踐與技巧 學…

分布式系統與單機系統的優劣勢對比

近期有遇到一個本地部署的需求&#xff0c;他們希望用主備方案&#xff0c;這就涉及到了備用系統怎么收費的問題。我們是單機系統&#xff0c;其他友商是分布式系統&#xff0c;那20坐席的手撥需求到底是選單機系統好&#xff0c;還是選分布式系統好呢&#xff1f;了解了兩者的…

深度學習:從手寫數字識別案例認識pytorch框架

目錄 一、PyTorch 核心優勢與框架定位 二、實戰基礎&#xff1a;核心庫與數據準備 1. 關鍵庫導入與功能說明 2. MNIST 數據集加載與可視化 &#xff08;1&#xff09;數據集下載與封裝 &#xff08;2&#xff09;數據集可視化&#xff08;可選&#xff09; 3. DataLoade…

二分|組合|旋轉數組

lc1976dijk min_pathpq. min_wlcr187同lc1823.約瑟夫環class Solution { public:int iceBreakingGame(int num, int target) {int x0;for(int i2;i<num;i){x(xtarget)%i;} return x;} };lc2972計算數組中可移除的子數組數量先找最長遞增前綴&#xff0c;再結合遞增后綴…

【C語言16天強化訓練】從基礎入門到進階:Day 10

&#x1f525;個人主頁&#xff1a;艾莉絲努力練劍 ?專欄傳送門&#xff1a;《C語言》、《數據結構與算法》、C語言刷題12天IO強訓、LeetCode代碼強化刷題、洛谷刷題、C/C基礎知識知識強化補充、C/C干貨分享&學習過程記錄 &#x1f349;學習方向&#xff1a;C/C方向學習者…

云計算與云原生技術探索

&#x1f31f; Hello&#xff0c;我是蔣星熠Jaxonic&#xff01; &#x1f308; 在浩瀚無垠的技術宇宙中&#xff0c;我是一名執著的星際旅人&#xff0c;用代碼繪制探索的軌跡。 &#x1f680; 每一個算法都是我點燃的推進器&#xff0c;每一行代碼都是我航行的星圖。 &#x…

STM32之ADC詳解

一、ADC概述 ADC&#xff08;模擬量轉數字量轉換器&#xff09;&#xff0c;在 STM32 開發中&#xff0c;利用 ADC 端口的電壓數據&#xff0c;轉換為對應的具體數字量數據內容。可通過 ADC 方式獲取常用數據內容有&#xff1a; 光敏電阻、電池電量、油箱油量 ADC 轉換…

深入理解計算機網絡:從基礎到應用的全面解析

標題&#xff1a;深入理解計算機網絡&#xff1a;從基礎到應用的全面解析 引言 計算機網絡已經滲透到我們生活的方方面面。從家庭Wi-Fi到全球互聯網&#xff0c;我們每天都在通過各種設備進行數據交換。本文將帶領你走進計算機網絡的世界&#xff0c;深入探討網絡的基礎知識、常…