一.python读取与输出json
1.python字典和json互转这里用json.dumps,还原则用json.loads,dumps以后就变为字符串了
import json# info = {'name': 'Damin', 'address': '北京', 'salary':88888}
info = {"name": "Damin", "address": "北京", "salary": 88888}
print(info["name"])info_json = json.dumps(info)
print('====info_json===', info_json)#\u,表示这两个编码是Unicode编码
print('===type(info_json):', type(info_json))#注意这个时候已经变为字符串了info_json_json = json.dumps(info_json)
print('===info_json_json:===', info_json_json)
print('===type(info_json_json):', type(info_json_json))#注意这个时候已经变为字符串了print(json.loads(info_json_json))
print('===type(json.loads(info_json_json)):', type(json.loads(info_json_json)))
print(json.loads(json.loads(info_json_json)))
print('==type(json.loads(json.loads(info_json_json))):', type(json.loads(json.loads(info_json_json))))
2.处理文件
2.1读取json用json.load
path = './image/003.json'
with open(path) as file:json_info = json.load(file)#print(len(json_info['shapes']))
2.2写入json
with open(path+'/'+name+'.json', 'w+') as fp:json.dump(data, fp=fp, ensure_ascii=False, indent=4, separators=(',', ': '))
with open(output_file, "w+") as jsonfile:json.dump(json_data, jsonfile, sort_keys=True, indent=4)
二.coco数据集介绍
{
"info": info, # dict
"licenses": [license], # list ,内部是dict
"images": [image], # list ,内部是dict
"annotations": [annotation], # list ,内部是dict
"categories": # list ,内部是dict
}
三.csv转coco的json文件
1,train的csv生成coco,json文件
csv文件如下:
图片如下:
import cv2
import math
import numpy as np
import xml.etree.ElementTree as ET
import pandas as pd
from PIL import Image, ImageEnhance
import glob
import jsonCLASS_NAMES = ['steel']
class COCOStyleDataset(object):# @staticmethod# def json_info(json_path=None):# # json_path = '/workspace/mmdetection-0p6rc/gangjin/train_annotations.json'# from pycocotools import coco# cd = coco.COCO(json_path)# print('anno num: ', len(cd.anns))# print('img num: ', len(cd.imgs))# return len(cd.imgs), len(cd.anns)@staticmethoddef json_annotations_for_train():class_name2id = {}for idx, cls_nm in enumerate(CLASS_NAMES):class_name2id[cls_nm] = idx + 1print(class_name2id)final_json = dict()final_json['info'] = Nonefinal_json['licenses'] = Nonefinal_json['categories'] = []final_json['images'] = []final_json['annotations'] = []# categoriesfor i in range(1):cat = dict()cat['id'] = i + 1cat['name'] = CLASS_NAMES[i]cat['supercategory'] = CLASS_NAMES[i]final_json['categories'].append(cat)print(final_json)sv_nm = 'train_annotations_example.json'data_root = './'path_prefix = 'JPEGImages_example/'img_path = data_root + path_prefixprint('img_path=', img_path)img_names = glob.glob1(img_path, '*jpg')print('img_names=', img_names)def pre_data(df):df.iloc[:, 1] = df.apply(lambda x: [float(a) for a in x[1].split(' ')], axis=1)def collect(df):rlt = dict()for i in range(df.shape[0]):img_nm = df.iloc[i, 0]if img_nm in rlt.keys():rlt[img_nm].append(df.iloc[i, 1])else:rlt[img_nm] = [df.iloc[i, 1]]for key, val in rlt.items():rlt[key] = np.array(val)return rltdf = pd.read_csv(data_root + '/train_labels.csv')print(df)"""add part csv_name"""for i in img_names:df_part=df[df['ID']==i]pre_data(df_part)print(df_part)lb_dict = collect(df_part)print(lb_dict)"""df_part replace df"""assert len(lb_dict) == len(img_names)img_names = list(lb_dict.keys())print(img_names)# gene jsonimage_id_counter = 250annotation_id_counter = 30942for img_name in img_names:print(image_id_counter, annotation_id_counter, img_name)# imageimage = dict()img_data = Image.open(img_path + img_name)image['id'] = image_id_counterimage['width'] = img_data.widthimage['height'] = img_data.heightimage['file_name'] = img_namefinal_json['images'].append(image)print(lb_dict[img_name])# annotationfor i in range(lb_dict[img_name].shape[0]):x, y, x2, y2 = lb_dict[img_name][i]width, height = x2 - x, y2 - yannotation = dict()annotation['id'] = annotation_id_counterannotation['image_id'] = image_id_counterannotation['category_id'] = 1annotation['bbox'] = [x, y, width, height]annotation['area'] = float(width * height)annotation['iscrowd'] = 0final_json['annotations'].append(annotation)# update annotation idannotation_id_counter += 1# update image idimage_id_counter += 1# write jsonwith open(data_root + sv_nm, 'w') as fp:json.dump(final_json, fp=fp, ensure_ascii=False, indent=4, separators=(',', ': '))@staticmethoddef json_annotations_for_test():final_json = dict()final_json['info'] = Nonefinal_json['licenses'] = Nonefinal_json['categories'] = []final_json['images'] = []final_json['annotations'] = []img_path = './JPEGImages_example/'img_names = sorted([nm for nm in os.listdir(img_path) if 'jpg' in nm])print('img num: ', len(img_names))image_id_counter = 0for img_name in img_names:print(image_id_counter, img_name)img_data = Image.open(img_path + img_name)# imageimage = dict()image['id'] = image_id_counterimage['width'] = img_data.widthimage['height'] = img_data.heightimage['file_name'] = img_namefinal_json['images'].append(image)image_id_counter += 1with open('./test_annotations_example.json', 'w') as fp:json.dump(final_json, fp=fp, ensure_ascii=False, indent=4, separators=(',', ': '))
if __name__ == '__main__':# COCOStyleDataset.json_annotations_for_train()COCOStyleDataset.json_annotations_for_test()
生成的json文件截图如下:
2,test的图片生成coco,json文件
test的json文件如下图
四,安装labelme
sudo apt-get install python-qt4 pyqt4-dev-tools
sudo pip install labelme # python2 works
labelme --nodata --autosave
会自动生成对应的json文件