def keypoint_resize_random_crop(image, annos, mask=None, size=(<NUM_LIT>, <NUM_LIT>)):
if len(np.shape(image)) == <NUM_LIT:2>:<EOL><INDENT>image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)<EOL><DEDENT>def resize_image(image, annos, mask, target_width, target_height):<EOL><INDENT>"""<STR_LIT>"""<EOL>y, x, _ = np.shape(image)<EOL>ratio_y = target_height / y<EOL>ratio_x = target_width / x<EOL>new_joints = []<EOL>for people in annos:<EOL><INDENT>new_keypoints = []<EOL>for keypoints in people:<EOL><INDENT>if keypoints[<NUM_LIT:0>] < <NUM_LIT:0> or keypoints[<NUM_LIT:1>] < <NUM_LIT:0>:<EOL><INDENT>new_keypoints.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>pts = (int(keypoints[<NUM_LIT:0>] * ratio_x + <NUM_LIT:0.5>), int(keypoints[<NUM_LIT:1>] * ratio_y + <NUM_LIT:0.5>))<EOL>if pts[<NUM_LIT:0>] > target_width - <NUM_LIT:1> or pts[<NUM_LIT:1>] > target_height - <NUM_LIT:1>:<EOL><INDENT>new_keypoints.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>new_keypoints.append(pts)<EOL><DEDENT>new_joints.append(new_keypoints)<EOL><DEDENT>annos = new_joints<EOL>new_image = cv2.resize(image, (target_width, target_height), interpolation=cv2.INTER_AREA)<EOL>if mask is not None:<EOL><INDENT>new_mask = cv2.resize(mask, (target_width, target_height), interpolation=cv2.INTER_AREA)<EOL>return new_image, annos, new_mask<EOL><DEDENT>else:<EOL><INDENT>return new_image, annos, None<EOL><DEDENT><DEDENT>_target_height = size[<NUM_LIT:0>]<EOL>_target_width = size[<NUM_LIT:1>]<EOL>if len(np.shape(image)) == <NUM_LIT:2>:<EOL><INDENT>image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)<EOL><DEDENT>height, width, _ = np.shape(image)<EOL>if height <= width:<EOL><INDENT>ratio = _target_height / height<EOL>new_width = int(ratio * width)<EOL>if height == width:<EOL><INDENT>new_width = _target_height<EOL><DEDENT>image, annos, mask = resize_image(image, annos, mask, new_width, _target_height)<EOL>if new_width > _target_width:<EOL><INDENT>crop_range_x = np.random.randint(<NUM_LIT:0>, new_width - _target_width)<EOL><DEDENT>else:<EOL><INDENT>crop_range_x = <NUM_LIT:0><EOL><DEDENT>image = image[:, crop_range_x:crop_range_x + _target_width, :]<EOL>if mask is not None:<EOL><INDENT>mask = mask[:, crop_range_x:crop_range_x + _target_width]<EOL><DEDENT>new_joints = []<EOL>for people in annos:<EOL><INDENT>new_keypoints = []<EOL>for keypoints in people:<EOL><INDENT>if keypoints[<NUM_LIT:0>] < -<NUM_LIT:10> or keypoints[<NUM_LIT:1>] < -<NUM_LIT:10>:<EOL><INDENT>new_keypoints.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>top = crop_range_x + _target_width - <NUM_LIT:1><EOL>if keypoints[<NUM_LIT:0>] >= crop_range_x and keypoints[<NUM_LIT:0>] <= top:<EOL><INDENT>pts = (int(keypoints[<NUM_LIT:0>] - crop_range_x), int(keypoints[<NUM_LIT:1>]))<EOL><DEDENT>else:<EOL><INDENT>pts = (-<NUM_LIT:1000>, -<NUM_LIT:1000>)<EOL><DEDENT>new_keypoints.append(pts)<EOL><DEDENT>new_joints.append(new_keypoints)<EOL><DEDENT>annos = new_joints<EOL><DEDENT>if height > width:<EOL><INDENT>ratio = _target_width / width<EOL>new_height = int(ratio * height)<EOL>image, annos, mask = resize_image(image, annos, mask, _target_width, new_height)<EOL>if new_height > _target_height:<EOL><INDENT>crop_range_y = np.random.randint(<NUM_LIT:0>, new_height - _target_height)<EOL><DEDENT>else:<EOL><INDENT>crop_range_y = <NUM_LIT:0><EOL><DEDENT>image = image[crop_range_y:crop_range_y + _target_width, :, :]<EOL>if mask is not None:<EOL><INDENT>mask = mask[crop_range_y:crop_range_y + _target_width, :]<EOL><DEDENT>new_joints = []<EOL>for people in annos:  <EOL><INDENT>new_keypoints = []<EOL>for keypoints in people:<EOL><INDENT>if keypoints[<NUM_LIT:0>] < <NUM_LIT:0> or keypoints[<NUM_LIT:1>] < <NUM_LIT:0>:<EOL><INDENT>new_keypoints.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>bot = crop_range_y + _target_height - <NUM_LIT:1><EOL>if keypoints[<NUM_LIT:1>] >= crop_range_y and keypoints[<NUM_LIT:1>] <= bot:<EOL><INDENT>pts = (int(keypoints[<NUM_LIT:0>]), int(keypoints[<NUM_LIT:1>] - crop_range_y))<EOL><DEDENT>else:<EOL><INDENT>pts = (-<NUM_LIT:1000>, -<NUM_LIT:1000>)<EOL><DEDENT>new_keypoints.append(pts)<EOL><DEDENT>new_joints.append(new_keypoints)<EOL><DEDENT>annos = new_joints<EOL><DEDENT>if mask is not None:<EOL><INDENT>return image, annos, mask<EOL><DEDENT>else:<EOL><INDENT>return image, annos, None<EOL><DEDENT>
Reszie the image to make either its width or height equals to the given sizes.
    Then randomly crop image without influence scales.
    Resize the image match with the minimum size before cropping, this API will change the zoom scale of object.

    Parameters
    -----------
    image : 3 channel image
        The given image for augmentation.
    annos : list of list of floats
        The keypoints annotation of people.
    mask : single channel image or None
        The mask if available.
    size : tuple of int
        The size (height, width) of returned image.

    Returns
    ----------
    preprocessed image, annos, mask