dataset:xml->txt

本文介绍了如何使用Python脚本对图像文件中的XML标注进行修改,将'hat'标签更改为'head',并处理错误图片。通过遍历XML文件,检测并替换指定类别,最后将处理后的图片和XML保存到新的目录。

filter

import glob
import cv2
import xml.etree.ElementTree as ET
import numpy as np
# color =


def filter1(xml_root, img_root):

    xml_list = glob.glob(f'{xml_root}/*')
    classes = []
    error_num = 0
    for i, xml_name in enumerate(xml_list):
        # try:
        tree = ET.parse(xml_name)
        root = tree.getroot()
        img_name = tree.findtext('./filename').split(".jpg")[0]
        img_path =  img_root + '/' + img_name+".jpg"
        img = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), 1)
        for a in root:
            for b in a:
                if b.tag == 'name':
                    if b.text == 'hat':
                        b.text = 'head'
        length = []
        for a in root:
            for b in a :
                if b.tag == 'name':
                    length.append(b.text)
        if len(length) != 0:
            try:
                # cv2.imwrite(f"data/1/img/{img_name}.jpg", img)
                name = f"data/1/img/{img_name}.jpg"
                # cv2.imencode('.jpg', img)[1].tofile(name)
                tree.write(f'new_xml_label/{img_name}.xml', encoding='UTF-8')
            except:
                print(img_root + '/' + img_name + ".jpg")
                error_num+=1
                continue
        # except:
        #     error_num += 1
        #     continue

    print(error_num)
    return 0




if __name__=="__main__":
    xml_roots = ['label']
    img_roots = ['image']


    filter1(xml_roots[0], img_roots[0])

trans

import glob
import xml.etree.ElementTree as ET
import cv2
import os

sets = ['train', 'test', 'val']
# classes = ['head', 'hat', 'person', 'lamp', 'cellphone' ]
classes = ['head','person','cellphone','lamp']

def convert(size, box): # size:(原图w,原图h) , box:(xmin,xmax,ymin,ymax)
    dw = 1./size[0]     # 1/w
    dh = 1./size[1]     # 1/h
    x = (box[0] + box[1])/2.0   # 物体在图中的中心点x坐标
    y = (box[2] + box[3])/2.0   # 物体在图中的中心点y坐标
    w = box[1] - box[0]         # 物体实际像素宽度
    h = box[3] - box[2]         # 物体实际像素高度
    x = x*dw    # 物体中心点x的坐标比(相当于 x/原图w)
    w = w*dw    # 物体宽度的宽度比(相当于 w/原图w)
    y = y*dh    # 物体中心点y的坐标比(相当于 y/原图h)
    h = h*dh    # 物体宽度的宽度比(相当于 h/原图h)
    return x, y, w, h  # 返回 相对于原图的物体中心点的x坐标比,y坐标比,宽度比,高度比,取值范围[0-1]

def convert_annotation(xml_path,i):
    in_file = xml_path

    # print(out_file.name)
    # 解析xml文件
    tree = ET.parse(in_file)
    # 获得对应的键值对
    root = tree.getroot()
    # 获得图片的尺寸大小
    img_name = root.find('filename').text.split('.jpg')[0].split('.png')[0]
    # if i % 10 != 0:
    #     out_file = open(f'txt_label/{img_name}.txt','w')
    # else :
    #     out_file = open(f'val_txt_label')\
    out_file = open(f'labels/{img_name}.txt', 'w')
    size = root.find('size')
    # 获得宽
    w = int(size.find('width').text)
    # 获得高
    h = int(size.find('height').text)
    for obj in root.iter('object'):
        try:
            cls = obj.find('name').text
            print(cls)
        except:
            continue
        cls_id = classes.index(cls)
        xmlbox = obj.find('bndbox')
        # 获取对应的bndbox的数组 = ['xmin','xmax','ymin','ymax']
        b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
             float(xmlbox.find('ymax').text))
        bb = convert((w, h), b)

        out_file.write(str(cls_id) + " " + " ".join([str(round(a,6)) for a in bb]) + '\n')
if __name__ == '__main__':
    xml_path_list = glob.glob("label/*")
    for i,xml_path in enumerate(xml_path_list):
        convert_annotation(xml_path,i)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值