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)[:-4])

    im_paths = []
    nums = []

    for dir_name in ['bounding_box_train', 'bounding_box_test', 'query']:
        im_paths_ = get_im_names(osp.join(raw_dir, dir_name),
                                 return_path=True,
                                 return_np=False)
        im_paths_.sort()
        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']
    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 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)[:-4])

  im_paths = []
  nums = []

  for dir_name in ['bounding_box_train', 'bounding_box_test', 'query']:
    im_paths_ = get_im_names(osp.join(raw_dir, dir_name),
                             return_path=True, return_np=False)
    im_paths_.sort()
    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']
  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 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])

    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
示例#4
0
def save_images(original_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(original_file))
    if save_dir is None:
        save_dir = root
    may_make_dir(osp.abspath(save_dir))
    #   with ZipFile(original_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.abspath(original_file)
    print('raw_dir: ', raw_dir)

    im_paths = []
    nums = []

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

    im_paths_ = get_im_names(osp.join(raw_dir, 'bbox_test'),
                             pattern='*/*.png',
                             return_path=True,
                             return_np=False)
    im_paths_.sort()
    im_paths_ = [
        p for p in im_paths_ if not osp.basename(p).startswith('00-1')
    ]
    im_paths += list(im_paths_)
    nums.append(len(im_paths_))
    print('dir_name:   bbox_test')
    print('nums:   ', nums)

    #   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_names = move_ims(im_paths, new_im_dir, parse_original_im_name,
                        new_im_name_tmpl)

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

    save_pickle(split, train_test_split_file)
    print('Saving images done.')
    return split
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])

  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
def save_images(data_dir, 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")
    #get the images and origin name of path
    new_im_dir = osp.join(save_dir, 'images')
    may_make_dir(osp.abspath(new_im_dir))
    # define paths of all images and number of files in four folders
    im_paths = []
    bb_test = []
    bb_test_num = 0
    bb_train_num = 0
    bb_train = []
    query = []
    query_num = 0
    gt_bb_num = 0
    gt_bb = []
    nums = []

    for data in dataset:
        raw_dir = osp.join(data_dir, data)
        im_paths_ = get_im_names(osp.join(raw_dir, 'bounding_box_train'),
                                 return_path=True,
                                 return_np=False)
        im_paths_.sort()
        bb_train += list(im_paths_)
        bb_train_num += 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')
        ]
        bb_test += list(im_paths_)
        bb_test_num += len(im_paths_)

        im_paths_ = get_im_names(osp.join(raw_dir, 'query'),
                                 return_path=True,
                                 return_np=False)
        im_paths_.sort()
        query += list(im_paths_)
        query_num += 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
        ]
        gt_bb += list(im_paths_)
        gt_bb_num += len(im_paths_)

    im_paths = bb_train + bb_test + query + gt_bb
    nums = [bb_train_num] + [bb_test_num] + [query_num] + [gt_bb_num]

    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