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标注,包含训练集和验证集),数据保存按照文件夹保存,经测试,可直接用作目标检测数据集,无需额外处理。 图像分辨率为400-1000的大分辨率RGB图片,数据集为生活里的无人机检测 【数据集介绍】无人机检测,1类别:drone 【数据总大小】76 MB 【数据集详情】data目录下分为两个目录,train为训练数据、test为测试数据,每个目录下各有两个子文件夹。images存放数据图片,labels目录存放标注文件。训练集共870张图片和870个xml解释文件。验证集共217张图片和217个xml解释文件 【json文件】1类别的json字典文件 为了方便查看数据,提供了可视化py文件,随机传入一张图片即可绘制边界框,并且保存在当前目录。脚本无需更改,可以直接运行得到标注后的图像
资源推荐
资源详情
资源评论
收起资源包目录
目标检测数据集:无人机检测(VOC标注,包含训练集和验证集) (2000个子文件)
pic_331.jpg 2.77MB
pic_854.jpg 1.61MB
pic_875.jpg 1.27MB
pic_107.jpg 1.04MB
pic_766.jpg 734KB
pic_1105.jpg 640KB
pic_009.jpg 638KB
pic_723.jpg 456KB
pic_1028.jpg 388KB
pic_375.jpg 362KB
pic_548.jpg 358KB
pic_258.jpg 349KB
pic_422.jpg 342KB
pic_823.jpg 341KB
pic_940.jpg 305KB
pic_058.jpg 293KB
pic_257.jpg 288KB
pic_762.jpg 271KB
pic_669.jpg 268KB
pic_022.jpg 264KB
pic_212.jpg 263KB
moto08295.jpg 254KB
yoto03075.jpg 249KB
moto04612.jpg 247KB
moto08962.jpg 247KB
pic_970.jpg 244KB
moto07686.jpg 240KB
moto05366.jpg 239KB
moto07918.jpg 237KB
moto03481.jpg 234KB
moto03452.jpg 234KB
moto02437.jpg 233KB
moto03423.jpg 233KB
moto07860.jpg 232KB
moto11746.jpg 232KB
moto03771.jpg 232KB
moto04699.jpg 232KB
moto05801.jpg 231KB
moto08324.jpg 231KB
moto03945.jpg 230KB
moto04641.jpg 230KB
moto04728.jpg 230KB
moto00088.jpg 229KB
moto04119.jpg 229KB
moto07657.jpg 229KB
moto11717.jpg 229KB
moto03916.jpg 229KB
moto04090.jpg 228KB
moto15574.jpg 228KB
moto05743.jpg 227KB
moto15690.jpg 226KB
moto00059.jpg 225KB
moto02524.jpg 224KB
pic_943.jpg 223KB
moto11688.jpg 222KB
moto03829.jpg 221KB
moto03278.jpg 221KB
moto07889.jpg 221KB
moto03974.jpg 220KB
moto15487.jpg 220KB
moto02553.jpg 220KB
moto03510.jpg 219KB
pic_185.jpg 219KB
moto15545.jpg 218KB
moto16154.jpg 218KB
moto11630.jpg 217KB
moto03858.jpg 217KB
moto00030.jpg 217KB
moto02408.jpg 216KB
moto10528.jpg 216KB
moto03800.jpg 216KB
moto15516.jpg 215KB
moto02379.jpg 213KB
moto03626.jpg 212KB
moto02495.jpg 212KB
moto15922.jpg 211KB
moto12442.jpg 211KB
moto11601.jpg 211KB
moto02466.jpg 211KB
moto16125.jpg 209KB
moto14240.jpg 209KB
pic_746.jpg 209KB
moto15458.jpg 209KB
moto01857.jpg 208KB
moto16241.jpg 208KB
moto11195.jpg 208KB
pic_950.jpg 207KB
moto11659.jpg 207KB
moto02002.jpg 207KB
moto02031.jpg 206KB
moto14211.jpg 206KB
moto16096.jpg 205KB
moto02060.jpg 204KB
moto01973.jpg 203KB
moto11166.jpg 203KB
moto15980.jpg 202KB
moto12500.jpg 200KB
moto01915.jpg 199KB
pic_023.jpg 199KB
moto16067.jpg 199KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
听风吹等浪起
- 粉丝: 1w+
- 资源: 1289
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功