code: def __get_crop_image(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], :] = 0
                  # print('find mask', m, file=sys.stderr)
          new_rec = roi_rec.copy()
  
          # choose one gt randomly
          SIZE = config.SCALES[0][0]
          TARGET_BOX_SCALES = np.array([16, 32, 64, 128, 256, 512])
          assert roi_rec["boxes"].shape[0] > 0
          candidates = []
          for i in range(roi_rec["boxes"].shape[0]):
              box = roi_rec["boxes"][i]
              box_size = max(box[2] - box[0], box[3] - box[1])
              if box_size < config.TRAIN.MIN_BOX_SIZE:
                  continue
              # if box[0]<0 or box[1]<0:
              #  continue
              # if box[2]>im.shape[1] or box[3]>im.shape[0]:
              #  continue;
              candidates.append(i)
          assert len(candidates) > 0
          box_ind = random.choice(candidates)
          box = roi_rec["boxes"][box_ind]
          box_size = max(box[2] - box[0], box[3] - box[1])
          dist = np.abs(TARGET_BOX_SCALES - box_size)
          nearest = np.argmin(dist)
          target_ind = random.randrange(
              min(len(TARGET_BOX_SCALES), nearest + 2)
          )
          target_box_size = TARGET_BOX_SCALES[target_ind]
          im_scale = float(target_box_size) / box_size
          # min_scale = float(SIZE)/np.min(im.shape[0:2])
          # if im_scale<min_scale:
          #  im_scale = min_scale
          im = cv2.resize(
              im,
              None,
              None,
              fx=im_scale,
              fy=im_scale,
              interpolation=cv2.INTER_LINEAR,
          )
          new_rec["boxes"] = roi_rec["boxes"].copy() * im_scale
          box_scale = new_rec["boxes"][box_ind].copy().astype(np.int)
          ul_min = box_scale[2:4] - SIZE
          ul_max = box_scale[0:2]
          assert ul_min[0] <= ul_max[0]
          assert ul_min[1] <= ul_max[1]
          # print('ul', ul_min, ul_max, box)
          up, left = np.random.randint(
              ul_min[1], ul_max[1] + 1
          ), np.random.randint(ul_min[0], ul_max[0] + 1)
          # print('box', box, up, left)
          M = [
              [1.0, 0.0, -left],
              [0.0, 1.0, -up],
          ]
          M = np.array(M)
          im = cv2.warpAffine(
              im, M, (SIZE, SIZE), borderValue=tuple(config.PIXEL_MEANS)
          )
          # tbox = np.array([left, left+SIZE, up, up+SIZE], dtype=np.int)
          # im_new = np.zeros( (SIZE, SIZE,3), dtype=im.dtype)
          # for i in range(3):
          #  im_new[:,:,i] = config.PIXEL_MEANS[i]
          new_rec["boxes"][:, 0] -= left
          new_rec["boxes"][:, 2] -= left
          new_rec["boxes"][:, 1] -= up
          new_rec["boxes"][:, 3] -= up
          box_trans = new_rec["boxes"][box_ind].copy().astype(np.int)
          # print('sel box', im_scale, box, box_scale, box_trans, file=sys.stderr)
          # print('before', new_rec['boxes'].shape[0])
          boxes_new = []
          classes_new = []
          for i in range(new_rec["boxes"].shape[0]):
              box = new_rec["boxes"][i]
              box_size = max(box[2] - box[0], box[3] - box[1])
              center = (
                  np.array(([box[0], box[1]] + [box[2], box[3]])) / 2
              )
              if (
                  center[0] < 0
                  or center[1] < 0
                  or center[0] >= im.shape[1]
                  or center[1] >= im.shape[0]
              ):
                  continue
              if box_size < config.TRAIN.MIN_BOX_SIZE:
                  continue
              boxes_new.append(box)
              classes_new.append(new_rec["gt_classes"][i])
          new_rec["boxes"] = np.array(boxes_new)
          new_rec["gt_classes"] = np.array(classes_new)
          # print('after', new_rec['boxes'].shape[0])
          # assert new_rec['boxes'].shape[0]>0
          DEBUG = True
          if DEBUG:
              global TMP_ID
              if TMP_ID < 10:
                  tim = im.copy()
                  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,
                      )
                  filename = "./trainimages/train%d.png" % TMP_ID
                  TMP_ID += 1
                  cv2.imwrite(filename, tim)
  
          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"] = im_info
          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 6 for calling a global function with example usage: __get_crop_image(roidb) and returns: processed_ims,  processed_roidb