一.半自动标注
yolov5(5.0)
修改detect.py如下,其他参数根据自己情况改
parser.add_argument(‘–save-txt’, default=‘exp’, help=‘save results to *.txt’)
检测结果,其中labels文件夹内保存YOLO格式标签
二.YOLO标签转 VOC标签
路径对应自己路径修改
src_img_root:图片路径(需要获取图片shape)
src_label_root:输入label路径(YOLO的txt格式)
dst_label_root:输出label路径(PASCAL VOC的xml格式)
import os
import cv2
import pathlib
from xml.dom.minidom import Document
def search_file_name(file_path, img_root):
file_format = ['.jpg', '.JPG', '.png', '.PNG', '.JPEG', '.jpeg']
file_path = pathlib.Path(file_path)
file_name = file_path.stem
for endsw in file_format:
file_judge = file_name + endsw
if os.path.isfile(os.path.join(img_root, file_judge)):
return file_name, endsw
return 'stop', 0
def yolo2voc(src_img_root, src_label_root, dst_label_root, class_map):
not_have_img = []
if not os.path.exists(dst_label_root):
os.makedirs(dst_label_root)
for i, src_label_name in enumerate(os.listdir(src_label_root)):
xmlBuilder = Document()
annotation = xmlBuilder.createElement("annotation")
xmlBuilder.appendChild(annotation)
src_label_path = os.path.join(src_label_root, src_label_name)
file_name, endsw = search_file_name(src_label_path, src_img_root)
if file_name == 'stop':
not_have_img.append(os.path.basename(src_label_path))
continue
with open(src_label_path, 'r') as fr:
txtlines = fr.readlines()
src_img_name = file_name + endsw
src_img_path = os.path.join(src_img_root, src_img_name)
image_np = cv2.imread(src_img_path)
Pheight, Pwidth, Pdepth = image_np.shape
folder = xmlBuilder.createElement("folder")
foldercontent = xmlBuilder.createTextNode("driving_annotation_dataset")
folder.appendChild(foldercontent)
annotation.appendChild(folder)
filename = xmlBuilder.createElement("filename")
filenamecontent = xmlBuilder.createTextNode(src_img_name)
filename.appendChild(filenamecontent)
annotation.appendChild(filename)
size = xmlBuilder.createElement("size")
width = xmlBuilder.createElement("width")
widthcontent = xmlBuilder.createTextNode(str(Pwidth))
width.appendChild(widthcontent)
size.appendChild(width)
height = xmlBuilder.createElement("height")
heightcontent = xmlBuilder.createTextNode(str(Pheight))
height.appendChild(heightcontent)
size.appendChild(height)
depth = xmlBuilder.createElement("depth")
depthcontent = xmlBuilder.createTextNode(str(Pdepth))
depth.appendChild(depthcontent)
size.appendChild(depth)
annotation.appendChild(size)
for line in txtlines:
oneline = line.strip().split(" ")
object = xmlBuilder.createElement("object")
picname = xmlBuilder.createElement("name")
namecontent = xmlBuilder.createTextNode(class_map[oneline[0]])
picname.appendChild(namecontent)
object.appendChild(picname)
pose = xmlBuilder.createElement("pose")
posecontent = xmlBuilder.createTextNode("Unspecified")
pose.appendChild(posecontent)
object.appendChild(pose)
truncated = xmlBuilder.createElement("truncated")
truncatedContent = xmlBuilder.createTextNode("0")
truncated.appendChild(truncatedContent)
object.appendChild(truncated)
difficult = xmlBuilder.createElement("difficult")
difficultcontent = xmlBuilder.createTextNode("0")
difficult.appendChild(difficultcontent)
object.appendChild(difficult)
bndbox = xmlBuilder.createElement("bndbox")
xmin = xmlBuilder.createElement("xmin")
mathData = int(((float(oneline[1])) * Pwidth + 1) - (float(oneline[3])) * 0.5 * Pwidth)
xminContent = xmlBuilder.createTextNode(str(mathData))
xmin.appendChild(xminContent)
bndbox.appendChild(xmin)
ymin = xmlBuilder.createElement("ymin")
mathData = int(((float(oneline[2])) * Pheight + 1) - (float(oneline[4])) * 0.5 * Pheight)
yminContent = xmlBuilder.createTextNode(str(mathData))
ymin.appendChild(yminContent)
bndbox.appendChild(ymin)
xmax = xmlBuilder.createElement("xmax")
mathData = int(((float(oneline[1])) * Pwidth + 1) + (float(oneline[3])) * 0.5 * Pwidth)
xmaxContent = xmlBuilder.createTextNode(str(mathData))
xmax.appendChild(xmaxContent)
bndbox.appendChild(xmax)
ymax = xmlBuilder.createElement("ymax")
mathData = int(((float(oneline[2])) * Pheight + 1) + (float(oneline[4])) * 0.5 * Pheight)
ymaxContent = xmlBuilder.createTextNode(str(mathData))
ymax.appendChild(ymaxContent)
bndbox.appendChild(ymax)
object.appendChild(bndbox)
annotation.appendChild(object)
dst_label_path = os.path.join(dst_label_root, file_name + '.xml')
with open(dst_label_path, 'w') as f:
xmlBuilder.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
print(src_img_path)
print('not have img:', not_have_img, '\n', not_have_img.__len__())
if __name__ == "__main__":
src_img_root = "/home/sunjiadong/桌面/yolov5-5.0/yolov5-5.0/images"
src_label_root = "/home/sunjiadong/桌面/yolov5-5.0/yolov5-5.0/labels"
dst_label_root = "/home/sunjiadong/桌面/yolov5-5.0/yolov5-5.0/voc"
class_map = {'0': "badminton"}
yolo2voc(src_img_root, src_label_root, dst_label_root, class_map)
labelimg工具查看
1.未知原因 labelimg-master工具打不开YOLO标签
2.打开VOC标签,效果还好
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)