def crop_image()

in utilities/misc.py [0:0]


def crop_image(im, joints, crop_size, fillvalue=[0]):
  """
  joints: list of 21x2 2D joint locations per each hand
  crops the im into a crop_size square centered at the mean of all joint
  locations
  returns cropped image and top-left pixel position of the crop in the full image
  """
  if im.ndim < 3:
    im = im[:, :, np.newaxis]
  if isinstance(fillvalue, list) or isinstance(fillvalue, np.ndarray):
    fillvalue = np.asarray(fillvalue).astype(im.dtype)
  else:
    fillvalue = np.asarray([fillvalue for _ in im.shape[2]]).astype(im.dtype)

  joints = np.vstack([j for j in joints if j is not None])
  bbcenter = np.round(np.mean(joints, axis=0)).astype(np.int)
  im_crop = np.zeros((crop_size, crop_size, im.shape[2]), dtype=im.dtype)
  tl = bbcenter - crop_size//2
  br = bbcenter + crop_size//2
  tl_crop = np.asarray([0, 0], dtype=np.int)
  br_crop = np.asarray([crop_size, crop_size], dtype=np.int)
  tl_spill = np.minimum(0, tl)
  tl -= tl_spill
  tl_crop -= tl_spill
  br_spill = np.maximum(0, br-np.array([im.shape[1], im.shape[0]]))
  br -= br_spill
  br_crop -= br_spill
  im_crop[tl_crop[1]:br_crop[1], tl_crop[0]:br_crop[0], :] = \
    im[tl[1]:br[1], tl[0]:br[0], :]
  return im_crop.squeeze(), tl