由labelme 生成的json格式如下:
其中生成imageData数据是将图像数据整体进行保存的包括数据头,除了jpg和jpeg以jpeg保存外其他格式图片都按png保存,并将保存在内存里的数据通过base64加密处理后输出到json中。
json格式如下:
{
"version": "3.16.7",
"flags": {},
"shapes": [
{
"label": "人群",
"line_color": null,
"fill_color": null,
"points": [
[
328.14285714285717,
114.04761904761904
],
[
318.61904761904765,
175.95238095238096
],
[
494.80952380952385,
314.04761904761904
],
[
504.33333333333337,
214.04761904761904
]
],
"shape_type": "polygon",
"flags": {}
},
{
"label": "天空",
"line_color": null,
"fill_color": null,
"points": [
[
842.4285714285714,
114.04761904761904
],
[
837.6666666666667,
204.52380952380963
],
[
1013.8571428571429,
318.80952380952385
],
[
999.5714285714287,
204.52380952380963
]
],
"shape_type": "polygon",
"flags": {}
}
],
"lineColor": [
0,
255,
0,
128
],
"fillColor": [
255,
0,
0,
128
],
"imagePath": "2019_6_25_9_4_29000.bmp",
"imageData": "iVBORw0KGgoAAAANSUhEUgAABgAAAAIACAAAAABhyvikAAEAAElEQVR4nDT9zY.....................,
"imageHeight": 512,
"imageWidth": 1536
}
相关保存源码如下:
with io.BytesIO() as f:
ext = osp.splitext(filename)[1].lower()
if PY2 and QT4:
format = 'PNG'
elif ext in ['.jpg', '.jpeg']:
format = 'JPEG'
else:
format = 'PNG'
image_pil.save(f, format=format)
f.seek(0)
return f.read()
def load(self, filename):
keys = [
'imageData',
'imagePath',
'lineColor',
'fillColor',
'shapes', # polygonal annotations
'flags', # image level flags
'imageHeight',
'imageWidth',
]
try:
with open(filename, 'rb' if PY2 else 'r') as f:
data = json.load(f)
if data['imageData'] is not None:
imageData = base64.b64decode(data['imageData'])
if PY2 and QT4:
imageData = utils.img_data_to_png_data(imageData)
else:
# relative path from label file to relative path from cwd
imagePath = osp.join(osp.dirname(filename), data['imagePath'])
imageData = self.load_image_file(imagePath)
flags = data.get('flags') or {}
imagePath = data['imagePath']
self._check_image_height_and_width(
base64.b64encode(imageData).decode('utf-8'),
data.get('imageHeight'),
data.get('imageWidth'),
)
lineColor = data['lineColor']
fillColor = data['fillColor']
shapes = (
(
s['label'],
s['points'],
s['line_color'],
s['fill_color'],
s.get('shape_type', 'polygon'),
s.get('flags', {}),
)
for s in data['shapes']
)
except Exception as e:
raise LabelFileError(e)
otherData = {}
for key, value in data.items():
if key not in keys:
otherData[key] = value
# Only replace data after everything is loaded.
self.flags = flags
self.shapes = shapes
self.imagePath = imagePath
self.imageData = imageData
self.lineColor = lineColor
self.fillColor = fillColor
self.filename = filename
self.otherData = otherData