コード例 #1
0
def save_images(zip_file, save_dir=None, train_test_split_file=None):
    """Rename and move all used images to a directory."""

    print("Extracting zip file")
    root = osp.dirname(osp.abspath(zip_file))
    if save_dir is None:
        save_dir = root
    may_make_dir(save_dir)
    # with ZipFile(zip_file) as z:
    #   z.extractall(path=save_dir)
    # print("Extracting zip file done")

    new_im_dir = osp.join(save_dir, 'images')
    may_make_dir(new_im_dir)
    # raw_dir = osp.join(save_dir, osp.basename(zip_file[:-7]))
    raw_dir = save_dir

    im_paths = []
    nums = []

    for dir_name in ['bounding_box_train', 'bounding_box_test', 'query']:

        # import pdb
        # pdb.set_trace()

        im_paths_ = get_im_names(osp.join(raw_dir, dir_name),
                                 pattern='*.jpg',
                                 return_path=True,
                                 return_np=False)
        im_paths_.sort()
        im_paths += list(im_paths_)
        nums.append(len(im_paths_))

    # import pdb
    # pdb.set_trace()

    im_names = move_ims(im_paths, new_im_dir, parse_original_im_name,
                        new_im_name_tmpl)

    split = dict()
    keys = ['trainval_im_names', 'gallery_im_names', 'q_im_names']
    inds = [0] + nums
    inds = np.cumsum(inds)
    for i, k in enumerate(keys):
        split[k] = im_names[inds[i]:inds[i + 1]]

    save_pickle(split, train_test_split_file)
    print('Saving images done.')
    return split
コード例 #2
0
def save_images_rap2(zip_file, annotation_file, query_file, save_dir=None, \
  train_test_split_file=None):
    """Rename and move all the used images to a diretory."""
    print("Extracting zip file")
    root = osp.dirname(osp.abspath(zip_file))
    if save_dir is None:
        save_dir = root
    may_make_dir(save_dir)
    with ZipFile(zip_file) as z:
        z.extractall(path=save_dir)
    print("Extracting zip file done")

    new_im_dir = osp.join(save_dir, 'images')
    may_make_dir(new_im_dir)
    raw_dir = osp.join(save_dir, osp.basename(zip_file)[:-4])

    # Get fnames
    img_fnames_train, img_fnames_test, img_fnames_query = \
      _get_im_names_rap2(annotation_file, query_file, 1)
    img_fnames_train.sort()
    img_fnames_test.sort()
    img_fnames_query.sort()
    im_paths = list(img_fnames_train) + list(img_fnames_test) + \
      list(img_fnames_query)
    nums = [img_fnames_train.shape[0], img_fnames_test.shape[0], \
      img_fnames_query.shape[0]]

    # Move images
    org_img_dir = osp.join(root, 'images-pedestrian')
    im_names = _move_ims(org_img_dir, im_paths, new_im_dir, \
      parse_original_im_name, new_im_name_tmpl)

    split = dict()
    keys = ['trainval_im_names', 'gallery_im_names', 'q_im_names']
    inds = [0] + nums
    inds = np.cumsum(inds)
    for i, k in enumerate(keys):
        split[k] = im_names[inds[i]:inds[i + 1]]
    save_pickle(split, train_test_split_file)
    print('Saving images done.')
    return split
def combine_trainval_sets(im_dirs, partition_files, save_dir):
    new_im_dir = ospj(save_dir, 'trainval_images')
    may_make_dir(new_im_dir)
    new_im_names = []
    new_start_id = 0
    for im_dir, partition_file in zip(im_dirs, partition_files):
        partitions = load_pickle(partition_file)
        im_paths = [ospj(im_dir, n) for n in partitions['trainval_im_names']]
        im_paths.sort()
        new_im_names_, id_mapping = move_ims(im_paths, new_im_dir,
                                             parse_im_name, new_im_name_tmpl,
                                             new_start_id)
        new_start_id += len(id_mapping)
        new_im_names += new_im_names_

    new_ids = range(new_start_id)
    partitions = {
        'trainval_im_names': new_im_names,
        'trainval_ids2labels': dict(zip(new_ids, new_ids)),
    }
    partition_file = ospj(save_dir, 'partitions.pkl')
    save_pickle(partitions, partition_file)
    print('Partition file saved to {}'.format(partition_file))
コード例 #4
0
def save_im(im, save_path):
    """im: shape [3, H, W]"""
    may_make_dir(ospdn(save_path))
    im = im.transpose(1, 2, 0)
    Image.fromarray(im).save(save_path)
コード例 #5
0
def save_images(zip_file, save_dir=None, train_test_split_file=None):
    """Rename and move all used images to a directory."""

    print("Extracting zip file")
    root = osp.dirname(osp.abspath(zip_file))
    if save_dir is None:
        save_dir = root
    may_make_dir(osp.abspath(save_dir))
    # with ZipFile(zip_file) as z:
    #   z.extractall(path=save_dir)
    # print("Extracting zip file done")

    new_im_dir = osp.join(save_dir, 'images')
    may_make_dir(osp.abspath(new_im_dir))
    # raw_dir = osp.join(save_dir, osp.basename(zip_file)[:-4])

    raw_dir = save_dir
    im_paths = []
    nums = []

    im_paths_ = get_im_names(osp.join(raw_dir, 'bounding_box_train'),
                             return_path=True,
                             return_np=False)
    im_paths_.sort()
    im_paths += list(im_paths_)
    nums.append(len(im_paths_))

    im_paths_ = get_im_names(osp.join(raw_dir, 'bounding_box_test'),
                             return_path=True,
                             return_np=False)
    im_paths_.sort()
    im_paths_ = [p for p in im_paths_ if not osp.basename(p).startswith('-1')]
    im_paths += list(im_paths_)
    nums.append(len(im_paths_))

    im_paths_ = get_im_names(osp.join(raw_dir, 'query'),
                             return_path=True,
                             return_np=False)
    im_paths_.sort()
    im_paths += list(im_paths_)
    nums.append(len(im_paths_))
    q_ids_cams = set([(parse_original_im_name(osp.basename(p), 'id'),
                       parse_original_im_name(osp.basename(p), 'cam'))
                      for p in im_paths_])

    im_paths_ = get_im_names(osp.join(raw_dir, 'gt_bbox'),
                             return_path=True,
                             return_np=False)
    im_paths_.sort()
    # Only gather images for those ids and cams used in testing.
    im_paths_ = [
        p for p in im_paths_
        if (parse_original_im_name(osp.basename(p), 'id'),
            parse_original_im_name(osp.basename(p), 'cam')) in q_ids_cams
    ]
    im_paths += list(im_paths_)
    nums.append(len(im_paths_))

    im_names = move_ims(im_paths, new_im_dir, parse_original_im_name,
                        new_im_name_tmpl)

    split = dict()
    keys = [
        'trainval_im_names', 'gallery_im_names', 'q_im_names', 'mq_im_names'
    ]
    inds = [0] + nums
    inds = np.cumsum(np.array(inds))
    for i, k in enumerate(keys):
        split[k] = im_names[inds[i]:inds[i + 1]]

    save_pickle(split, train_test_split_file)
    print('Saving images done.')
    return split
            '~/Dataset/cuhk03/{}/partitions.pkl'.format(cuhk03_im_type)))

    parser.add_argument('--duke_im_dir',
                        type=str,
                        default=ospeu('~/Dataset/duke/images'))
    parser.add_argument('--duke_partition_file',
                        type=str,
                        default=ospeu('~/Dataset/duke/partitions.pkl'))

    parser.add_argument('--save_dir',
                        type=str,
                        default=ospeu('~/Dataset/market1501_cuhk03_duke'))

    args = parser.parse_args()

    im_dirs = [
        ospap(ospeu(args.market1501_im_dir)),
        ospap(ospeu(args.cuhk03_im_dir)),
        ospap(ospeu(args.duke_im_dir))
    ]
    partition_files = [
        ospap(ospeu(args.market1501_partition_file)),
        ospap(ospeu(args.cuhk03_partition_file)),
        ospap(ospeu(args.duke_partition_file))
    ]

    save_dir = ospap(ospeu(args.save_dir))
    may_make_dir(save_dir)

    combine_trainval_sets(im_dirs, partition_files, save_dir)