예제 #1
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)
예제 #2
0
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='')
def print_locations(json_file: str) -> None:
    """
    Args:
        json_file: str, path to COCO-style JSON file
    """
    with open(json_file, 'rt') as fi:
        js = json.load(fi)
    print('Locations used:')
    print(
        sorted({
            coco.loadImgs([im['original_key']])[0][LOCATION_KEY]
            for im in js['images']
        }))
    #js_keys = ['/'.join(im['file_name'].split('/')[1:])[:-4] for im in js['images']]
    #for tk in js_keys:
    #    assert np.isclose(1, np.sum(detections[tk]['detection_scores'] > 0.5))
    class_to_name = {c['id']: c['name'] for c in js['categories']}
    sorted_class_ids = sorted(class_to_name.keys())
    if CLASSLIST_OUTPUT is not None and json_file == TRAIN_JSON:
        with open(CLASSLIST_OUTPUT, 'wt') as fi:
            fi.write('\n'.join([class_to_name[c] for c in sorted_class_ids]))
    labels = np.array([a['category_id'] for a in js['annotations']])
    print(f'In total {len(class_to_name)} classes and {len(labels)} images.')
    print('Classes with one or more images:', len(set(labels)))
    print('Images per class:')
    print('{:5} {:<15} {:>11}'.format('ID', 'Name', 'Image count'))
    for c in sorted_class_ids:
        name = class_to_name[c]
        count = np.sum(labels == c)
        print('{:5} {:<15} {:>11}'.format(c, name, count))
예제 #4
0
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
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
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
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 __getitem__(self, index:int ):
        coco = self.coco
        img_id = self.ids[index]
        
        mask = cocoSegmentationToSegmentationMap(coco, img_id)

        path = coco.loadImgs(img_id)[0]['file_name']
        img = Image.open(os.path.join(self.root, path)).convert('RGB')

        if self.transforms is not None:
            img, mask = self.transforms(img, mask)
        
        return img, mask, img_id
예제 #10
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
예제 #11
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
예제 #12
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)
예제 #13
0
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))

    for i, img_id in enumerate(img_ids):

        print(i)
        # 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)
        prop_img = img.copy()
        img_props = props[i]
        # img_props = img_props[img_props[:,-1]>VIS_THR, :4]

        for j, prop in enumerate(img_props):

            bbox = _coco_box_to_bbox(prop)
            # bbox = np.asarray(prop, dtype=np.int32)
            cat_id = 1
            prop_img = add_box(prop_img, bbox, VIS_THR, cat_id)

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

        prop_img = add_to_canvas(prop_img)

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

        prog_bar.update()

    make_video(img_seq, VID_NAME)
예제 #14
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
예제 #15
0
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)
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()
예제 #17
0
                    0.5, (0, 0, 0),
                    thickness=1)
    cv2.rectangle(image, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color, 2)
    return image


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'])
           (ii, idx, tmpCat['name'], tmpCat['supercategory']))
 #
 tmpDictFoodImgIds = {}
 for ii, idx in enumerate(listSortedFoodIds):
     tmpImgIds = coco.getImgIds(catIds=idx)
     for timgId in tmpImgIds:
         if tmpDictFoodImgIds.has_key(timgId):
             tmpDictFoodImgIds[timgId].append(idx)
         else:
             tmpDictFoodImgIds[timgId] = [idx]
 setAllFoodImgIds = sorted(tmpDictFoodImgIds.keys())
 print('#list/#set = %d' % len(tmpDictFoodImgIds.keys()))
 numImages = len(setAllFoodImgIds)
 for ii, kk in enumerate(setAllFoodImgIds):
     print('[%d/%d]' % (ii, numImages))
     timgInfo = coco.loadImgs(kk)[0]
     foutImg = '%s/%s' % (dirOut, timgInfo['file_name'])
     foutMsk = '%s/%s-mskfood.png' % (dirOut, timgInfo['file_name'])
     foutPrv = '%s/%s-preview.jpg' % (dirOut, timgInfo['file_name'])
     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]
예제 #19
0
def eval(model_arch, model_path, img_dir, gt_annot_path):
    output = "output"
    if os.path.exists(output):
        shutil.rmtree(output)
    os.mkdir(output)
    os.environ['CUDA_VISIBLE_DEVICES'] = "0"
    if LoadImagesAndLabels.num_classes <= 5:
        colors = [(55, 55, 250), (255, 155, 50), (128, 0, 0), (255, 0, 255),
                  (128, 255, 128), (255, 0, 0)]
    else:
        colors = [(v // 32 * 64 + 64, (v // 8) % 4 * 64, v % 8 * 32)
                  for v in range(1, LoadImagesAndLabels.num_classes + 1)][::-1]
    detector = CtdetDetector(model_arch, model_path)

    print('\n/****************** Eval ****************/\n')
    import tqdm
    import pycocotools.coco as coco
    from pycocotools.cocoeval import COCOeval

    print("gt path: {}".format(gt_annot_path))
    result_file = '../evaluation/instances_det.json'
    coco = coco.COCO(gt_annot_path)
    images = coco.getImgIds()
    num_samples = len(images)
    print('find {} samples in {}'.format(num_samples, gt_annot_path))
    #------------------------------------------------
    coco_res = []
    for index in tqdm.tqdm(range(num_samples)):
        img_id = images[index]
        file_name = coco.loadImgs(ids=[img_id])[0]['file_name']
        image_path = os.path.join(img_dir, file_name)
        img = cv2.imread(image_path)
        results, hm = detector.work(img)  # 返回检测结果和置信度图

        class_num = {}
        for res in results:
            cls, conf, bbox = res[0], res[1], res[2]
            coco_res.append({
                'bbox':
                [bbox[0], bbox[1], bbox[2] - bbox[0], bbox[3] - bbox[1]],
                'category_id':
                LoadImagesAndLabels.class_name.index(cls),
                'image_id':
                img_id,
                'score':
                conf
            })
            if cls in class_num:
                class_num[cls] += 1
            else:
                class_num[cls] = 1
            color = colors[LoadImagesAndLabels.class_name.index(cls)]

            # 绘制标签&置信度
            label_ = '{}:{:.1f}'.format(cls, conf)
            plot_one_box(bbox,
                         img,
                         color=color,
                         label=label_,
                         line_thickness=2)

        cv2.imwrite(output + "/" + os.path.basename(image_path), img)
        cv2.namedWindow("heatmap", 0)
        cv2.imshow("heatmap", np.hstack(hm[0].cpu().numpy()))
        cv2.namedWindow("img", 0)
        cv2.imshow("img", img)
        key = cv2.waitKey(1)
    #-------------------------------------------------
    with open(result_file, 'w') as f_dump:
        json.dump(coco_res, f_dump, cls=NpEncoder)

    cocoDt = coco.loadRes(result_file)
    cocoEval = COCOeval(coco, cocoDt, 'bbox')
    # cocoEval.params.imgIds  = imgIds
    cocoEval.params.catIds = [1]  # 1代表’Hand’类,你可以根据需要增减类别
    cocoEval.evaluate()
    print('\n/***************************/\n')
    cocoEval.accumulate()
    print('\n/***************************/\n')
    cocoEval.summarize()
    draw_pr(cocoEval)
예제 #20
0
_eig_vec = np.array([[-0.58752847, -0.69563484, 0.41340352],
                     [-0.5832747, 0.00994535, -0.81221408],
                     [-0.56089297, 0.71832671, 0.41158938]],
                    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)
예제 #21
0
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))
예제 #22
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
def run_detection(sess: tf.Session,
                  coco: pycocotools.coco.COCO,
                  cat_id_to_names: Mapping[Any, str],
                  cat_id_to_new_id: Mapping[Any, int],
                  detections: Dict[str, Dict[str, Any]],
                  train_locations: List[str],
                  train_json: Dict[str, List],
                  test_json: Dict[str, List],
                  train_tfr_writer: Optional[TFRecordsWriter],
                  test_tfr_writer: Optional[TFRecordsWriter],
                  image_dir: str,
                  coco_output_dir: Optional[str],
                  split_by: str,
                  exclude_categories: List[str],
                  detection_threshold: float,
                  padding_factor: float) -> None:
    """Run object detector on images.

    Should be run with a default graph already loaded, e.g.,
        with graph.as_default():
            run_detection(...)

    Args:
        sess: tf.Session
        coco: pycocotools.coco.COCO, representation of JSON
        cat_id_to_names: dict, maps "old" category IDs to names
        cat_id_to_new_id: dict, maps "old" category IDs to new IDs
        detections: dict, maps image ID to dict of detection results
            updated in-place with detection results of each image
        train_locations: list of str, locations used for training set
        train_json: dict, COCO-style dict for train images
        test_json: dict, COCO-style dict for test images
        train_tfr_writer: TFRecordsWriter, for train images
        test_tfr_writer: TFRecordsWriter, for test images
        **for all other args, see description for main()
    """
    graph = tf.get_default_graph()

    ### Preparations: get all the output tensors
    tensor_dict = {
        key: graph.get_tensor_by_name(f'{key}:0')
        for key in ['num_detections', 'detection_boxes', 'detection_scores',
                    'detection_classes']
    }
    image_tensor = graph.get_tensor_by_name('image_tensor:0')

    # For all images listed in the annotations file
    next_image_id = 0
    next_annotation_id = 0
    for image_id in tqdm.tqdm(sorted(vv['id'] for vv in coco.imgs.values())):
        # Skip the image if it is annotated with more than one category
        category_ids = {ann['category_id'] for ann in coco.imgToAnns[image_id]}
        if len(category_ids) != 1:
            continue

        # Get "old" and "new" category ID and category name for this image.
        # Skip if in excluded categories.
        cat_id = list(category_ids)[0]
        json_cat_id = cat_id_to_new_id[cat_id]
        cat_name = cat_id_to_names[cat_id]
        if cat_name in exclude_categories:
            continue

        # get path to image
        cur_image = coco.loadImgs([image_id])[0]
        cur_file_name = cur_image['file_name']
        img_path = os.path.join(image_dir, cur_file_name)

        # If we already have detection results, we can use them
        # Otherwise run detector and add detections to the collection
        if image_id not in detections:
            try:
                detections[image_id] = detect_single_img(
                    sess, tensor_dict, image_tensor, img_path)
            except (FileNotFoundError, TypeError) as err:
                print(err)
                continue
        output_dict = detections[image_id]

        # Only select detections with confidence larger than DETECTION_THRESHOLD
        # Skip image if no detection selected
        selection = output_dict['detection_scores'] > detection_threshold
        if np.sum(selection) < 1 or selection.size == 0:
            continue
        normalized_bboxes = output_dict['detection_boxes'][selection]

        # whether it belongs to a training or testing location
        if cur_image[split_by] in train_locations:
            cur_json = train_json
            tfr_writer = train_tfr_writer
        else:
            cur_json = test_json
            tfr_writer = test_tfr_writer

        next_image_id, next_annotation_id = save_outputs(
            image_id, img_path, cur_image, cat_name, json_cat_id, next_image_id,
            next_annotation_id, normalized_bboxes, cur_json, tfr_writer,
            coco_output_dir, padding_factor)