from lxml import etree
import cv2
# 读取 xml 文件信息,并返回字典形式
def parse_xml_to_dict(xml):
if len(xml) == 0: # 遍历到底层,直接返回 tag对应的信息
return {xml.tag: xml.text}
result = {}
for child in xml:
child_result = parse_xml_to_dict(child) # 递归遍历标签信息
if child.tag != 'object':
result[child.tag] = child_result[child.tag]
else:
if child.tag not in result: # 因为object可能有多个,所以需要放入列表里
result[child.tag] = []
result[child.tag].append(child_result[child.tag])
return {xml.tag: result}
# xml 标注文件的可视化
def xmlShow(img, xml, save=True):
image = cv2.imread(img)
with open(xml, encoding='gb18030', errors='ignore') as fid: # 防止出现非法字符报错
xml_str = fid.read()
xml = etree.fromstring(xml_str)
data = parse_xml_to_dict(xml)["annotation"] # 读取 xml文件信息
ob = [] # 存放目标信息
for i in data['object']: # 提取检测框
name = str(i['name']) # 检测的目标类别
bbox = i['bndbox']
xmin = int(bbox['xmin'])
ymin = int(bbox['ymin'])
xmax = int(bbox['xmax'])
ymax = int(bbox['ymax'])
tmp = [name, xmin, ymin, xmax, ymax] # 单个检测框
ob.append(tmp)
# 绘制检测框
for name, x1, y1, x2, y2 in ob:
cv2.rectangle(image, (x1, y1), (x2, y2), color=(255, 0, 0), thickness=2) # 绘制矩形框
cv2.putText(image, name, (x1, y1 - 10), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=0.5, thickness=1, color=(0, 0, 255))
# 保存图像
if save:
cv2.imwrite('result.png', image)
# # 展示图像
# cv2.imshow('test', image)
# cv2.waitKey()
# cv2.destroyAllWindows()
if __name__ == "__main__":
import os
import random
img_path = './data/train/images/' # 传入图片
image = [os.path.join(img_path, i) for i in os.listdir(img_path)]
r = random.randint(0, len(image) - 1) # 生成随机索引
image_path = image[r]
labels_path = image_path.replace('images', 'labels') # 自动获取对应的 xml 标注文件
labels_path = labels_path.replace('.jpg', '.xml')
xmlShow(img=image_path, xml=labels_path, save=True)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
项目包含:硬纸板缺陷目标检测数据集(VOC标注格式的xml文件,已经做了训练集和测试集划分),数据保存按照文件夹保存,经测试,可直接用作目标检测数据集,无需额外处理。 图像分辨率为1080*1440的RGB图片,数据为纸板的破损、起泡、划痕等缺陷检测 【数据集介绍】硬纸板缺陷图像数据,1类别:detect 【数据总大小】111MB 【数据集详情】data目录下分为两个目录,train为训练数据、test为测试数据,每个目录下各有两个子文件夹。images存放数据图片,labels目录存放标注文件。训练集共845张图片和845个xml解释文件。训练集共211张图片和211个xml解释文件 【json文件】1类别的json字典文件 为了方便查看数据,提供了可视化py文件,随机传入一张图片即可绘制边界框,并且保存在当前目录。脚本无需更改,可以直接运行!
资源推荐
资源详情
资源评论
收起资源包目录
目标检测数据集:硬纸板缺陷目标检测数据集(VOC标注格式的xml文件,已经做了训练集和测试集划分) (2000个子文件)
0437.jpg 237KB
0767.jpg 228KB
0136.jpg 186KB
0403.jpg 185KB
0458.jpg 176KB
0459.jpg 172KB
0125.jpg 170KB
0034.jpg 167KB
0314.jpg 165KB
0131.jpg 165KB
0377.jpg 164KB
0123.jpg 164KB
0380.jpg 164KB
0132.jpg 163KB
0374.jpg 162KB
0443.jpg 162KB
0061.jpg 161KB
0118.jpg 160KB
0110.jpg 160KB
0376.jpg 158KB
0042.jpg 158KB
0404.jpg 157KB
0057.jpg 157KB
0318.jpg 157KB
0109.jpg 156KB
0050.jpg 156KB
0044.jpg 156KB
0124.jpg 156KB
0382.jpg 155KB
0099.jpg 155KB
0119.jpg 155KB
0412.jpg 154KB
0112.jpg 154KB
0291.jpg 154KB
0402.jpg 154KB
0111.jpg 153KB
0388.jpg 153KB
0116.jpg 153KB
0346.jpg 152KB
0033.jpg 152KB
0338.jpg 152KB
0070.jpg 151KB
0305.jpg 151KB
0370.jpg 151KB
0300.jpg 150KB
0058.jpg 150KB
0066.jpg 150KB
0117.jpg 150KB
0072.jpg 150KB
0321.jpg 150KB
0337.jpg 149KB
0415.jpg 149KB
0590.jpg 149KB
0316.jpg 149KB
0104.jpg 149KB
0069.jpg 149KB
0557.jpg 149KB
0284.jpg 148KB
0067.jpg 148KB
0701.jpg 148KB
0514.jpg 148KB
0224.jpg 148KB
0228.jpg 147KB
0219.jpg 147KB
0411.jpg 147KB
0487.jpg 147KB
0429.jpg 146KB
0408.jpg 146KB
0387.jpg 146KB
0575.jpg 146KB
0320.jpg 145KB
0339.jpg 145KB
0302.jpg 145KB
0389.jpg 145KB
0126.jpg 144KB
0094.jpg 144KB
0457.jpg 144KB
0413.jpg 144KB
0400.jpg 143KB
0579.jpg 143KB
0398.jpg 143KB
0003.jpg 143KB
0969.jpg 142KB
0567.jpg 142KB
0062.jpg 142KB
0560.jpg 142KB
0032.jpg 142KB
0588.jpg 142KB
1020.jpg 142KB
1017.jpg 141KB
0101.jpg 141KB
0322.jpg 141KB
0544.jpg 141KB
0617.jpg 141KB
1025.jpg 141KB
0414.jpg 140KB
0206.jpg 140KB
0568.jpg 140KB
0550.jpg 140KB
0715.jpg 140KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
- A2_2_B_9S_2024-05-14感谢大佬分享的资源给了我灵感,果断支持!感谢分享~
听风吹等浪起
- 粉丝: 1w+
- 资源: 1329
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功