当前位置:网站首页>YOLO物体识别,生成数据用到的工具
YOLO物体识别,生成数据用到的工具
2022-07-02 06:34:00 【懵懂的梦花火】
YOLO物体识别,生成数据用到的工具
1 remove_spaces(去掉文件名称中的空格,将“ ”替换为“_”)
运行前:
import os
import string
def listdir(path, list_name):
for file in os.listdir(path):
file_path = os.path.join(path, file)
if os.path.isdir(file_path):
listdir(file_path, list_name)
elif os.path.splitext(file_path)[1] == '.xml' or os.path.splitext(file_path)[1] == '.jpg':
list_name.append(file_path)
if (1):
# 1. 读取xml文件
file_path = "C:/31/darknet-master/build/darknet/x64/data/voc/VOC2020/JPEGImages"
list_name = []
listdir(file_path, list_name)
# print(list_name)
size = 0
str_ = ""
for file_name in list_name:
# print(tree)
# 2. 属性修改
# A. 找到父节点
str_ = ((file_name).replace(' ','_'))
os.rename(file_name, str_)
size+=1
print( file_name + " " + str(size) +' succeful! ')
运行后:
2 change_xml (修改xml文件中出现的中文和路径错误)
生成数据集时出现如下错误:
运行前:
from xml.etree.ElementTree import ElementTree, Element
def read_xml(in_path):
'''''读取并解析xml文件
in_path: xml路径
return: ElementTree'''
tree = ElementTree()
tree.parse(in_path)
return tree
def write_xml(tree, out_path):
'''''将xml文件写出
tree: xml树
out_path: 写出路径'''
tree.write(out_path, encoding="utf-8", xml_declaration=True)
def if_match(node, kv_map):
'''''判断某个节点是否包含所有传入参数属性
node: 节点
kv_map: 属性及属性值组成的map'''
for key in kv_map:
if node.get(key) != kv_map.get(key):
return False
return True
# ---------------search -----
def find_nodes(tree, path):
'''''查找某个路径匹配的所有节点
tree: xml树
path: 节点路径'''
return tree.findall(path)
def get_node_by_keyvalue(nodelist, kv_map):
'''''根据属性及属性值定位符合的节点,返回节点
nodelist: 节点列表
kv_map: 匹配属性及属性值map'''
result_nodes = []
for node in nodelist:
if if_match(node, kv_map):
result_nodes.append(node)
return result_nodes
# ---------------change -----
def change_node_properties(nodelist, kv_map, is_delete=False):
'''''修改/增加 /删除 节点的属性及属性值
nodelist: 节点列表
kv_map:属性及属性值map'''
for node in nodelist:
for key in kv_map:
if is_delete:
if key in node.attrib:
del node.attrib[key]
else:
node.set(key, kv_map.get(key))
def change_node_text(nodelist, text, is_add=False, is_delete=False):
'''''改变/增加/删除一个节点的文本
nodelist:节点列表
text : 更新后的文本'''
for node in nodelist:
if is_add:
node.text += text
elif is_delete:
node.text = ""
else:
node.text = text
def create_node(tag, property_map, content):
'''''新造一个节点
tag:节点标签
property_map:属性及属性值map
content: 节点闭合标签里的文本内容
return 新节点'''
element = Element(tag, property_map)
element.text = content
return element
def add_child_node(nodelist, element):
'''''给一个节点添加子节点
nodelist: 节点列表
element: 子节点'''
for node in nodelist:
node.append(element)
def del_node_by_tagkeyvalue(nodelist, tag, kv_map):
'''''同过属性及属性值定位一个节点,并删除之
nodelist: 父节点列表
tag:子节点标签
kv_map: 属性及属性值列表'''
for parent_node in nodelist:
children = parent_node.getchildren()
for child in children:
if child.tag == tag and if_match(child, kv_map):
parent_node.remove(child)
import os
import string
def listdir(path, list_name):
for file in os.listdir(path):
file_path = os.path.join(path, file)
if os.path.isdir(file_path):
listdir(file_path, list_name)
elif os.path.splitext(file_path)[1] == '.xml':
list_name.append(file_path)
if (1):
# 1. 读取xml文件
file_path = "C:/Users/zhumengbo/Desktop/new/original"
list_name = []
listdir(file_path, list_name)
# print(list_name)
size = 0
str_ = ""
for strPath in list_name:
tree = read_xml(strPath)
# print(tree)
# 2. 属性修改
# A. 找到父节点
nodes = find_nodes(tree, "path")
str_ = ((strPath).replace('\\','/'))
change_node_text(nodes, str_)
write_xml(tree, str_)
size+=1
print( str_ + " " + str(size) +' succeful! ')
修改文件名:将 新建文件夹 修改 为fruit_plates,程序自动根据当前新的文件夹名称来修改原本的路径
运行后:
3 去掉多余jpg和xml文件
在数据集中,jpg文件和xml需要成对出现
import os
import string
def listdir(path, list_name, format):
for file in os.listdir(path):
file_path = os.path.join(path, file)
if os.path.isdir(file_path):
listdir(file_path, list_name)
elif os.path.splitext(file_path)[1] == format:
list_name.append(os.path.splitext(file_path)[0].split('\\')[-1])
def aligning(path):
list_xml_name = []
list_jpg_name = []
listdir(path, list_xml_name, '.xml')
listdir(path, list_jpg_name, '.jpg')
# 删除多余的xml文件
for file_name_xml in list_xml_name:
is_exit = 0
for file_name_jpg in list_jpg_name:
if file_name_xml == file_name_jpg:
is_exit = 1
break
if is_exit == 0:
current_file_path = path + '/' + file_name_xml + '.xml'
if os.path.exists(current_file_path): # 如果文件存在
# 删除文件,可使用以下两种方法。
os.remove(current_file_path)
# 删除多余的jpg文件
for file_name_jpg in list_jpg_name:
is_exit = 0
for file_name_xml in list_xml_name:
if file_name_jpg == file_name_xml:
is_exit = 1
break
if is_exit == 0:
current_file_path = path + '/' + file_name_jpg + '.xml'
if os.path.exists(current_file_path): # 如果文件存在
# 删除文件,可使用以下两种方法。
os.remove(current_file_path)
if (1):
# 1. 读取xml文件
aligning('C:/Users//Desktop/img/label20201009/label/yellow_belt/yellow_belt2_xml')
边栏推荐
- 西瓜书--第五章.神经网络
- From concept to method, the statistical learning method -- Chapter 3, k-nearest neighbor method
- ClassFile - Attributes - Code
- MySql报错:unblock with mysqladmin flush-hosts
- Solutions to Chinese garbled code in CMD window
- Enterprise level SaaS CRM implementation
- 告别996,IDEA中必装插件有哪些?
- 机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案
- 记录一下初次使用Xray的有趣过程
- [go practical basis] gin efficient artifact, how to bind parameters to structures
猜你喜欢
"Redis source code series" learning and thinking about source code reading
A detailed explanation takes you to reproduce the statistical learning method again -- Chapter 2, perceptron model
数构(C语言)——第四章、矩阵的压缩存储(下)
每天睡前30分钟阅读Day6_Day6_Date_Calendar_LocalDate_TimeStamp_LocalTime
Redis installation and deployment (windows/linux)
Matplotlib剑客行——布局指南与多图实现(更新)
Machine learning practice: is Mermaid a love movie or an action movie? KNN announces the answer
微服务实战|负载均衡组件及源码分析
[go practical basis] how to customize and use a middleware in gin
BugkuCTF-web21(详细解题思路及步骤)
随机推荐
Knife4j 2. Solution to the problem of file control without selection when uploading x version files
c语言编程题
Oracle delete tablespace and user
微服务实战|声明式服务调用OpenFeign实践
每天睡觉前30分钟阅读_day4_Files
图像识别-数据标注
分享一篇博客(水一篇博客)
Matplotlib swordsman - a stylist who can draw without tools and code
西瓜书--第六章.支持向量机(SVM)
Mysql默认事务隔离级别及行锁
BugkuCTF-web16(备份是个好习惯)
破茧|一文说透什么是真正的云原生
C语言之判断直角三角形
双非本科生进大厂,而我还在底层默默地爬树(上)
Read 30 minutes before going to bed every day_ day3_ Files
攻防世界-Web进阶区-unserialize3
VIM操作命令大全
JVM instruction mnemonic
微服务实战|熔断器Hystrix初体验
Machine learning practice: is Mermaid a love movie or an action movie? KNN announces the answer