transform_cuhk03_1.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. def save_images(mat_file, save_dir, new_im_name_tmpl):
  2. def deref(mat, ref):
  3. return mat[ref][:].T
  4. def dump(mat, refs, pid, cam, im_dir):
  5. """Save the images of a person under one camera."""
  6. for i, ref in enumerate(refs):
  7. im = deref(mat, ref)
  8. if im.size == 0 or im.ndim < 2: break
  9. fname = new_im_name_tmpl.format(pid, cam, i)
  10. imsave(osp.join(im_dir, fname), im)
  11. mat = h5py.File(mat_file, 'r')
  12. labeled_im_dir = osp.join(save_dir, 'labeled/images')
  13. detected_im_dir = osp.join(save_dir, 'detected/images')
  14. all_im_dir = osp.join(save_dir, 'all/images')
  15. may_make_dir(labeled_im_dir)
  16. may_make_dir(detected_im_dir)
  17. may_make_dir(all_im_dir)
  18. # loop through camera pairs
  19. pid = 0
  20. for labeled, detected in zip(mat['labeled'][0], mat['detected'][0]):
  21. labeled, detected = deref(mat, labeled), deref(mat, detected)
  22. assert labeled.shape == detected.shape
  23. # loop through ids in a camera pair
  24. for i in range(labeled.shape[0]):
  25. # We don't care about whether different persons are under same cameras,
  26. # we only care about the same person being under different cameras or not.
  27. dump(mat, labeled[i, :5], pid, 0, labeled_im_dir)
  28. dump(mat, labeled[i, 5:], pid, 1, labeled_im_dir)
  29. dump(mat, detected[i, :5], pid, 0, detected_im_dir)
  30. dump(mat, detected[i, 5:], pid, 1, detected_im_dir)
  31. dump(mat, chain(detected[i, :5], labeled[i, :5]), pid, 0, all_im_dir)
  32. dump(mat, chain(detected[i, 5:], labeled[i, 5:]), pid, 1, all_im_dir)
  33. pid += 1
  34. if pid % 100 == 0:
  35. sys.stdout.write('\033[F\033[K')
  36. print('Saving images {}/{}'.format(pid, 1467))