yolo格式转voc格式

下面展示一些 内联代码片

// 传入图片路径以及txt路径
import argparse
from os.path import isfile, isdir, join
import random, yaml
import sys, cv2 ,os
from shutil import copyfile
from distutils.dir_util import copy_tree
from xml.dom import minidom
import xml.etree.ElementTree as ET
from lxml.etree import Element, SubElement, tostring
from xml.dom.minidom import parseString


def txtToXml(txtPath, picPath, xmlPath):
    for item in os.listdir(txtPath):
        try:
            print("item:",item)
            picFlie = os.path.join(picPath, item.replace('txt','jpg'))
            picName = item.replace('txt','jpg')

            if not os.path.exists(picFlie):
                picFlie = os.path.join(picPath, item.replace('txt','png'))
                picName = item.replace('txt','png')

            node_root = Element('annotation')

            node_folder = SubElement(node_root, 'folder')
            node_folder.text = 'IMAGE'

            node_filename = SubElement(node_root, 'filename')
            node_filename.text = picName

            node_size = SubElement(node_root, 'size')
            node_width = SubElement(node_size, 'width')

            print("picPath:",picFlie)
            img = cv2.imread(picFlie)
            height, width, _ = img.shape

            node_width.text = '%s' % width
            node_height = SubElement(node_size, 'height')
            node_height.text = '%s' % height

            node_depth = SubElement(node_size, 'depth')
            node_depth.text = '%s' % 3

            #read txt 
            path = os.path.join(txtPath, item)
            # print("path:",path)
            f2 = open(path,"r")
            lines = f2.readlines()
            bbox = [0,0,0,0]
            for line3 in lines:
            # for i in inds:
                # bbox = dets[i, :4]
                # score = dets[i, -1]

                #rm [0,0,0,0]
                line3 = line3.rstrip("\n")
                line = line3.split(' ')
                # print("line:",line)
                # print("line[3]:",line[3])
                bbox[0] = float(line[1])*width - float(line[3])*width/2
                bbox[1] = float(line[2])*height - float(line[4])*height/2
                bbox[2] = float(line[1])*width + float(line[3])*width/2
                bbox[3] = float(line[2])*height + float(line[4])*height/2

                if bbox[0]==0 and bbox[2]==0 and bbox[1]==0 and bbox[3]==0 : continue

                node_object = SubElement(node_root, 'object')
                node_name = SubElement(node_object, 'name')
                # if int(line[0]) == 1:
                #     node_name.text = 'hand'
                # else:
                #     node_name.text = 'goods'

                node_name.text = line[0]

                node_difficult = SubElement(node_object, 'difficult')
                node_difficult.text = '0'
                node_bndbox = SubElement(node_object, 'bndbox')
                node_xmin = SubElement(node_bndbox, 'xmin')
                node_xmin.text = '%s' % bbox[0]
                node_ymin = SubElement(node_bndbox, 'ymin')
                node_ymin.text = '%s' % bbox[1]
                node_xmax = SubElement(node_bndbox, 'xmax')
                node_xmax.text = '%s' % bbox[2]
                node_ymax = SubElement(node_bndbox, 'ymax')
                node_ymax.text = '%s' % bbox[3]

            xml = tostring(node_root, pretty_print=True)    

            save_ml = os.path.join(xmlPath, item.replace('txt', 'xml'))
            with open(save_ml, 'wb') as f:
                f.write(xml)
        except:
            pass

def create_dir_not_exist(path):
    if not os.path.exists(path):
        os.mkdir(path)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--picPath', type=str, default = 'dataResult')
    parser.add_argument('--txtPath', type=str, default='dataResult/xml')
    opt = parser.parse_args()

    picPath = opt.picPath
    txtPath = opt.txtPath
    xmlPath = os.path.join(picPath, "xml")
    create_dir_not_exist(xmlPath)

    txtToXml(txtPath, picPath, xmlPath)

    # allPicPath = "/home/hs/data/0_paimian/batch"
    # allXmlPath = "/home/hs/yolov5/yolov5/runs/detect"
    # for picFile in os.listdir(allPicPath):
    #     try:
    #         txtPath = allXmlPath + '/' + picFile + '/labels'
    #         xmlPath = allPicPath + '/' + picFile + '/xmls'
    #         create_dir_not_exist(xmlPath)
    #         picPath = allPicPath + '/' + picFile
    #         # print("txtPath:", txtPath)
    #         # print("xmlPath:", xmlPath)
    #         # print("picPath:", picPath)
    #         txtToXml(txtPath, picPath, xmlPath)
    #     except Exception as e:
    #         print(e)
    #         continue
    # pass

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值