def labelme2mvtec(path):
"""
:param path: 路径
:return: 无
将labelme标注的图转为MVTecAD格式
"""
files = os.listdir(path)
savepath = r'E:\Projects\006_datasets\insulant\ground_truth\defect'
for i, file in enumerate(files):
print("第 %d 共 %d" % (i + 1, len(files)))
filename = file.split('.')[0]
filepath = os.path.join(path,file)
if filepath.endswith('.json'):
labelme_json = json.load(open(filepath, encoding='utf-8'))
H,W = labelme_json['imageHeight'],labelme_json['imageWidth']
mask = np.zeros((H, W, 1), dtype=np.uint8)
for i in range(len(labelme_json['shapes'])):
points = labelme_json['shapes'][i]['points']
points = np.array(points)
points = points.reshape(-1, 1, 2)
points = points.astype(np.int32)
cv2.fillConvexPoly(mask, points, (255,))
cv2.imencode('.png', mask)[1].tofile(os.path.join(savepath, '%s_mask.png' % (filename)))
if __name__ == '__main__':
path = r'E:\Projects\006_datasets\insulant\ng'
labelme2mvtec(path)
