yolov5手动标注太累,可以先标注一部分图片,训练个模型出来,然后在用模型推理图片,把结果保存起来,转为labelme格式,在微调
代码:
import os
import json
import cv2
import base64
version = '4.5.13'
flags = {}
for file in os.listdir('data/'):
if 'png' in file:
dic = {}
dic['version'] = version
dic['flags'] = flags
dic['shapes'] = []
#print(file) 1090.png
#print('data/{}'.format(file)) data/1090.png
#print(file.split('.')[0]) 1090
img = cv2.imread('data/{}'.format(file))
imageHeight, imageWidth, _ = img.shape
with open('data/{}.txt'.format(file.split('.')[0])) as f:
#print('data/{}.txt'.format(file.split('.')[0])) data/1090.txt
datas = f.readlines()
for data in datas:
#print(data) #1 0.8191 0.203355 0.130203334 0.17
shape = {}
#print(int(data[:2]))
#print(int(data[:2]) == 2)
if str(data[:2]) == '0 ':
print("000000")
shape['label'] = '0'
data = data.strip().split(' ')
x = float(data[1]) * imageWidth
y = float(data[2]) * imageHeight
w = float(data[3]) * imageWidth
h = float(data[4]) * imageHeight
x1 = x - w / 2
y1 = y - h / 2
x2 = x1 + w
y2 = y1 + h
shape['points'] = [[x1, y1], [x2, y2]]
shape['shape_type'] = 'rectangle'
shape['flags'] = {}
dic['shapes'].append(shape)
if str(data[:2]) == '1 ':
print("111111")
shape['label'] = '1'
data =