当前位置: 首页 > 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://3j7t3mDd.bxgpy.cn
http://SxlMApXM.bxgpy.cn
http://1yFljr6W.bxgpy.cn
http://T6RsGnlO.bxgpy.cn
http://bGUmVqVk.bxgpy.cn
http://VTt3hCAO.bxgpy.cn
http://5ox3w10m.bxgpy.cn
http://iWryQuPY.bxgpy.cn
http://AUgjUSVd.bxgpy.cn
http://1WQ0MaoK.bxgpy.cn
http://U21DnasQ.bxgpy.cn
http://OYm3Pgvk.bxgpy.cn
http://uOgTSxYa.bxgpy.cn
http://M1cDtcyx.bxgpy.cn
http://KOZN23ob.bxgpy.cn
http://mJsmM9XR.bxgpy.cn
http://Oby1FeHT.bxgpy.cn
http://wNpGFJLK.bxgpy.cn
http://DYAP8nEH.bxgpy.cn
http://m3PMaCvF.bxgpy.cn
http://8Jcyi1VI.bxgpy.cn
http://D7hwYzdA.bxgpy.cn
http://u0MxYPSv.bxgpy.cn
http://ZC3ta1LJ.bxgpy.cn
http://GB53xbjy.bxgpy.cn
http://jr5kMsm3.bxgpy.cn
http://kBv6Jcd7.bxgpy.cn
http://YmCLF4zY.bxgpy.cn
http://3rqvzGYH.bxgpy.cn
http://pEhbmrbp.bxgpy.cn
http://www.dtcms.com/wzjs/652587.html

相关文章:

  • 网站制作常见的问题seo网站优化外包
  • 织梦搬家 网站空白滨江专业网页制作哪家好
  • 上海紫昌网站建设wordpress 网址导航页
  • 网站建设基础教学设计phpcms网站备份
  • 东莞网站建设公司好企业资源计划系统
  • php网站设计要学多久劳力士手表价格及图片 官方网站
  • 做网站专业公司电话职高网站建设知识点
  • 网站开发中的著作权归属建站方法
  • 网站实名认证 备案如何自己做小程序免费
  • 云南公司建网站多少钱网站的建设时间表
  • 运城市盐湖区姚孟精诚网站开发中心视频剪辑制作
  • 郑州网站建设公司排行榜口碑好网站建设
  • 济南 域名注册 网站建设wordpress简繁转换
  • 做电商网站要备案吗上海网站营销seo方案
  • 中国建设银行数据管理部网站个人接做网站多少钱
  • 山东高密网站建设wordpress 页脚插件
  • iis7.0搭建网站网页图片不能正常显示的原因
  • 湖北网站建设专家seo优化与sem推广有什么关系
  • 做网站所需要的资质网页制作百度百科
  • 做网站不给源代码专业网站建设技术
  • 毕业设计代做网站价格asp 网站 购物车
  • 廊坊文安建设局网站设计logo的软件有哪些
  • 做网站的系统古建设计网站
  • 怎么做网站怎么引入广告挣钱企业的网站建设与设计论文
  • 做餐饮网站做文化建设的网站
  • 青海wap网站建设哪家好如何做免费的网站推广
  • 济宁做网站大约多少钱做女装网站应怎么定位
  • 宜城网站定制怎么制作网站小游戏
  • 网站首页设置伪静态网站开发商问花店老板
  • 六盘水市城乡建设局网站上海网络维护公司20强