当前位置:网站首页>Tools used for Yolo object recognition and data generation
Tools used for Yolo object recognition and data generation
2022-07-02 09:38:00 【Ignorant dream fireworks】
YOLO Object recognition , Tools used to generate data
1 remove_spaces( Remove the space in the file name , take “ ” Replace with “_”)
Before running :
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. Read xml file
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. Attribute changes
# A. Find the parent node
str_ = ((file_name).replace(' ','_'))
os.rename(file_name, str_)
size+=1
print( file_name + " " + str(size) +' succeful! ')
After operation :
2 change_xml ( modify xml Chinese and path errors in the file )
The following error occurred while generating the dataset :
Before running :
from xml.etree.ElementTree import ElementTree, Element
def read_xml(in_path):
''''' Read and parse xml file
in_path: xml route
return: ElementTree'''
tree = ElementTree()
tree.parse(in_path)
return tree
def write_xml(tree, out_path):
''''' take xml The document says
tree: xml Trees
out_path: Write the path '''
tree.write(out_path, encoding="utf-8", xml_declaration=True)
def if_match(node, kv_map):
''''' Judge whether a node contains all the passed in parameter attributes
node: node
kv_map: Attribute and attribute value map'''
for key in kv_map:
if node.get(key) != kv_map.get(key):
return False
return True
# ---------------search -----
def find_nodes(tree, path):
''''' Find all nodes matching a path
tree: xml Trees
path: Node path '''
return tree.findall(path)
def get_node_by_keyvalue(nodelist, kv_map):
''''' Locate the matching node according to the attribute and attribute value , Return to node
nodelist: The node list
kv_map: Match attribute and attribute value 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):
''''' modify / increase / Delete Attributes and attribute values of nodes
nodelist: The node list
kv_map: Property and property value 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):
''''' change / increase / Delete the text of a node
nodelist: The node list
text : Updated 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):
''''' Create a new node
tag: Node labels
property_map: Property and property value map
content: The text content in the node closure label
return New node '''
element = Element(tag, property_map)
element.text = content
return element
def add_child_node(nodelist, element):
''''' Add child nodes to a node
nodelist: The node list
element: Child node '''
for node in nodelist:
node.append(element)
def del_node_by_tagkeyvalue(nodelist, tag, kv_map):
''''' Locate a node through the attribute and attribute value , And delete it
nodelist: List of parent nodes
tag: Child node label
kv_map: Attribute and attribute value list '''
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. Read xml file
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. Attribute changes
# A. Find the parent node
nodes = find_nodes(tree, "path")
str_ = ((strPath).replace('\\','/'))
change_node_text(nodes, str_)
write_xml(tree, str_)
size+=1
print( str_ + " " + str(size) +' succeful! ')
Change file name : take New folder modify by fruit_plates, The program automatically modifies the original path according to the current new folder name
After operation :
3 Get rid of the excess jpg and xml file
In the data set ,jpg Document and xml You need to be in pairs
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')
# Remove redundant xml file
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): # If the file exists
# Delete file , Two methods can be used .
os.remove(current_file_path)
# Remove redundant jpg file
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): # If the file exists
# Delete file , Two methods can be used .
os.remove(current_file_path)
if (1):
# 1. Read xml file
aligning('C:/Users//Desktop/img/label20201009/label/yellow_belt/yellow_belt2_xml')
边栏推荐
- zk配置中心---Config Toolkit配置与使用
- Mysql 多列IN操作
- Oracle modify database character set
- Solution to amq4036 error in remote connection to IBM MQ
- Customize redis connection pool
- How to choose between efficiency and correctness of these three implementation methods of distributed locks?
- View the port of the application published by was
- 三相逆变器离网控制——PR控制
- Read 30 minutes before going to bed every day_ day4_ Files
- What is the function of laravel facade
猜你喜欢
Creation and jump of activity
Enterprise level SaaS CRM implementation
Break the cocoon | one article explains what is the real cloud primordial
Number structure (C language -- code with comments) -- Chapter 2, linear table (updated version)
Redis installation and deployment (windows/linux)
TD联合Modelsim进行功能仿真
How to use pyqt5 to make a sensitive word detection tool
Read 30 minutes before going to bed every day_ day4_ Files
三相并网逆变器PI控制——离网模式
Typeerror: X () got multiple values for argument 'y‘
随机推荐
自定義Redis連接池
Creation and jump of activity
记录一下初次使用Xray的有趣过程
企业级SaaS CRM实现
C语言之判断直角三角形
cmake的命令-官方文档
DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!
FragmentTabHost实现房贷计算器界面
How to use PHP spoole to implement millisecond scheduled tasks
微服务实战|Eureka注册中心及集群搭建
A detailed explanation takes you to reproduce the statistical learning method again -- Chapter 2, perceptron model
MySQL事务
Oracle modify database character set
web安全与防御
Knowledge points are very detailed (code is annotated) number structure (C language) -- Chapter 3, stack and queue
三相并网逆变器PI控制——离网模式
AMQ 4043 solution for errors when using IBM MQ remote connection
C语言之最小数
Double non undergraduate students enter the factory, while I am still quietly climbing trees at the bottom (Part 1)
Idempotent design of Internet API interface