xy = np.reshape(xy, [1, -1, 2])
        #mask = { class_id : xy }
        #masks.append(mask)
        
        category_info = {'id': class_id, 'is_crowd': False}
        
        bg = np.ones(image_shape_tuple, dtype="uint8")
        bg.fill(255)

        img = np.zeros_like(image)

        cv2.fillPoly(img, xy, (255,255,255))

        im = Image.fromarray(img)
        
        binary_mask = np.asarray(im.convert('1')).astype(np.uint8)

        annotation_info = pycococreatortools.create_annotation_info(
                segmentation_id, image_id, category_info, binary_mask)
        
        coco_output["annotations"].append(annotation_info)
        
        segmentation_id = segmentation_id + 1
    image_id = image_id + 1
    
    
with open(dataDir + output_filename, 'w') as output_json_file:
    json.dump(coco_output, output_json_file)


Exemple #2
0
def main():
    """debug:
          bbox = cocoInfo.CocoInfo['annotations'][-1]['bbox']
          img = cv2.imread(image_files[i], 0)
          img = cv2.rectangle(img, tuple(bbox[:2]), tuple(bbox[2:]), 255, 2)
          plt.figure()
          plt.imshow(img, cmap='gray')
          if i > 10:
               return
     return cocoInfo.CocoInfo['images'], cocoInfo.CocoInfo['annotations']
     """
    coco_train = COCOINFO()
    coco_test = COCOINFO()
    #     csv_annos = readCSV()
    image_files = glob('cleanImages/*.png')
    shuffle(image_files)
    ntrain = np.round(len(image_files) * 0.8).astype(np.int)
    segmentation_id = 0
    for i in tqdm(range(ntrain)):
        img_id = i + 1
        copy_file(image_files[i], train=True)
        imageInfo = getImageInfo(img_id, image_files[i])
        annmasks = getAnnImages(image_files[i])
        for annmask in annmasks:
            binary_mask = get_binary_mask(annmask)
            segmentation_id += 1
            annoInfo = pycococreatortools.create_annotation_info(
                segmentation_id,
                img_id,
                category_info={
                    'id': 1,
                    'is_crowd': 0
                },
                binary_mask=binary_mask,
                image_size=(512, 512),
                tolerance=2)
            coco_train.update(image=imageInfo)
            coco_train.update(annotation=annoInfo)

    # write train.json file
    with open('cleanNodules/annotations/instance_nodule_train.json',
              'a+',
              newline='') as f:
        json.dump(coco_train.CocoInfo, f)

    for i in tqdm(range(ntrain, len(image_files))):
        img_id = i + 1
        copy_file(image_files[i], train=False)
        imageInfo = getImageInfo(img_id, image_files[i])
        annmasks = getAnnImages(image_files[i])
        for annmask in annmasks:
            binary_mask = get_binary_mask(annmask)
            segmentation_id += 1
            annoInfo = pycococreatortools.create_annotation_info(
                segmentation_id,
                img_id,
                category_info={
                    'id': 1,
                    'is_crowd': 0
                },
                binary_mask=binary_mask,
                image_size=(512, 512),
                tolerance=2)
            coco_test.update(image=imageInfo)
            coco_test.update(annotation=annoInfo)

    # write test.json file
    with open('cleanNodules/annotations/instance_nodule_test.json',
              'a+',
              newline='') as f:
        json.dump(coco_test.CocoInfo, f)

    print('*' * 25, 'All Done', '*' * 25)
    def generate_coco_annotations(self):
        coco_output = {
            "info": self.INFO,
            "licenses": self.LICENSES,
            "categories": self.CATEGORIES,
            "images": [],
            "annotations": []
        }

        image_id = 1
        segmentation_id = 1
        classes = [x['supercategory'] for x in self.CATEGORIES]
        print(
            '[INFO] Generating annotation .json file from instance masks . . .'
        )
        for root, _, files in walk(split(self.rgb_dir)[0]):
            image_files = self.filter_for_png(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, basename(image_filename), image.size)
                coco_output["images"].append(image_info)

                # filter for associated png annotations
                for root, _, files in walk(self.coco_instances):
                    annotation_files = self.filter_for_annotations(
                        root, files, image_filename)

                    # go through each associated annotation
                    for annotation_filename in annotation_files:

                        # print(annotation_filename)
                        for class_label in classes:
                            if class_label in annotation_filename:
                                class_id = classes.index(class_label) + 1
                        # class_id = [x['id'] for x in CATEGORIES][0]

                        category_info = {
                            'id': class_id,
                            'is_crowd': 'crowd' in image_filename
                        }
                        # category_info = {'id': 1, 'is_crowd': 0}
                        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=0)

                        if annotation_info is not None:
                            coco_output["annotations"].append(annotation_info)

                        segmentation_id = segmentation_id + 1

                image_id = image_id + 1

        with open(join(self.root, 'instances_{}2016.json'.format(self.split)),
                  'w') as output_json_file:
            json.dump(coco_output, output_json_file)
        # Visualize results
        result = results[0]

        # Loop for each annotation
        for i in range(len(result["class_ids"])):
            # Get mask
            masks = result["masks"]
            m = masks[:, :, i]

            # https://patrickwasp.com/create-your-own-coco-style-dataset/
            category_info = {
                'id': result["class_ids"][i].item(),
                'is_crowd': 0
            }

            # Create annotation info
            ann_data = pycococreatortools.create_annotation_info(
                image_id * 10000 + i,
                image_id,
                category_info,
                m,
                image_size,
                tolerance=2)

            data["annotations"].append(ann_data)

# Write output file
with open("./data/video/annotations/instances_video2014.json", "w") as outfile:
    json.dump(data, outfile, indent=4)
def main():

    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": []
    }

    # load the configuration defining which color match which categories in the images
    with open(SLY_DIR + '/obj_class_to_machine_color.json') as json_file:
        json_data = json.load(json_file)
        print(json_data)

    # load and check supervisely images
    sly_imgs_path = SLY_DIR + '/ds/masks_machine/'
    slyimg_list = os.listdir(sly_imgs_path)
    slyimg_list.sort()
    print("total number of supervisely format images: ", len(slyimg_list))

    # These ids will be automatically increased as we go
    annotation_id = 1
    image_id = 1

    # Create the annotations
    annotations = []
    is_crowd = 1

    # go through each image
    for i in tqdm(range(len(slyimg_list))):
        # load mask image
        mask_image = Image.open(sly_imgs_path + slyimg_list[i])
        # create image info
        image_info = create_image_info(mask_image, image_id, slyimg_list[i])
        coco_output["images"].append(image_info)

        # create sub masks and annotations
        sub_masks = create_sub_masks(mask_image)
        for color, sub_mask in sub_masks.items():
            category_id = CATEGORY_IDS[color]
            if category_id == 6:
                continue
            sub_mask = np.asarray(sub_mask)
            category_info = {'id': category_id, 'is_crowd': 1}
            annotation = pycococreatortools.create_annotation_info(
                annotation_id,
                image_id,
                category_info,
                sub_mask,
                reversed(sub_mask.shape),
                tolerance=2)
            # create_sub_mask_annotation(sub_mask, image_id, category_id, annotation_id, is_crowd)
            if annotation is not None:
                coco_output["annotations"].append(annotation)
                annotation_id += 1
        image_id += 1

    # write final annotation file
    with open('{}/test.json'.format(SAVE_DIR), 'w') as output_json_file:
        json.dump(coco_output, output_json_file)
Exemple #6
0
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 'Bird' in annotation_filename:
                        class_id = 1
                    elif 'Ground_Animal' in annotation_filename:
                        class_id = 2
                    elif 'Crosswalk_Plain' in annotation_filename:
                        class_id = 3
                    elif 'Person' in annotation_filename:
                        class_id = 4
                    elif 'Bicyclist' in annotation_filename:
                        class_id = 5
                    elif 'Motorcyclist' in annotation_filename:
                        class_id = 6
                    elif 'Other_Rider' in annotation_filename:
                        class_id = 7
                    elif 'Lane_Marking_-_Crosswalk' in annotation_filename:
                        class_id = 8
                    elif 'Banner' in annotation_filename:
                        class_id = 9
                    elif 'Bench' in annotation_filename:
                        class_id = 10
                    elif 'Bike_Rack' in annotation_filename:
                        class_id = 11
                    elif 'Billboard' in annotation_filename:
                        class_id = 12
                    elif 'Catch_Basin' in annotation_filename:
                        class_id = 13
                    elif 'CCTV_Camera' in annotation_filename:
                        class_id = 14
                    elif 'Fire_Hydrant' in annotation_filename:
                        class_id = 15
                    elif 'Junction_Box' in annotation_filename:
                        class_id = 16
                    elif 'Mailbox' in annotation_filename:
                        class_id = 17
                    elif 'Manhole' in annotation_filename:
                        class_id = 18
                    elif 'Phone_Booth' in annotation_filename:
                        class_id = 19
                    elif 'Street_Light' in annotation_filename:
                        class_id = 20
                    elif 'Pole' in annotation_filename:
                        class_id = 21
                    elif 'Traffic_Sign_Frame' in annotation_filename:
                        class_id = 22
                    elif 'Utility_Pole' in annotation_filename:
                        class_id = 23
                    elif 'Traffic_Light' in annotation_filename:
                        class_id = 24
                    elif 'Traffic_Sign_(Back)' in annotation_filename:
                        class_id = 25
                    elif 'Traffic_Sign_(Front)' in annotation_filename:
                        class_id = 26
                    elif 'Trash_Can' in annotation_filename:
                        class_id = 27
                    elif 'Bicycle' in annotation_filename:
                        class_id = 28
                    elif 'Boat' in annotation_filename:
                        class_id = 29
                    elif 'Bus' in annotation_filename:
                        class_id = 30
                    elif 'Car' in annotation_filename:
                        class_id = 31
                    elif 'Caravan' in annotation_filename:
                        class_id = 32
                    elif 'Motorcycle' in annotation_filename:
                        class_id = 33
                    elif 'Other_Vehicle' in annotation_filename:
                        class_id = 34
                    elif 'Trailer' in annotation_filename:
                        class_id = 35
                    elif 'Truck' in annotation_filename:
                        class_id = 36
                    elif 'Wheeled_Slow' in annotation_filename:
                        class_id = 37

                    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_shape_train2018.json'.format(ROOT_DIR),
              'w') as output_json_file:
        json.dump(coco_output, output_json_file)
Exemple #7
0
def genCoco(DIR):
    coco_output ={
        "info": {
            "description": DIR,
            "url": "...",
            "version": "1.0",
            "year": 2019,
            "contributor": "gp",
            "date_created": datetime.datetime.utcnow().isoformat(' ')
        },
        "licenses": [
            {
                "id": 2112,
                "name": "DFV Srl Surano",
                "url": "..."
            }
        ],
        "categories" : [],
        "images": [],
        "annotations": []
    }

    subdirList = []
    subdirList = os.listdir(DIR)

    catList = getCategories()
    for category in catList:
        coco_output["categories"].append(category)

    compositeId = 0
    annotationId = 0

    for subDir in subdirList: #per ogni esecuzione
        composite = OUT_NAME.split(".")[0]+"_"+subDir+"."+OUT_NAME.split(".")[1]
        i = Image.open(DIR+subDir+"/"+composite)
        coco_output["images"].append( # aggiunge l'immagine composita 
                pycococreatortools.create_image_info(
                    compositeId,
                    os.path.basename(composite),
                    i.size
                )
            )
        
        #ottiene le immagini singole (composita esclusa)
        imageList = []
        imageList = os.listdir(DIR+subDir)
        imageList.remove(composite)
        imageList.remove(JSON_NAME.split(".")[0]+"_"+subDir+"."+JSON_NAME.split(".")[1])

        for image in imageList:
            img = Image.open(DIR+subDir+"/"+image)

            category_info = { # id categoria dell'immagine singola
                "id": next((cat for cat in catList if cat["name"] == image.split("_")[0]), None)["id"],
                "is_crowd": 0
            }

            binary_mask = np.asarray(img.convert("1")).astype(np.uint8)

            annotation_info = pycococreatortools.create_annotation_info(
                annotationId, compositeId, category_info, binary_mask, img.size, tolerance = 2
            )

            coco_output["annotations"].append(annotation_info)

            annotationId += 1
        
        compositeId += 1

    with open(DIR+COCO_NAME, "w") as out:
        json.dump(coco_output, out)
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:
                    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 = png_to_binary_mask(annotation_filename)
                    annotation_info = pycococreatortools.create_annotation_info(
                        segmentation_id,
                        image_id,
                        category_info,
                        binary_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("{}/annotation.json".format(ROOT_DIR), "w") as output_json_file:
        json.dump(coco_output, output_json_file)
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)  # 图片个数
        # 遍历每一张图片
        print(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)
            print("images has done")

            # 内层循环是mask,把每一张图片的mask搜索出来
            rle_masks = df.loc[df['ImageId'] == image_name,
                               'EncodedPixels'].tolist()

            num_of_rle_masks = len(rle_masks)
            print(num_of_rle_masks)

            for index in range(num_of_rle_masks):
                kk = rle_masks[index]

                binary_mask = rle_decode(str(kk))
                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('/home/gnss/Desktop/multi_ships/1.json',
              'w') as output_json_file:
        # json.dump(coco_output, output_json_file)
        json.dump(coco_output, output_json_file, indent=4)
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():


    ANNOTATION = "binary_masks"
    ROOT_DIR=sys.argv[1]
    IMAGE_DIR = os.path.join(ROOT_DIR, "train2014")
    ANNOTATION_DIR = os.path.join(ROOT_DIR, ANNOTATION)

    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": []
    }

    image_id = 1
    segmentation_id = 1

    # filter for png images
    for root, _, files in os.walk(IMAGE_DIR):
        files.sort()
        image_files = filter_for_png(root, files)
        image_files.sort()
        # go through each image
        for image_filename in image_files:
            print("Image file",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):
                files.sort()
                annotation_files = filter_for_annotations(root, files, image_filename)
                #print("Len annotation_files",len(annotation_files))
                #if (len(annotation_files) > 1):
                    #print("T",image_filename,annotation_files)
                    #exit(1)
                # go through each associated annotation
                for annotation_filename in annotation_files:

                    print(annotation_filename)
                    class_id = 1# np.random.randint(5)
                    print("class id",class_id)

                    category_info = {'id': class_id, 'is_crowd': 0}
                    #binary_mask = np.asarray(Image.open(annotation_filename).convert('1')).astype(np.uint8)
                    binary_mask = cv2.imread(annotation_filename,-1)
                    #img2gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
                    #ret, mask = cv2.threshold(img2gray, 254, 255, cv2.THRESH_BINARY)
                    #binary_mask = cv2.bitwise_not(mask)
                    #cv2.imshow('maskBGR',binary_mask)
                    #cv2.waitKey(0)
                    #exit(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

    if not os.path.exists(ROOT_DIR+'/annotations'):
        os.mkdir(ROOT_DIR+'/annotations')
    outname = ROOT_DIR + '/annotations' + '/instances_train2014.json'
    with open(outname, 'w') as output_json_file:
        json.dump(coco_output, output_json_file)
    print("Saved json file",outname)
def main_old():

    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)

        print(image_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)

                print(annotation_files)
                input("stop")

                # go through each associated annotation
                for annotation_filename in annotation_files:
                    # if annotation_filename.find("32_panel_0")==-1:
                    # if annotation_filename.find("32_panel_0")!=-1:
                    #     continue
                    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
                    }

                    # print("here0")
                    tmp_img = np.array(Image.open(annotation_filename))
                    tmp_img[tmp_img < 255] = 0
                    tmp_img = Image.fromarray(tmp_img)

                    # binary_mask = np.asarray(Image.open(annotation_filename).convert('1')).astype(np.uint8)  #Image convert("1") -> grayscale
                    # binary_mask = np.asarray(tmp_img.convert('1')).astype(np.uint8)
                    binary_mask = np.array(tmp_img).astype(np.uint8)

                    # print("here1")
                    annotation_info = pycococreatortools.create_annotation_info(
                        segmentation_id,
                        image_id,
                        category_info,
                        binary_mask,
                        image.size,
                        tolerance=2)

                    # print("here2")
                    if annotation_info is not None:
                        coco_output["annotations"].append(annotation_info)

                    segmentation_id = segmentation_id + 1

            image_id = image_id + 1

    with open('{}/panel_coco.json'.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

    all_img_str = []
    all_ann_str = []
    ### img_ann_dict : {'0':['0_panel_0','0_panel_1'], '1':['1_panel_0','1_panel_1']...}
    img_ann_dict = {}

    for img_name in os.listdir(IMAGE_DIR):
        all_img_str.append(img_name)

    for ann_name in os.listdir(ANNOTATION_DIR):
        all_ann_str.append(ann_name)

    for ann_name in all_ann_str:
        pos = ann_name.find('_')
        img_id = ann_name[:pos]

        if img_id in img_ann_dict.keys():
            img_ann_dict[img_id].append(ann_name)
        else:
            img_ann_dict[img_id] = [ann_name]

    # go through each image
    for img_id in img_ann_dict.keys():
        image_filename = IMAGE_DIR + img_id + ".png"
        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)
        ann_files = img_ann_dict[img_id]
        # go through each associated annotation
        for ann_str in ann_files:
            # if annotation_filename.find("32_panel_0")==-1:
            # if annotation_filename.find("32_panel_0")!=-1:
            #     continue
            annotation_filename = ANNOTATION_DIR + ann_str
            # 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
            }

            tmp_img = np.array(Image.open(annotation_filename))

            tmp_img[tmp_img < 255] = 0
            tmp_img = Image.fromarray(tmp_img)

            # binary_mask = np.asarray(Image.open(annotation_filename).convert('1')).astype(np.uint8)  #Image convert("1") -> grayscale
            # binary_mask = np.asarray(tmp_img.convert('1')).astype(np.uint8)
            binary_mask = np.array(tmp_img).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

        if image_id % 1000 == 0:
            print("Finished num:", image_id)
        image_id = image_id + 1

    with open('{}/panel_coco_200k.json'.format(ROOT_DIR),
              'w') as output_json_file:
        json.dump(coco_output, output_json_file)
Exemple #14
0
def generateJson(image_files, annotation_files, boundings, path):
    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": []
    }

    image_id = 1
    segmentation_id = 1

    # go through each image
    for i in range(len(image_files)):

        if np.array(boundings[i]).sum() == 0:
            continue

        image = Image.open(image_files[i])
        image_info = pycococreatortools.create_image_info(
            image_id, image_files[i], image.size)
        coco_output["images"].append(image_info)

        annotation_filename = annotation_files[i]

        # go through each associated annotation
        #print(annotation_filename)
        for j in range(len(boundings[i])):
            img = cv2.imread(annotation_filename)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            binary_mask = np.zeros((np.shape(img)[0], np.shape(img)[1]),
                                   np.uint8)

            y1 = boundings[i][j][0]
            x1 = boundings[i][j][1]
            y2 = boundings[i][j][2]
            x2 = boundings[i][j][3]

            if y1 == y2 and x1 == x2:
                continue

            binary_mask[x1:x2, y1:y2] = img[x1:x2, y1:y2]

            binary_mask = np.uint8(binary_mask > 0)

            class_id = np.max(img)
            if class_id == 255:
                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:
                annotation_info = {
                    "id": segmentation_id,
                    "image_id": image_id,
                    "category_id": category_info["id"],
                    "iscrowd": category_info["is_crowd"],
                    "area": 0,
                    "bbox": [],
                    "segmentation": [],
                    "width": binary_mask.shape[1],
                    "height": binary_mask.shape[0],
                }
                coco_output["annotations"].append(annotation_info)'''

            segmentation_id = segmentation_id + 1

        image_id = image_id + 1

    with open(path.format(ROOT_DIR), 'w') as output_json_file:
        json.dump(coco_output, output_json_file, cls=NumpyEncoder)
Exemple #15
0
def result_to_coco(result,
                   class_names,
                   image_size,
                   tolerance=2,
                   INFO=None,
                   LICENSES=None):
    """Encodes Mask R-CNN detection result into COCO format
    """

    if INFO is None:
        INFO = {
            "description": "Mask R-CNN Result",
            "url": "https://github.com/waspinator/deep-learning-explorer",
            "version": "0.1.0",
            "year": 2018,
            "contributor": "waspinator",
            "date_created": datetime.datetime.utcnow().isoformat(' ')
        }

    if LICENSES is None:
        LICENSES = [{
            "id": 1,
            "name": "Attribution-NonCommercial-ShareAlike License",
            "url": "http://creativecommons.org/licenses/by-nc-sa/2.0/"
        }]

    IMAGES = [{
        "id": 1,
        "width": image_size[1],
        "height": image_size[0],
        "license": 1
    }]

    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": [],
        "images": IMAGES,
        "annotations": []
    }

    for index, class_name in enumerate(class_names):
        if class_name == 'BG':
            continue

        category = {"id": index, "name": class_name, "supercategory": ""}

        coco_output["categories"].append(category)

    for index in range(result['masks'].shape[-1]):
        mask = result['masks'][..., index]

        bounding_box = np.array([
            result['rois'][index][1], result['rois'][index][0],
            result['rois'][index][3] - result['rois'][index][1],
            result['rois'][index][2] - result['rois'][index][0]
        ])

        annotation = pycococreatortools.create_annotation_info(
            annotation_id=index,
            image_id=1,
            category_info={
                "id": result['class_ids'][index].item(),
                "is_crowd": False
            },
            binary_mask=mask,
            image_size=image_size,
            tolerance=tolerance,
            bounding_box=bounding_box)

        if annotation is not None:
            annotation['confidence'] = "{:.4f}".format(
                result['scores'][index].item())
            coco_output['annotations'].append(annotation)

    return coco_output
Exemple #16
0
def create_annotations(root_dir, blacks_dir):
    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": []
    }

    image_id = 1
    segmentation_id = 1
    move_image = False

    img_dir = os.path.join(root_dir, "Images")
    seg_dir = os.path.join(root_dir, "Segmentation")
    subset = root_dir.split("/")[-1]

    if not os.path.exists(blacks_dir):
        os.makedirs(blacks_dir)

    # filter for jpeg images
    image_files = filter_for_jpeg(img_dir, os.listdir(img_dir))
    message = "Processing images"

    pbar = tqdm(desc=message, total=len(image_files))

    # go through each image
    for image_filename in image_files:
        image_name_only = image_filename.split("/")[-1]
        image = Image.open(image_filename)
        image_info = pycococreatortools.create_image_info(
            image_id, os.path.basename(image_filename), image.size)

        # filter for associated png annotations
        for root, _, files in os.walk(seg_dir):
            annotation_files = filter_for_annotations(root, files, image_filename)

            # go through each associated annotation
            for annotation_filename in annotation_files:
                annotation_name_only = annotation_filename.split("/")[-1]
                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:
                    move_image = False
                    coco_output["annotations"].append(annotation_info)
                else:
                    move_image = True
                    os.rename(annotation_filename, os.path.join(blacks_dir, annotation_name_only))

                segmentation_id = segmentation_id + 1

        if move_image:
            os.rename(image_filename, os.path.join(blacks_dir, image_name_only))
        else:
            coco_output["images"].append(image_info)

        image_id = image_id + 1
        pbar.update(1)

    with open('{}/annotations_isic_{}.json'.format(root_dir, subset), 'w') as output_json_file:
        json.dump(coco_output, output_json_file)

    pbar.close()
    print("\n")
def main():
    
    datasets = [f for f in os.listdir(ROOT_DIR) if not f.startswith('.') if not f.startswith('syn')]
    print("Datasets to be encoded as COCO JSON files: ")
    print(datasets)
    
    for DATASET in datasets:
        
        INFO = {
            "description": DATASET,
            "year": 2018,
            "contributor": "dpaddon",
            "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': 'c_elegans',
                'supercategory': 'worm',
            },
]
        
        print("Processing {}".format(DATASET))
        
        IMAGE_DIR = os.path.join(ROOT_DIR, DATASET)
    
        coco_output = {
            "info": INFO,
            "licenses": LICENSES,
            "categories": CATEGORIES,
            "images": [],
            "annotations": []
        }
    
#        image_id = 1
        segmentation_id = 1
        
        
        #Get list of frame IDs
        filenames = sorted([f for f in os.listdir(IMAGE_DIR) if not f.startswith('.')], key=int)
            
#        print(filenames)
        
        # go through each image
        for fname in filenames:
            image_id = fname
            image = Image.open(os.path.join(IMAGE_DIR, fname, 'image/image_{}.png'.format(fname)))
            image_info = pycococreatortools.create_image_info(
                image_id, fname, image.size)
            coco_output["images"].append(image_info)
    
    
                
            ANNOTATION_DIR = os.path.join(IMAGE_DIR, fname, 'masks')
            annotation_files = sorted([f for f in os.listdir(ANNOTATION_DIR) if not f.startswith('.')])
    
            # go through each associated annotations
            for annotation_filename in annotation_files:
                
    #            print(annotation_filename)
    
                category_info = {'id': 1, 'is_crowd': False}
                binary_mask = np.asarray(Image.open(os.path.join(ANNOTATION_DIR,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:
                    #add score for COCO evaluation
                    annotation_info['score'] = 1.0
                    coco_output["annotations"].append(annotation_info)
    
                    segmentation_id = segmentation_id + 1
    
#            image_id = image_id + 1
    
        os.makedirs(os.path.join(OUTPUTS_DIR, MODE), exist_ok=True)
        with open('{}/{}/{}.json'.format(OUTPUTS_DIR, MODE, DATASET), 'w') as output_json_file:
            json.dump(coco_output, output_json_file)
Exemple #18
0
    def __call__(self, img_desc):
        img_dir, img_id, lbl_cat = img_desc
        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.zeros(lbl.shape, np.int32)
        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
            iss_instance_id = len(cat)
            mask_i = lbl == city_id

            # Save ISS format annotation
            cat.append(iss_class_id)
            iscrowd.append(1 if iscrowd_i else 0)
            lbl_out[mask_i] = iss_instance_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_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_id, path.join(img_dir, img_id + _IMAGE_EXT), 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"))

        img_meta = {
            "id": img_id,
            "cat": cat,
            "size": (lbl_size[1], lbl_size[0]),
            "iscrowd": iscrowd
        }

        return img_meta, coco_img, coco_ann
Exemple #19
0
            person_id = int(image_name[:8])

            class_id = ids2labels_dict[person_id]
            category_info = {
                'id': class_id,
                'is_crowd': 'crowd' in image_filename
            }
            # binary_mask = np.asarray(Image.open(annotation_filename)
            # .convert('1')).astype(np.uint8)
            binary_mask = np.ones(
                (image.size[1], image.size[0])).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:
                annotation_info['classes_or_attributions'] = 0
                annotation_info['mark'] = marks_list[image_idx]
                coco_output["annotations"].append(annotation_info)
            else:
                print 'no annotation_info for: ', image_idx, image_name
                exit(0)

            segmentation_id = segmentation_id + 1
            image_id = image_id + 1
Exemple #20
0
def json_generate():
    road = 0
    sidework = 0
    traffic_light = 0
    traffic_sign = 0
    person = 0
    rider = 0
    car = 0
    truck = 0
    bus = 0
    caravan = 0
    trailer = 0
    train = 0
    motorcycle = 0
    bicycle = 0
    building = 0
    # files = os.listdir(IMAGE_SAVE_DIR)
    files = os.listdir(
        os.path.join(ROOT_DIR, "data", "leftImg8bit",
                     "val_images/"))  # train_images  OR val_images

    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_path = os.path.join(
            os.path.join(ROOT_DIR, "data", "leftImg8bit", "val_images/"),
            image_filename)  # train_images  OR val_images
        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("no 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
                # class_id = [x['id'] for x in CATEGORIES if x['name'] in annotation_filename][0]
                if class_id == 1:
                    road += 1
                elif class_id == 2:
                    sidework += 1
                elif class_id == 3:
                    traffic_light += 1
                elif class_id == 4:
                    traffic_sign += 1
                elif class_id == 5:
                    person += 1
                elif class_id == 6:
                    rider += 1
                elif class_id == 7:
                    car += 1
                elif class_id == 8:
                    truck += 1
                elif class_id == 9:
                    bus += 1
                elif class_id == 10:
                    caravan += 1
                elif class_id == 11:
                    trailer += 1
                elif class_id == 12:
                    train += 1
                elif class_id == 13:
                    motorcycle += 1
                elif class_id == 14:
                    bicycle += 1
                elif class_id == 15:
                    building += 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_image-15.json'.format(ROOT_DIR),
              'w') as output_json_file:  # train_images  OR val_images
        json.dump(coco_output, output_json_file)
    print(road)
Exemple #21
0
            "licenses": LICENSES,
            "categories": CATEGORIES,
            "images": [],
            "annotations": []
        }
        for i, sample in enumerate(tqdm(ds)):
            fname, image, boxes, masks, labels, _ = sample
            image_fpath = split + "_rgb/" + fname
            image_info = pycococreatortools.create_image_info(
                img_id, image_fpath, image.size)
            coco_output["images"].append(image_info)
            for label, box, mask in zip(labels, boxes, masks):
                label_name = class40[label - 1]
                # ignore some classes
                if label == 0 or label_name not in keep_class:
                    continue
                label = keep_class.index(label_name) + 1
                category_info = {'id': label, 'is_crowd': 1}
                anno_info = pycococreatortools.create_annotation_info(
                        ann_id, img_id, category_info, mask,
                        image.size, tolerance=2)
                anno_info['width'] = image.size[0]
                anno_info['height'] = image.size[1]
                if anno_info is not None:
                    coco_output["annotations"].append(anno_info)
                ann_id += 1
            img_id += 1
        with open('{}/cocolike_nyuv2_{}_{}.json'.format(rootdir, len(keep_class), split), 'w') as f:
            json.dump(coco_output, f)

Exemple #22
0
def profiles2Coco(WorkingDir, OUT_NAME):
    IMAGE_DIR = WorkingDir
    #print IMAGE_DIR
    ANNOTATION_DIR = WorkingDir
    #print ANNOTATION_DIR
    CATEGORIES, profiles=CreateCategories(WorkingDir, OUT_NAME)
    INFO = {
        "description": WorkingDir,
        "url": "...",
        "version": "1.0",
        "year": 2018,
        "contributor": "Pier",
        "date_created": datetime.datetime.utcnow().isoformat(' ')
     }

    LICENSES = [
    {
        "id": 2112,
        "name": "DFV Srl Surano",
        "url": "..."
    }
    ]


    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories":CATEGORIES,
        "images": [],
        "annotations": []
    }

    image_id = 1
    segmentation_id = 1
    i = 1

    # filter for jpeg images
    for root, _, files in os.walk(IMAGE_DIR):
        files.remove("composite.png")
        image_files = filter_for_png(root, files)
        #print "x"
        print "image_files: ", image_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)
            #print "y"
            # 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:
                    #prefix = annotation_filename.split('.')
                    #name, seq = prefix.split('_')
                    fileNameAnnotated=os.path.splitext(os.path.basename(annotation_filename))[0]
                    filen, name, seq = fileNameAnnotated.split('_') 
                    class_id=profiles[name]
                    #print(str(i) + " " + name)
                    
                    

                    category_info = {'id': class_id, 'is_crowd': 'crowd' in image_filename}
                    # binary_mask=get_red_to_binary(annotation_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 None:
                        print "asd"
                    print "annotation: \n", annotation_info

                    if annotation_info is not None:
                        coco_output["annotations"].append(annotation_info)

                    segmentation_id = segmentation_id + 1

            image_id = image_id + 1
            i += 1

    with open(WorkingDir+"dataset.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_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:
                    
                    if '3m' in annotation_filename:
                        class_id  = 1
                    elif 'andes' in annotation_filename:
                        class_id = 2
                    elif 'cocacola' in annotation_filename:
                        class_id = 3
                    elif 'crayola' in annotation_filename:
                        class_id = 4
                    elif 'folgers' in annotation_filename:
                        class_id = 5
                    elif 'heineken' in annotation_filename:
                        class_id = 6
                    elif 'hunts' in annotation_filename:
                        class_id = 7
                    elif 'kellogg' in annotation_filename:
                        class_id = 8
                    elif 'kleenex' in annotation_filename:
                        class_id = 9
                    elif 'kotex' in annotation_filename:
                        class_id = 10
                    elif 'libava' in annotation_filename:
                        class_id = 11
                    elif 'macadamia' in annotation_filename:
                        class_id = 12
                    elif 'milo' in annotation_filename:
                        class_id = 13
                    elif 'mm' in annotation_filename:
                        class_id = 14
                    elif 'pocky' in annotation_filename:
                        class_id = 15
                    elif 'raisins' in annotation_filename:
                        class_id = 16
                    elif 'stax' in annotation_filename:
                        class_id = 17
                    elif 'swissmiss' in annotation_filename:
                        class_id = 18
                    elif 'vanish' in annotation_filename:
                        class_id = 19
                    else:
                        class_id = 20
                    
                    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)
                    print(str(image_id)+ " images write into the {}.json,{} left".format(json_name,len(image_files)-image_id))
                    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(ROOT_DIR,json_name), 'w') as output_json_file:
        json.dump(coco_output, output_json_file)
def result_to_coco(result,
                   class_names,
                   width,
                   height,
                   tolerance=2,
                   INFO=None,
                   LICENSES=None):
    """Encodes semantic segmentation result into COCO format
    """

    if INFO is None:
        INFO = {
            "description": "Semantic Segmentation Result",
            "url": "https://github.com/waspinator/deep-learning-explorer",
            "version": "0.1.0",
            "year": 2018,
            "contributor": "waspinator",
            "date_created": datetime.datetime.utcnow().isoformat(' ')
        }

    if LICENSES is None:
        LICENSES = [{
            "id": 1,
            "name": "Attribution-NonCommercial-ShareAlike License",
            "url": "http://creativecommons.org/licenses/by-nc-sa/2.0/"
        }]

    IMAGES = [{"id": 1, "width": width, "height": height, "license": 1}]

    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": [],
        "images": IMAGES,
        "annotations": []
    }

    for class_info in class_names:
        if class_info['name'] == 'BG':
            continue

        category = {
            "id": class_info['id'],
            "name": class_info['name'],
            "supercategory": ""
        }

        coco_output["categories"].append(category)

        mask = result == class_info['id']

        annotation = pycococreatortools.create_annotation_info(
            annotation_id=1,
            image_id=1,
            category_info={
                "id": class_info['id'],
                "is_crowd": True
            },
            binary_mask=mask,
            image_size=(width, height),
            tolerance=tolerance)

        if annotation is not None:
            coco_output['annotations'].append(annotation)

    return coco_output
Exemple #25
0
def from_mask_to_coco(root, MARK, IMAGE, ANNOTATION):
    ROOT_DIR = root + '/' + MARK
    IMAGE_DIR = ROOT_DIR + '/' + IMAGE
    ANNOTATION_DIR = ROOT_DIR + '/' + ANNOTATION
    if os.path.exists(ROOT_DIR):
        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)
                        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_greenhouse_{}2019.json'.format(ROOT_DIR, MARK),
                  'w') as output_json_file:
            json.dump(coco_output, output_json_file)
    else:
        print(ROOT_DIR + ' does not exit!')
def main():

    if CLASS_LABELS == "nyu40":
        CATEGORIES = NYU40_CATEGORIES
    elif CLASS_LABELS == "coco":
        CATEGORIES = COCO_CATEGORIES

    MAX_CATEGORIES = len(CATEGORIES)

    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": []
    }

    image_id = 1  # maps to 1 + encoded scene + image_id
    depth_id = 1  # maps to 2 + encoded scene + depth_id
    segmentation_id = 1  # counter

    (colorTable,
     categoryTable) = loadMP40(os.path.join(ROOT_DIR, 'mpcat40.tsv'))

    running_id = 0

    iddict = {}

    if opt.do_stats:
        sizelist = []
        multiview_stats = []
        inst_data_dict = {}

    # Filter for color jpeg images
    if opt.export_color_images:

        for house in HOUSES:

            if opt.do_stats:
                inst_data_dict = {}

            cc = os.path.join(ROOT_DIR, SCENE_DIR, house,
                              'matterport_skybox_images')
            for root, _, files in os.walk(cc):
                image_files = filter_for_jpeg(root, files)

                # Go through each color image
                for image_filename in image_files:
                    image = Image.open(image_filename)
                    image_id = generate_color_image_id(image_filename)  # FTT

                    file_name = SCENE_DIR + '/' + house + '/' + 'matterport_skybox_images' + '/' + image_id + '.jpg'
                    filenum = os.path.basename(image_filename).rsplit('.')[0]

                    image_info = pycococreatortools.create_image_info(
                        running_id, file_name, image.size)
                    coco_output["images"].append(image_info)

                    iddict[running_id] = image_id

                    # get instance to label mapping
                    mapping_filename = os.path.join(
                        ROOT_DIR, SRC_ANNOTATION_DIR, house,
                        'sphere_points_smooth',
                        image_id + '_filtered_aggregation.json')
                    if (not os.path.exists(mapping_filename)):
                        print('WARNING, cannot find mapping file',
                              mapping_filename)
                        #sys.exit(1)
                        continue
                    mapping = json.load(open(mapping_filename))

                    # Filter for annotation mask file associated with color image and label
                    instance_filename = image_filename.replace(
                        'matterport_skybox_images',
                        'segmentation_maps_instances').replace('.jpg', '.png')
                    print(instance_filename)

                    img = Image.open(instance_filename)
                    pixel = np.array(img)
                    # Go through each colour, as instances are encoded as colour values
                    pixelcolors = [x[1] for x in img.getcolors()
                                   ]  # [0] = pixelcount, [1] = colour
                    for colortuple in pixelcolors:
                        instance_id = classIdFromColor(colortuple,
                                                       categoryTable)

                        key = str(instance_id)
                        category_label = ''
                        category_id = 0
                        for seggrp in mapping.get('segGroups'):
                            if int(seggrp.get('id')) == instance_id:
                                category_label = seggrp.get('label')
                                if CLASS_LABELS == "nyu40":
                                    category_id = getNYUClassId(
                                        category_label, categoryTable
                                    )  # val contains raw and nyu40 label
                                if CLASS_LABELS == "coco":
                                    category_id = getCOCOClassId(
                                        category_label, categoryTable)  #
                        if category_id == 0:  # 0 is background
                            continue
                        if category_id < 1 or category_id > MAX_CATEGORIES:  # Just make sure, we are safe
                            print('Ignore category ' + str(category_id))
                            continue

                        # Labels are nyu40id and coded as pixel colours (1 .. 40 decimal)
                        category_info = {
                            'id': category_id,
                            'is_crowd': 'crowd' in image_filename
                        }
                        binary_mask = np.all(
                            pixel == colortuple, axis=-1
                        )  # Create a binary mask for each of the labels

                        # use morphology to clean masks
                        if opt.clean_masks:
                            selem = disk(4)
                            binary_mask = opening(binary_mask, selem)
                            binary_mask = binary_fill_holes(binary_mask)

                        if opt.discard_wrap_around_regions > 0:
                            width, height = img.size
                            centerstart = int(width / 2 -
                                              opt.discard_wrap_around_regions /
                                              2)
                            centerend = int(width / 2 +
                                            opt.discard_wrap_around_regions /
                                            2)

                            labelled_mask, n_labels = measure.label(
                                binary_mask, return_num=True)
                            labelled_mask_center = labelled_mask[:,
                                                                 centerstart:
                                                                 centerend]

                            keep_labels = np.unique(labelled_mask_center)
                            for i in range(1, keep_labels.shape[0]):
                                labelled_mask[labelled_mask ==
                                              keep_labels[i]] = n_labels + 1

                            binary_mask = labelled_mask > n_labels

                        # from multiple regions, keep the largest two, if they cross the image border, split
                        labelled_mask, n_labels = measure.label(
                            binary_mask, return_num=True)
                        if n_labels > 2:
                            width, height = img.size
                            centerx = width / 2

                            props = measure.regionprops(labelled_mask)

                            mostcentralSz = 0
                            largestIdx = 0
                            largestSz = 0
                            largestIdx2 = 0
                            largestSz2 = 0
                            idx = 0
                            for prop in props:
                                d = abs(prop.centroid[1] - centerx)
                                if prop.area > largestSz:
                                    largestSz2 = largestSz
                                    largestIdx2 = largestIdx
                                    largestSz = prop.area
                                    largestIdx = idx
                                elif prop.area > largestSz2:
                                    largestSz2 = prop.area
                                    largestIdx2 = idx

                                idx = idx + 1

                            labelled_mask[labelled_mask == largestIdx +
                                          1] = n_labels + 1
                            labelled_mask[labelled_mask == largestIdx2 +
                                          1] = n_labels + 1

                            binary_mask = labelled_mask > n_labels

                        labelled_mask, n_labels = measure.label(
                            binary_mask, return_num=True)
                        split = False
                        if n_labels > 1 and opt.discard_wrap_around_regions:
                            width, height = img.size
                            centerstart = int(width / 2 -
                                              opt.discard_wrap_around_regions /
                                              2)
                            centerend = int(width / 2 +
                                            opt.discard_wrap_around_regions /
                                            2)

                            #check if labelled content is also outside center
                            labelled_mask_center = copy(labelled_mask)
                            labelled_mask_center[:, centerstart:centerend] = 0
                            remaining = np.unique(labelled_mask_center)
                            if len(remaining) > 1:
                                split = True
                                binary_mask = labelled_mask == 1
                                binary_mask2 = labelled_mask == 2

                        # check min size
                        width, height = img.size
                        imgarea = width * height
                        minrs = opt.min_region_area * imgarea
                        if np.sum(binary_mask) < minrs:
                            continue

                        # store size
                        if opt.do_stats:
                            sizelist.append(
                                float(np.sum(binary_mask)) / imgarea)

                            if instance_id in inst_data_dict.keys():
                                inst_data_dict[instance_id].append(
                                    float(np.sum(binary_mask)) / imgarea)
                            else:

                                inst_data_dict[instance_id] = [
                                    float(np.sum(binary_mask)) / imgarea
                                ]

                        # debug mask image outputs
                        #maskfile='dbg/mask_' + str(image_id) + '-' + str(instance_id) + '.png'
                        #Image.fromarray((binary_mask * 255).astype(np.uint8)).save(maskfile)

                        annotation_id = generate_annotation_id(
                            image_id, instance_id)

                        annotation_info = pycococreatortools.create_annotation_info(
                            annotation_id,
                            running_id,
                            category_info,
                            binary_mask,
                            image.size,
                            tolerance=TOLERANCE)  # 2)

                        if annotation_info is not None:
                            coco_output["annotations"].append(annotation_info)

                        if split:
                            annotation_id = generate_annotation_id(
                                image_id, instance_id + 1000)

                            annotation_info = pycococreatortools.create_annotation_info(
                                annotation_id,
                                running_id,
                                category_info,
                                binary_mask2,
                                image.size,
                                tolerance=TOLERANCE)  # 2)

                            if annotation_info is not None:
                                coco_output["annotations"].append(
                                    annotation_info)

                        segmentation_id = segmentation_id + 1

                    running_id = running_id + 1

            if opt.do_stats:
                nMulti = 0
                nMulti005 = 0
                nMulti002 = 0
                nMulti001 = 0

                for key in inst_data_dict.keys():
                    inst_data = inst_data_dict[key]
                    inst_ar = np.array(inst_data)
                    inst_ar = inst_ar[inst_ar > 0]
                    if len(inst_ar) > 1:
                        nMulti = nMulti + 1
                        mininst = min(inst_ar)
                        maxinst = max(inst_ar)

                        if mininst <= maxinst * 0.01:
                            nMulti001 = nMulti001 + 1
                        elif mininst <= maxinst * 0.02:
                            nMulti002 = nMulti002 + 1
                        elif mininst <= maxinst * 0.05:
                            nMulti005 = nMulti005 + 1

                mvdata = [
                    float(nMulti) / len(inst_data_dict),
                    float(nMulti005) / len(inst_data_dict),
                    float(nMulti002) / len(inst_data_dict),
                    float(nMulti001) / len(inst_data_dict)
                ]

                multiview_stats.append(mvdata)

    # Filter for depth images (not implemented in this version)
    if opt.export_depth_images:
        print("Depth images: tbd")

    with open(OUTPUT, 'w') as output_json_file:
        json.dump(coco_output, output_json_file)

    with open(OUTPUT + '.csv', 'w') as f:
        for key in iddict.keys():
            f.write("%d,%s\n" % (key, iddict[key]))

    if opt.do_stats:
        print("\nstats:\n\n")
        print("region sizes:\n")
        maxval = 20
        sizelist = np.array(sizelist)
        for i in range(1, maxval, 1):
            print("    " + str(i) + ": " + str(
                np.sum(
                    np.logical_and((sizelist > ((i - 1) / 200.0)),
                                   (sizelist < (i / 200.0)))) / len(sizelist)))
        print("  >=" + str(maxval) + ": " +
              str(np.sum(sizelist > (maxval / 200.0)) / len(sizelist)))
        print("\n\nmultiview stats:\n\n")
        print("multiple   0.05   0.02   0.01 ")
        print(np.mean(np.array(multiview_stats), axis=0))
Exemple #27
0
    def __call__(self, img_id):
        coco_ann = []

        # Load the annotation
        with Image.open(path.join(self.root_dir, _LABELS_DIR, img_id + "." + _LABELS_EXT)) as lbl_img:
            lbl = np.array(lbl_img, dtype=np.uint16)
            lbl_size = lbl_img.size

        mvd_ids = np.unique(lbl)

        # Compress the labels and compute cat
        lbl_out = np.zeros(lbl.shape, np.int32)
        cat = [255]
        iscrowd = [0]
        for mvd_id in mvd_ids:
            mvd_class_id = int(mvd_id // 255)
            category = self.categories[mvd_class_id]

            # If it's a void class just skip it
            if not category["evaluate"]:
                continue

            # Extract all necessary information
            iss_class_id = self.cat_id_mvd_to_iss[mvd_class_id]
            iss_instance_id = len(cat)
            iscrowd_i = 1 if "group" in category["name"] else 0
            mask_i = lbl == mvd_id

            # Save ISS format annotation
            cat.append(iss_class_id)
            iscrowd.append(iscrowd_i)
            lbl_out[mask_i] = iss_instance_id

            # Compute COCO detection format annotation
            if category["instances"]:
                category_info = {"id": iss_class_id, "is_crowd": iscrowd_i == 1}
                coco_ann_i = pct.create_annotation_info(
                    counter.increment(), img_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_id, img_id + "." + _IMAGES_EXT, lbl_size)

        # Write output
        out_msk_dir = path.join(self.out_dir, "msk")
        out_img_dir = path.join(self.out_dir, "img")
        _ensure_dir(out_msk_dir)
        _ensure_dir(out_img_dir)

        Image.fromarray(lbl_out).save(path.join(out_msk_dir, img_id + ".png"))
        shutil.copy(path.join(self.root_dir, _IMAGES_DIR, img_id + "." + _IMAGES_EXT),
                    path.join(out_img_dir, img_id + "." + _IMAGES_EXT))

        img_meta = {
            "id": img_id,
            "cat": cat,
            "size": (lbl_size[1], lbl_size[0]),
            "iscrowd": iscrowd
        }

        return img_meta, coco_img, coco_ann
Exemple #28
0
def main():

    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": []
    }

    image_id = 1
    segmentation_id = 1

    # filter for jpeg images
    files = sorted(os.listdir(IMAGE_DIR))
    image_files = filter_for_jpeg(IMAGE_DIR, 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
        files = sorted(os.listdir(ANNOTATION_DIR))
        annotation_files = filter_for_annotations(ANNOTATION_DIR, 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,
                data_type=args.path)

            if args.keypoints is not "":
                annotation_info["keypoints"] = KEYPOINTS_LIST[segmentation_id -
                                                              1]
                annotation_info["num_keypoints"] = "17"

            if annotation_info is not None:
                coco_output["annotations"].append(annotation_info)

            segmentation_id = segmentation_id + 1

        image_id = image_id + 1

    if not os.path.isdir("{}/annotations".format(ROOT_DIR)):
        os.makedirs("{}/annotations".format(ROOT_DIR))

    with open('{}/annotations/instances_{}.json'.format(ROOT_DIR, args.path),
              '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(annotation_filename)

                    # 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('{}/DOTA_v1.5_train768_add_difficult.json'.format(ROOT_DIR),
              'w') as output_json_file:
        json.dump(coco_output, output_json_file)
def save_annotations(categories):
    """
    categories: array starting with null of categories saved in dashboard

    Saves the annotations in annotation/seg/ and annotation/rgb/ into COCO 
    format for use in retraining. The resulting file is located at the filepath
    for coco_file_name. 
    """

    seg_dir = "annotation_data/seg/"
    img_dir = "annotation_data/rgb/"
    coco_file_name = "annotation_data/coco/coco_results.json"

    fs = [x.split(".")[0] + ".jpg" for x in os.listdir(seg_dir)]

    INFO = {}
    LICENSES = [{}]
    CATEGORIES = []
    id_to_label = {}
    for i, label in enumerate(categories):
        if not label:
            continue
        CATEGORIES.append({"id": i, "name": label, "supercategory": "shape"})
        id_to_label[i] = label

    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": [],
    }

    count = 0
    for x in fs:
        image_id = int(x.split(".")[0])
        # load the annotation file
        try:
            seg_path = os.path.join(seg_dir, "{:05d}.npy".format(image_id))
            annot = np.load(seg_path, allow_pickle=True).astype(np.uint8)
        except Exception as e:
            print(e)
            continue

        img_filename = "{:05d}.jpg".format(image_id)
        img = Image.open(os.path.join(img_dir, img_filename))
        image_info = pycococreatortools.create_image_info(
            image_id, os.path.basename(img_filename), img.size)

        coco_output["images"].append(image_info)

        # for each annotation add to coco format
        for i in np.sort(np.unique(annot.reshape(-1), axis=0)):
            try:
                category_info = {"id": int(i), "is_crowd": False}
                if category_info["id"] < 1:
                    continue
            except:
                print("label value doesnt exist for", i)
                continue
            binary_mask = (annot == i).astype(np.uint8)

            annotation_info = pycococreatortools.create_annotation_info(
                count,
                image_id,
                category_info,
                binary_mask,
                img.size,
                tolerance=2)
            if annotation_info is not None:
                coco_output["annotations"].append(annotation_info)
                count += 1

    Path("annotation_data/coco").mkdir(parents=True, exist_ok=True)
    with open(coco_file_name, "w") as output_json:
        json.dump(coco_output, output_json)
        print("Saved annotations to", coco_file_name)