最近在做物体检测方面 要将xml文件转化为yolo的txt格式
写了个java脚本(python不太熟)
代码贴在下面 需要自取
String className =doc.getElementsByTagName(“name”).item(i).getFirstChild().getNodeValue();
(此处是获取xml文件中的name,你的name与我的不同自行修改下面判断语句)
(linux环境自己将//改为\ )
package utils;
import java.io.*;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class WriteText {
public static final String TXT_FILE_PATH = "D:\\txt\\";//转换后txt文件的路径
public static void main(String[] args) {
ArrayList<String> files = getFiles("D:\\BaiduNetdiskDownload\\My_Data_VOC(复件)\\Annotations\\");//标注文件xml路径
for (String file:files
) {
getXml(file);
}
}
public static String getXml(String path) {
long lasting = System.currentTimeMillis();
try {
File f = new File(path);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList nl = doc.getElementsByTagName("object");
int width = Integer.valueOf(doc.getElementsByTagName("width").item(0).getFirstChild().getNodeValue());
int height = Integer.valueOf(doc.getElementsByTagName("height").item(0).getFirstChild().getNodeValue());
ArrayList<String> lines = new ArrayList<>();
for (int i = 0; i < nl.getLength(); i++) {
String className = doc.getElementsByTagName("name").item(i).getFirstChild().getNodeValue();
if (className.equals("person")) {
className = "0";
} else {
className = "1";
}
//读取xml中的标签
int x1 = Integer.valueOf(doc.getElementsByTagName("xmin").item(i).getFirstChild().getNodeValue());
int y1 = Integer.valueOf(doc.getElementsByTagName("ymin").item(i).getFirstChild().getNodeValue());
int x2 = Integer.valueOf(doc.getElementsByTagName("xmax").item(i).getFirstChild().getNodeValue());
int y2 = Integer.valueOf(doc.getElementsByTagName("ymax").item(i).getFirstChild().getNodeValue());
// 进行转换
double dw = 1.0 / width;
double dh = 1.0 / height;
double w = x2 - x1;
double h = y2 - y1;
double x = x1 + (w / 2);
double y = y1 + (h / 2);
x = x * dw;
w = w * dw;
y = y * dh;
h = h * dh;
String line = className + " " + x + " " + y + " " + w + " " + h+"\n";
lines.add(line);
}
WriteToTxt(path, lines);
return "success";
} catch (Exception e) {
e.printStackTrace();
}
return "fail";
}
public static void WriteToTxt(String path, ArrayList<String> lines) {
String fileName2 = TXT_FILE_PATH + getFileName2(path)+".txt";
File file = new File(fileName2);
try {
file.createNewFile();
FileWriter fw = new FileWriter(fileName2, true);
for (String line : lines
) {
fw.write(line);
}
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getFileName2(String filePath) {
String s = filePath.substring(filePath.lastIndexOf("\\") + 1);
String[] split = s.split("\\.");
return split[0];
}
public static ArrayList<String> getFiles(String filepath) {
ArrayList<String> files = new ArrayList<String>();
File file = new File(filepath);
File[] tempLists = file.listFiles();
for (int i = 0; i < tempLists.length; i++) {
if (tempLists[i].isFile()) {
files.add(tempLists[i].toString());
}
}
return files;
}
}