def save_images(mat_file, save_dir, new_im_name_tmpl): def deref(mat, ref): return mat[ref][:].T def dump(mat, refs, pid, cam, im_dir): """Save the images of a person under one camera.""" for i, ref in enumerate(refs): im = deref(mat, ref) if im.size == 0 or im.ndim < 2: break fname = new_im_name_tmpl.format(pid, cam, i) imsave(osp.join(im_dir, fname), im) mat = h5py.File(mat_file, 'r') labeled_im_dir = osp.join(save_dir, 'labeled/images') detected_im_dir = osp.join(save_dir, 'detected/images') all_im_dir = osp.join(save_dir, 'all/images') may_make_dir(labeled_im_dir) may_make_dir(detected_im_dir) may_make_dir(all_im_dir) # loop through camera pairs pid = 0 for labeled, detected in zip(mat['labeled'][0], mat['detected'][0]): labeled, detected = deref(mat, labeled), deref(mat, detected) assert labeled.shape == detected.shape # loop through ids in a camera pair for i in range(labeled.shape[0]): # We don't care about whether different persons are under same cameras, # we only care about the same person being under different cameras or not. dump(mat, labeled[i, :5], pid, 0, labeled_im_dir) dump(mat, labeled[i, 5:], pid, 1, labeled_im_dir) dump(mat, detected[i, :5], pid, 0, detected_im_dir) dump(mat, detected[i, 5:], pid, 1, detected_im_dir) dump(mat, chain(detected[i, :5], labeled[i, :5]), pid, 0, all_im_dir) dump(mat, chain(detected[i, 5:], labeled[i, 5:]), pid, 1, all_im_dir) pid += 1 if pid % 100 == 0: sys.stdout.write('\033[F\033[K') print('Saving images {}/{}'.format(pid, 1467))