def png2coco(INFO,LICENSES,CATEGORIES,output_filepath,dataset): coco_output = { "info":INFO, "licenses":LICENSES, "categories":CATEGORIES, "images":[], "annotations":[] } image_id = 1 segmentation_id = 1 for i in range(len(dataset)): print(i,'/',len(dataset)) image_files = listdir(dataset[i]) for j in range(len(image_files)): if 'image' in image_files[j]: image_filename = join(dataset[i],image_files[j]) image = Image.open(image_filename) image_info = pycococreatortools.create_image_info( i,image_filename,image.size) coco_output["images"].append(image_info) else: annotation_filename = join(dataset[i],image_files[j]) class_id = [x['id'] for x in CATEGORIES if x['name'] in image_files[j]][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( i,image_id,category_info,binary_mask, image.size,tolerance=2) if annotation_info is not None: coco_output["annotations"].append(annotation_info) with open(output_filepath,'w') as output_json_file: json.dump(coco_output,output_json_file)
def main(): coco_output = {"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) # all image name # 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) # all this image 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 ][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( ROOT_DIR + '/amodel/frame{}.json'.format( os.path.basename(image_filename)[:-4]), 'w') as output_json_file: json.dump(coco_output, output_json_file)
def main(): # json dictionary coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } image_id = 1 segmentation_id = 1 for root, _, files in os.walk(IMAGE_DIR): image_paths = filter_for_jpeg(root, files) num_of_image_files = len(image_paths) for image_path in image_paths: image = Image.open(image_path) image_name = os.path.basename(image_path) image_info = pycococreatortools.create_image_info( image_id, image_name, image.size) coco_output["images"].append(image_info) # get maskes rle_masks = df.loc[df['ImageId'] == image_name, 'EncodedPixels'].tolist() num_of_rle_masks = len(rle_masks) for index in range(num_of_rle_masks): binary_mask = rle_decode(rle_masks[index]) class_id = 1 # all images are 1, with ship category_info = {'id': class_id, 'is_crowd': 0} annotation_info = pycococreatortools.create_annotation_info( segmentation_id, image_id, category_info, binary_mask, image.size, tolerance=2) # filter out low-quality annotations # save low-quality annotations for later if annotation_info is not None: coco_output["annotations"].append(annotation_info) else: save_bad_ann(image_name, binary_mask, segmentation_id) # assign an id to each annotation (no matter if it's filtered out) segmentation_id = segmentation_id + 1 if not image_id % 100: print("%d of %d is done." % (image_id, num_of_image_files)) image_id = image_id + 1 with open('../data/annotations/train_ship_instances_v2.json', 'w') as output_json_file: # json.dump(coco_output, output_json_file) json.dump(coco_output, output_json_file, indent=4)
def coco_img_info(self): img_info = pycococreatortools.create_image_info( self.img_id, self.img_path, self.img_size ) return img_info
def main(): # 最终放进json文件里的字典 coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], # 放一个空列表占位置,后面再append "annotations": [] } image_id = 1 segmentation_id = 1 # 最外层的循环是图片,因为图片的基本信息需要共享 # IMAGE_DIR路径下找到所有的图片 for root, _, files in os.walk(IMAGE_DIR): image_paths = filter_for_jpeg(root, files) # 图片文件地址 num_of_image_files = len(image_paths) # 图片个数 # 遍历每一张图片 for image_path in image_paths: # 提取图片信息 image = Image.open(image_path) image_name = os.path.basename(image_path) # 不需要具体的路径,只要图片文件名 image_info = pycococreatortools.create_image_info( image_id, image_name, image.size) coco_output["images"].append(image_info) # 内层循环是mask,把每一张图片的mask搜索出来 rle_masks = df.loc[df['ImageId'] == image_name, 'EncodedPixels'].tolist() num_of_rle_masks = len(rle_masks) for index in range(num_of_rle_masks): binary_mask = rle_decode(rle_masks[index]) class_id = 1 # 所有图片的类别都是1,ship category_info = {'id': class_id, 'is_crowd': 0} 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: save_bad_ann(image_name, binary_mask, segmentation_id) # 无论标注是否被写入数据集,均分配一个编号 segmentation_id = segmentation_id + 1 print("%d of %d is done."%(image_id,num_of_image_files)) image_id = image_id + 1 with open('{}/annotations/instances_ships_train2018.json'.format(ROOT_DIR), 'w') as output_json_file: # json.dump(coco_output, output_json_file) json.dump(coco_output, output_json_file,indent=4)
def create_annotations(): print("Started processing.") coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } image_id = 1 segmentation_id = 1 for root, _, files in os.walk(IMAGE_DIR): image_paths = filter_for_jpeg(root, files) num_of_image_files = len(image_paths) for image_path in image_paths: image = Image.open(image_path) image_name = os.path.basename(image_path) image_info = pycococreatortools.create_image_info( image_id, image_name, image.size) coco_output["images"].append(image_info) rle_masks = df.loc[df['ImageId'] == image_name, 'EncodedPixels'].tolist() num_of_rle_masks = len(rle_masks) for index in range(num_of_rle_masks): binary_mask = rle_decode(rle_masks[index]) class_id = 1 category_info = {'id': class_id, 'is_crowd': 0} 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: save_bad_ann(image_name, binary_mask, segmentation_id) segmentation_id = segmentation_id + 1 if (image_id % 1000 == 0): print("Processing %d of %d is done. %d perc." % (image_id, num_of_image_files, np.round(image_id / num_of_image_files, 2) * 100)) image_id = image_id + 1 return coco_output
def gen_coco(): 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_image(root, files) # 如果是肿瘤图片,直接添加。 # 如果是正常图片,先收拢一起,然后随机采样和肿瘤图片一样数量的,保正负样本均衡。 tumor_image_files = [image_filename for image_filename in image_files if 'tumor' in os.path.basename(image_filename)] normal_image_files = random.sample([image_filename for image_filename in image_files if 'normal' in os.path.basename(image_filename)],len(tumor_image_files)) # go through each image for image_filename in tqdm(tumor_image_files+normal_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: class_id = [x['id'] for x in CATEGORIES if x['name'] in os.path.basename(annotation_filename)][0] binary_mask = np.asarray(Image.open(annotation_filename).convert('1')).astype(np.uint8) # class_id = 1 if binary_mask.sum() > 0 else 0 category_info = {'id': class_id, 'is_crowd': 0} 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_camelyon16_train2020.json'.format(ROOT_DIR), 'w') as output_json_file: json.dump(coco_output, output_json_file)
def run(args): infer_dataset = dataloader.VOC12ImageDataset(args.infer_list, voc12_root=args.voc12_root) infer_data_loader = DataLoader(infer_dataset, shuffle=False, num_workers=args.num_workers, pin_memory=True) val_json = json.load( open(os.path.join(VOC2012_JSON_FOLDER, 'pascal_val2012.json'))) # Do not use this file for evaluation! coco_output = {} coco_output["images"] = [] coco_output["annotations"] = [] coco_output['categories'] = val_json['categories'] coco_output['type'] = val_json['type'] for iter, pack in tqdm(enumerate(infer_data_loader), total=len(infer_dataset)): img_name = pack['name'][0] img_id = int(img_name[:4] + img_name[5:]) img_size = pack['img'].shape[2:] image_info = pycococreatortools.create_image_info( img_id, img_name + ".jpg", (img_size[1], img_size[0])) coco_output["images"].append(image_info) ann = np.load(os.path.join(args.ins_seg_out_dir, img_name) + '.npy', allow_pickle=True).item() instance_id = 1 for score, mask, class_id in zip(ann['score'], ann['mask'], ann['class']): if score < 1e-5: continue category_info = {'id': class_id, 'is_crowd': False} annotation_info = pycococreatortools.create_annotation_info( instance_id, img_id, category_info, mask, img_size[::-1], tolerance=0) instance_id += 1 coco_output['annotations'].append(annotation_info) with open('voc2012_train_custom.json', 'w') as outfile: json.dump(coco_output, outfile)
def main(): coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } image_id = 0 # segmentation_id = 1 # filter for jpeg images for root, _, files in os.walk(IMAGE_DIR): image_files = filter_for_jpeg(root, files) for root, _, files in os.walk(mask_DIR): image_GT = filter_for_jpeg(root, files) # for image_filename in image_files: for num in range(len(image_files)): image = cv2.imread(image_files[num]) image_info = pycococreatortools.create_image_info( image_id, os.path.basename(image_files[num]), image.shape) print(image.shape) coco_output["images"].append(image_info) # image_name = image_files[num].split("/")[-1] f_str = image_files[num].split("/")[-1].split(".")[0] numGT = searchD(f_str, image_GT) print("PAIRS-->", f_str) print(image_GT[numGT]) # # filter for associated png annotations # for root, _, files in os.walk(ANNOTATION_DIR): class_id = 1 # SOS ayto allagh !!! isxyei gia to diko mou path ! # print ("File=", image_name) # print (class_id) # print (ROOT_DIR+'/masks/'+image_files[num].split("/")[-1]) # if "GT" in image_files[num].split("/")[-1]: # # print(image) # cv2.imwrite(ROOT_DIR+'/masks/'+image_files[num].split("/")[-1], image) # os.remove(os.path.join(image_files[num])) annotation_info = create_annotation(image_GT[numGT], image_id, class_id) if annotation_info is not None: coco_output["annotations"].append(annotation_info) # segmentation_id = segmentation_id + 1 image_id = image_id + 1 json_name = 'annotdatasetMin.json' json_path = '{}/annot/' + json_name with open(json_path.format(ROOT_DIR), '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 img_root, _, img_files in os.walk(IMAGE_DIR): image_files = filter_for_jpeg(img_root, img_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 annotation_root, _, annotation_filenames in os.walk(ANNOTATION_DIR): annotation_files = filter_for_annotations(annotation_root, annotation_filenames, 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(os.path.join(ROOT_DIR, JSON_FILENAME), 'w') as output_json_file: json.dump(coco_output, output_json_file)
def main(config): ROOT_DIR = config.ROOT_DIR_PYCOCOCREATOR IMAGE_DIR =config.IMAGE_DIR ANNOTATION_DIR = config.ANNOTATION_DIR FILE_NAME = config.JSON_OUTPUT_FILENAME CATEGORIES, coco_output = create_structure(config) 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(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('{}/{}'.format(ROOT_DIR, FILE_NAME), 'w') as output_json_file: json.dump(coco_output, output_json_file)
def get_images(IMAGE_DIR, image_id): images=[] # filter for jpeg images for root, _, files in os.walk(IMAGE_DIR): image_files = filter_for_png(root, files) image_files.sort() # ensure order # go through each image for image_filename in tqdm(image_files): image = Image.open(image_filename) image_info = pycococreatortools.create_image_info( image_id, os.path.basename(image_filename), image.size ) images.append(image_info) image_id +=1 return images,image_id
def mainfun(f_path,f2_path, bg_path, k, image_id, rangeN, class_idX, class_idY, coco_output, imageDir = IMAGE_DIR): for i in range(rangeN): image_filename = str(k) + ".jpg" img = cv2.imread(random.choice(f_path)) print (img.shape[:2]) image_info = pycococreatortools.create_image_info(image_id, os.path.basename(image_filename), img.shape[:2]) coco_output["images"].append(image_info) class_id = class_idX #image_filename.split("_")[-2][-1] # SOS ayto allagh !!! isxyei gia to diko mou path ! print ("File=", image_filename) print ("class_id", class_id) im_path= IMAGE_DIR + "/"+ image_filename cv2.imwrite(im_path, img) annotation_info = create_annotation(im_path, image_id, class_id) if annotation_info is not None: coco_output["annotations"].append(annotation_info) # segmentation_id = segmentation_id + 1 bgname = random.choice(bg_path) imgBack = cv2.imread(bgname) imFirst = replaceBG(img, imgBack) if f2_path!=None: img2 = cv2.imread(random.choice(f2_path)) cv2.imwrite(im_path, img2) class_id = class_idY #image_filename.split("_")[-2][-1] # SOS this works for this ! print ("File=", image_filename) print (class_id) annotation_info = create_annotation(im_path, image_id, class_id) if annotation_info is not None: coco_output["annotations"].append(annotation_info) imFinal = replaceBG(img2, imFirst) cv2.imwrite(im_path, imFinal) image_id = image_id + 1 k -=1 else: cv2.imwrite(im_path, imFirst) image_id = image_id + 1 k -=1 return coco_output, k, image_id
def main(): coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } image_id = 0 # segmentation_id = 1 # filter for jpeg images for root, _, files in os.walk(IMAGE_DIR): image_files = filter_for_jpeg(root, files) # # print (IMAGE_DIR) # # print (image_files) # # go through each image # print(image_files) 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) print (image.size) coco_output["images"].append(image_info) # # filter for associated png annotations # for root, _, files in os.walk(ANNOTATION_DIR): class_id = image_filename.split("_")[-2][-1] # SOS ayto allagh !!! isxyei gia to diko mou path ! print ("File=", image_filename) print (class_id) annotation_info = create_annotation(image_filename, image_id, class_id) if annotation_info is not None: coco_output["annotations"].append(annotation_info) # segmentation_id = segmentation_id + 1 image_id = image_id + 1 json_name = '2clAluVal.json' json_path = '{}/annot/'+ json_name with open(json_path.format(ROOT_DIR), 'w') as output_json_file: json.dump(coco_output, output_json_file)
def create_json_single_core(proc_id, png_files, inst_v_dir, id_converter, neighbor=False, rle=False): image_info = [] annotation_info = [] for idx, png_file in enumerate(png_files): # iid = int(png_file.split('_')[0]+png_file.split('_')[3]) vid = int(png_file[:4]) iid = int(png_file[:9]) # print(iid) # file_path = osp.join(INSTANCE_DIR, inst_v_dir, png_file) file_path = osp.join(PANOPTIC_DIR, inst_v_dir, png_file) insts = png2insts(file_path, id_converter, rle) for inst in insts: inst['image_id'] = iid if inst['fcn_id'] <= 10: # cuz stuff_max = 10 print('Warning: wrong FCN id') continue inst['category_id'] = inst['fcn_id'] # inst['category_id'] = FCNID2TRAINID[inst['fcn_id']] inst['width'] = W inst['height'] = H img_name = os.path.join( inst_v_dir, png_file.replace('final_mask', 'newImg8bit').replace('gtFine_color', 'leftImg8bit')) im_info = pycococreatortools.create_image_info(iid, MODE, img_name, (W, H)) image_info.append(im_info) annotation_info.extend(insts) return image_info, annotation_info
def worker_create_annotation_info(image_filename, image_id): image = Image.open(image_filename) image_info = pycococreatortools.create_image_info(image_id, os.path.basename(image_filename), image.size) #多进程计数器必须标明global global ann_counter # filter for associated png annotations annotation_infos=[] 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: class_id = [x['id'] for x in CATEGORIES if x['name'] in os.path.basename(annotation_filename)][0] binary_mask = np.asarray(Image.open(annotation_filename).convert('1')).astype(np.uint8) category_info = {'id': class_id, 'is_crowd': 0} annotation_info = pycococreatortools.create_annotation_info( ann_counter.increment(), image_id, category_info, binary_mask, image.size, tolerance=2) if annotation_info is not None: annotation_infos.append(annotation_info) return image_info, annotation_infos
def main(): EVAL_PUSH_NUMBER = -1 ROOT_DIR = sys.argv[1] if len(sys.argv) == 3: EVAL_PUSH_NUMBER = int(sys.argv[2]) coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } image_id = 1 segmentation_id = 1 for trial_dir in os.listdir(ROOT_DIR): if os.path.isdir(os.path.join(ROOT_DIR, trial_dir)): print(trial_dir) image_dir = os.path.join(ROOT_DIR, trial_dir, IMAGE_DIR) annotation_dir = os.path.join(ROOT_DIR, trial_dir, ANNOTATION_DIR) print("image dir", image_dir) print("annotation dir", annotation_dir) # filter for png images for root, _, files in os.walk(image_dir): files.sort() if (EVAL_PUSH_NUMBER > len(files) - 1): #files = files[len(files)-1:len(files)] continue elif (EVAL_PUSH_NUMBER >= 0): files = files[EVAL_PUSH_NUMBER:EVAL_PUSH_NUMBER + 1] image_files = filter_for_png(root, files) image_files.sort() #go through each image for image_filename in image_files: #print("image filename",image_filename) image = Image.open(image_filename) image_info = pycococreatortools.create_image_info( image_id, relpath(image_filename, ROOT_DIR), image.size) coco_output["images"].append(image_info) # filter for associated png annotations for root, _, files in os.walk(annotation_dir): files.sort() annotation_files = filter_for_annotations( root, files, image_filename) if (len(annotation_files) == 0): print("No annotations for", image_filename) coco_output["images"].remove(image_info) break for annotation_filename in annotation_files: print(annotation_filename) class_id = 1 # Assign workspace class if annotation_filename.find("_11.png") > -1: class_id = 2 #print("class id",class_id) category_info = {'id': class_id, 'is_crowd': 0} binary_mask = cv2.imread(annotation_filename, -1) print("Segmentation id", segmentation_id) 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 basename = os.path.basename(ROOT_DIR) basename = ''.join([i for i in basename if not i.isdigit()]) if not os.path.exists(ROOT_DIR + '/annotations'): os.makedirs(ROOT_DIR + '/annotations') outname = ROOT_DIR + '/annotations' + '/instances_push' + str( EVAL_PUSH_NUMBER).zfill(2) + '.json' with open(outname, 'w') as output_json_file: json.dump(coco_output, output_json_file) print("Saved json file", outname)
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): # png_to_jpg(root, files) image_files = filter_for_jpeg(root, files) # go through each image for image_filename in image_files: print(image_filename) 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: # TODO: naive bug fix,限定於cat是按照複雜度排列,仍有可能出錯 class_id = [ x["id"] for x in CATEGORIES if x["name"] in annotation_filename ][-1] category_info = { "id": class_id, # "is_crowd": "crowd" in image_filename, "is_crowd": True, # 強制讓segmentation用png->binary mask } binary_mask = png_to_binary_mask(annotation_filename) annotation_info = pycococreatortools.create_annotation_info_without_mask( segmentation_id, image_id, category_info, binary_mask, image.size, # tolerance=10, # tolerance=2, ) # print(annotation_info) if annotation_info is not None: coco_output["annotations"].append(annotation_info) segmentation_id = segmentation_id + 1 image_id = image_id + 1 with open("{}/annotation.json".format(ROOT), "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 c = 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: if 'mass' and 'mask' not in image_filename: # print(image_filename) print( str(c + 1) + '/' + str(sum([len(files) for _, _, files in os.walk(IMAGE_DIR)]))) c += 1 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_ann, _, ann in os.walk(ANNOTATION_DIR): annotation_files = filter_for_annotations( root_ann, ann, image_filename) # go through each associated annotation for annotation_filename in annotation_files: # print(annotation_filename) class_id = 0 if 'mass' or 'mask' in annotation_filename: class_id = 1 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 else: print('No image files') continue with open(os.path.join(ROOT_DIR, 'instances_shape_test2019.json'), '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_png(root, files) image_files.sort() # ensure order # go through each image for image_filename in tqdm(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: class_id = int( re.search(r'\d(?=_inst\d.png)', annotation_filename).group()) category_info = { "id": class_id, "is_crowd": "crowd" in image_filename, } binary_mask = np.asarray( Image.open(annotation_filename)).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( "{0}/{1}_{2}.json".format(SAVE_DIR, args.json_prefix, GROUP_NAME), "w") as output_json_file: json.dump(coco_output, output_json_file, indent=4)
def create_json_direct(images_path, new_path): ''' :param images_path: :param new_path: :return: ''' def get_mask_path(im_path): image_name = os.path.basename(im_path) sub_dir_path = os.path.dirname(os.path.dirname(im_path)) mask_dir_name = image_name.split('.')[0] + "_outlines" mask_dir_path = os.path.join(sub_dir_path, "crops", mask_dir_name) mask_image_name = os.listdir(mask_dir_path) mask_image_name = [ i for i in mask_image_name if len(i.split('_')) == 3 ] mask_image_path = [ os.path.join(mask_dir_path, i) for i in mask_image_name ] return mask_image_path coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } train_images_path = os.path.join(new_path, "train2020_scale") if not os.path.exists(train_images_path): os.mkdir(train_images_path) train_anno_path = os.path.join(new_path, "annotations", "instances_cell_train2020_scale.json") train_image_bad_path = os.path.join(new_path, "bad_ann") images_num = len(images_path) image_id = 1 segmentation_id = 1 print("Total image len:{}".format(images_num)) for im_path in images_path: # new_image_name is the new name of this image in train2020_sacle and coco json file # './new_training/76-9-10052-crop2/xy-images/crop2-Composite-10051.tif' print("=====================================") print(im_path) dir_name = im_path.split('/')[-3] im = os.path.basename(im_path).split('.')[0] im = '-'.join(im.split('-')[-2:]) new_image_name = dir_name + "-" + im + ".png" print(new_image_name) # read image and save to new dir # image.size 是 w,h image = Image.open(im_path) image_info = pycococreatortools.create_image_info( image_id, new_image_name, image.size) coco_output["images"].append(image_info) print("image_info:\n{}".format(image_info)) # save to new dir new_image_path = os.path.join(train_images_path, new_image_name) image.save(new_image_path) print("Save as:{}".format(new_image_path)) # find this image`s masks and make boxes & segmentation info masks_path = get_mask_path(im_path) print("Masks num of image:{}".format(len(masks_path))) for c in range(len(masks_path)): ms_path = masks_path[c] # ms_path is one of the masks path for this image binary_mask = np.asarray(Image.open(ms_path).convert("L")).astype( np.uint8) binary_mask[binary_mask > 0] = 1 class_id = 1 category_info = {'id': class_id, 'is_crowd': 0} annotation_info = pycococreatortools.create_annotation_info( segmentation_id, image_id, category_info, binary_mask, image.size, tolerance=2) print("image size:{} {} mask size:{}".format( image.size[1], image.size[0], binary_mask.shape)) print("Annotation info:{}".format(annotation_info)) if annotation_info is not None: coco_output["annotations"].append(annotation_info) else: # coco_output["annotations"].append(annotation_info) print("There is an bad binary mask in :{}-{}".format( new_image_name, c)) save_bad_ann(train_image_bad_path, new_image_path, binary_mask, segmentation_id) segmentation_id += 1 image_id += 1 with open(train_anno_path, 'w') as output_json_file: # json.dump(coco_output, output_json_file) json.dump(coco_output, output_json_file, indent=4)
def __call__(self, img_desc): img_dir, img_id, lbl_cat = img_desc img_unique_id = counter.increment() coco_ann = [] # Load the annotation with Image.open( path.join(self.msk_base_dir, img_dir, img_id + "_" + lbl_cat + _INSTANCE_EXT)) as lbl_img: lbl = np.array(lbl_img) lbl_size = lbl_img.size ids = np.unique(lbl) # Compress the labels and compute cat lbl_out = np.ones(lbl.shape, np.uint8) * 255 cat = [255] iscrowd = [0] for city_id in ids: if city_id < 1000: # Stuff or group cls_i = city_id iscrowd_i = cs_labels[cls_i].hasInstances else: # Instance cls_i = city_id // 1000 iscrowd_i = False # If it's a void class just skip it if cs_labels[cls_i].trainId == 255 or cs_labels[ cls_i].trainId == -1: continue # Extract all necessary information iss_class_id = cs_labels[cls_i].trainId mask_i = lbl == city_id lbl_out[mask_i] = iss_class_id # Compute COCO detection format annotation if cs_labels[cls_i].hasInstances: category_info = {"id": iss_class_id, "is_crowd": iscrowd_i} coco_ann_i = pct.create_annotation_info(counter.increment(), img_unique_id, category_info, mask_i, lbl_size, tolerance=2) if coco_ann_i is not None: coco_ann.append(coco_ann_i) # COCO detection format image annotation coco_img = pct.create_image_info(img_unique_id, img_id + ".png", lbl_size) # Write output Image.fromarray(lbl_out).save(path.join(self.msk_dir, img_id + ".png")) shutil.copy(path.join(self.img_base_dir, img_dir, img_id + _IMAGE_EXT), path.join(self.img_dir, img_id + ".png")) return coco_img, coco_ann
def convert(self, train_type): mask_dir = self.ROOT_DIR + '/Part_Masks/{}'.format(train_type) image_dir = self.ROOT_DIR + '/Images/{}'.format(train_type) coco_output = { "info": self.INFO, "licenses": self.LICENSES, "categories": self.CATEGORIES, "images": [], "annotations": [] } image_id = 1 segmentation_id = 1 # filter for jpeg images for root, _, files in os.walk(image_dir): image_files = self.filter_for_jpeg(root, files) # go through each image for index, image_filename in enumerate(tqdm(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 = self.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 self.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('{}/{}.json'.format(self.ROOT_DIR, train_type), 'w') as output_json_file: json.dump(coco_output, output_json_file)
def json_generate(): car = 0 pedestrian = 0 files = os.listdir(IMAGE_SAVE_DIR) coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } image_id = 1 segmentation_id = 1 # go through each image for image_filename in files: image_name = image_filename.split('.')[0] image_path = os.path.join(IMAGE_SAVE_DIR, image_filename) image = Image.open(image_path) image_info = pycococreatortools.create_image_info( image_id, os.path.basename(image_filename), image.size) coco_output["images"].append(image_info) print(image_filename) annotation_sub_path = os.path.join(INSTANCE_DIR, image_name) ann_files = os.listdir(annotation_sub_path) if len(ann_files) == 0: print("ao avaliable annotation") continue else: for annotation_filename in ann_files: annotation_path = os.path.join(annotation_sub_path, annotation_filename) for x in CATEGORIES: if x['name'] in annotation_filename: class_id = x['id'] break if class_id == 1: car += 1 elif class_id == 2: pedestrian += 1 else: print('illegal class id') category_info = { 'id': class_id, 'is_crowd': 'crowd' in image_filename } binary_mask = np.asarray( Image.open(annotation_path).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 print(image_id) with open('{}/val.json'.format(ROOT_DIR), 'w') as output_json_file: json.dump(coco_output, output_json_file) print(car, pedestrian)
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) #print(image_info) 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) print(annotation_metadata) annotation_data = AnnotationData(annotation_array, annotation_metadata) object_classes = annotation_data.get_classes() #print(object_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) print(annotation_info) 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(): image_id = 1 segmentation_id = 1 VIDEOS =[] ANNOTATIONS =[] IMAGES_VAL =[] IMAGES_TRAIN =[] ANNOTATIONS_VAL =[] ANNOTATIONS_TRAIN =[] root_dir = IMAGE_DIR train, val = data_split(FOLD_NUM) # filter for jpeg images for dir in os.listdir(root_dir): print(dir) path = os.path.join(root_dir,dir)+"/left_frames/" ANNOTATION_DIR = os.path.join(root_dir,dir)+"/ground_truth/" #print(path) # filter for jpeg images for root, _, files in os.walk(path): image_files = [os.path.join(path,f) for f in files]#filter_for_png(root, files) image_files.sort() # ensure order #print(image_files) # go through each image for image_filename in image_files: #tqdm(image_files): image = Image.open(image_filename) #print(image_filename) image_info = pycococreatortools.create_image_info( image_id, os.path.basename(image_filename), image.size ) image_info["file_name"]=os.path.join(dir,image_info["file_name"]) # filter for associated png annotations annotation_files = filter_for_annotations( ANNOTATION_DIR,image_filename ) #print("here",annotation_files) if len(annotation_files): if dir not in train: IMAGES_VAL.append(image_info) else: IMAGES_TRAIN.append(image_info) image_id +=1 # go through each associated annotation for annotation_filename in annotation_files: label,class_id = get_label(annotation_filename.split("/")[-2]) if not class_id: print(annotation_filename) print(label,class_id) category_info = { "id": class_id } binary_mask = cv2.imread(annotation_filename,0).astype(np.uint8) area,bbox,segmentation = create_annotation_info(binary_mask,None) if area: annotation_info= { "id": segmentation_id, "image_id": image_id, "category_id": class_id, "iscrowd":None, "area": area, "bbox": bbox, "segmentation": segmentation, "width": binary_mask.shape[1], "height": binary_mask.shape[0], } segmentation_id+=1 if dir not in train: ANNOTATIONS_VAL.append(annotation_info) else: ANNOTATIONS_TRAIN.append(annotation_info) coco_output_train = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": IMAGES_TRAIN, "annotations": ANNOTATIONS_TRAIN, } coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": IMAGES_VAL, "annotations": ANNOTATIONS_VAL, } with open( "instances_train_sub.json", "w") as output_json_file: json.dump(coco_output_train, output_json_file, indent=4) with open( "instances_val_sub.json", "w") as output_json_file: json.dump(coco_output, output_json_file, indent=4)
with open(template_path) as f: data_val = json.load(f) # Loop all files and annotate them print(str(num_images) + " images found") annotation_id = 0 num_images = len([name for name in os.listdir(IMAGE_DIR) if os.path.isfile(os.path.join(IMAGE_DIR, name))]) for i, image_file in enumerate(sorted(os.listdir(IMAGE_DIR))): print("{}: working on image {}".format(i, image_file)) image = skimage.io.imread(os.path.join(IMAGE_DIR, image_file)) a, b = image.shape[0:2] image_size = b, a image_id = int(image_file.split('_')[-1][:-4]) img_data = pycococreatortools.create_image_info(image_id, image_file, image_size) if i < num_images * DATA_SPLIT: data_train["images"].append(img_data) else: data_val["images"].append(img_data) # Run detection try: results = model.detect([image]) # Continue if no detection was made except IndexError: print("No detection found") annotation_errors[image_file] = "No detection found" continue except:
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(annotation_filename) if 'c01' in annotation_filename: class_id = 1 elif 'c02' in annotation_filename: class_id = 2 elif 'c03' in annotation_filename: class_id = 3 elif 'c04' in annotation_filename: class_id = 4 elif 'c05' in annotation_filename: class_id = 5 elif 'c06' in annotation_filename: class_id = 6 elif 'c07' in annotation_filename: class_id = 7 elif 'c08' in annotation_filename: class_id = 8 elif 'c09' in annotation_filename: class_id = 9 elif 'c10' in annotation_filename: class_id = 10 elif 'c11' in annotation_filename: class_id = 11 elif 'c12' in annotation_filename: class_id = 12 elif 'c13' in annotation_filename: class_id = 13 elif 'c14' in annotation_filename: class_id = 14 elif 'c15' in annotation_filename: class_id = 15 elif 'c16' in annotation_filename: class_id = 16 elif 'c17' in annotation_filename: class_id = 17 elif 'c18' in annotation_filename: class_id = 18 elif 'c19' in annotation_filename: class_id = 19 elif 'c20' in annotation_filename: class_id = 20 elif 'c21' in annotation_filename: class_id = 21 elif 'c22' in annotation_filename: class_id = 22 elif 'c23' in annotation_filename: class_id = 23 else: class_id = 24 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('{0}/MFISH_chromosomes_{1}2018.json'.format(ANNOTATION_DIR, SET_NAME), 'w') as output_json_file: json.dump(coco_output, output_json_file)
def main(): CLASS_ID = 1 CATEGORIES = \ [{ 'id': CLASS_ID, 'name': 'jenga_block', 'supercategory': 'shape', }] coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "images": [], "annotations": [] } image_global_id = 1 segmentation_global_id = 1 # filter for jpeg images img_seg_files_dict = filter_for_jpeg() # print(img_seg_files_dict) img_size = (224, 224) for img_file_path in list(img_seg_files_dict.keys()): image_info = pycococreatortools.create_image_info( image_global_id, img_file_path, img_size) mask_valid = False for seg_file_path in img_seg_files_dict[img_file_path]: # Crowd has to be 0 or else no anno get loaded in maskrcnn category_info = {'id': CLASS_ID, 'is_crowd': 0} # print(seg_file_path) binary_mask = np.asarray(Image.open(seg_file_path)) if np.count_nonzero(binary_mask) > 0: annotation_info = pycococreatortools.create_annotation_info( segmentation_global_id, image_global_id, category_info, binary_mask, img_size, tolerance=2) if annotation_info is not None: coco_output["annotations"].append(annotation_info) mask_valid = True segmentation_global_id += 1 if mask_valid == True: # pss coco_output["images"].append(image_info) else: print("No valid mask") if image_global_id % 100 == 0: print("Image {}/{}".format(image_global_id, len(list(img_seg_files_dict.keys())))) image_global_id = image_global_id + 1 with open('{}/{}.json'.format(ROOT_OUTDIR, OUTFILE_NAME), 'w') as output_json_file: json.dump(coco_output, output_json_file)
def main(): # object_settings_file = Path(os.path.join(IMAGE_DIR_LIST[0], "_object_settings.json")) # camera_settings_file = Path(os.path.join(IMAGE_DIR_LIST[0], "_camera_settings.json")) if object_settings_file.is_file(): with open(object_settings_file) as file: object_settings_data = json.load(file) if SELECTED_OBJECTS is None: CLASSES = object_settings_data['exported_object_classes'] else: CLASSES = SELECTED_OBJECTS CATEGORIES = [{ 'id': i, 'name': CLASSES[i].replace('_16k', '').replace('_16K', ''), 'supercategory': 'shape', } for i in range(0,len(CLASSES))] FIXED_TRANSFORMS = {} for i in range(0,len(object_settings_data['exported_object_classes'])): class_name = object_settings_data['exported_objects'][i]['class'] transform = object_settings_data['exported_objects'][i]['fixed_model_transform'] if class_name in CLASSES: class_name = class_name.replace('_16k', '').replace('_16K', '') FIXED_TRANSFORMS[class_name] = transform # print(FIXED_TRANSFORMS) # SEGMENTATION_DATA = object_settings_data['exported_objects'] else: raise Exception("Object settings file not found") if camera_settings_file.is_file(): with open(camera_settings_file) as file: camera_settings_data = json.load(file) CAMERA_INTRINSICS = camera_settings_data['camera_settings'][0]['intrinsic_settings'] else: raise Exception("Camera settings file not found") VIEWPOINTS = [viewpoints_xyz[i].tolist() for i in range(0, len(viewpoints_xyz))] INPLANE_ROTATIONS = [inplane_rot_angles[i] for i in range(0, len(inplane_rot_angles))] coco_output = { "info": INFO, "licenses": LICENSES, "categories": CATEGORIES, "viewpoints" : VIEWPOINTS, "inplane_rotations" : INPLANE_ROTATIONS, "camera_intrinsic_settings": CAMERA_INTRINSICS, "fixed_transforms": FIXED_TRANSFORMS, "images": [], "annotations": [] } image_global_id = 1 segmentation_global_id = 1 # filter for jpeg images for IMAGE_DIR_T in IMAGE_DIR_LIST: # for root, _, files in os.walk(IMAGE_DIR): for SCENE in SCENES: if IMAGE_DIR_T == "": IMAGE_DIR = os.path.join(ROOT_DIR, SCENE) else: IMAGE_DIR = os.path.join(ROOT_DIR, IMAGE_DIR_T, SCENE) all_dir_files = os.listdir(IMAGE_DIR) image_files = filter_for_jpeg(IMAGE_DIR, all_dir_files) # dir_name = os.path.basename(IMAGE_DIR) SEGMENTATION_DATA = get_segmentation_data_for_scene(IMAGE_DIR) print(SEGMENTATION_DATA) # go through each image for ii in trange(len(image_files)): image_filename = image_files[ii] if IMAGE_DIR_T == "": image_out_filename = os.path.join(SCENE, os.path.basename(image_filename)) else: image_out_filename = os.path.join(IMAGE_DIR_T, SCENE, os.path.basename(image_filename)) img_size = (960,540) image_info = pycococreatortools.create_image_info( image_global_id, image_out_filename, img_size ) # plt.figure() # skimage.io.imshow(skimage.io.imread(image_filename)) # plt.show() # filter for associated png annotations # for root, _, files in os.walk(IMAGE_DIR): segmentation_image_files = filter_for_annotations(IMAGE_DIR, all_dir_files, image_filename) label_files = filter_for_labels(IMAGE_DIR, all_dir_files, image_filename) boxes = [] labels = [] segmentation_ids = [] label_filename = label_files[0] # go through each associated json file containing objects data # for label_filename in label_files: # print("File %d - %s"% (image_global_id, label_filename)) my_file = Path(label_filename) segmentation_image = skimage.io.imread(segmentation_image_files[0]) # print("File %d - %s"% (image_global_id, segmentation_image_files[0])) if my_file.is_file(): with open(label_filename) as file: label_data = json.load(file) # all_objects_yaw_only = True for i in range(0, len(label_data['objects'])): class_name = label_data['objects'][i]['class'] if class_name not in SELECTED_OBJECTS: continue # print(class_name) class_bounding_box = label_data['objects'][i]['bounding_box'] quat = label_data['objects'][i]['quaternion_xyzw'] angles = RT_transform.quat2euler(get_wxyz_quaternion(quat)) # angles = RT_transform.quat2euler(get_wxyz_quaternion(quat), 'syxz') # angles = apply_angle_symmetry(angles, SYMMETRY_INFO[class_name]) # This function gives angles with this convention of euler - https://en.wikipedia.org/wiki/Euler_angles#Signs_and_ranges (geometric definition) # if np.isclose(angles[1], 0): # print("Test") theta, phi = euler2sphere(angles[1], angles[0]) actual_angles = np.array([1, theta, phi]) xyz_coord = sphere2cart(1, theta, phi) viewpoint_id = find_viewpoint_id(viewpoints_xyz, xyz_coord) r_xyz = get_viewpoint_from_id(viewpoints_xyz, viewpoint_id) recovered_angles = np.array(cart2sphere(r_xyz[0], r_xyz[1], r_xyz[2])) inplane_rotation_id = find_inplane_rotation_id(inplane_rot_angles, angles[2]) # inplate_rotation_angle = get_inplane_rotation_from_id(INPLANE_ROTATIONS, inplane_rotation_id) if np.all(np.isclose(actual_angles, recovered_angles, atol=0.4)) == False: print("Mismatch in : {}".format(label_filename)) print("sphere2cart angles : {}".format(actual_angles)) print("cart2sphere angles : {}".format(recovered_angles)) # elif np.all(np.isclose(actual_angles, recovered_angles, atol=0.4)) == True: # print("Match") # print(inplate_rotation_angle) class_label = [x['id'] for x in CATEGORIES if x['name'] in class_name][0] segmentation_id = [x['segmentation_class_id'] for x in SEGMENTATION_DATA if x['class'] in class_name][0] boxes.append(class_bounding_box['top_left'] + class_bounding_box['bottom_right']) labels.append(class_label) segmentation_ids.append([x['segmentation_class_id'] for x in SEGMENTATION_DATA if x['class'] in class_name][0]) # Create binary masks from segmentation image for every object # for segmentation_image_file in segmentation_image_files: # segmentation_image = skimage.io.imread(segmentation_image_file) binary_mask = np.copy(segmentation_image) binary_mask[binary_mask != segmentation_id] = 0 binary_mask[binary_mask == segmentation_id] = 1 # skimage.io.imshow(binary_mask, cmap=plt.cm.gray) # plt.show() # TODO : check if its actually a crowd in case of multiple instances of one object type # class_label = [x['class'] for x in SEGMENTATION_DATA if x['segmentation_class_id'] in segmentation_id][0] category_info = {'id': class_label, 'is_crowd': 0} annotation_info = pycococreatortools.create_annotation_info( segmentation_global_id, image_global_id, category_info, binary_mask, img_size, tolerance=2) # print(annotation_info) if annotation_info is not None: annotation_info['viewpoint_id'] = int(viewpoint_id) annotation_info['inplane_rotation_id'] = int(inplane_rotation_id) annotation_info['camera_pose'] = label_data['camera_data'] annotation_info['location'] = label_data['objects'][i]['location'] annotation_info['quaternion_xyzw'] = quat coco_output["annotations"].append(annotation_info) coco_output["images"].append(image_info) else: tqdm.write("File %s doesn't have boxes or labels in json file" % image_filename) segmentation_global_id = segmentation_global_id + 1 else: tqdm.write("File %s doesn't have a label file" % image_filename) image_global_id = image_global_id + 1 with open('{}/{}.json'.format(ROOT_DIR, OUTFILE_NAME), 'w') as output_json_file: json.dump(coco_output, output_json_file)