一、软件安装
labelimg安装(anaconda)
方法一、
pip install labelImg
方法二、
pip install PyQt5 -i https://pypi.tuna.tsinghua.edu.cn/simple/
pip install pyqt5-tools -i https://pypi.tuna.tsinghua.edu.cn/simple/
pip install lxml -i https://pypi.tuna.tsinghua.edu.cn/simple/
pip install labelImg -i https://pypi.tuna.tsinghua.edu.cn/simple/
二、软件标注
打开labelimg环境,运行如下命令即可:
conda activate labelimg
labelimg
三、格式转换
标签文件.xml转换为.txt格式
import xml.etree.ElementTree as ET
import os
from os import getcwd
classes = ["pikaqiu",'apple'] # 改成自己的类别
abs_path = os.getcwd()
print(abs_path)
def convert(size, box):
dw = 1. / (size[0])
dh = 1. / (size[1])
x = (box[0] + box[1]) / 2.0 - 1
y = (box[2] + box[3]) / 2.0 - 1
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return x, y, w, h
def convert_annotation(xml_id_path,txtpath):
in_file = open(xml_id_path, encoding='UTF-8')
txtpath1=os.path.join(txtpath,xml_id_path.split('\\')[-1].split('.')[0]+'.txt')
out_file = open(txtpath1, 'w')
tree = ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
for obj in root.iter('object'):
difficult = obj.find('difficult').text
#difficult = obj.find('Difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult) == 1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
float(xmlbox.find('ymax').text))
b1, b2, b3, b4 = b
# 标注越界修正
if b2 > w:
b2 = w
if b4 > h:
b4 = h
b = (b1, b2, b3, b4)
bb = convert((w, h), b)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
#xml标签文档地址
xmlpath=r'C:\Users\admin\xxxxxxxx\labels_xml'
listxmls=os.listdir(xmlpath)
#txt文档保存地址
txtpath=r'C:\Users\admin\xxxxxxxx\labels_txt'
for xml_id in listxmls:
xml_id_path=os.path.join(xmlpath,xml_id)
convert_annotation(xml_id_path,txtpath)
四、训练数据参数设置
训练代码链接:项目目录预览 - yolov9 - GitCode
到yolov9主目录下的./data路径下重新编写一个.yaml文件
pikaqiu.yaml
train: /media/hadoop/xxxxxxx/VOC/images/train
val: /media/hadoop/xxxxxxxxx/VOC/images/val
test: /media/hadoop/xxxxxxxx/VOC/images/test
# number of classes
nc: 2
# class names
names: [ 'pikaqiu','apple']
将该文件填入到训练文件train.py中的数据参数设置--data中: