标注后的数据信息文档一般有两种格式:xml和json
读取相关json的数据的代码整理了一下发出来,方便大家使用
#json文件
{
"version": "4.5.7",
"flags": {},
"shapes": [
{
"label": "fastener",
"points": [
[
444.44444444444446,
173.91304347826087
],
[
509.1787439613527,
224.6376811594203
],
[
439.13043478260875,
313.5265700483092
],
[
372.46376811594206,
262.3188405797102
]
],
"group_id": null,
"shape_type": "polygon",
"flags": {}
}
],
"imagePath": "..\\1.jpg",
"imageData": null,
"imageHeight": 1080,
"imageWidth": 1920
}
读取上述json文件的代码
#json文件的函数 label_path:文件名称
def get_objects_string_from_label_file(label_path, quad_format):
with open(label_path, 'r') as fp:
data = json.load(fp)
objects = []
for shape in data['shapes']:
pts = np.round(np.array(shape['points'])).astype(int)
pts = np.reshape(pts, -1)
cls_name = shape['label']
objects.append([*pts,
cfg.Customer_DATA['CLASSES'].index(cls_name)])
line = ''
if quad_format:
for obj in objects:
info_str = ','.join(list(map(str, obj)))
line += ' ' + info_str
else:
for obj in objects:
coors = np.reshape(obj[:-1], (-1, 2))
left, top = np.min(coors, 0)
right, bottom = np.max(coors, 0)
info_str = ','.join(list(map(str, [left, top, right, bottom, obj[-1]])))
line += ' ' + info_str
return line
返回的line是字符串格式,要保存在一个txt文档中,后面依次读取N张图像,所有的读取到的相关数据都存在这一个txt中