xml to DOTA

'''
2020/6/15,标注文件转换xml转txt(vol to yolo)转完后需添加labels文件,即数字序号对应的标签名。
'''

import xml.etree.ElementTree as ET
import numpy as np
import pickle
import os
import cv2
from os import listdir, getcwd
from os.path import join

classes = ['Boeing737', 'Boeing747', 'Boeing777', 'Boeing787', 'C919', 'A220', 'A321', 'A330', 'A350', 'ARJ21',
           'other-airplane', 'Passenger Ship', 'Motorboat', 'Fishing Boat', 'Tugboat', 'Engineering Ship',
           'Liquid Cargo Ship', 'Dry Cargo Ship', 'Warship', 'other-ship', 'Small Car', 'Bus', 'Cargo Truck',
           'Dump Truck', 'Van', 'Trailer', 'Tractor', 'Excavator', 'Truck Tractor', 'other-vehicle', 'Basketball Court',
           'Tennis Court', 'Football Field', 'Baseball Field', 'Intersection', 'Roundabout', 'Bridge']


def convert(size, box):
    dw = 1. / (size[0])
    dh = 1. / (size[1])
    x = (box[0] + box[1]) / 2.0 - 1
    y = (box[2] + box[3]) / 2.0 - 1
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    if w >= 1:
        w = 0.99
    if h >= 1:
        h = 0.99
    return (x, y, w, h)


def convert_annotation(rootpath, xmlpath, xmlname):
    xmlfile = os.path.join(xmlpath, xmlname)
    with open(xmlfile, "r", encoding='UTF-8') as in_file:
        txtname = xmlname[:-4] + '.txt'
        txtpath = rootpath + '/labelTxt'  # 生成的.txt文件会被保存在labelTxt目录下
        if not os.path.exists(txtpath):
            os.makedirs(txtpath)
        txtfile = os.path.join(txtpath, txtname)
        with open(txtfile, "w+", encoding='UTF-8') as out_file:
            tree = ET.parse(in_file)
            root = tree.getroot()
            size = root.find('size')
            w = int(size.find('width').text)  # 图片的宽
            h = int(size.find('height').text)
            # print(w, h)
            out_file.truncate()
            for obj in root.iter('object'):
                possibleresult = obj.find('possibleresult')
                cls_name = possibleresult.find('name').text
                if cls_name not in classes:
                    print("Appear not clear class name!!!!")
                    exit()
                cls_name = cls_name.replace(" ", "-")
                points = obj.find('points')
                data = ""
                for i in range(0, len(points) - 1):  # 只遍历前四个点
                    point = points[i]
                    xy = point.text.split(",")
                    x = int(float(xy[0]))
                    y = int(float(xy[1]))
                    data = data + str(x) + " "
                    data = data + str(y) + " "
                data = data + cls_name + " " + str(0)
                print(data)
                out_file.write(data + '\n')


if __name__ == "__main__":
    rootpath = 'train'
    xmlpath = rootpath + '/labelXml'  # 标签文件所在的路径
    list = os.listdir(xmlpath)
    for i in range(0, len(list)):
        path = os.path.join(xmlpath, list[i])
        if ('.xml' in path) or ('.XML' in path):
            convert_annotation(rootpath, xmlpath, list[i])
            print('---------------done--------------', i, '----------------------------------------')
        else:
            print('not xml file', i)

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中有很多第三方库可以将XML转换为JSON,其中比较常用的是Jackson和JSONObject。下面分别介绍这两种方法的实现。 1. 使用Jackson 首先,需要引入Jackson的依赖: ``` <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.12.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.12.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.1</version> </dependency> ``` 然后,我们可以编写以下代码: ``` import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; public class XmlToJson { public static void main(String[] args) throws IOException { String xml = "<root><name>John</name><age>30</age></root>"; XmlMapper xmlMapper = new XmlMapper(); JsonNode node = xmlMapper.readTree(xml.getBytes()); ObjectMapper jsonMapper = new ObjectMapper(); String json = jsonMapper.writeValueAsString(node); System.out.println(json); } } ``` 在上面的代码中,我们首先定义了一个XML字符串,然后使用XmlMapper将其转换为JsonNode对象,最后使用ObjectMapper将JsonNode对象转换为JSON字符串。 2. 使用JSONObject 如果不想使用Jackson,我们也可以使用Java标准库中的JSONObject来实现XML到JSON的转换。 以下是一个示例代码: ``` import org.json.JSONArray; import org.json.JSONObject; import org.json.XML; public class XmlToJson { public static void main(String[] args) { String xml = "<root><name>John</name><age>30</age></root>"; JSONObject json = XML.toJSONObject(xml); System.out.println(json.toString()); } } ``` 在上面的代码中,我们首先定义了一个XML字符串,然后使用XML类中的toJSONObject方法将其转换为JSONObject对象,最后使用toString方法将JSONObject对象转换为JSON字符串。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值