当前位置: 首页 > wzjs >正文

网站的电子画册怎么做资源搜索

网站的电子画册怎么做,资源搜索,网站维护公司,广州网站优化公司1. 函数定义和注释 def makexml(picPath, txtPath, xmlPath): """此函数用于将yolo格式txt标注文件转换为voc格式xml标注文件 在自己的标注图片文件夹下建三个子文件夹,分别命名为picture、txt、xml """ 函数接收三个参数…

1. 函数定义和注释

def makexml(picPath, txtPath, xmlPath):
    """此函数用于将yolo格式txt标注文件转换为voc格式xml标注文件
    在自己的标注图片文件夹下建三个子文件夹,分别命名为picture、txt、xml
    """

函数接收三个参数:

picPath:图片所在文件夹路径。

txtPath:YOLO 格式的 .txt 文件所在文件夹路径。xmlPath:生成的 .xml 文件保存路径。

2. 类别字典

dic = {'0': "light debris",  # 创建字典用来对类型进行转换
          '1': "nest",          # 此处的字典要与自己的classes.txt文件中的类对应,且顺序要一致
       }

定义了一个字典 dic,将 YOLO 格式中的类别 ID(0, 1)映射到具体的类别名称("light debris", "nest")。

注意:这里的类别顺序需要与 YOLO 的 classes.txt 文件一致。

3. 遍历 txt 文件

files = os.listdir(txtPath)
for i, name in enumerate(files):

os.listdir(txtPath) 获取 txtPath 文件夹中的所有文件名。

使用 enumerate 遍历文件列表,i 是索引,name 是文件名(例如 image1.txt)。

4. 创建 XML 结构

xmlBuilder = Document()
annotation = xmlBuilder.createElement("annotation")  # 创建annotation标签
xmlBuilder.appendChild(annotation)

使用 xml.dom.minidom.Document 创建一个 XML 文档对象。

创建根标签 <annotation>,并将其添加到文档中。

5. 读取图片尺寸

txtFile = open(txtPath + '\\' + name)
txtList = txtFile.readlines()
for root, dirs, filename in os.walk(picPath):
    img = cv2.imread(root + '\\' + filename[i])
    Pheight, Pwidth, Pdepth = img.shape

打开当前处理的 .txt 文件,读取所有行到 txtList。

使用 os.walk 遍历 picPath 中的图片文件。

使用 OpenCV 的 cv2.imread 读取图片,img.shape 返回图片的高度 (Pheight)、宽度 (Pwidth) 和通道数 (Pdepth,通常为 3,表示 RGB)。

注意:这里假设 filename[i] 对应的图片与 name(.txt 文件名)匹配。

6. 添加基本信息到 XML

folder = xmlBuilder.createElement("folder")
foldercontent = xmlBuilder.createTextNode("driving_annotation_dataset")
folder.appendChild(foldercontent)
annotation.appendChild(folder)

filename = xmlBuilder.createElement("filename")
filenamecontent = xmlBuilder.createTextNode(name[0:-4] + ".jpg")
filename.appendChild(filenamecontent)
annotation.appendChild(filename)

size = xmlBuilder.createElement("size")
width = xmlBuilder.createElement("width")
widthcontent = xmlBuilder.createTextNode(str(Pwidth))
width.appendChild(widthcontent)
size.appendChild(width)
# 类似地添加 height 和 depth
annotation.appendChild(size)

创建 <folder> 标签,内容固定为 "driving_annotation_dataset"。

创建 <filename> 标签,内容为去掉 .txt 后缀并加上 .jpg 的文件名(假设图片格式为 .jpg)。

创建 <size> 标签,包含 <width>、<height> 和 <depth> 子标签,分别记录图片的宽度、高度和通道数。

7. 处理 YOLO 标注并转换为 VOC 格式

for j in txtList:
    oneline = j.strip().split(" ")
    object = xmlBuilder.createElement("object")
    picname = xmlBuilder.createElement("name")
    namecontent = xmlBuilder.createTextNode(dic[oneline[0]])
    picname.appendChild(namecontent)
    object.appendChild(picname)

遍历 txtList 中的每一行(每行是一个目标的标注)。

YOLO 格式的 .txt 文件每行通常是:类别ID 中心x 中心y 宽度 高度(归一化坐标)。

oneline[0] 是类别 ID,通过 dic 转换为类别名称,写入 <name> 标签。

创建 <object> 标签,表示一个目标。

8. 添加其他目标属性

pose = xmlBuilder.createElement("pose")
posecontent = xmlBuilder.createTextNode("Unspecified")
pose.appendChild(posecontent)
object.appendChild(pose)

truncated = xmlBuilder.createElement("truncated")
truncatedContent = xmlBuilder.createTextNode("0")
truncated.appendChild(truncatedContent)
object.appendChild(truncated)

difficult = xmlBuilder.createElement("difficult")
difficultcontent = xmlBuilder.createTextNode("0")
difficult.appendChild(difficultcontent)
object.appendChild(difficult)

<pose>:目标姿态,这里固定为 "Unspecified"。

<truncated>:是否被截断,0 表示未截断。

<difficult>:是否难以识别,0 表示不难。

9. 计算边界框坐标

bndbox = xmlBuilder.createElement("bndbox")
xmin = xmlBuilder.createElement("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)

YOLO 的坐标是归一化的(0 到 1),需要转换为 VOC 的绝对像素坐标。
YOLO 格式:
  oneline[1]:中心点 x 坐标(归一化)。
  oneline[2]:中心点 y 坐标(归一化)。
  oneline[3]:宽度(归一化)。
  oneline[4]:高度(归一化)。
计算公式:
  xmin = (中心x * 图片宽度 + 1) - (宽度 * 图片宽度 / 2)。
  ymin = (中心y * 图片高度 + 1) - (高度 * 图片高度 / 2)。
  xmax = (中心x * 图片宽度 + 1) + (宽度 * 图片宽度 / 2)。
  ymax = (中心y * 图片高度 + 1) + (高度 * 图片高度 / 2)。
+1 是为了避免边界问题。

10. 保存 XML 文件

f = open(xmlPath + '\\' + name[0:-4] + ".xml", 'w')
xmlBuilder.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
f.close()

将生成的 XML 文件保存到 xmlPath,文件名与 .txt 文件相同(去掉 .txt 后缀,改为 .xml)。writexml 格式化输出,带缩进和换行,编码为 UTF-8。

11. 主程序

if __name__ == "__main__":
    picPath = "D:\\w-dataset\\net\\yolonet\\images\\val\\"
    txtPath = "D:\\w-dataset\\net\\yolonet\\labels\\val\\"
    xmlPath = "D:\\w-dataset\\tielu_voc\\Annotations\\"
    makexml(picPath, txtPath, xmlPath)

完整代码

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标注文件在自己的标注图片文件夹下建三个子文件夹,分别命名为picture、txt、xml"""dic = {'0': "light debris",  # 创建字典用来对类型进行转换'1': "nest",  # 此处的字典要与自己的classes.txt文件中的类对应,且顺序要一致}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()for root,dirs,filename in os.walk(picPath):img = cv2.imread(root+ '\\'+filename[i])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 = "D:\\w-dataset\\net\\yolonet\\images\\val\\"  # 图片所在文件夹路径,后面的/一定要带上txtPath = "D:\\w-dataset\\net\\yolonet\\labels\\val\\"  # yolo txt所在文件夹路径,后面的/一定要带上xmlPath = "D:\\w-dataset\\tielu_voc\\Annotations\\"  # xml文件保存路径,后面的/一定要带上makexml(picPath, txtPath, xmlPath)

http://www.dtcms.com/wzjs/417831.html

相关文章:

  • 百度 搜索热度宁波seo资源
  • 做搜狗手机网站优化软企业营销培训课程
  • 如何联系网站站长长春seo优化
  • 便宜的网站设计企业北京网络营销推广外包
  • 网站建设忄金手指专业seo排名点击
  • 做多级分销的网站网站运营优化培训
  • 电子商务网站建设方面的论文咖啡的营销推广软文
  • 陕西精诚展览装饰有限公司seo行业网
  • 建设搜索引擎网站营销型网站策划书
  • 有哪些装修网站百度热搜广告设计公司
  • 政府网站建设的责任主体免费seo培训
  • ui做交互式网站吗网络营销个人感悟小结
  • 网站建设公司 南京西安seo外包行者seo
  • 网站建设网站建设公司南宁百度seo软件
  • 怎么开个网站网络营销策划与推广
  • 政府网站的构建与运作企业培训课程推荐
  • 全球外贸网站制作教程查询百度关键词排名
  • 网站开发技术视频多合一seo插件破解版
  • 直播视频网站如何做宁波免费seo排名优化
  • 天门网站建设合肥搜索引擎推广
  • wordpress 自动升级白帽seo是什么
  • wordpress直播功能插件seo学校培训课程
  • 北京市城市建设档案馆网站首页知名网站排名
  • 做电影ppt模板下载网站知识营销
  • 做分享衣服网站的初衷是什么百度网站名称及网址
  • 网站权重划分关键词seo排名优化推荐
  • 陕西渭南住房和城乡建设厅网站网站优化种类
  • 金山建设机械网站线上推广
  • 网站 制作莫停之科技windows优化大师
  • 网站集约化建设启示和建议外链查询