1.准备数据
1.1 为数据打标签
假设你已经有了自己要训练的照片,我们可以使用lableme或者其他工具为图像标注,这里不再过多演示,因为标注的数据是json格式而YOLO需要的是下面格式的数据(类别 矩形框的中心点坐标x,y 框的长宽)
所以需要用脚本进行数据的转换如下:
# 处理labelme多边形矩阵的标注 json转化txt
import json
import os
name2id = {'People': 0, 'NoPeople': 1} #此处需要根据你自己的数据集类型进行修改
def convert(img_size, box):
dw = 1. / (img_size[0])
dh = 1. / (img_size[1])
x = (box[0] + box[2]) / 2.0
y = (box[1] + box[3]) / 2.0
w = abs(box[2] - box[0])
h = abs(box[3] - box[1])
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return (x, y, w, h)
def decode_json(json_floder_path, txt_outer_path, json_name):
txt_name = txt_outer_path + json_name[:-5] + '.txt'
with open(txt_name, 'w') as f:
json_path = os.path.join(json_floder_path, json_name) # os路径融合
data = json.load(open(json_path, 'r', encoding='gb2312', errors='ignore'))
img_w = data['imageWidth'] # 图片的高
img_h = data['imageHeight'] # 图片的宽
isshape_type = data['shapes'][0]['shape_type']
print(isshape_type)
for i in data['shapes']:
label_name = i['label'] # 得到json中你标记的类名
if (i['shape_type'] == 'polygon'): # 数据类型为多边形 需要转化为矩形
x_max = 0
y_max = 0
x_min = 100000
y_min = 100000
for lk in range(len(i['points'])):
x1 = float(i['points'][lk][0])
y1 = float(i['points'][lk][1])
# print(x1)
if x_max < x1:
x_max = x1
if y_max < y1:
y_max = y1
if y_min > y1:
y_min = y1
if x_min > x1:
x_min = x1
bb = (x_min, y_max, x_max, y_min)
if (i['shape_type'] == 'rectangle'