code: def get_crop_image1(roidb):
      """
      preprocess image and return processed roidb
      :param roidb: a list of roidb
      :return: list of img as in mxnet format
      roidb add new item['im_info']
      0 --- x (width, second dim of im)
      |
      y (height, first dim of im)
      """
      # roidb and each roi_rec can not be changed as it will be reused in next epoch
      num_images = len(roidb)
      processed_ims = []
      processed_roidb = []
      for i in range(num_images):
          roi_rec = roidb[i]
          if "stream" in roi_rec:
              im = cv2.imdecode(roi_rec["stream"], cv2.IMREAD_COLOR)
          else:
              assert os.path.exists(
                  roi_rec["image"]
              ), "{} does not exist".format(roi_rec["image"])
              im = cv2.imread(roi_rec["image"])
          if roidb[i]["flipped"]:
              im = im[:, ::-1, :]
          if "boxes_mask" in roi_rec:
              # im = im.astype(np.float32)
              boxes_mask = roi_rec["boxes_mask"].copy()
              boxes_mask = boxes_mask.astype(np.int)
              for j in range(boxes_mask.shape[0]):
                  m = boxes_mask[j]
                  im[m[1] : m[3], m[0] : m[2], :] = 127
                  # print('find mask', m, file=sys.stderr)
          SIZE = config.SCALES[0][0]
          PRE_SCALES = [0.3, 0.45, 0.6, 0.8, 1.0]
          # PRE_SCALES = [0.3, 0.45, 0.6, 0.8, 1.0, 0.8, 1.0, 0.8, 1.0]
          _scale = random.choice(PRE_SCALES)
          # _scale = np.random.uniform(PRE_SCALES[0], PRE_SCALES[-1])
          size = int(np.min(im.shape[0:2]) * _scale)
          # size = int(np.round(_scale*np.min(im.shape[0:2])))
          im_scale = float(SIZE) / size
          # origin_im_scale = im_scale
          # size = np.round(np.min(im.shape[0:2])*im_scale)
          # im_scale *= (float(SIZE)/size)
          origin_shape = im.shape
          if _scale > 10.0:  # avoid im.size<SIZE, never?
              sizex = int(np.round(im.shape[1] * im_scale))
              sizey = int(np.round(im.shape[0] * im_scale))
              if sizex < SIZE:
                  sizex = SIZE
                  print("keepx", sizex)
              if sizey < SIZE:
                  sizey = SIZE
                  print("keepy", sizex)
              im = cv2.resize(
                  im, (sizex, sizey), interpolation=cv2.INTER_LINEAR
              )
          else:
              im = cv2.resize(
                  im,
                  None,
                  None,
                  fx=im_scale,
                  fy=im_scale,
                  interpolation=cv2.INTER_LINEAR,
              )
  
          assert im.shape[0] >= SIZE and im.shape[1] >= SIZE
          # print('image size', origin_shape, _scale, SIZE, size, im_scale)
  
          new_rec = roi_rec.copy()
          new_rec["boxes"] = roi_rec["boxes"].copy() * im_scale
          if config.FACE_LANDMARK:
              new_rec["landmarks"] = roi_rec["landmarks"].copy()
              new_rec["landmarks"][:, :, 0:2] *= im_scale
          retry = 0
          LIMIT = 25
          size = SIZE
          while retry < LIMIT:
              up, left = (
                  np.random.randint(0, im.shape[0] - size + 1),
                  np.random.randint(0, im.shape[1] - size + 1),
              )
              boxes_new = new_rec["boxes"].copy()
              im_new = im[up : (up + size), left : (left + size), :]
              # print('crop', up, left, size, im_scale)
              boxes_new[:, 0] -= left
              boxes_new[:, 2] -= left
              boxes_new[:, 1] -= up
              boxes_new[:, 3] -= up
              if config.FACE_LANDMARK:
                  landmarks_new = new_rec["landmarks"].copy()
                  landmarks_new[:, :, 0] -= left
                  landmarks_new[:, :, 1] -= up
                  # for i in range(0,10,2):
                  #  landmarks_new[:,i] -= left
                  # for i in range(1,10,2):
                  #  landmarks_new[:,i] -= up
                  valid_landmarks = []
              # im_new = cv2.resize(im_new, (SIZE, SIZE), interpolation=cv2.INTER_LINEAR)
              # boxes_new *= im_scale
              # print(origin_shape, im_new.shape, im_scale)
              valid = []
              valid_boxes = []
              for i in range(boxes_new.shape[0]):
                  box = boxes_new[i]
                  # center = np.array(([box[0], box[1]]+[box[2], box[3]]))/2
                  centerx = (box[0] + box[2]) / 2
                  centery = (box[1] + box[3]) / 2
  
                  # box[0] = max(0, box[0])
                  # box[1] = max(0, box[1])
                  # box[2] = min(im_new.shape[1], box[2])
                  # box[3] = min(im_new.shape[0], box[3])
                  box_size = max(box[2] - box[0], box[3] - box[1])
  
                  if (
                      centerx < 0
                      or centery < 0
                      or centerx >= im_new.shape[1]
                      or centery >= im_new.shape[0]
                  ):
                      continue
                  if box_size < config.TRAIN.MIN_BOX_SIZE:
                      continue
                  # filter by landmarks? TODO
                  valid.append(i)
                  valid_boxes.append(box)
                  if config.FACE_LANDMARK:
                      valid_landmarks.append(landmarks_new[i])
              if len(valid) > 0 or retry == LIMIT - 1:
                  im = im_new
                  new_rec["boxes"] = np.array(valid_boxes)
                  new_rec["gt_classes"] = new_rec["gt_classes"][valid]
                  if config.FACE_LANDMARK:
                      new_rec["landmarks"] = np.array(valid_landmarks)
                  if config.HEAD_BOX:
                      face_box = new_rec["boxes"]
                      head_box = expand_bboxes(
                          face_box,
                          image_width=im.shape[1],
                          image_height=im.shape[0],
                      )
                      new_rec["boxes_head"] = np.array(head_box)
                  break
  
              retry += 1
  
          if config.COLOR_MODE > 0 and config.COLOR_JITTERING > 0.0:
              im = im.astype(np.float32)
              im = color_aug(im, config.COLOR_JITTERING)
  
          # assert np.all(new_rec['landmarks'][:,10]>0.0)
          global TMP_ID
          if TMP_ID >= 0 and TMP_ID < 10:
              tim = im.copy().astype(np.uint8)
              for i in range(new_rec["boxes"].shape[0]):
                  box = new_rec["boxes"][i].copy().astype(np.int)
                  cv2.rectangle(
                      tim,
                      (box[0], box[1]),
                      (box[2], box[3]),
                      (255, 0, 0),
                      1,
                  )
                  print("draw box:", box)
              if config.FACE_LANDMARK:
                  for i in range(new_rec["landmarks"].shape[0]):
                      landmark = new_rec["landmarks"][i].copy()
                      if landmark[0][2] < 0:
                          print("zero", landmark)
                          continue
                      landmark = landmark.astype(np.int)
                      print("draw landmark", landmark)
                      for k in range(5):
                          color = (0, 0, 255)
                          if k == 0 or k == 3:
                              color = (0, 255, 0)
                          pp = (landmark[k][0], landmark[k][1])
                          cv2.circle(tim, (pp[0], pp[1]), 1, color, 2)
              filename = "./trainimages/train%d.png" % TMP_ID
              print("write", filename)
              cv2.imwrite(filename, tim)
              TMP_ID += 1
  
          im_tensor = transform(
              im,
              config.PIXEL_MEANS,
              config.PIXEL_STDS,
              config.PIXEL_SCALE,
          )
  
          processed_ims.append(im_tensor)
          # print('boxes', new_rec['boxes'], file=sys.stderr)
          im_info = [im_tensor.shape[2], im_tensor.shape[3], im_scale]
          new_rec["im_info"] = np.array(im_info, dtype=np.float32)
          processed_roidb.append(new_rec)
      return processed_ims, processed_roidb
  
imports: from __future__ import print_function
  import numpy
  import cv2
  import os
  import math
  import sys
  import random
  from ..config import config
label_desc: Use image node_id 8 for calling a global function with example usage: get_crop_image1(roidb) and returns: processed_ims,  processed_roidb