Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3648477
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Python/Ruby

2021-04-02 17:04:07

xml数据:

8a0004.jpg

960

1280

3

object

314

366

427

430

object

805

9

960

277

想要转换成的csv形式:(每一行一个bbox

(图片名称,xminyminxmaxymaxclasswh

test_0.jpg,1653,1290,1773,1535,object,2448,3264

test_0.jpg,1485,1221,1648,1544,object,2448,3264

test_0.jpg,1345,1295,1481,1540,object,2448,3264

test_0.jpg,1221,1290,1341,1543,object,2448,3264

test_0.jpg,1079,1332,1216,1537,object,2448,3264

test_0.jpg,927,1285,1069,1531,object,2448,3264

test_0.jpg,679,1279,845,1539,object,2448,3264

test_0.jpg,2187,2536,2276,2764,object,2448,3264

test_0.jpg,232,519,361,774,object,2448,3264

test_0.jpg,5,521,225,774,object,2448,3264

test_1.jpg,457,436,574,526,object,1920,2560

test_1.jpg,537,1949,612,2093,object,1920,2560

test_1.jpg,436,2020,534,2095,object,1920,2560

test_1.jpg,1774,1751,1870,1854,object,1920,2560

test_1.jpg,1679,1759,1769,1852,object,1920,2560

test_1.jpg,1578,1762,1674,1852,object,1920,2560

test_1.jpg,1470,1752,1566,1863,object,1920,2560

test_1.jpg,1403,1747,1465,1872,object,1920,2560

test_1.jpg,1231,1764,1330,1869,object,1920,2560

test_1.jpg,1130,1771,1224,1874,object,1920,2560

test_1.jpg,1028,1771,1121,1879,object,1920,2560

test_1.jpg,924,1774,1017,1872,object,1920,2560

test_1.jpg,847,1839,921,1881,object,1920,2560

test_1.jpg,844,1740,919,1833,object,1920,2560

test_1.jpg,759,1839,843,1890,object,1920,2560

test_1.jpg,764,1771,834,1838,object,1920,2560

转换代码:

# coding: utf-8

import xml.etree.ElementTree as ET

import os

names_dict = {}

cnt = 0

f = open('/home/hub/wsy/YOLOv3_TensorFlow/misc/new_data/sku.txt', 'r').readlines()#含有类别的txt

for line in f:

    line = line.strip()

    names_dict[line] = cnt

    cnt += 1

pic_path = '/home/hub/wsy/YOLOv3_TensorFlow/new_data/val'

anno_path = [os.path.join(pic_path, 'Annotations')]#xml文件夹

img_path = [os.path.join(pic_path, 'JPEGImages')]#图片文件夹

val_path = [os.path.join(pic_path, 'val.txt')]#保存图片名称的txt

def parse_xml(path,file):

    tree = ET.parse(path)

    img_name = path.split('/')[-1][:-4]

    # print(img_name)

    height = tree.findtext("./size/height")

    width = tree.findtext("./size/width")

    for obj in tree.findall('object'):

        # objects = []

        difficult = obj.find('difficult').text

        if difficult == '1':

            continue

        bbox = obj.find('bndbox')

        xmin = bbox.find('xmin').text

        ymin = bbox.find('ymin').text

        xmax = bbox.find('xmax').text

        ymax = bbox.find('ymax').text

        objects = img_name + ".jpg,"+str(xmin) +","+str(ymin) +","+str(xmax) +","+str(ymax) +","+ \

                      "object,"+str(width) + ","+str(height)

        file.write(objects + '\n')

        # print(objects)

test_cnt = 0

def gen_test_txt(txt_path):

    global test_cnt

    f = open(txt_path, 'w+')

    for i, path in enumerate(val_path):

        img_names = open(path, 'r').readlines()

        # file = open('val_sku1.txt', 'w+')

        for img_name in img_names:

            img_name = img_name.rstrip('\n')

            # print(img_name)

            xml_path = anno_path[i] + '/' + img_name + '.xml'

            # print(xml_path)

            parse_xml(xml_path,f)

    f.close()

gen_test_txt('val_2.txt')

阅读(1301) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~