一个将VOC标注的xml文件转换为yolo格式txt的java脚本

最近在做物体检测方面 要将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;
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值