def convert(phase, data_path): assert phase in ['train', 'val'] coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } image_id = 1 annotation_id = 1 bbs_dict = parse_cityperson_mat('%s/anno_%s.mat' % (data_path, phase), phase) fn_lst = glob.glob('%s/leftImg8bit/%s/*/*.png' % (data_path, phase)) positive_box_num = 0 ignore_box_num = 0 for image_filename in fn_lst: base_name = os.path.basename(image_filename) if base_name in bbs_dict: image = Image.open(image_filename) image_info = pycococreatortools.create_image_info( image_id, image_filename[image_filename.index('leftImg8bit'):], image.size) coco_output["images"].append(image_info) boxes = bbs_dict[base_name] # go through each associated annotation slt_msk = np.logical_and(boxes[:, 0] == 1, boxes[:, 4] >= 50) boxes_gt = boxes[slt_msk, 1:5] positive_box_num += boxes_gt.shape[0] for annotation in boxes_gt: annotation = annotation.tolist() class_id = 1 category_info = {'id': class_id, 'is_crowd': False} annotation_info = pycococreatortools.create_annotation_info( annotation_id, image_id, category_info, annotation, image.size) if annotation_info is not None: coco_output["annotations"].append(annotation_info) annotation_id += 1 slt_msk = np.logical_or(boxes[:, 0] != 1, boxes[:, 4] < 50) boxes_ig = boxes[slt_msk, 1:5] ignore_box_num += boxes_ig.shape[0] for annotation in boxes_ig: annotation = annotation.tolist() category_info = {'id': 1, 'is_crowd': True} annotation_info = pycococreatortools.create_annotation_info( annotation_id, image_id, category_info, annotation, image.size) if annotation_info is not None: coco_output["annotations"].append(annotation_info) annotation_id += 1 image_id = image_id + 1 print('positive_box_num: ', positive_box_num) print('ignore_box_num: ', ignore_box_num) with open(data_path + phase + '.json', 'w') as output_json_file: json.dump(coco_output, output_json_file)
def process(img_name, th_fg): img_id = img_name.replace('_', '') img = cv2.imread(os.path.join(img_dir, img_name + '.jpg')) h, w = img.shape[:2] image_info = create_image_info(int(img_id), img_name + '.jpg', (w, h)) ann = get_final_mask_with_topCRF(img_name, mask_dir, img, th_fg) return ann, image_info, w, h
def main(): coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } image_id = 0 segmentation_id = 0 # filter for jpeg images for root, _, files in os.walk(IMAGE_DIR): image_files = filter_for_jpeg(root, files) # go through each image for image_filename in image_files: image = Image.open(image_filename) image_info = pycococreatortools.create_image_info( image_id, os.path.basename(image_filename), image.size) coco_output["images"].append(image_info) # filter for associated png annotations for root, _, files in os.walk(ANNOTATION_DIR): time_id = os.path.splitext(image_filename)[0].split('_')[-1] # annotation_files = filter_for_annotations(root, files, image_filename.replace('rgbImg', 'Mask4Seg')) annotation_files = filter_for_annotations(root, files, time_id) print(annotation_files) # go through each associated annotation for annotation_filename in annotation_files: print(annotation_filename) class_id = [x['id'] for x in CATEGORIES if x['name'] in annotation_filename.split('/')[-1]][0] print(class_id) #COCO 支持两种类型的标注,其格式取决于标注的是单个物体(single object) 还是密集物体("crowd" objects). category_info = {'id': class_id, 'is_crowd': 'crowd' in image_filename} binary_mask = np.asarray(Image.open(annotation_filename) .convert('1')).astype(np.uint8) # tolerance 参数表示了用于记录单个物体的轮廓精度. 该参数数值越大,则标注的质量越低,但文件大小也越小. 一般采用 tolerance=2 annotation_info = pycococreatortools.create_annotation_info( segmentation_id, image_id, category_info, binary_mask, image.size, tolerance=2) if annotation_info is not None: coco_output["annotations"].append(annotation_info) segmentation_id = segmentation_id + 1 image_id = image_id + 1 with open('{}/{}'.format(ROOT_DIR, json_filename), 'w') as output_json_file: json.dump(coco_output, output_json_file)
def each_sub_proc(file_name, dir_name, dataset_root, image_id, labels, each_image_json): print("File name-{}-{}".format(file_name, image_id)) file_name = file_name[:-4] instance_path = "{}/{}/instances/{}.png".format(dataset_root, dir_name, file_name) instance_image = Image.open(instance_path) instance_array = np.array(instance_image, dtype=np.uint16) image_label_instance_infomatrix = split_to_coco_creator(instance_array, labels) image_info = pycococreatortools.create_image_info( image_id, file_name + ".jpg", instance_image.size ) each_image_json["images"].append(image_info) segmentation_id = 1 for item in image_label_instance_infomatrix: class_id = convert_class_id(item["label_name"]) category_info = {"id": class_id, "is_crowd": 0} binary_mask = item["image"] annotation_info = pycococreatortools.create_annotation_info( segmentation_id, image_id, category_info, binary_mask, instance_image.size, tolerance=2, ) if annotation_info is not None: each_image_json["annotations"].append(annotation_info) segmentation_id = segmentation_id + 1 if each_image_json["images"] is []: print("Image {} doesn't contain one image".format(image_id)) save_path = "{}/{}/massive_annotations/image{}_info.json".format( dataset_root, dir_name, image_id ) print("Saving to {}".format(save_path)) with open(save_path, "w") as fp: json.dump(each_image_json, fp)
def parse_ccpd_filename(inputs): try: image = Image.open(inputs['ImgPath'].absolute()) annos = inputs['ImgPath'].name.split('.')[0].split('-') lt, rb = [[float(eel) for eel in el.split('&')] for el in annos[2].split('_')] nh, nw = abs(rb[1] - lt[1]), abs(rb[0] - lt[0]) bbox = [lt[0], lt[1], nw, nh] poly = [[float(eel) for eel in el.split('&')] for el in annos[3].split('_')] poly = sort_polygon(np.array(poly)) kpts = np.array([[p[0], p[1], 2] for p in poly]) if not inputs['Extend'] is None: bbox, poly = inputs['Extend'](inputs['ImgPath'], inputs['SavePath'], bbox, poly) area = compute_polygon_area(poly) image_info = create_image_info( image_id=0, file_name=inputs['ImgPath'].name, image_size=image.size, ) annotation_info = create_annotation_info_polygon( annotation_id=0, image_id=0, area=area, category_id=1, image_size=image.size, bounding_box=bbox, segmentation=poly.tolist(), num_keypoints=4, keypoints=kpts.flatten().tolist(), ) return image_info, annotation_info except Exception: logger.print_exception() return None, None
def main(args): coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } image_id = 1 for image_name in os.listdir(args.test_img_dir): image = Image.open(os.path.join(args.test_img_dir, image_name)) image_info = pycococreatortools.create_image_info( image_id, image_name, image.size) coco_output["images"].append(image_info) image_id += 1 if not os.path.exists(os.path.join(args.json_save_dir)): os.mkdir(os.path.join(args.json_save_dir)) with open('{}/{}.json'.format(args.json_save_dir, args.dataset), 'w') as output_json_file: json.dump(coco_output, output_json_file)
def main(): coco_output = { 'info': INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } image_id = 1 segmentation_id = 1 # filter for jpeg images image_files = os.listdir(IMAGE_DIR) annotation_filenames = os.listdir(ANNOTATION_DIR) # go through each image for image_filename, annotation_filename in zip(image_files, annotation_filenames): image = Image.open(os.path.join(IMAGE_DIR, image_filename)) image_info = pycococreatortools.create_image_info( image_id, image_filename, image.size) coco_output["images"].append(image_info) print(image_filename, annotation_filename) binary_mask = np.asarray( Image.open(os.path.join(ANNOTATION_DIR, annotation_filename)).convert('1')).astype( np.uint8) mask = np.unique(binary_mask) for i in mask: if i < 255: class_id = i category_info = { "id": class_id, "is_crowd": "crowd" in image_filename } annotation_info = pycococreatortools.create_annotation_info( segmentation_id, image_id, category_info, binary_mask, image.size, tolerance=2) coco_output["annotations"].append(annotation_info) segmentation_id = segmentation_id + 1 image_id = image_id + 1 class NumpyEncoder(json.JSONEncoder): """ Special json encoder for numpy types """ def default(self, obj): if isinstance( obj, (np.int_, np.intc, np.intp, np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64)): return int(obj) elif isinstance(obj, (np.float_, np.float16, np.float32, np.float64)): return float(obj) elif isinstance(obj, (np.ndarray, )): return obj.tolist() return json.JSONEncoder.default(self, obj) # operation = json.dumps(coco_output, cls=NumpyEncoder) with open("{}/instances_diangan_train2020.json".format(ROOT_DIR), "w", encoding="utf-8") as output_json_file: json.dump(coco_output, output_json_file, ensure_ascii=False, indent=1, cls=NumpyEncoder)
def main(): total_imagenum_count = 1 for coco_type in total_coco: coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } ROOT_DIR = os.path.join(maindir, 'data', mother_dataset_name) IMAGE_DIR = os.path.join(ROOT_DIR, coco_type) ANNOTATION_DIR = os.path.join(ROOT_DIR, coco_type + '_annotations') RENAME_IMAGE_DIR = os.path.join(ROOT_DIR, "rename_" + coco_type) RENAME_ANNOTATION_DIR = os.path.join( ROOT_DIR, "rename_" + coco_type + "_annotations") os.makedirs(RENAME_IMAGE_DIR, exist_ok=True) os.makedirs(RENAME_ANNOTATION_DIR, exist_ok=True) img_files = sorted(os.listdir(IMAGE_DIR)) anno_files = sorted(os.listdir(ANNOTATION_DIR)) for i in range(len(anno_types)): blank = [name for name in anno_files if anno_types[i] in name] if annotation_dict[i]['annotation_types'] == anno_types[i]: annotation_dict[i]['filenames'] = blank count = total_imagenum_count for num_ in range(len(img_files)): img_name = img_files[num_] img_ext = pathlib.PurePosixPath(img_name).suffix img_new_name = img_name.replace( img_name, DATASET_NAME_PREFIX + "_" + TRAIN_DATASET + "_{0:012d}".format(count)) + img_ext origin_img = cv2.imread(os.path.join(IMAGE_DIR, img_name)) resize_img = resize_image(origin_img, width=780) cv2.imwrite(os.path.join(RENAME_IMAGE_DIR, img_new_name), resize_img) for anno_num in range(len(anno_types)): anno_type = anno_types[anno_num] #dog, cat anno_name = annotation_dict[anno_num]['filenames'][num_] anno_ext = pathlib.PurePosixPath(anno_name).suffix anno_new_name = anno_name.replace( anno_name, DATASET_NAME_PREFIX + "_" + TRAIN_DATASET + "_{0:012d}".format(count)) + "_" + anno_types[ anno_num] + anno_ext # 분류될 이름을 반드시 넣을 것!!! cat 이부분에 origin_anno = cv2.imread( os.path.join(ANNOTATION_DIR, anno_name)) resize_anno = resize_image(origin_anno, width=780) cv2.imwrite(os.path.join(RENAME_ANNOTATION_DIR, anno_new_name), resize_anno) count += 1 print("Renaming image file name is done!") print("# of Dataset : {}".format(count - 1)) total_imagenum_count += count image_id = 1 segmentation_id = 1 view_count = 1 for root, _, files in os.walk(RENAME_IMAGE_DIR): image_files = filter_for_images(root, files) # image파일 골라내기 for image_filename in image_files: # rename 이미지 파일들 하나씩 불러오기 image = Image.open(image_filename) image_info = pycococreatortools.create_image_info( image_id, os.path.basename(image_filename), image.size) # coco데이터 셋에 정보를 넣기위해 이미지 번호,이름,이미지 크기등을 입력하여 coco_output["images"].append(image_info) # 위 coco_output의 images 칸에 image_info를 넣기 for root, _, files in os.walk(RENAME_ANNOTATION_DIR): annotation_files = filter_for_annotations( root, files, image_filename) # go through each associated annotation for annotation_filename in annotation_files: class_id = [ x['id'] for x in CATEGORIES if x['name'] in annotation_filename ][0] # CATEGORIES에서 목록을 뽑아내고 annotation_filename에 # x의 'name'부분과 일치하는 부분이 있는지 확인 후, 일치하는 id number를 가져오는 부분. category_info = { 'id': class_id, 'is_crowd': 'crowd' in image_filename } # is_crowd가 뭔지 아직 모름 binary_mask = np.asarray( Image.open(annotation_filename).convert( '1')).astype(np.uint8) #0과 1로 이루어진 마스크를 생성. annotation_info = pycococreatortools.create_annotation_info( segmentation_id, image_id, category_info, binary_mask, image.size, tolerance=2) # annotation의 정보를 만들어주는 듯. segmentation number를 저장하고 (위 image_id와 같은 세트이므로 같아야됨) # 그리고 해당 annotation의 category 정보를 넣어주고, binary 마스크를 입력, 이미지 크기 또한 입력한다. if annotation_info is not None: coco_output["annotations"].append(annotation_info) segmentation_id = segmentation_id + 1 print("--------------------------") print("{}/{} is completed ! ".format(view_count, len(image_files))) view_count += 1 image_id = image_id + 1 with open( os.path.join(RENAME_ANNOTATION_DIR, coco_type + '2020.json').format(ROOT_DIR), 'w') as output_json_file: json.dump(coco_output, output_json_file)
# Get original satellite images from folder train = in_img val = r'C:\Users\ahls_st\Documents\MasterThesis\IKONOS\With_Hedges\FourBands\Splits\imgs\val' #change # combine original files with augmented files files = getFiles(train, ending='.png') files2 = getFiles(val, ending='.png') files3 = getFiles(os.path.join(aug_img_dir, 'Cleaned'), ending='.png') files_full = sorted(files + files2 + files3) len(files_full) #Get images annotation info image_infos = [] for file in files_full: i_info = coco.create_image_info(file, (320, 320)) image_infos.append(i_info) # ensure the lengths and indexes are matching properly len(mask_anno) len(image_infos) random_index = randrange(0, len(mask_anno)) print(mask_anno[random_index]) print(image_infos[random_index]) if mask_anno[random_index][0]['filename'] != image_infos[random_index][ 'file_name']: raise ValueError('Mask and image annotations are not properly matched') #Create the rest of the json sections categories = [{'supercategory': 'Vegetation', 'id': 1, 'name': 'Hedge'}]
def convert_masks_to_COCO(kits_val, kits_masks, kits_annotations): """ Returns: Converted masks in COCO format """ INFO = { "description": "KiTS Dataset", "url": "https://github.com/AbtinJ/Kidneys_MaskRCNN", "version": "1.0", "year": 2019, "contributor": "Abtin", "date_created": datetime.datetime.utcnow().isoformat(' ') } LICENSES = [{ "id": 1, "name": "Attribution-NonCommercial-ShareAlike License", "url": "http://creativecommons.org/licenses/by-nc-sa/2.0/" }] CATEGORIES = [ { 'id': 1, 'name': 'kidney', 'supercategory': 'organ', }, { 'id': 2, 'name': 'tumor', 'supercategory': 'organ', }, ] def filter_for_jpeg(root, files): file_types = ['*.jpeg', '*.jpg'] file_types = r'|'.join([fnmatch.translate(x) for x in file_types]) files = [os.path.join(root, f) for f in files] files = [f for f in files if re.match(file_types, f)] return files def filter_for_annotations(root, files, image_filename): file_types = ['*.png'] file_types = r'|'.join([fnmatch.translate(x) for x in file_types]) basename_no_extension = os.path.splitext( os.path.basename(image_filename))[0] file_name_prefix = basename_no_extension + '.*' files = [os.path.join(root, f) for f in files] files = [f for f in files if re.match(file_types, f)] files = [ f for f in files if re.match(file_name_prefix, os.path.splitext(os.path.basename(f))[0]) ] return files coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } image_id = 0 segmentation_id = 0 # filter for jpeg images for root, _, files in os.walk(kits_val): image_files = filter_for_jpeg(root, files) # go through each image for image_filename in image_files: image = Image.open(image_filename) image_info = pycococreatortools.create_image_info( image_id, os.path.basename(image_filename), image.size) coco_output["images"].append(image_info) # filter for associated png annotations for root, _, files in os.walk(kits_masks): annotation_files = filter_for_annotations( root, files, image_filename) # go through each associated annotation for annotation_filename in annotation_files: print(annotation_filename) class_id = [ x['id'] for x in CATEGORIES if x['name'] in annotation_filename ][0] category_info = { 'id': class_id, 'is_crowd': 'crowd' in image_filename } binary_mask = np.asarray( Image.open(annotation_filename).convert('1')).astype( np.uint8) annotation_info = pycococreatortools.create_annotation_info( segmentation_id, image_id, category_info, binary_mask, image.size, tolerance=2) if annotation_info is not None: coco_output["annotations"].append(annotation_info) segmentation_id = segmentation_id + 1 image_id = image_id + 1 # with open('{}/instances_train2019.json'.format(ANNOTATION_DIR), 'w') as output_json_file: # json.dump(coco_output, output_json_file) with open('{}/instances_val2019.json'.format(kits_annotations), 'w') as output_json_file: json.dump(coco_output, output_json_file)
def main(): coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } image_id = 1 segmentation_id = 1 # filter for jpeg images for root, _, files in os.walk(IMAGE_DIR): image_files = filter_for_jpeg(root, files) # go through each image for image_filename in image_files: image = Image.open(image_filename) image_info = pycococreatortools.create_image_info( image_id, os.path.basename(image_filename), image.size) coco_output["images"].append(image_info) # filter for associated png annotations for root, _, files in os.walk(ANNOTATION_DIR): annotation_files = filter_for_annotations( root, files, image_filename) # go through each associated annotation for annotation_filename in annotation_files: # print('Considering:', annotation_filename) class_id = [ x['id'] for x in CATEGORIES if x['name'] in annotation_filename ][0] category_info = { 'id': class_id, 'is_crowd': 'crowd' in image_filename } binary_mask = np.asarray( Image.open(annotation_filename).convert('1')).astype( np.uint8) # print('Sending:', 'segmentation_id:', segmentation_id, 'image_id:', image_id, # 'category_info:', category_info, 'binary_mask:', binary_mask, # 'image.size:', image.size) annotation_info = pycococreatortools.create_annotation_info( segmentation_id, image_id, category_info, binary_mask, image.size, tolerance=2) # print('Annotation Info:', annotation_info) # plt.imshow(binary_mask) # plt.show() if annotation_info is not None: coco_output["annotations"].append(annotation_info) segmentation_id = segmentation_id + 1 image_id = image_id + 1 with open('{}/fs_train_tmp.json'.format(ROOT_DIR), 'w') as output_json_file: json.dump(coco_output, output_json_file)
def convert(): identity_lst = ['pedestrian', 'bicycle-group', 'person-group-far-away', 'scooter-group', 'co-rider', 'scooter', \ 'motorbike', 'bicycle', 'rider', 'motorbike-group', 'rider+vehicle-group-far-away', None, \ 'buggy-group', 'wheelchair-group', 'tricycle-group', 'buggy', 'wheelchair', 'tricycle'] identity2index = dict(zip(identity_lst, range(0, len(identity_lst)))) image_id = 1 annotation_id = 1 data_path = 'datasets/EuroCity/' #check the path for day in ['day']: for phase in ['train', 'val', 'test']: coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } box_num = 0 ig_box_num = 0 fold_path = '%s/ECP/%s/img/%s' % (data_path, day, phase) print(fold_path) fn_lst = glob.glob('%s/*/*.png' % fold_path) for img_name in fn_lst: image = Image.open(img_name) image_info = pycococreatortools.create_image_info( image_id, img_name[img_name.index('ECP'):], image.size) coco_output["images"].append(image_info) if phase != 'test': anno_fn = img_name.replace('img', 'labels').replace( 'png', 'json') anno = json.load(open(anno_fn)) boxes = [] for each in anno['children']: if len(each["children"]) > 0: # print(each) for each2 in each["children"]: boxes.append([identity2index[each2["identity"]], float(each2['x0']), float(each2['y0']), \ float(each2['x1']) - float(each2['x0']), float(each2['y1']) - float(each2['y0'])]) if "occluded>80" in each2['tags']: boxes[-1].append(1) # print('heavy occluded') else: # print('normal') boxes[-1].append(0) boxes.append([identity2index[each["identity"]], float(each['x0']), float(each['y0']), \ float(each['x1']) - float(each['x0']), float(each['y1']) - float(each['y0'])]) if "occluded>80" in each['tags']: boxes[-1].append(1) # print('heavy occluded') else: boxes[-1].append(0) boxes = np.array(boxes) if len(boxes) > 0: # slt = np.logical_and(boxes[:, 0]==0, boxes[:, 4]>20, boxes[:, 5]<1) slt = (boxes[:, 0] == 0) boxes_gt = boxes[slt, 1:5].tolist() boxes_ig = boxes[~slt, 1:5].tolist() else: boxes_gt = [] boxes_ig = [] box_num += len(boxes_gt) ig_box_num += len(boxes_ig) for annotation in boxes_gt: class_id = 1 category_info = {'id': class_id, 'is_crowd': False} annotation_info = pycococreatortools.create_annotation_info( annotation_id, image_id, category_info, annotation, image.size) if annotation_info is not None: coco_output["annotations"].append(annotation_info) annotation_id += 1 for annotation in boxes_ig: category_info = {'id': 1, 'is_crowd': True} annotation_info = pycococreatortools.create_annotation_info( annotation_id, image_id, category_info, annotation, image.size) if annotation_info is not None: coco_output["annotations"].append(annotation_info) annotation_id += 1 image_id = image_id + 1 with open(data_path + day + '_' + phase + '_all.json', 'w') as output_json_file: json.dump(coco_output, output_json_file) print('box num: ', box_num) print('ignore box num: ', ig_box_num)
def main(): coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } image_id = 1 segmentation_id = 1 count = 1 # filter for jpeg images for root, _, files in os.walk(IMAGE_DIR): image_files = filter_for_jpeg(root, files) # go through each image print(len(image_files)) for image_filename in image_files: count = count + 1 print(image_filename) print(count) image = Image.open(image_filename) image_info = pycococreatortools.create_image_info( image_id, os.path.basename(image_filename), image.size) coco_output["images"].append(image_info) # filter for associated png annotations for root, _, files in os.walk(ANNOTATION_DIR): annotation_files = filter_for_annotations( root, files, image_filename) # go through each associated annotation print(len(annotation_files)) for annotation_filename in annotation_files: class_id = 1 print(annotation_filename) # class_id = [x['id'] for x in CATEGORIES if x['name'] in annotation_filename][0] category_info = { 'id': class_id, 'is_crowd': 'crowd' in image_filename } binary_mask = np.asarray( Image.open(annotation_filename).convert('1')).astype( np.uint8) ########### annotation_filename2 = os.path.join( ANNOTATION_DIR2, annotation_filename.split('/')[-1]) print(annotation_filename2) binary_mask2 = np.asarray( Image.open(annotation_filename2).convert('1')).astype( np.uint8) print(binary_mask2.shape) ########## annotation_info = pycococreatortools.create_annotation_info( segmentation_id, image_id, category_info, binary_mask, binary_mask2, image.size, tolerance=0.1) if annotation_info is not None: coco_output["annotations"].append(annotation_info) segmentation_id = segmentation_id + 1 image_id = image_id + 1 with open( '{}/annotations/instances_plants_validate2019.json'.format( ROOT_DIR), 'w') as output_json_file: json.dump(coco_output, output_json_file)
def append_images_to_annotations(original_ann_filename, ROOT_DIR, IMAGE_DIR, ANNOTATION_DIR, output_filename): """ args: ROOT_DIR: file where the anonation and the image folders are original_ann_filename: json file where we want to append images IMAGE_DIR: directory with the images we want to add ANNOTATION_DIR: directory with masks of the elements in the images (.png files) """ print("creating anotations..") original_ann_filename = os.path.join(ROOT_DIR, original_ann_filename) IMAGE_DIR = os.path.join(ROOT_DIR, IMAGE_DIR) ANNOTATION_DIR = os.path.join(ROOT_DIR, ANNOTATION_DIR) #start with original json file with open(original_ann_filename) as json_file: coco_output = json.load(json_file) image_id = len(coco_output['images']) #start with the last id segmentation_id = len( coco_output['annotations']) #start with the last id of annotations CATEGORIES = coco_output['categories'] # filter for jpeg images for root, _, files in os.walk(IMAGE_DIR): image_files = pycococreatortools.filter_for_jpeg(root, files) # go through each image for image_filename in image_files: image = Image.open(image_filename) image_filename_path = os.path.basename( IMAGE_DIR) + "/" + os.path.basename(image_filename) image_info = pycococreatortools.create_image_info( image_id, image_filename_path, image.size) coco_output["images"].append(image_info) # filter for associated png annotations for root, _, files in os.walk(ANNOTATION_DIR): annotation_files = pycococreatortools.filter_for_annotations( root, files, image_filename) if len(annotation_files) == 0: print("no ann files " + image_filename) # go through each associated annotation for annotation_filename in annotation_files: #print(annotation_filename) #skip BG if "BG" not in annotation_filename: s = os.path.basename(annotation_filename) category_in_file = ( s.split("_"))[2].split("_")[0] #extract category class_id = [ x["id"] for x in CATEGORIES if x["name"] == category_in_file ][0] category_info = { "id": class_id, "is_crowd": 'crowd' in image_filename } binary_mask = np.asarray( Image.open(annotation_filename).convert( 'P')).astype(np.uint8) annotation_info = pycococreatortools.create_annotation_info( segmentation_id, image_id, category_info, binary_mask, image.size, tolerance=2) if annotation_info is not None: coco_output["annotations"].append(annotation_info) else: print("No_anotation " + annotation_filename) segmentation_id = segmentation_id + 1 image_id = image_id + 1 with open('{}/{}'.format(ROOT_DIR, output_filename), 'w') as output_json_file: json.dump(coco_output, output_json_file) print("Finished creating anotations.")
def main(args): INFO = { "description": args.dataset + " Dataset", "url": "", "version": "", "year": 2020, "contributor": "qiu", "date_created": datetime.datetime.utcnow().isoformat(' ') } LICENSES = [{"id": 1, "name": "", "url": ""}] CATEGORIES = [ { 'id': 1, 'name': 'person', 'supercategory': 'person', }, ] # coco_output = { # "info": INFO, # "licenses": LICENSES, # "categories": CATEGORIES, # "images": [], # "annotations": [] # } # image_id = 1 # segmentation_id = 1 # train_imgs = os.listdir(args.train_img_dir) # for idx, image_name in enumerate(train_imgs): # if image_name.endswith(('jpg', 'png')): # # print(image_name+'\n') # try: # image = Image.open(os.path.join(args.train_img_dir, # image_name)) # except: # print('corrupt img', image_name) # image_info = pycococreatortools.create_image_info( # image_id, image_name, image.size) # coco_output["images"].append(image_info) # mask_name_prefix = image_name.split('.')[0] # mask_path = os.path.join(args.train_anno_dir, # mask_name_prefix + '.png') # mask = np.asarray(Image.open(mask_path)) # if len(mask.shape) == 3: # mask = mask[:, :, 0] # gt_labels = np.unique(mask) # for k in range(1, len(gt_labels)): # category_info = {'id': 1, 'is_crowd': 0} # binary_mask = np.uint8(mask == k) # annotation_info = pycococreatortools.create_annotation_info( # segmentation_id, # image_id, # category_info, # binary_mask, # image.size, # tolerance=0) # if annotation_info is not None: # coco_output["annotations"].append(annotation_info) # segmentation_id += 1 # image_id += 1 # if image_id % 100 == 0: # print(image_id) # if not os.path.exists(args.json_save_dir): # os.makedirs(args.json_save_dir) # if not args.use_val: # with open('{}/{}_train.json'.format(args.json_save_dir, args.dataset), # 'w') as output_json_file: # json.dump(coco_output, output_json_file) # val coco_output_val = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } image_id_val = 1 segmentation_id_val = 1 val_imgs = os.listdir(args.val_img_dir) for idx, image_name in enumerate(val_imgs): if image_name.endswith(('jpg', 'png')): # print(image_name+'\n') try: image = Image.open(os.path.join(args.val_img_dir, image_name)) except: print('corrupt img', image_name) image_info = pycococreatortools.create_image_info( image_id_val, image_name, image.size) coco_output_val["images"].append(image_info) mask_name_prefix = image_name.split('.')[0] mask_path = os.path.join(args.val_anno_dir, mask_name_prefix + '.png') mask = np.asarray(Image.open(mask_path)) if len(mask.shape) == 3: mask = mask[:, :, 0] gt_labels = np.unique(mask) for k in range(1, len(gt_labels)): category_info = {'id': 1, 'is_crowd': 0} binary_mask = np.uint8(mask == k) annotation_info = pycococreatortools.create_annotation_info( segmentation_id_val, image_id_val, category_info, binary_mask, image.size, tolerance=0) if annotation_info is not None: coco_output_val["annotations"].append(annotation_info) segmentation_id_val += 1 image_id_val += 1 if image_id_val % 100 == 0: print(image_id_val) if not os.path.exists(args.json_save_dir): os.makedirs(args.json_save_dir) if not args.use_val: with open('{}/{}_val.json'.format(args.json_save_dir, args.dataset), 'w') as output_json_file_val: json.dump(coco_output_val, output_json_file_val)
def create_coco_style(self, input_path, des): maxInt = sys.maxsize while True: try: csv.field_size_limit(maxInt) break except: maxInt = int(maxInt / 10) data_path = input_path + 'train.csv' # json_des = '/kaggle/working/label_descriptions.json' # with open(json_des, 'r') as f: # des = json.load(f) info = des['info'] categories = des['categories'] attributes = des['attributes'] #f = open(data_path, 'r') # X = pd.read_csv(data_path) X_train, X_test = train_test_split(X, test_size=0.2) # X_train = X X_dtrain = X_train.to_dict('records', into=OrderedDict) X_dtest = X_test.to_dict('records', into=OrderedDict) #reader = csv.DictReader(f)#, fieldnames=('imageid', 'height', 'width', 'encodedpixels', 'classid')) rows_train = [] rows_test = [] myorder = ['ImageId', 'Height', 'Width', 'EncodedPixels', 'ClassId'] image_id = 1 segmentation_id = 1 coco_output = { "info": info, "licenses": "", "categories": categories, "images": [], "annotations": [] } coco_output_test = coco_output with open('{}train.txt'.format(input_path), 'w') as output_text_file: for row in X_dtrain: # Write training text output_text_file.write('{} '.format(row['ImageId'])) # Ordered Dict ordered = OrderedDict((k, row[k]) for k in myorder) # ordered['EncodedPixels'] = list(map(int, ordered['EncodedPixels'].split(' '))) if len(ordered['ClassId']) > 2: classes = [list(map(int, ordered['ClassId'].split('_')))] ordered['ClassId'] = classes[0][0] else: ordered['ClassId'] = int(ordered['ClassId']) # COCO image_info = pycoco.create_image_info( image_id, input_path + row['ImageId'], (row['Width'], row['Height'])) coco_output["images"].append(image_info) rle, binary_mask = rle_decode(row['EncodedPixels'], (row['Height'], row['Width'])) fortran_binary_mask = np.asfortranarray( binary_mask.astype(np.uint8)) binary_mask_encoded = mask.encode(fortran_binary_mask) # rle2 = pycoco.binary_mask_to_rle(fortran_binary_mask) area = mask.area(binary_mask_encoded) bounding_box = mask.toBbox(binary_mask_encoded) annotation_info = { "id": segmentation_id, "image_id": image_id, "category_id": ordered['ClassId'], "iscrowd": 1, "area": area.tolist(), "bbox": bounding_box.tolist(), "segmentation": rle, "width": row['Width'], "height": row['Height'], } coco_output["annotations"].append(annotation_info) segmentation_id += 1 image_id += 1 rows_train.append(ordered) with open('{}train.json'.format(input_path), 'w') as output_json_file: json.dump(coco_output, output_json_file) with open('{}test.txt'.format(input_path), 'w') as output_text_file: for row in X_dtest: # Write test text output_text_file.write('{} '.format(row['ImageId'])) # Ordered Dict ordered = OrderedDict((k, row[k]) for k in myorder) # ordered['EncodedPixels'] = list(map(int, ordered['EncodedPixels'].split(' '))) if len(ordered['ClassId']) > 2: classes = [list(map(int, ordered['ClassId'].split('_')))] ordered['ClassId'] = classes[0][0] else: ordered['ClassId'] = int(ordered['ClassId']) # COCO image_info = pycoco.create_image_info( image_id, input_path + row['ImageId'], (row['Width'], row['Height'])) coco_output_test["images"].append(image_info) rle, binary_mask = rle_decode(row['EncodedPixels'], (row['Height'], row['Width'])) fortran_binary_mask = np.asfortranarray( binary_mask.astype(np.uint8)) binary_mask_encoded = mask.encode(fortran_binary_mask) # rle2 = pycoco.binary_mask_to_rle(fortran_binary_mask) area = mask.area(binary_mask_encoded) bounding_box = mask.toBbox(binary_mask_encoded) annotation_info = { "id": segmentation_id, "image_id": image_id, "category_id": ordered['ClassId'], "iscrowd": 1, "area": area.tolist(), "bbox": bounding_box.tolist(), "segmentation": rle, "width": row['Width'], "height": row['Height'], } coco_output_test["annotations"].append(annotation_info) segmentation_id += 1 image_id += 1 rows_train.append(ordered) with open('{}test.json'.format(input_path), 'w') as output_json_file: json.dump(coco_output_test, output_json_file)
def main(): coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } image_id = 0 segmentation_id = 0 # filter for jpeg images for root, _, files in os.walk(IMAGE_DIR): image_files = filter_for_jpeg(root, files) # go through each image for image_filename in image_files: image = Image.open(image_filename) image_info = pycococreatortools.create_image_info( image_id, os.path.basename(image_filename), image.size) coco_output["images"].append(image_info) # filter for associated png annotations for root, _, files in os.walk(MASK_DIR): annotation_files = filter_for_annotations( root, files, image_filename) # go through each associated annotation for annotation_filename in annotation_files: print(annotation_filename) class_id = [ x['id'] for x in CATEGORIES if x['name'] in annotation_filename ][0] category_info = { 'id': class_id, 'is_crowd': 'crowd' in image_filename } binary_mask = np.asarray( Image.open(annotation_filename).convert( '1')).astype(np.uint8) annotation_info = pycococreatortools.create_annotation_info( segmentation_id, image_id, category_info, binary_mask, image.size, tolerance=2) if annotation_info is not None: coco_output["annotations"].append(annotation_info) segmentation_id = segmentation_id + 1 image_id = image_id + 1 if IMAGE_DIR == TRAIN_DIR: with open('{}/instances_train2019.json'.format(ANNOTATION_DIR), 'w') as output_json_file: json.dump(coco_output, output_json_file) elif IMAGE_DIR == VAL_DIR: with open('{}/instances_val2019.json'.format(ANNOTATION_DIR), 'w') as output_json_file: json.dump(coco_output, output_json_file)
def main(args): INFO = { "description": args.split_name + " Dataset", "url": "", "version": "", "year": 2019, "contributor": "xyq", "date_created": datetime.datetime.utcnow().isoformat(' ') } LICENSES = [ { "id": 1, "name": "", "url": "" } ] CATEGORIES = [ { 'id': 1, 'name': 'person', 'supercategory': 'person', }, ] coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } image_id = 1 segmentation_id = 1 for image_name in os.listdir(args.train_img_dir): image = Image.open(os.path.join(args.train_img_dir, image_name)) image_info = pycococreatortools.create_image_info( image_id, image_name, image.size ) coco_output["images"].append(image_info) human_mask_name = os.path.splitext(image_name)[0] + '.png' human_mask = np.asarray(Image.open(os.path.join(args.train_anno_dir, human_mask_name))) human_gt_labels = np.unique(human_mask) for i in range(1, len(human_gt_labels)): category_info = {'id': 1, 'is_crowd': 0} binary_mask = np.uint8(human_mask == i) annotation_info = pycococreatortools.create_annotation_info( segmentation_id, image_id, category_info, binary_mask, image.size, tolerance=10 ) if annotation_info is not None: coco_output["annotations"].append(annotation_info) segmentation_id += 1 image_id += 1 if not os.path.exists(args.json_save_dir): os.makedirs(args.json_save_dir) if not args.use_val: with open('{}/{}_train.json'.format(args.json_save_dir, args.split_name), 'w') as output_json_file: json.dump(coco_output, output_json_file) else: for image_name in os.listdir(args.val_img_dir): image = Image.open(os.path.join(args.val_img_dir, image_name)) image_info = pycococreatortools.create_image_info( image_id, image_name, image.size ) coco_output["images"].append(image_info) human_mask_name = os.path.splitext(image_name)[0] + '.png' human_mask = np.asarray(Image.open(os.path.join(args.val_anno_dir, human_mask_name))) human_gt_labels = np.unique(human_mask) for i in range(1, len(human_gt_labels)): category_info = {'id': 1, 'is_crowd': 0} binary_mask = np.uint8(human_mask == i) annotation_info = pycococreatortools.create_annotation_info( segmentation_id, image_id, category_info, binary_mask, image.size, tolerance=10 ) if annotation_info is not None: coco_output["annotations"].append(annotation_info) segmentation_id += 1 image_id += 1 with open('{}/{}_trainval.json'.format(args.json_save_dir, args.split_name), 'w') as output_json_file: json.dump(coco_output, output_json_file) coco_output_val = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } image_id_val = 1 segmentation_id_val = 1 for image_name in os.listdir(args.val_img_dir): image = Image.open(os.path.join(args.val_img_dir, image_name)) image_info = pycococreatortools.create_image_info( image_id_val, image_name, image.size ) coco_output_val["images"].append(image_info) human_mask_name = os.path.splitext(image_name)[0] + '.png' human_mask = np.asarray(Image.open(os.path.join(args.val_anno_dir, human_mask_name))) human_gt_labels = np.unique(human_mask) for i in range(1, len(human_gt_labels)): category_info = {'id': 1, 'is_crowd': 0} binary_mask = np.uint8(human_mask == i) annotation_info = pycococreatortools.create_annotation_info( segmentation_id_val, image_id_val, category_info, binary_mask, image.size, tolerance=10 ) if annotation_info is not None: coco_output_val["annotations"].append(annotation_info) segmentation_id_val += 1 image_id_val += 1 with open('{}/{}_val.json'.format(args.json_save_dir, args.split_name), 'w') as output_json_file_val: json.dump(coco_output_val, output_json_file_val)
def main(): """ Main entry point of the app """ coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } segmentation_id = 1 image_id = 1 for root, directories, files in os.walk(IMAGE_DIR): file_types = ['*.jpeg', '*.jpg'] file_types = r'|'.join([fnmatch.translate(x) for x in file_types]) files = [os.path.join(root, f) for f in files] files = [f for f in files if re.match(file_types, f)] # go through each image for i, filename in enumerate(files): print(filename) parent_directory = root.split(os.path.sep)[-1] basename_no_extension = os.path.splitext( os.path.basename(filename))[0] image = Image.open(filename) image_info = pycococreatortools.create_image_info( image_id, os.path.basename(filename), image.size) coco_output["images"].append(image_info) # go through each associated annotation for root, directories, files in os.walk(ANNOTATION_DIR): file_types = ['*.png'] file_types = r'|'.join([fnmatch.translate(x) for x in file_types]) file_name_prefix = basename_no_extension + '.*' files = [os.path.join(root, f) for f in files] files = [f for f in files if re.match(file_types, f)] files = [f for f in files if re.match( file_name_prefix, os.path.splitext(os.path.basename(f))[0])] for filename in files: parent_directory = root.split(os.path.sep)[-1] basename_no_extension = os.path.splitext( os.path.basename(filename))[0] annotation_array = np.array(Image.open(filename)) # load annotation information annotation_metadata = get_metadata(filename) annotation_data = AnnotationData( annotation_array, annotation_metadata) object_classes = annotation_data.get_classes() # go through each class for j, object_class in enumerate(object_classes): if ANNOTATOR_CATEGORIES.get(object_class) == None: print("missing: {}".format(object_class)) continue # go through each object for object_instance in range(object_classes[object_class]): object_mask = annotation_data.get_mask( object_class, object_instance) if object_mask is not None: object_mask = object_mask.astype(np.uint8) annotation_info = pycococreatortools.create_annotation_info(segmentation_id, image_id, ANNOTATOR_CATEGORIES.get(object_class), object_mask, image.size, tolerance=2) if annotation_info is not None: coco_output["annotations"].append( annotation_info) segmentation_id = segmentation_id + 1 image_id = image_id + 1 with open('{}/coco.json'.format(DATA_DIR), 'w') as output_json_file: json.dump(coco_output, output_json_file)
def main(args): INFO = { "description": args.dataset + " Dataset", "url": "", "version": "", "year": 2020, "contributor": "qiu", "date_created": datetime.datetime.utcnow().isoformat(' ') } LICENSES = [{"id": 1, "name": "", "url": ""}] CATEGORIES = [ { 'id': 1, 'name': 'foot', 'supercategory': 'person', }, ] coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } # mhp2_foot_labels = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 32, 33] image_id = 1 segmentation_id = 1 train_imgs = os.listdir(args.train_img_dir) for idx, image_name in enumerate(train_imgs): if image_name.endswith(('jpg', 'png')): # print(image_name+'\n') try: image = Image.open(os.path.join(args.train_img_dir, image_name)) except: print('corrupt img', image_name) # image=cv2.imread(os.path.join(args.train_img_dir, image_name)) image_info = pycococreatortools.create_image_info( image_id, image_name, image.size) coco_output["images"].append(image_info) mask_name_prefix = image_name.split('.')[0] # mask_temp = np.ones((image.size[1], image.size[0]),dtype=np.uint8) mask_temp = np.zeros((image.size[1], image.size[0]), dtype=np.uint8) count_str = '' ins_count = 0 for i in range(1, 100): if i < 10: count_str = '_0' + str(i) else: count_str = '_' + str(i) if os.path.exists( os.path.join(args.train_anno_dir, mask_name_prefix + count_str + '_01.png')): ins_count = i break temp_count = '' for i in range(ins_count): if i < 9: temp_count = '_0' + str(i + 1) else: temp_count = '_' + str(i + 1) mask_path = os.path.join( args.train_anno_dir, mask_name_prefix + count_str + temp_count + '.png') mask = np.asarray(Image.open(mask_path)) # mask=cv2.imread(mask_path) if len(mask.shape) == 3: mask = mask[:, :, 0] for j in range(2): # # MHP v2 foot area # mask_temp = np.where((mask >= 20) & (mask <= 33) & # (mask != 30) & (mask != 31), 1, 0) # MHP v1 foot area mask_temp = np.where((mask == 9 + j), 1, 0) # cv2.imshow("ss", mask_temp*255.0) # cv2.waitKey(-1) gt_labels = np.unique(mask_temp) for k in range(1, len(gt_labels)): category_info = {'id': 1, 'is_crowd': 0} binary_mask = np.uint8(mask_temp) annotation_info = pycococreatortools.create_annotation_info( segmentation_id, image_id, category_info, binary_mask, image.size, tolerance=0) if annotation_info is not None: coco_output["annotations"].append(annotation_info) segmentation_id += 1 image_id += 1 if image_id % 100 == 0: print(image_id) if not os.path.exists(args.json_save_dir): os.makedirs(args.json_save_dir) if not args.use_val: with open('{}/{}_train.json'.format(args.json_save_dir, args.dataset), 'w') as output_json_file: json.dump(coco_output, output_json_file) coco_output_val = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } image_id_val = 1 segmentation_id_val = 1 val_imgs = os.listdir(args.val_img_dir) for idx, image_name in enumerate(val_imgs): if image_name.endswith(('jpg', 'png')): try: image = Image.open(os.path.join(args.val_img_dir, image_name)) except: print('corrupt img', image_name) # print(image_name+'\n') # image=cv2.imread(os.path.join(args.val_img_dir, image_name)) image_info = pycococreatortools.create_image_info( image_id_val, image_name, image.size) coco_output_val["images"].append(image_info) mask_name_prefix = image_name.split('.')[0] # mask_temp = np.ones((image.size[1], image.size[0])) mask_temp = np.zeros((image.size[1], image.size[0]), dtype=np.uint8) count_str = '' ins_count = 0 for i in range(1, 100): if i < 10: count_str = '_0' + str(i) else: count_str = '_' + str(i) if os.path.exists( os.path.join(args.val_anno_dir, mask_name_prefix + count_str + '_01.png')): ins_count = i break temp_count = '' for i in range(ins_count): if i < 9: temp_count = '_0' + str(i + 1) else: temp_count = '_' + str(i + 1) mask_path = os.path.join( args.val_anno_dir, mask_name_prefix + count_str + temp_count + '.png') mask = np.asarray(Image.open(mask_path)) # mask=cv2.imread(mask_path) if len(mask.shape) == 3: mask = mask[:, :, 0] # # MHP v2 foot area mask_temp = np.where( (mask >= 20) & (mask <= 33) & (mask != 30) & (mask != 31), 1, 0) # MHP v1 foot area # mask_temp = np.where((mask == 9) | (mask == 10), 1, 0) # cv2.imshow("ss", mask_temp*255.0) # cv2.waitKey(-1) gt_labels = np.unique(mask_temp) for i in range(1, len(gt_labels)): category_info = {'id': 1, 'is_crowd': 0} binary_mask = np.uint8(mask_temp) annotation_info = pycococreatortools.create_annotation_info( segmentation_id_val, image_id_val, category_info, binary_mask, image.size, tolerance=0) if annotation_info is not None: coco_output_val["annotations"].append(annotation_info) segmentation_id_val += 1 image_id_val += 1 if image_id_val % 100 == 0: print(image_id_val) with open('{}/{}_val.json'.format(args.json_save_dir, args.dataset), 'w') as output_json_file_val: json.dump(coco_output_val, output_json_file_val)