json文件的基本类型详解,json的共享部分是什么,如何转为coco数据集?
JSON是全称为JavaScript Object Notation,是一种有条理,易于访问的存储信息的方法。它为我们提供了一个可读的数据集合,我们可以通过合理的方式来访问这些数据。JSON文件可以存储简单的数据结构和对象。JSON文件在许多不同的编程API中都被支持。如今,JSON已被用于许多Web应用程序来进行数据交换,并且它们实际上不会在硬盘驱动器上保存“.JSON”文件,可以在互联网连接的计算机之间进行数据交换。某些应用程序允许用户将其保存在“.JSON”文件中
提到json,我们首先应该想到的是COCO格式的数据集。
COCO的 全称是Common Objects in COntext,是微软团队提供的一个可以用来进行图像识别的数据集。MS COCO数据集中的图像分为训练、验证和测试集。COCO通过在Flickr上搜索80个对象类别和各种场景类型来收集图像,其使用了亚马逊的Mechanical Turk(AMT)。
COCO通过大量使用Amazon Mechanical Turk来收集数据。COCO数据集现在有3种标注类型:object instances(目标实例), object keypoints(目标上的关键点), 和image captions(看图说话),使用JSON文件存储。
基本的JSON结构体类型(共享部分)有object instances(目标实例)、object keypoints(目标上的关键点)、image captions(看图说话)这3种类型共享这些基本类型:info、image、license。而annotation类型则呈现出了多态:
{
"info": info, # dict
"licenses": [license], # list ,内部是dict
"images": [image], # list ,内部是dict
"annotations": [annotation], # list ,内部是dict
"categories": # list ,内部是dict
}
info{ # 数据集信息描述
"description": str, # 数据集描述
"url": str, # 下载地址
"version": str, # 版本
"year": int, # 年份
"contributor": str, # 提供者
"date_created": str # 数据创建日期
},
license{
"id": int,
"name": str,
"url": str,
}
image{
"id": int,# 图片的ID编号(每张图片ID是唯一的)
"width": int,# 宽
"height": int,# 高
"file_name": str,# 图片名
"license": int,
"flickr_url": str,# flickr网路地址
"coco_url": str,
"date_captured": datetime,# 数据获取日期
}
dic1 = {"name":"abc", "age":"123"}
dic2 = {"name":"efg", "age":"456"}
dic_full = [dic1, dic2]
# 写成 .json 文件
with open("df.json", "w") as file:
json.dump(dic_full, file)
# 读取文件
with open("df.json", "r") as f:
for idx, line in enumerate(f):
# print(idx)
# print(line)
d = json.loads(line)
print(d)
print(type(d))
[{'name': 'abc', 'age': '123'}, {'name': 'efg', 'age': '456'}]
<class 'list'>
读取具体项目:
for i, j in enumerate(d):
print(i, type(i))
print(j, type(j))
0 <class 'int'>
{'name': 'abc', 'age': '123'} <class 'dict'>
1 <class 'int'>
{'name': 'efg', 'age': '456'} <class 'dict'>