将所有单个json标注文件合并成一个总的json标注文件(COCO数据集格式)
生活随笔
收集整理的這篇文章主要介紹了
将所有单个json标注文件合并成一个总的json标注文件(COCO数据集格式)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import sys
import os
import json
from PIL import Image
from tqdm import tqdm
from itertools import chainSTART_BOUNDING_BOX_ID = 1 # testing need to change 701 235 721# If necessary, pre-define category and its id
'''
material:1、化纖 Chemical-fiber,2、棉麻 Cotton-linen,3、羊毛 wool,4、皮革 leather,5、皮草 fur,6、牛仔 jeans,7、絲質 silk;
color:1、深色 deep-color,2、淺色 light-color,3、白色 white;
style:1、上衣 jacket,2、褲子 pants,3、內衣 underwear
'''
PRE_DEFINE_CATEGORIES = {"111": 1, "112": 2, "113": 3, "121": 4, "122": 5, "123": 6, "131": 7, "132": 8, "133": 9, "211": 10, "212": 11, "213": 12, "221": 13, "222": 14, "223": 15, "231": 16, "232": 17, "233": 18, "311": 19, "312": 20, "313": 21, "321": 22, "322": 23, "323": 24, "331": 25, "332": 26, "333": 27, "411": 28, "412": 29, "413": 30, "421": 31, "422": 32, "423": 33, "431": 34, "432": 35, "433": 36, "511": 37, "512": 38, "513": 39, "521": 40, "522": 41, "523": 42, "531": 43, "532": 44, "533": 45, "611": 46, "612": 47, "613": 48, "621": 49, "622": 50, "623": 51, "631": 52, "632": 53, "633": 54, "711": 55, "712": 56, "713": 57, "721": 58, "722": 59, "723": 60, "731": 61, "732": 62, "733": 63}def convert(jsonsFile, json_file, imgPath):# ########################################### define the head #################################################imgs = os.listdir(imgPath)json_dict = {"info":{}, "licenses":[], "images":[], "annotations": [], "categories": []}# ######################################### info, type is dict ################################################info = {'description': 'Clothes Dataset', 'url': 'None', 'version': '1.0', 'year': 2021, 'contributor': 'Donghao Zhangdi etc', 'note': 'material: Chemical-fiber,Cotton-linen,wool,leather,Fur,jeans,silk; color: deep-color,light-color,white; style: jacket,pants,underwear. total 63(7x3x3) categories', 'date_created': '2021/11/25'}json_dict['info'] = info# ####################################### licenses, type is list ##############################################license = {'url': 'None', 'id': 1, 'name': 'None'}json_dict['licenses'].append(license)# ###################################### categories, type is list #############################################categories = PRE_DEFINE_CATEGORIESfor cate, cid in categories.items():cat = {'supercategory': 'none', 'id': cid , 'name': cate} # no + 1json_dict['categories'].append(cat)bnd_id = START_BOUNDING_BOX_IDjsonnamelist = os.listdir(jsonsFile)jsonnamelist = [item for item in jsonnamelist if item[-4:] == 'json']for idx, jsonname in enumerate(tqdm(jsonnamelist)):# ###################################### images, type is list #############################################image_id = idx + 1image_name = jsonname.replace(".json", ".jpg")if image_name not in imgs:with open('./error.txt', 'a') as target:info = f'No image file in image path:\n{jsonname} ==> {image_name}\n\n'target.write(info)continueimg = Image.open(os.path.join(imgPath, image_name))width, height = img.sizeimage = {'license': 1, 'file_name': image_name, 'clothes_url': 'None', 'height': height, 'width': width, 'date_captured': '1998-02-05 05:02:01', 'flickr_url': 'None', 'id': image_id}json_dict['images'].append(image)# ###################################### annotations, type is list #############################################json_path = os.path.join(jsonsFile, jsonname)with open(json_path, 'r') as load_f:load_dict = json.load(load_f)for obj in load_dict:label = obj['name']if label not in categories.keys():new_id = len(categories)categories[label] = new_id+1category_id = categories[label]points = obj['points']pointsList = list(chain.from_iterable(points))pointsList = [float(p) for p in pointsList] ####### point 必須是浮點數!!!!!!!!!!!!!!seg = [pointsList]row = pointsList[0::2]clu = pointsList[1::2]left_top_x = min(row)left_top_y = min(clu)right_bottom_x = max(row)right_bottom_y = max(clu)wd = right_bottom_x - left_top_xhg = right_bottom_y - left_top_yann = {'segmentation': seg, 'area': wd*hg, 'iscrowd': 0, 'image_id': image_id, 'bbox': [left_top_x, left_top_y, wd, hg], 'category_id': category_id, 'id': bnd_id}json_dict['annotations'].append(ann)bnd_id = bnd_id + 1print(image_id, bnd_id)# ######################################### write into local ################################################with open(json_file, 'w') as json_fp:json.dump(json_dict, json_fp)if __name__ == '__main__':jsonsFile = "hx_clothes_1122/total_train_jsons"imgPath = "hx_clothes_1122/total_train"destJson = "./clothes_train_COCO.json"# jsonsFile = "hx_clothes_1122/total_val_jsons"# imgPath = "hx_clothes_1122/total_val"# destJson = "./clothes_val_COCO.json"convert(jsonsFile, destJson, imgPath)
總結
以上是生活随笔為你收集整理的将所有单个json标注文件合并成一个总的json标注文件(COCO数据集格式)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深度学习 之 数据增广(包含源码及注释文
- 下一篇: 文件分类tkinter UI小程序,界面