├── README ├── convert_to_coco_format.py ├── show_process.py └── show_process.pyc /README: -------------------------------------------------------------------------------- 1 | 1 use the offical code to export object detection in concise format by: 2 | 3 | python3 label2det.py bdd100k/labels/bdd100k_labels_images_train.json \ 4 | bdd100k/detection_train.json 5 | 2 use python2 convert_to_coco_format.py as follows 6 | 7 | usage: convert_to_coco_format.py [-h] [--src SRC_PATH] [--dst DST_PATH] 8 | [--dir IM_DIR] 9 | convert the file to coco format 10 | 11 | optional arguments: 12 | -h, --help show this help message and exit 13 | --src SRC_PATH /path/to/source 14 | --dst DST_PATH /path/to/save.json 15 | --dir IM_DIR /path/to/image/ 16 | -------------------------------------------------------------------------------- /convert_to_coco_format.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import os 4 | import argparse 5 | import json 6 | import sys 7 | from PIL import Image 8 | from show_process import ShowProcess 9 | 10 | """the coco dataset's json is a dict, and its keys is categories, images, annotations; 11 | the categories is a list, and each item is a dict which keys are supercategory,id,name; 12 | the images is a list, and each item is a dict which keys are file_name,height,width,id,depth; 13 | the annotations is a list, and each item is a dict which keys are category_id,segmentation,area,id,iscrowd""" 14 | 15 | filenames = [] 16 | category_names = ["bike","bus", "car", "motor", "person", "rider", "traffic light", "traffic sign", "train", "truck"] 17 | 18 | def parse_args(): 19 | parser = argparse.ArgumentParser(description='convert the file to coco format') 20 | parser.add_argument( 21 | '--src', 22 | dest='src_path', 23 | help='/path/to/source', 24 | default=None, type=str 25 | ) 26 | parser.add_argument( 27 | '--dst', 28 | dest='dst_path', 29 | help='/path/to/save.json', 30 | default=None, 31 | type=str 32 | ) 33 | parser.add_argument( 34 | '--dir', 35 | dest='im_dir', 36 | help='/path/to/image/', 37 | default=None, 38 | type=str 39 | ) 40 | if len(sys.argv) == 1: 41 | parser.print_help() 42 | sys.exit(1) 43 | return parser.parse_args() 44 | 45 | def visual_bbox(im_path, bbox): 46 | im = cv2.imread(im_path) 47 | bbox = [int(i) for i in bbox] 48 | point1 = (bbox[0],bbox[1]) 49 | point2 = (bbox[2],bbox[3]) 50 | cv2.rectangle(im,point1,point2,(255,0,0)) 51 | return im 52 | 53 | def make_coco_categories(): 54 | categoriesList = [] 55 | for i in range(len(category_names)): 56 | eachcategoryDict = {} 57 | eachcategoryDict['supercategory'] = 'none' 58 | eachcategoryDict['id'] = i + 1 59 | eachcategoryDict['name'] = category_names[i] 60 | categoriesList.append(eachcategoryDict) 61 | return categoriesList 62 | 63 | def make_coco_images(src_json_file): 64 | imagesList = [] 65 | global filenames 66 | for i in range(len(src_json_file)): 67 | anno = src_json_file[i] 68 | filename = anno['name'] 69 | filenames.append(filename) 70 | filenames = list(set(filenames)) 71 | print "it is make_coco_images......." 72 | process = ShowProcess(len(filenames)) 73 | for index in range(len(filenames)): 74 | process.show_process() 75 | eachImageDict = dict() 76 | filename = filenames[index] 77 | filepath = os.path.join(args.im_dir,filename) 78 | assert os.path.isfile(filepath),"{} not is a file".format(filepath) 79 | #im = cv2.imread(filepath) 80 | #height,width,depth = im.shape 81 | im = Image.open(filepath) 82 | width,height = im.size 83 | eachImageDict['height'] = height 84 | eachImageDict['width'] = width 85 | #eachImageDict['depth'] = depth 86 | eachImageDict['id'] = index 87 | eachImageDict['file_name'] = filename 88 | imagesList.append(eachImageDict) 89 | return imagesList 90 | 91 | def make_coco_annotations(src_json_file): 92 | global filenames 93 | annotationsList = [] 94 | print "it is make_coco_annotations....." 95 | process = ShowProcess(len(src_json_file)) 96 | for i in range(len(src_json_file)): 97 | process.show_process() 98 | eachAnnotationDict = dict() 99 | anno = src_json_file[i] 100 | category = anno['category'] 101 | bbox = anno['bbox'] 102 | filename = anno['name'] 103 | bbox_width = bbox[2] - bbox[0] 104 | bbox_height = bbox[3] - bbox[1] 105 | xmin = bbox[0] 106 | ymin = bbox[1] 107 | box = [xmin, ymin, bbox_width, bbox_height] 108 | eachAnnotationDict['image_id'] = filenames.index(filename) 109 | eachAnnotationDict['bbox'] = box 110 | eachAnnotationDict['category_id'] = category_names.index(category) + 1 111 | eachAnnotationDict['segmentation'] = [[0,0]] 112 | eachAnnotationDict['area'] = 1 113 | eachAnnotationDict['id'] = i 114 | eachAnnotationDict['iscrowd'] = 0 115 | annotationsList.append(eachAnnotationDict) 116 | return annotationsList 117 | 118 | def convert_coco(args): 119 | src = json.load(open(args.src_path)) 120 | assert(type(src) == list),"unsupported type" 121 | allInfo = dict() 122 | allInfo['categories'] = make_coco_categories() 123 | allInfo['images'] = make_coco_images(src) 124 | allInfo['annotations'] = make_coco_annotations(src) 125 | print allInfo 126 | save_path = args.dst_path 127 | with open(save_path,'w') as writer: 128 | json.dump(allInfo,writer) 129 | 130 | def main(args): 131 | convert_coco(args) 132 | 133 | if __name__ == "__main__": 134 | args = parse_args() 135 | main(args) 136 | -------------------------------------------------------------------------------- /show_process.py: -------------------------------------------------------------------------------- 1 | # -*- coding: UTF-8 -*- 2 | 3 | import sys, time 4 | 5 | class ShowProcess(): 6 | """ 7 | 显示处理进度的类 8 | 调用该类相关函数即可实现处理进度的显示 9 | """ 10 | i = 0 # 当前的处理进度 11 | max_steps = 0 # 总共需要处理的次数 12 | max_arrow = 50 #进度条的长度 13 | infoDone = 'done' 14 | 15 | # 初始化函数,需要知道总共的处理次数 16 | def __init__(self, max_steps, infoDone = 'Done'): 17 | self.max_steps = max_steps 18 | self.i = 0 19 | self.infoDone = infoDone 20 | 21 | # 显示函数,根据当前的处理进度i显示进度 22 | # 效果为[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>]100.00% 23 | def show_process(self, i=None): 24 | if i is not None: 25 | self.i = i 26 | else: 27 | self.i += 1 28 | num_arrow = int(self.i * self.max_arrow / self.max_steps) #计算显示多少个'>' 29 | num_line = self.max_arrow - num_arrow #计算显示多少个'-' 30 | percent = self.i * 100.0 / self.max_steps #计算完成进度,格式为xx.xx% 31 | process_bar = '[' + '>' * num_arrow + '-' * num_line + ']'\ 32 | + '%.2f' % percent + '%' + '\r' #带输出的字符串,'\r'表示不换行回到最左边 33 | sys.stdout.write(process_bar) #这两句打印字符到终端 34 | sys.stdout.flush() 35 | if self.i >= self.max_steps: 36 | self.close() 37 | 38 | def close(self): 39 | print('') 40 | print(self.infoDone) 41 | self.i = 0 42 | 43 | if __name__=='__main__': 44 | max_steps = 100 45 | 46 | process_bar = ShowProcess(max_steps, 'OK') 47 | 48 | for i in range(max_steps): 49 | process_bar.show_process() 50 | time.sleep(0.01) 51 | -------------------------------------------------------------------------------- /show_process.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hzhang33BEI/convert_bdd100k_to_coco_format/3ed8bce0a191583da10ace0677887d55f0d1f9ed/show_process.pyc --------------------------------------------------------------------------------