在Anaconda Prompt终端打开,输入代码pip install labelme

安装成功之后,输入labelme,打开标注应用


选择要标注的文件夹


选择标注好文件保存的位置

标注好以后自动保存

开始标注

右键点击图片,弹出下图
第一个表示创建矩形框
如果不选择右键,直接在图中点击画图,就可以选择不规则图像

表示修改已经标注的框图


表示继续标注该文件夹下的下一张图片


标注完之后的文件格式
里面的数值分别表示所选的点point在该图片下的位置


cx和cy表示该框在这个图片中中心点位置坐标
w和h表示该框的宽和高
注意该文件数值不能直接用于yolov5的训练学习,需要进行转换归一化处理(下图博主地址:树莓派-yolo目标检测-数据的采集与标注)

格式转换


dic_labels= {'led':0,
'buzzer':1,
'teeth':2,
'path_json':'labs',
'ratio':0.9}
0.9表示
90%为训练数据
10%为验证数据
import os
import json
import random
import base64
import shutil
import argparse
from pathlib import Path
from glob import glob
from dic_lab import dic_labels
def generate_labels(dic_labs):
path_input_json = dic_labels['path_json']
ratio = dic_labs['ratio']
for index, labelme_annotation_path in enumerate(glob(f'{path_input_json}/*.json')):
# 读取文件名
image_id = os.path.basename(labelme_annotation_path).rstrip('.json')
# 计算是train 还是 valid
train_or_valid = 'train' if random.random() < ratio else 'valid'
# 读取labelme格式的json文件
labelme_annotation_file = open(labelme_annotation_path, 'r')
labelme_annotation = json.load(labelme_annotation_file)
# yolo 格式的 lables
yolo_annotation_path = os.path.join(train_or_valid, 'labels',image_id + '.txt')
yolo_annotation_file = open(yolo_annotation_path, 'w')
# yolo 格式的图像保存
yolo_image = base64.decodebytes(labelme_annotation['imageData'].encode())
yolo_image_path = os.path.join(train_or_valid, 'images', image_id + '.jpg')
yolo_image_file = open(yolo_image_path, 'wb')
yolo_image_file.write(yolo_image)
yolo_image_file.close()
# 获取位置信息
for shape in labelme_annotation['shapes']:
if shape['shape_type'] != 'rectangle':
print(
f'Invalid type `{shape["shape_type"]}` in annotation `annotation_path`')
continue
points = shape['points']
scale_width = 1.0 / labelme_annotation['imageWidth']
scale_height = 1.0 / labelme_annotation['imageHeight']
width = (points[1][0] - points[0][0]) * scale_width
height = (points[1][1] - points[0][1]) * scale_height
x = ((points[1][0] + points[0][0]) / 2) * scale_width
y = ((points[1][1] + points[0][1]) / 2) * scale_height
object_class = dic_labels[shape['label']]
yolo_annotation_file.write(f'{object_class} {x} {y} {width} {height}\n')
yolo_annotation_file.close()
print("creat lab %d : %s"%(index,image_id))
if __name__ == "__main__":
os.makedirs(os.path.join("train",'images'),exist_ok=True)
os.makedirs(os.path.join("train",'labels'),exist_ok=True)
os.makedirs(os.path.join("valid",'images'),exist_ok=True)
os.makedirs(os.path.join("valid",'labels'),exist_ok=True)
generate_labels(dic_labels)
文章介绍了如何在AnacondaPrompt中使用Labelme工具进行图像标注,标注后的内容需要进行转换以适应YOLOv5的训练。通过提供的Python脚本,可以将Labelme的JSON标注转换为YOLO格式,包括读取JSON文件,计算训练和验证数据的比例,以及将标注信息和图像数据写入到对应的train和valid目录中。
5307

被折叠的 条评论
为什么被折叠?



