当前位置: 首页> 教育> 就业 > 热门网站建设代理_宁波seo网络推广推荐公众号_公众号推广方法_百度seo竞价推广是什么

热门网站建设代理_宁波seo网络推广推荐公众号_公众号推广方法_百度seo竞价推广是什么

时间:2025/7/13 12:25:11来源:https://blog.csdn.net/qq_43757976/article/details/144980112 浏览次数:1次
热门网站建设代理_宁波seo网络推广推荐公众号_公众号推广方法_百度seo竞价推广是什么

1、yolo转xml

from xml.dom.minidom import Document
import os
import cv2
# def makexml(txtPath, xmlPath, picPath):  # txt所在文件夹路径,xml文件保存路径,图片所在文件夹路径
def makexml(picPath, txtPath, xmlPath):  # txt所在文件夹路径,xml文件保存路径,图片所在文件夹路径"""此函数用于将yolo格式txt标注文件转换为voc格式xml标注文件"""dic = {'0': "plane",  # 创建字典用来对类型进行转换'1': "baseball diamond",  # 此处的字典要与自己的classes.txt文件中的类对应,且顺序要一致'2': "bridge",'3': "ground track field",'4': "small vehicle",'5': "large vehicle",'6': "ship",'7': "tennis court",'8': "basketball court",'9': "storage tank",'10': "soccer ball field",'11': "roundabout",'12': "harbor",'13': "swimming pool",'14': "helicopter",}files = os.listdir(txtPath)for i, name in enumerate(files):xmlBuilder = Document()annotation = xmlBuilder.createElement("annotation")  # 创建annotation标签xmlBuilder.appendChild(annotation)txtFile = open(txtPath + name)txtList = txtFile.readlines()img = cv2.imread(picPath + name[0:-4] + ".jpg")Pheight, Pwidth, Pdepth = img.shapefolder = xmlBuilder.createElement("folder")  # folder标签foldercontent = xmlBuilder.createTextNode("driving_annotation_dataset")folder.appendChild(foldercontent)annotation.appendChild(folder)  # folder标签结束filename = xmlBuilder.createElement("filename")  # filename标签filenamecontent = xmlBuilder.createTextNode(name[0:-4] + ".jpg")filename.appendChild(filenamecontent)annotation.appendChild(filename)  # filename标签结束size = xmlBuilder.createElement("size")  # size标签width = xmlBuilder.createElement("width")  # size子标签widthwidthcontent = xmlBuilder.createTextNode(str(Pwidth))width.appendChild(widthcontent)size.appendChild(width)  # size子标签width结束height = xmlBuilder.createElement("height")  # size子标签heightheightcontent = xmlBuilder.createTextNode(str(Pheight))height.appendChild(heightcontent)size.appendChild(height)  # size子标签height结束depth = xmlBuilder.createElement("depth")  # size子标签depthdepthcontent = xmlBuilder.createTextNode(str(Pdepth))depth.appendChild(depthcontent)size.appendChild(depth)  # size子标签depth结束annotation.appendChild(size)  # size标签结束for j in txtList:oneline = j.strip().split(" ")object = xmlBuilder.createElement("object")  # object 标签picname = xmlBuilder.createElement("name")  # name标签namecontent = xmlBuilder.createTextNode(dic[oneline[0]])picname.appendChild(namecontent)object.appendChild(picname)  # name标签结束pose = xmlBuilder.createElement("pose")  # pose标签posecontent = xmlBuilder.createTextNode("Unspecified")pose.appendChild(posecontent)object.appendChild(pose)  # pose标签结束truncated = xmlBuilder.createElement("truncated")  # truncated标签truncatedContent = xmlBuilder.createTextNode("0")truncated.appendChild(truncatedContent)object.appendChild(truncated)  # truncated标签结束difficult = xmlBuilder.createElement("difficult")  # difficult标签difficultcontent = xmlBuilder.createTextNode("0")difficult.appendChild(difficultcontent)object.appendChild(difficult)  # difficult标签结束bndbox = xmlBuilder.createElement("bndbox")  # bndbox标签xmin = xmlBuilder.createElement("xmin")  # xmin标签mathData = int(((float(oneline[1])) * Pwidth + 1) - (float(oneline[3])) * 0.5 * Pwidth)xminContent = xmlBuilder.createTextNode(str(mathData))xmin.appendChild(xminContent)bndbox.appendChild(xmin)  # xmin标签结束ymin = xmlBuilder.createElement("ymin")  # ymin标签mathData = int(((float(oneline[2])) * Pheight + 1) - (float(oneline[4])) * 0.5 * Pheight)yminContent = xmlBuilder.createTextNode(str(mathData))ymin.appendChild(yminContent)bndbox.appendChild(ymin)  # ymin标签结束xmax = xmlBuilder.createElement("xmax")  # xmax标签mathData = int(((float(oneline[1])) * Pwidth + 1) + (float(oneline[3])) * 0.5 * Pwidth)xmaxContent = xmlBuilder.createTextNode(str(mathData))xmax.appendChild(xmaxContent)bndbox.appendChild(xmax)  # xmax标签结束ymax = xmlBuilder.createElement("ymax")  # ymax标签mathData = int(((float(oneline[2])) * Pheight + 1) + (float(oneline[4])) * 0.5 * Pheight)ymaxContent = xmlBuilder.createTextNode(str(mathData))ymax.appendChild(ymaxContent)bndbox.appendChild(ymax)  # ymax标签结束object.appendChild(bndbox)  # bndbox标签结束annotation.appendChild(object)  # object标签结束f = open(xmlPath + name[0:-4] + ".xml", 'w')xmlBuilder.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')f.close()
if __name__ == "__main__":picPath = "./images/test/"  # 图片所在文件夹路径,后面的/一定要带上txtPath = "./labels/test/"  # txt所在文件夹路径,后面的/一定要带上xmlPath = "./annotations/test/"  # xml文件保存路径,后面的/一定要带上makexml(picPath, txtPath, xmlPath)

xml格式

<?xml version="1.0" encoding="utf-8"?><annotation><folder>driving_annotation_dataset</folder><filename>0.jpg</filename><size><width>640</width><height>640</height><depth>3</depth></size><object><name>plane</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>315</xmin><ymin>248</ymin><xmax>422</xmax><ymax>304</ymax></bndbox></object></annotation>

2、xml转dota

import os
import xml.etree.ElementTree as ETdef convert_voc_to_dota_simple(xml_folder, output_folder):# 创建输出文件夹(如果不存在)if not os.path.exists(output_folder):os.makedirs(output_folder)# 遍历 XML 文件夹中的所有文件for xml_file in os.listdir(xml_folder):if xml_file.endswith('.xml'):  # 只处理 XML 文件# 解析 XML 文件tree = ET.parse(os.path.join(xml_folder, xml_file))root = tree.getroot()# 初始化 DOTA 格式的字符串列表dota_annotations = []# 遍历 XML 中的 'object' 元素for obj in root.iter('object'):# 获取标准矩形边界框坐标bndbox = obj.find('bndbox')# 获取类别和难度级别category = obj.find('name').textdifficult = obj.find('difficult').text# 提取矩形边界框的坐标xmin = int(bndbox.find('xmin').text)ymin = int(bndbox.find('ymin').text)xmax = int(bndbox.find('xmax').text)ymax = int(bndbox.find('ymax').text)# 将矩形边界框的四个角作为旋转边界框的顶点coords = [xmin, ymin,  # 左上角xmax, ymin,  # 右上角xmax, ymax,  # 右下角xmin, ymax  # 左下角]# 将坐标、类别和难度级别转换为 DOTA 格式dota_format = ' '.join(map(str, coords + [category, difficult]))dota_annotations.append(dota_format)# 写入转换后的信息到 TXT 文件output_file_path = os.path.join(output_folder, xml_file.replace('.xml', '.txt'))with open(output_file_path, 'w') as f:for annotation in dota_annotations:f.write("%s\n" % annotation)  # 每个注释占一行# 调用函数,传入 XML 文件夹路径和输出文件夹路径
xml_folder = './annotations/val'  # 请替换为您的 XML 文件夹路径
output_folder = './DOTA_labels/val'  # 请替换为您希望保存输出 TXT 文件的文件夹路径
convert_voc_to_dota_simple(xml_folder, output_folder)

3、验证dota 画框

import xml.etree.ElementTree as ET
import os
import math
import cv2
import numpy as np
import dota_utils as util
import randomimg_root = r"./images/train/"
label_root = r"./DOTA_labels/train/"
drawed_img_root = r"./DOTA_labels_drawed/train/"
image_name = os.listdir(img_root)
for i in range(len(image_name)):img_path = os.path.join(img_root, image_name[i])label_path = os.path.join(label_root, image_name[i].split('.')[0] + '.txt')drawed_img_path = os.path.join(drawed_img_root, image_name[i])objects = util.parse_dota_poly(label_path)print(objects)img = cv2.imread(img_path)poly = []for i in range(len(objects)):poly.append(np.array(objects[i]['poly'], dtype=np.int32))print(poly)cv2.polylines(img, poly, isClosed=True, color=(35, 37, 133), thickness=2)drawed_img_path = drawed_img_path.replace('.bmp', '.png')cv2.imwrite(drawed_img_path, img)
关键字:热门网站建设代理_宁波seo网络推广推荐公众号_公众号推广方法_百度seo竞价推广是什么

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: