コード例 #1
0
ファイル: reg_dataset1.py プロジェクト: zz10001/al_ins_seg
def get_coco_dicts(data_dir):
    if 'train' in data_dir:
        file_path = '/media/tangyp/Data/coco/train2014'
    elif 'val' in data_dir:
        file_path = '/media/tangyp/Data/coco/val2014'
    json_file = data_dir
    coco = COCO(json_file)
    catIds = coco.getCatIds(catNms=['person'])
    imgIds = coco.getImgIds(catIds=catIds)
    imgs = coco.loadImgs(imgIds)
    dataset_dicts = []
    for img in imgs:
        dataset_dict = {}
        new_img = {'file_name': os.path.join(file_path, img['file_name']), 'height': img['height'],
                   'width': img['width'],
                   'image_id': img['id']}
        annId = coco.getAnnIds(imgIds=img['id'])
        anns = coco.loadAnns(ids=annId)
        annotation = {}
        annotation['annotations'] = []
        for ann in anns:
            new_ann = {'iscrowd': ann['iscrowd'], 'bbox': ann['bbox'], 'category_id': ann['category_id'],
                       'segmentation': ann['segmentation'], 'bbox_mode': BoxMode(1)}
            annotation['annotations'].append(new_ann)
        dataset_dict.update(new_img)
        dataset_dict.update(annotation)
        dataset_dicts.append(dataset_dict)
    return dataset_dicts
コード例 #2
0
ファイル: show_utils.py プロジェクト: xc-csc101/some-function
def show_json_angle():
    """
    according json file to visualization
    :return:
    """
    img_path = '/Dataset/DOTA_TEST/images/'
    json_path = '/Dataset/DOTA_TEST/DOTA_DOTA_TEST_angle.json'
    save_path = '/Dataset/DOTA_TEST/show/'
    import pycocotools.coco as coco
    coco = coco.COCO(json_path)
    images = coco.getImgIds()
    image_num = len(os.listdir(img_path))
    for idx in range(image_num):
        img_id = images[idx]
        file_name = coco.loadImgs(ids=[img_id])[0]['file_name']
        pics_path = os.path.join(img_path, file_name)
        ann_ids = coco.getAnnIds(imgIds=[img_id])
        anns = coco.loadAnns(ids=ann_ids)
        num_objs = len(anns)
        img = cv2.imread(pics_path)
        for k in range(num_objs):

            bbox = np.array(anns[k]['bbox'])
            # print((bbox[0], bbox[1]), (bbox[2], bbox[3]), bbox[4]/math.pi*180)
            bbox8 = cv2.boxPoints(((bbox[0], bbox[1]), (bbox[2], bbox[3]), bbox[4] / math.pi * 180))

            # cat = cate[count]
            img = add_bbox(img, bbox8, anns[k]['category_id'])

        cv2.imwrite(save_path + file_name, img)
        print('\r{}/{}'.format(idx, image_num), end='')
コード例 #3
0
def main():

    print("\nDATA:{}\nOUT_TYPE:{}\n".format(DATA, OUT_TYPE))

    dets = []

    img_ids = coco.getImgIds()
    num_images = len(img_ids)

    # ipdb.set_trace()

    for i, img_id in enumerate(img_ids):

        if i % 10 == 0: print("{}/{}".format(i, len(img_ids)), end="\r")

        # ipdb.set_trace()
        if DEBUG_ and i > DEBUG_: break

        img_info = coco.loadImgs(ids=[img_id])[0]
        if DATA == "lvis":
            img_name = img_info['file_name']
        elif DATA == "coco":
            img_name = "COCO_val2014_" + img_info['file_name']
        img_path = IMG_PATH + img_name
        img = cv2.imread(img_path)
        gt_ids = coco.getAnnIds(imgIds=[img_id])
        gts = coco.loadAnns(gt_ids)
        gt_img = img.copy()

        for j, pred in enumerate(gts):
            bbox = _coco_box_to_bbox(pred['bbox'])
            gt_img = add_box(gt_img, bbox, 1.00)

        img_name = '{}_{}.jpg'.format(str(img_id).zfill(12), OUT_NAME_SUFFIX)
        cv2.imwrite(os.path.join(OUT_PATH, img_name), gt_img)
コード例 #4
0
ファイル: vis_bbox_epi1.py プロジェクト: ishann/detectron2
def main():

    dets = []
    # Get img_ids from lvis, instead.
    img_ids = coco.getImgIds()
    num_images = len(img_ids)

    # import pdb; pdb.set_trace()
    prog_bar = mmcv.ProgressBar(len(img_ids))

    for i, img_id in enumerate(img_ids):

        # ipdb.set_trace()
        if DEBUG_ is True:
            if i > 100: break

        img_info = coco.loadImgs(ids=[img_id])[0]
        img_path = IMG_PATH + img_info['file_name']
        img = cv2.imread(img_path)
        gt_ids = coco.getAnnIds(imgIds=[img_id])
        gts = coco.loadAnns(gt_ids)
        gt_img = img.copy()

        for j, pred in enumerate(gts):
            bbox = _coco_box_to_bbox(pred['bbox'])
            cat_id = pred['category_id']
            gt_img = add_box(gt_img, bbox, 1.00, cat_id)

        img_name = '{}_gt.jpg'.format(str(img_id).zfill(12))
        cv2.imwrite(os.path.join(OUT_PATH, img_name), gt_img)

        prog_bar.update()
コード例 #5
0
ファイル: reg_dataset1.py プロジェクト: zz10001/al_ins_seg
def get_custom_dicts(data_dir):
    if 'train' in data_dir:
        file_path = '/media/tangyp/Data/coco/train2014'
    elif 'val' in data_dir:
        file_path = '/media/tangyp/Data/coco/val2014'
    json_file = data_dir
    coco = COCO(json_file)
    with open(json_file) as f:
        imgs_anns = json.load(f)
    dataset_dicts = []
    imgs = imgs_anns['images']
    for img in imgs:
        dataset_dict = {}
        # new_img = {'file_name':  '/media/tangyp/Data/coco/train2014' + '/' + img['file_name'], 'height': img['height'], 'width': img['width'],
        #                'image_id': img['id']}
        new_img = {'file_name': os.path.join(file_path, img['file_name']), 'height': img['height'], 'width': img['width'],
                    'image_id': img['id']}
        annId = coco.getAnnIds(imgIds=img['id'])
        anns = coco.loadAnns(ids=annId)
        annotation = {}
        annotation['annotations'] = []
        for ann in anns:
            new_ann = {'iscrowd': ann['iscrowd'], 'bbox': ann['bbox'], 'category_id': ann['category_id'],
                            'segmentation': ann['segmentation'], 'bbox_mode': BoxMode(1)}
            annotation['annotations'].append(new_ann)
        dataset_dict.update(new_img)
        dataset_dict.update(annotation)
        dataset_dicts.append(dataset_dict)
        debug = 1
    return dataset_dicts
コード例 #6
0
def iter_images(category_name, parameters, _filter, image_callback=None):
    """
    :param category_name: COCO category name.
    :param parameters: Dict with keys:
        parameters = {
            'coco-data-dir': pathlib.Path('/data/datasets/coco2017/'),
            'annotations': pathlib.Path('annotations/person_keypoints_train2017.json'),
            'train-images': pathlib.Path('train/images'),
            'results-dir': pathlib.Path('results')
        }
    :param _filter: Callable with _filter(image_path, annotations, min_size, rescale).
    :param image_callback: If not None, is called for each image with arguments
        image_callback(image_name, image_path, results_dir, annotations)
    """
    coco = pycocotools.coco.COCO(
        str(parameters['coco-data-dir'] / parameters['annotations']))

    category_id, image_ids = get_category_info([category_name],
                                               coco)[category_name]

    results_dir = parameters['results-dir'] / category_name
    results_dir.mkdir(parents=True, exist_ok=True)

    num_crops = 0
    plot_images = []

    for i, image_id in enumerate(image_ids):
        image_info = coco.loadImgs(image_id)[0]
        annotations = coco.loadAnns(
            coco.getAnnIds(imgIds=image_id, catIds=category_id, iscrowd=0))

        image_path = parameters['coco-data-dir'] / parameters[
            'train-images'] / (image_info['file_name'])

        image_name = f'{category_name}-{image_id}-{i}'

        if image_callback:
            image_callback(image_name, image_path, results_dir, annotations)

        for j, (cropped, cropped_original_bg) in enumerate(
                _filter(image_path, annotations, parameters['min-size'],
                        parameters['rescale'])):

            num_crops += 1

            crop_name = image_name + f'-{j}'

            cropped.save(results_dir / str(crop_name + '.png'))
            cropped_original_bg.save(results_dir / str(crop_name + '-bg.png'))

            if len(plot_images) < 36:
                plot_images.append(np.array(cropped))
                plot_images.append(np.array(cropped_original_bg))

                if len(plot_images) == 36:
                    plot.grid(np.array(plot_images), 6, 6,
                              results_dir / 'examples.pdf')

    return num_crops
コード例 #7
0
 def _load_image_anns(self, img_id, coco, img_dir):
   img_info = coco.loadImgs(ids=[img_id])[0]
   file_name = img_info['file_name']
   img_path = os.path.join(img_dir, file_name)
   ann_ids = coco.getAnnIds(imgIds=[img_id])
   anns = copy.deepcopy(coco.loadAnns(ids=ann_ids))
   img = cv2.imread(img_path)
   return img, anns, img_info, img_path
コード例 #8
0
ファイル: vis_bbox_epi4.py プロジェクト: ishann/detectron2
def main():

    img_seq = []
    # Get img_ids from lvis, instead.
    img_ids = coco.getImgIds()
    num_images = len(img_ids)

    # import ipdb; ipdb.set_trace()
    dets = []
    dets.append(coco.loadRes(PRE_PATH))

    prog_bar = mmcv.ProgressBar(len(img_ids))
    for i, img_id in enumerate(img_ids):

        # ipdb.set_trace()
        if DEBUG_ is True:
            if i == DEBUG_FRAMES: break

        img_info = coco.loadImgs(ids=[img_id])[0]
        img_path = IMG_PATH + img_info['file_name']
        img = cv2.imread(img_path)

        # Create a gt labeled img.
        gt_ids = coco.getAnnIds(imgIds=[img_id])
        gts = coco.loadAnns(gt_ids)
        gt_img = img

        for j, pred in enumerate(gts):
            bbox = _coco_box_to_bbox(pred['bbox'])
            cat_id = pred['category_id']
            gt_img = add_box(gt_img, bbox, 1.00, cat_id)
        gt_img = add_to_canvas(gt_img)

        # Create a predictions labeled img.
        for k in range(len(dets)):
            pred_ids = dets[k].getAnnIds(imgIds=[img_id])
            preds = dets[k].loadAnns(pred_ids)
            pred_img = img.copy()
            for j, pred in enumerate(preds):
                bbox = _coco_box_to_bbox(pred['bbox'])
                sc = pred['score']
                cat_id = pred['category_id']
                if sc > VIS_THR:
                    pred_img = add_box(pred_img, bbox, sc, cat_id)

        # ipdb.set_trace()
        pred_img = add_to_canvas(pred_img)

        # Create a super image and save it.
        sup_img = np.concatenate((gt_img, pred_img), axis=1)
        sup_img_name = 'pre_{}.jpg'.format(str(img_id).zfill(12))
        cv2.imwrite(os.path.join(OUT_PATH, sup_img_name), sup_img)
        img_seq.append(sup_img)

        prog_bar.update()
コード例 #9
0
def coco(writer, name_index, profile, row, verify=False):
    root = os.path.expanduser(os.path.expandvars(row['root']))
    year = str(row['year'])
    name = profile + year
    path = os.path.join(root, 'annotations', 'instances_%s.json' % name)
    if not os.path.exists(path):
        tf.logging.warn(path + ' not exists')
        return False
    import pycocotools.coco
    coco = pycocotools.coco.COCO(path)
    catIds = coco.getCatIds(catNms=list(name_index.keys()))
    cats = coco.loadCats(catIds)
    id_index = dict((cat['id'], name_index[cat['name']]) for cat in cats)
    imgIds = coco.getImgIds()
    path = os.path.join(root, name)
    imgs = coco.loadImgs(imgIds)
    _imgs = list(filter(lambda img: os.path.exists(os.path.join(path, img['file_name'])), imgs))
    if len(imgs) > len(_imgs):
        tf.logging.warn('%d of %d images not exists' % (len(imgs) - len(_imgs), len(imgs)))
    cnt_noobj = 0
    for img in tqdm.tqdm(_imgs):
        annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
        anns = coco.loadAnns(annIds)
        if len(anns) <= 0:
            cnt_noobj += 1
            continue
        imagepath = os.path.join(path, img['file_name'])
        width, height = img['width'], img['height']
        imageshape = [height, width, 3]
        objects_class = np.array([id_index[ann['category_id']] for ann in anns], dtype=np.int64)
        objects_coord = [ann['bbox'] for ann in anns]
        objects_coord = [(x, y, x + w, y + h) for x, y, w, h in objects_coord]
        objects_coord = np.array(objects_coord, dtype=np.float32)
        if verify:
            if not verify_coords(objects_coord, imageshape):
                tf.logging.error('failed to verify coordinates of ' + imagepath)
                continue
            if not verify_image_jpeg(imagepath, imageshape):
                tf.logging.error('failed to decode ' + imagepath)
                continue
        assert len(objects_class) == len(objects_coord)
        example = tf.train.Example(features=tf.train.Features(feature={
            'imagepath': tf.train.Feature(bytes_list=tf.train.BytesList(value=[tf.compat.as_bytes(imagepath)])),
            'imageshape': tf.train.Feature(int64_list=tf.train.Int64List(value=imageshape)),
            'objects': tf.train.Feature(bytes_list=tf.train.BytesList(value=[objects_class.tostring(), objects_coord.tostring()])),
        }))
        writer.write(example.SerializeToString())
    if cnt_noobj > 0:
        tf.logging.warn('%d of %d images have no object' % (cnt_noobj, len(_imgs)))
    return True
コード例 #10
0
ファイル: coco.py プロジェクト: sean512/pytoolkit
def load_ss_data(coco_dir, data_name, cache_dir, input_size=None):
    """セマンティックセグメンテーションのデータの読み込み。"""
    from pycocotools import coco, mask as cocomask

    coco_dir = pathlib.Path(coco_dir)
    cache_dir = pathlib.Path(cache_dir)
    if isinstance(input_size, int):
        input_size = (input_size, input_size)

    coco = coco.COCO(
        str(coco_dir / "annotations" / f"instances_{data_name}.json"))

    class_names = [c["name"] for c in coco.loadCats(coco.getCatIds())]
    jsonclassid_to_index = {
        c["id"]: class_names.index(c["name"])
        for c in coco.loadCats(coco.getCatIds())
    }

    X, y = [], []
    for entry in utils.tqdm(coco.loadImgs(coco.getImgIds()),
                            desc="load_ss_data"):
        dirname, filename = entry["coco_url"].split("/")[-2:]
        save_path = cache_dir / dirname / (filename + ".npy")
        X.append(coco_dir / dirname / filename)
        y.append(save_path)
        if not save_path.exists():
            # 読み込み
            objs = coco.loadAnns(
                coco.getAnnIds(imgIds=entry["id"], iscrowd=None))
            mask = np.zeros(
                (entry["height"], entry["width"], len(class_names)),
                dtype=np.uint8)
            for obj in objs:
                if obj.get("ignore", 0) == 1:
                    continue
                rle = cocomask.frPyObjects(obj["segmentation"],
                                           entry["height"], entry["width"])
                m = cocomask.decode(rle)
                class_id = jsonclassid_to_index[obj["category_id"]]
                mask[:, :, class_id] |= m
            mask = np.where(mask, np.uint8(255), np.uint8(0))
            # リサイズ
            if input_size is not None:
                mask = ndimage.resize(mask, input_size[1], input_size[0])
            # 保存
            save_path.parent.mkdir(parents=True, exist_ok=True)
            np.save(str(save_path), mask)

    return np.array(X), np.array(y), class_names
コード例 #11
0
ファイル: coco.py プロジェクト: ak110/pytoolkit
def load_od_data(coco_dir, data_name, use_crowded):
    """物体検出のデータの読み込み。"""
    import pycocotools.coco

    coco_dir = pathlib.Path(coco_dir)
    coco = pycocotools.coco.COCO(
        str(coco_dir / "annotations" / f"instances_{data_name}.json"))

    class_names = [c["name"] for c in coco.loadCats(coco.getCatIds())]
    jsonclassid_to_index = {
        c["id"]: class_names.index(c["name"])
        for c in coco.loadCats(coco.getCatIds())
    }

    labels = []
    for entry in coco.loadImgs(coco.getImgIds()):
        dirname, filename = entry["coco_url"].split("/")[-2:]
        objs = coco.loadAnns(
            coco.getAnnIds(imgIds=entry["id"],
                           iscrowd=None if use_crowded else False))

        bboxes, classes, areas, crowdeds = [], [], [], []
        width, height = entry["width"], entry["height"]
        for obj in objs:
            if obj.get("ignore", 0) == 1:
                continue
            x, y, w, h = obj["bbox"]
            bbox = np.array([x, y, x + w, y + h]) / np.array(
                [width, height, width, height])
            bbox = np.clip(bbox, 0, 1)
            if (bbox[:2] < bbox[2:]).all():
                bboxes.append(bbox)
                classes.append(jsonclassid_to_index[obj["category_id"]])
                areas.append(obj["area"])
                crowdeds.append(obj["iscrowd"])

        labels.append(
            tk.od.ObjectsAnnotation(
                path=coco_dir / dirname / filename,
                width=width,
                height=height,
                classes=classes,
                bboxes=bboxes,
                areas=areas,
                crowdeds=crowdeds,
            ))
    return tk.od.ObjectsAnnotation.create_dataset(labels,
                                                  class_names=class_names)
コード例 #12
0
    def _load_image_anns(self, img_id, coco, img_dir):
        img_info = coco.loadImgs(ids=[img_id])[0]
        file_name = img_info['file_name']
        img_path = os.path.join(img_dir, file_name)
        ann_ids = coco.getAnnIds(imgIds=[img_id])
        anns = copy.deepcopy(coco.loadAnns(ids=ann_ids))
        if '.npy' in img_path:
            img = np.load(img_path)
        else:
            assert os.path.exists(img_path), img_path
            img = cv2.imread(img_path)

            if self.args.cvt_gray:
                img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
                img = np.tile(img[..., None], 3)

        img, anns = self._pad_image_boxes(img, anns)
        return img[..., ::-1], anns, img_info, img_path
コード例 #13
0
ファイル: vis_bbox_epi2.py プロジェクト: ishann/detectron2
def main():

    img_seq = []
    # Get img_ids from lvis, instead.
    img_ids = coco.getImgIds()
    num_images = len(img_ids)

    # import pdb; pdb.set_trace()
    prog_bar = mmcv.ProgressBar(len(img_ids))

    ipdb.set_trace()
    if DEBUG_FOR_VIS:
        img_ids = random.sample(img_ids, 600)

    for i, img_id in enumerate(img_ids):

        # ipdb.set_trace()
        if DEBUG_ is True:
            if i == DEBUG_FRAMES: break

        img_info = coco.loadImgs(ids=[img_id])[0]
        img_path = IMG_PATH + img_info['file_name']
        img = cv2.imread(img_path)
        gt_ids = coco.getAnnIds(imgIds=[img_id])
        gts = coco.loadAnns(gt_ids)
        gt_img = img.copy()

        for j, pred in enumerate(gts):
            bbox = _coco_box_to_bbox(pred['bbox'])
            cat_id = pred['category_id']
            gt_img = add_box(gt_img, bbox, 1.00, cat_id)

        # ipdb.set_trace()
        img_name = 'gt_{}.jpg'.format(str(img_id).zfill(12))

        gt_img = add_to_canvas(gt_img)

        cv2.imwrite(os.path.join(OUT_PATH, img_name), gt_img)
        img_seq.append(gt_img)

        prog_bar.update()

    ipdb.set_trace()
    make_video(img_seq, VID_NAME)
コード例 #14
0
def visualize(img_id):
    img_descriptor = coco.loadImgs(img_id)
    file_name = coco_data_folder + "val/" + img_descriptor[0]['file_name']

    fig, ax = plt.subplots(1)
    img = mpimg.imread(file_name)
    ax.imshow(img)

    gt_ann_ids = coco.getAnnIds(imgIds=[img_id])
    gt_anns = coco.loadAnns(gt_ann_ids)
    dets = detections_by_imgid[img_id]
    print("Image", img_id, "Dets", len(dets), "GT", len(gt_anns))

    for gt in gt_anns:
        draw_box(ax, gt['bbox'], 'r', gt['category_id'], 1.0)
    for det in dets:
        draw_box(ax, det['bbox'], 'b', det['category_id'], det['score'])

    plt.show()
コード例 #15
0

if __name__ == '__main__':
    dets = []
    img_ids = coco.getImgIds()
    num_images = len(img_ids)
    for k in range(1, len(sys.argv)):
        pred_path = sys.argv[k]
        dets.append(coco.loadRes(pred_path))
    # import pdb; pdb.set_trace()
    for i, img_id in enumerate(img_ids):
        img_info = coco.loadImgs(ids=[img_id])[0]
        img_path = IMG_PATH + img_info['file_name']
        img = cv2.imread(img_path)
        gt_ids = coco.getAnnIds(imgIds=[img_id])
        gts = coco.loadAnns(gt_ids)
        gt_img = img.copy()
        for j, pred in enumerate(gts):
            bbox = _coco_box_to_bbox(pred['bbox'])
            cat_id = pred['category_id']
            gt_img = add_box(gt_img, bbox, 0, cat_id)
        for k in range(len(dets)):
            pred_ids = dets[k].getAnnIds(imgIds=[img_id])
            preds = dets[k].loadAnns(pred_ids)
            pred_img = img.copy()
            for j, pred in enumerate(preds):
                bbox = _coco_box_to_bbox(pred['bbox'])
                sc = pred['score']
                cat_id = pred['category_id']
                if sc > 0.2:
                    pred_img = add_box(pred_img, bbox, sc, cat_id)
 if os.path.isfile(foutPrv):
     print('\timage already processed [%s]' % foutImg)
     continue
 #
 fimg = '%s/%s' % (dirImg, timgInfo['file_name'])
 timg = skio.imread(fimg)
 # assert (timg.ndim==3)
 if timg.ndim == 2:
     timg = skcl.gray2rgb(timg)
 twidth = timgInfo['width']
 theight = timgInfo['height']
 vv = tmpDictFoodImgIds[kk]
 tmsk = None
 for vvi in vv:
     tannIds = coco.getAnnIds(imgIds=kk, catIds=vvi, iscrowd=False)
     tanns = coco.loadAnns(tannIds)
     print('\t :: processing: %d -> %s' % (vvi, reversedDirFoods[vvi]))
     tmpMsk = None
     for ttt in tanns:
         rle = mask.frPyObjects(ttt['segmentation'], theight, twidth)
         tmpm = mask.decode(rle)
         if tmpm.shape[2] > 1:
             print('\t\t**** multiple shape out :(  --> [%s]' % fimg)
         tmpm = np.sum(tmpm, axis=2)
         if tmpMsk is None:
             tmpMsk = tmpm
         else:
             tmpMsk += tmpm
     if tmsk is None:
         tmsk = np.zeros(tmpMsk.shape, dtype=tmpMsk.dtype)
     tmsk[tmpMsk > 0] = vvi
コード例 #17
0
def simple_demo_example():
    if False:
        annotation_filepath = '/path/to/annotation.json'
        data_dir_path = '/path/to/data'
    elif False:
        if 'posix' == os.name:
            data_base_dir_path = '/home/sangwook/work/dataset'
        else:
            data_base_dir_path = 'D:/work/dataset'
        publaynet_dir_path = data_base_dir_path + '/text/layout/publaynet/publaynet'

        #annotation_filepath = publaynet_dir_path + '/train.json'
        annotation_filepath = publaynet_dir_path + '/val.json'

        if 'train' in annotation_filepath:
            data_dir_path = publaynet_dir_path + '/train'
        elif 'val' in annotation_filepath:
            data_dir_path = publaynet_dir_path + '/val'
        else:
            print('Invalid directory tag.')
            return
    else:
        if 'posix' == os.name:
            data_base_dir_path = '/home/sangwook/work/dataset'
        else:
            data_base_dir_path = 'D:/work/dataset'
        coco_dir_path = data_base_dir_path + '/coco'

        annotation_filepath = coco_dir_path + '/annotations/instances_train2014.json'
        #annotation_filepath = coco_dir_path + '/annotations/instances_val2014.json'
        #annotation_filepath = coco_dir_path + '/annotations/instances_train2017.json'
        #annotation_filepath = coco_dir_path + '/annotations/instances_val2017.json'
        #annotation_filepath = coco_dir_path + '/annotations/person_keypoints_train2014.json'
        #annotation_filepath = coco_dir_path + '/annotations/person_keypoints_val2014.json'
        #annotation_filepath = coco_dir_path + '/annotations/person_keypoints_train2017.json'
        #annotation_filepath = coco_dir_path + '/annotations/person_keypoints_val2017.json'
        #annotation_filepath = coco_dir_path + '/annotations/captions_train2014.json'
        #annotation_filepath = coco_dir_path + '/annotations/captions_val2014.json'
        #annotation_filepath = coco_dir_path + '/annotations/captions_train2017.json'
        #annotation_filepath = coco_dir_path + '/annotations/captions_val2017.json'

        if 'train2014' in annotation_filepath:
            data_dir_path = coco_dir_path + '/train2014'
        elif 'val2014' in annotation_filepath:
            data_dir_path = coco_dir_path + '/val2014'
        elif 'train2017' in annotation_filepath:
            data_dir_path = coco_dir_path + '/train2017'
        elif 'val2017' in annotation_filepath:
            data_dir_path = coco_dir_path + '/val2017'
        else:
            print('Invalid directory tag.')
            return

    try:
        print(
            'Start loading a COCO data from {}...'.format(annotation_filepath))
        start_time = time.time()
        coco = pycocotools.coco.COCO(annotation_filepath)
        print('End loading a COCO data: {} secs.'.format(time.time() -
                                                         start_time))
    except UnicodeDecodeError as ex:
        print('Unicode decode error in {}: {}.'.format(annotation_filepath,
                                                       ex))
        return
    except FileNotFoundError as ex:
        print('File not found, {}: {}.'.format(annotation_filepath, ex))
        return

    #--------------------
    # REF [site] >> Data format.
    #	https://cocodataset.org/#format-data

    # Show information on data.
    if True:
        #print('Dataset: {}.'.format(coco.dataset))
        print('Dataset keys = {}.'.format(list(coco.dataset.keys())))

        if 'info' in coco.dataset:
            #print('Info: {}.'.format(coco.dataset['info']))
            print('Info: {}.'.format(coco.info()))
        if 'license' in coco.dataset:
            print('License: {}.'.format(coco.dataset['license']))

        if 'images' in coco.dataset:
            print('Images:')
            #print('\tData: {}.'.format(coco.dataset['images']))
            print('\tKeys = {}.'.format(list(
                coco.dataset['images'][0].keys())))
            print('\t#images = {}.'.format(len(coco.dataset['images'])))
            print('\tMin and max IDs = ({}, {}).'.format(
                functools.reduce(lambda mm, img: min(mm, img['id']),
                                 coco.dataset['images'],
                                 coco.dataset['images'][0]['id']),
                functools.reduce(lambda mm, img: max(mm, img['id']),
                                 coco.dataset['images'], 0)))
        if 'categories' in coco.dataset:
            print('Categories:')
            #print('\tData: {}.'.format(coco.dataset['categories']))
            print('\tKeys = {}.'.format(
                list(coco.dataset['categories'][0].keys())))
            print('\t#categories = {}.'.format(len(
                coco.dataset['categories'])))
            print('\tMin and max IDs = ({}, {}).'.format(
                functools.reduce(lambda mm, cat: min(mm, cat['id']),
                                 coco.dataset['categories'],
                                 coco.dataset['categories'][0]['id']),
                functools.reduce(lambda mm, cat: max(mm, cat['id']),
                                 coco.dataset['categories'], 0)))
            print('\tCategory = {}.'.format(
                list(cat['name'] for cat in coco.dataset['categories'])))
        if 'annotations' in coco.dataset:
            print('Annotations:')
            #print('\tData: {}.'.format(coco.dataset['annotations']))
            print('\tKeys = {}.'.format(
                list(coco.dataset['annotations'][0].keys())))
            print('\t#annotations = {}.'.format(
                len(coco.dataset['annotations'])))
            print('\tMin and max IDs = ({}, {}).'.format(
                functools.reduce(lambda mm, ann: min(mm, ann['id']),
                                 coco.dataset['annotations'],
                                 coco.dataset['annotations'][0]['id']),
                functools.reduce(lambda mm, ann: max(mm, ann['id']),
                                 coco.dataset['annotations'], 0)))
        if 'segment_infos' in coco.dataset:
            print('Segment infos:')
            #print('\tData: {}.'.format(coco.dataset['segment_infos']))
            print('\tKeys = {}.'.format(
                list(coco.dataset['segment_infos'][0].keys())))
            print('\t#segment infos = {}.'.format(
                len(coco.dataset['segment_infos'])))
            print('\tMin and max IDs = ({}, {}).'.format(
                functools.reduce(lambda mm, si: min(mm, si['id']),
                                 coco.dataset['segment_infos'],
                                 coco.dataset['segment_infos'][0]['id']),
                functools.reduce(lambda mm, si: max(mm, si['id']),
                                 coco.dataset['segment_infos'], 0)))

        #--------------------
        """
		APIs for pycocotools.coco.COCO:
			annIds = coco.getAnnIds(imgIds=[], catIds=[], areaRng=[], iscrowd=None)
			catIds = coco.getCatIds(catNms=[], supNms=[], catIds=[])
			imgIds = coco.getImgIds(imgIds=[], catIds=[])

			anns = coco.loadAnns(ids=[])
			cats = coco.loadCats(ids=[])
			imgs = coco.loadImgs(ids=[])

			coco_res = coco.loadRes(resFile)
			anns = coco.loadNumpyAnnotations(data)

			rle = coco.annToRLE(ann)
			mask = coco.annToMask(ann)

			import matplotlib.pyplot as plt
			coco.showAnns(anns, draw_bbox=False)
			plt.show()
		"""

        if 'categories' in coco.dataset:
            cat_counts = dict()
            for cat in coco.dataset['categories']:
                annIds = coco.getAnnIds(imgIds=[],
                                        catIds=[cat['id']],
                                        areaRng=[],
                                        iscrowd=None)
                cat_counts[cat['name']] = len(annIds)
            print("#annotations per category: {}.".format(cat_counts))

        #imgIds = coco.getImgIds(imgIds=[1, 3, 7], catIds=[1])
        #images = coco.loadImgs(ids=imgIds)
        #print('Image IDs = {}.'.format(imgIds))
        #print('Images = {}.'.format(images))

        #catIds = coco.getCatIds(catNms=[coco.dataset['categories'][1 - 1]['name'], coco.dataset['categories'][2 - 1]['name']], supNms=[], catIds=[2, 3])
        #categories = coco.loadCats(ids=annIds)
        #print('Category IDs = {}.'.format(catIds))
        #print('Categories = {}.'.format(categories))

        #annIds = coco.getAnnIds(imgIds=[1], catIds=[], areaRng=[], iscrowd=None)
        #annotations = coco.loadAnns(ids=annIds)
        #print('Annotation IDs = {}.'.format(annIds))
        #print('Annotation = {}.'.format(annotations))

    #--------------------
    # Visualize data.
    if True:
        import random
        num_data_to_visualize = 10
        for image_info in random.sample(
                coco.dataset['images'],
                min(num_data_to_visualize, len(coco.dataset['images']))):
            image_filepath = image_info['file_name']
            image_id = image_info['id']
            image_height, image_width = image_info['height'], image_info[
                'width']

            image_filepath = os.path.join(data_dir_path, image_filepath)

            annIds = coco.getAnnIds(imgIds=[image_id],
                                    catIds=[],
                                    areaRng=[],
                                    iscrowd=None)
            annotations = coco.loadAnns(ids=annIds)

            if True:
                import PIL.Image
                import matplotlib.pyplot as plt

                try:
                    with PIL.Image.open(image_filepath) as img:
                        if img.width != image_width or img.height != image_height:
                            print('Invalid image size, ({}, {}) != ({}, {}).'.
                                  format(img.width, img.height, image_width,
                                         image_height))
                            return
                        plt.imshow(
                            visualize_coco_annotations(img, annotations, coco))
                        plt.tight_layout()
                        plt.axis('off')
                except IOError as ex:
                    print('Failed to load an image, {}: {}.'.format(
                        image_filepath, ex))
                    raise
                plt.tight_layout()
                plt.show()
            else:
                import cv2

                img = cv2.imread(image_filepath, cv2.IMREAD_COLOR)
                if img is None:
                    print(
                        'Failed to load an image, {}.'.format(image_filepath))
                    return
                if img.shape[0] != image_height or img.shape[1] != image_width:
                    print('Invalid image shape, ({}, {}) != ({}, {}).'.format(
                        img.shape[0], img.shape[1], image_height, image_width))
                    return

                colors = [(0, 0, 255), (0, 255, 0), (255, 0, 0), (0, 255, 255),
                          (255, 0, 255), (255, 255, 0)]

                print('Labels = {}.'.format(
                    list(ann['category_id'] for ann in annotations)))
                for ann in annotations:
                    bbox = ann['bbox']  # [x, y, width, height].
                    #area = ann['area']
                    segmentation = ann['segmentation']  # RLE or [polygon].
                    category_id = ann['category_id']
                    #iscrowd = ann['iscrowd']  # 0 or 1.

                    assert len(segmentation) == 1
                    segmentation = segmentation[0]

                    color = colors[(category_id - 1) % len(colors)]
                    left, top, right, bottom = bbox[0], bbox[
                        1], bbox[0] + bbox[2], bbox[1] + bbox[3]
                    #assert left >= 0 and top >= 0 and right <= img.shape[1] and bottom <= img.shape[0], ((left, top, right, bottom), (img.shape))
                    left, top, right, bottom = max(math.floor(left), 0), max(
                        math.floor(top),
                        0), min(math.ceil(right),
                                img.shape[1] - 1), min(math.ceil(bottom),
                                                       img.shape[0] - 1)
                    cv2.rectangle(img, (left, top), (right, bottom), color, 2,
                                  cv2.LINE_8)
                    segmentation = np.expand_dims(np.round(
                        np.array(
                            list(segmentation[si:si + 2] for si in range(
                                0, len(segmentation), 2)))).astype(np.int),
                                                  axis=1)
                    overlay = np.zeros_like(img)
                    cv2.drawContours(overlay, [segmentation], 0, color,
                                     cv2.FILLED, cv2.LINE_8)
                    img = cv2.addWeighted(img, 1.0, overlay, 0.25, 0)
                cv2.imshow('Image', img)
                cv2.waitKey(0)
コード例 #18
0
                    dtype=np.float32)

coco = coco.COCO(anno_path)
images = coco.getImgIds()
catIds = coco.getCatIds(class_name[-1])
assert catIds == _valid_ids
images = coco.getImgIds(images, catIds)
num_samples = len(images)

index = np.random.randint(num_samples)
img_id = images[index]

file_name = coco.loadImgs(ids=[img_id])[0]['file_name']
img_path = os.path.join(img_dir, file_name)
ann_ids = coco.getAnnIds(imgIds=[img_id])
anns = coco.loadAnns(ids=ann_ids)

anns = list(
    filter(lambda x: x['category_id'] in _valid_ids and x['iscrowd'] != 1,
           anns))
num_objs = min(len(anns), max_objs)

img = cv2.imread(img_path)
print(file_name)
print(img.shape)

height, width = img.shape[0], img.shape[1]
c = np.array([img.shape[1] / 2., img.shape[0] / 2.], dtype=np.float32)
s = max(img.shape[0], img.shape[1]) * 1.0
rot = 0
print(c)
コード例 #19
0
ファイル: coco_show.py プロジェクト: zhuzhu18/utilities
from pycocotools import coco
import matplotlib.pyplot as plt
import skimage.io as io
import time

time1 = time.time()
annFile = '/media/zhuzhu/ec114170-f406-444f-bee7-a3dc0a86cfa2/dataset/coco/annotations/person_keypoints_val2017.json'
dataDir = '/media/zhuzhu/ec114170-f406-444f-bee7-a3dc0a86cfa2/dataset/coco/images/val2017'
saveDir = '/media/zhuzhu/ec114170-f406-444f-bee7-a3dc0a86cfa2/coco_ground/val2017'

coco = coco.COCO(annFile)
catIds = coco.getCatIds(catNms=['person'])
imgIds = coco.getImgIds(catIds=catIds)

for idx in imgIds:
    img = coco.loadImgs(ids=idx)[0]
    annIds = coco.getAnnIds(imgIds=idx, catIds=catIds)
    anns = coco.loadAnns(ids=annIds)
    plt.figure(idx)
    I = io.imread('%s/%s' % (dataDir, img['file_name']))
    plt.imshow(I)
    plt.axis('off')
    coco.showAnns(anns=anns)
    plt.savefig('%s/%s' % (saveDir, img['file_name']))
    plt.close()

time2 = time.time()
print('spent t = %.2f min' % ((time2 - time1) / 60))
コード例 #20
0
def cache(config, path, category_index):
    phase = os.path.splitext(os.path.basename(path))[0]
    data = []
    for i, row in pd.read_csv(os.path.splitext(__file__)[0] + '.tsv',
                              sep='\t').iterrows():
        logging.info('loading data %d (%s)' %
                     (i, ', '.join([k + '=' + str(v)
                                    for k, v in row.items()])))
        root = os.path.expanduser(os.path.expandvars(row['root']))
        year = str(row['year'])
        suffix = phase + year
        path = os.path.join(root, 'annotations', 'instances_%s.json' % suffix)
        if not os.path.exists(path):
            logging.warning(path + ' not exists')
            continue
        coco = pycocotools.coco.COCO(path)
        catIds = coco.getCatIds(catNms=list(category_index.keys()))
        cats = coco.loadCats(catIds)
        id_index = dict(
            (cat['id'], category_index[cat['name']]) for cat in cats)
        imgIds = coco.getImgIds()
        path = os.path.join(root, suffix)
        imgs = coco.loadImgs(imgIds)
        _imgs = list(
            filter(
                lambda img: os.path.exists(os.path.join(
                    path, img['file_name'])), imgs))
        if len(imgs) > len(_imgs):
            logging.warning('%d of %d images not exists' %
                            (len(imgs) - len(_imgs), len(imgs)))
        for img in tqdm.tqdm(_imgs):
            annIds = coco.getAnnIds(imgIds=img['id'],
                                    catIds=catIds,
                                    iscrowd=None)
            anns = coco.loadAnns(annIds)
            if len(anns) <= 0:
                continue
            path = os.path.join(path, img['file_name'])
            width, height = img['width'], img['height']
            bbox = np.array([ann['bbox'] for ann in anns], dtype=np.float32)
            yx_min = bbox[:, 1::-1]
            hw = bbox[:, -1:1:-1]
            yx_max = yx_min + hw
            cls = np.array([id_index[ann['category_id']] for ann in anns],
                           dtype=np.int)
            difficult = np.zeros(cls.shape, dtype=np.uint8)
            try:
                if config.getboolean('cache', 'verify'):
                    size = (height, width)
                    image = cv2.imread(path)
                    assert image is not None
                    assert image.shape[:2] == size[:2]
                    utils.cache.verify_coords(yx_min, yx_max, size[:2])
            except configparser.NoOptionError:
                pass
            assert len(yx_min) == len(cls)
            assert yx_min.shape == yx_max.shape
            assert len(yx_min.shape) == 2 and yx_min.shape[-1] == 2
            data.append(
                dict(path=path,
                     yx_min=yx_min,
                     yx_max=yx_max,
                     cls=cls,
                     difficult=difficult))
        logging.warning('%d of %d images are saved' % (len(data), len(_imgs)))
    return data
コード例 #21
0
ファイル: coco.py プロジェクト: Joffrey-Liu/Mask_RCNN
    def load_coco(self,
                  dataset_dir,
                  subset,
                  year=DEFAULT_DATASET_YEAR,
                  class_ids=None,
                  class_map=None,
                  return_coco=False,
                  auto_download=False):
        """Load a subset of the COCO dataset.
        dataset_dir: The root directory of the COCO dataset.
        subset: What to load (train, val, minival, valminusminival)
        year: What dataset year to load (2014, 2017) as a string, not an integer
        class_ids: If provided, only loads images that have the given classes.
        class_map: TODO: Not implemented yet. Supports maping classes from
            different datasets to the same class ID.
        return_coco: If True, returns the COCO object.
        auto_download: Automatically download and unzip MS-COCO images and annotations
        """

        if auto_download is True:
            self.auto_download(dataset_dir, subset, year)

        coco = COCO("{}/annotations/instances_{}{}.json".format(
            dataset_dir, subset, year))
        if subset == "minival" or subset == "valminusminival":
            subset = "val"
        image_dir = "{}/{}{}".format(dataset_dir, subset, year)

        # Load all classes or a subset?
        if not class_ids:
            # All classes
            class_ids = sorted(coco.getCatIds())

        # All images or a subset?
        if class_ids:
            image_ids = []
            for id in class_ids:
                image_ids.extend(list(coco.getImgIds(catIds=[id])))
            # Remove duplicates
            image_ids = list(set(image_ids))
        else:
            # All images
            image_ids = list(coco.imgs.keys())

        # Add classes
        for i in class_ids:
            self.add_class("coco", i, coco.loadCats(i)[0]["name"])

        # Add images
        for i in image_ids:
            self.add_image("coco",
                           image_id=i,
                           path=os.path.join(image_dir,
                                             coco.imgs[i]['file_name']),
                           width=coco.imgs[i]["width"],
                           height=coco.imgs[i]["height"],
                           annotations=coco.loadAnns(
                               coco.getAnnIds(imgIds=[i],
                                              catIds=class_ids,
                                              iscrowd=None)))
        if return_coco:
            return coco