parameters: (self, im, window)
return_statement: return crop
docstring: Crop a window from the image for detection. Include surrounding context
          according to the `context_pad` configuration.
  
          Parameters
          ----------
          im: H x W x K image ndarray to crop.
          window: bounding box coordinates as ymin, xmin, ymax, xmax.
  
          Returns
          -------
          crop: cropped window.
docstring_summary: Crop a window from the image for detection. Include surrounding context
          according to the `context_pad` configuration.
docstring_tokens: Crop a window from the image for detection . Include surrounding context according to the context_pad configuration .
function: def crop(self, im, window):
          """
          Crop a window from the image for detection. Include surrounding context
          according to the `context_pad` configuration.
  
          Parameters
          ----------
          im: H x W x K image ndarray to crop.
          window: bounding box coordinates as ymin, xmin, ymax, xmax.
  
          Returns
          -------
          crop: cropped window.
          """
          # Crop window from the image.
          crop = im[window[0]:window[2], window[1]:window[3]]
  
          if self.context_pad:
              box = window.copy()
              crop_size = self.blobs[self.inputs[0]].width  # assumes square
              scale = crop_size / (1. * crop_size - self.context_pad * 2)
              # Crop a box + surrounding context.
              half_h = (box[2] - box[0] + 1) / 2.
              half_w = (box[3] - box[1] + 1) / 2.
              center = (box[0] + half_h, box[1] + half_w)
              scaled_dims = scale * np.array((-half_h, -half_w, half_h, half_w))
              box = np.round(np.tile(center, 2) + scaled_dims)
              full_h = box[2] - box[0] + 1
              full_w = box[3] - box[1] + 1
              scale_h = crop_size / full_h
              scale_w = crop_size / full_w
              pad_y = round(max(0, -box[0]) * scale_h)  # amount out-of-bounds
              pad_x = round(max(0, -box[1]) * scale_w)
  
              # Clip box to image dimensions.
              im_h, im_w = im.shape[:2]
              box = np.clip(box, 0., [im_h, im_w, im_h, im_w])
              clip_h = box[2] - box[0] + 1
              clip_w = box[3] - box[1] + 1
              assert(clip_h > 0 and clip_w > 0)
              crop_h = round(clip_h * scale_h)
              crop_w = round(clip_w * scale_w)
              if pad_y + crop_h > crop_size:
                  crop_h = crop_size - pad_y
              if pad_x + crop_w > crop_size:
                  crop_w = crop_size - pad_x
  
              # collect with context padding and place in input
              # with mean padding
              context_crop = im[box[0]:box[2], box[1]:box[3]]
              context_crop = caffe.io.resize_image(context_crop, (crop_h, crop_w))
              crop = np.ones(self.crop_dims, dtype=np.float32) * self.crop_mean
              crop[pad_y:(pad_y + crop_h), pad_x:(pad_x + crop_w)] = context_crop
  
          return crop
function_tokens: def crop ( self , im , window ) : # Crop window from the image. crop = im [ window [ 0 ] : window [ 2 ] , window [ 1 ] : window [ 3 ] ] if self . context_pad : box = window . copy ( ) crop_size = self . blobs [ self . inputs [ 0 ] ] . width # assumes square scale = crop_size / ( 1. * crop_size - self . context_pad * 2 ) # Crop a box + surrounding context. half_h = ( box [ 2 ] - box [ 0 ] + 1 ) / 2. half_w = ( box [ 3 ] - box [ 1 ] + 1 ) / 2. center = ( box [ 0 ] + half_h , box [ 1 ] + half_w ) scaled_dims = scale * np . array ( ( - half_h , - half_w , half_h , half_w ) ) box = np . round ( np . tile ( center , 2 ) + scaled_dims ) full_h = box [ 2 ] - box [ 0 ] + 1 full_w = box [ 3 ] - box [ 1 ] + 1 scale_h = crop_size / full_h scale_w = crop_size / full_w pad_y = round ( max ( 0 , - box [ 0 ] ) * scale_h ) # amount out-of-bounds pad_x = round ( max ( 0 , - box [ 1 ] ) * scale_w ) # Clip box to image dimensions. im_h , im_w = im . shape [ : 2 ] box = np . clip ( box , 0. , [ im_h , im_w , im_h , im_w ] ) clip_h = box [ 2 ] - box [ 0 ] + 1 clip_w = box [ 3 ] - box [ 1 ] + 1 assert ( clip_h > 0 and clip_w > 0 ) crop_h = round ( clip_h * scale_h ) crop_w = round ( clip_w * scale_w ) if pad_y + crop_h > crop_size : crop_h = crop_size - pad_y if pad_x + crop_w > crop_size : crop_w = crop_size - pad_x # collect with context padding and place in input # with mean padding context_crop = im [ box [ 0 ] : box [ 2 ] , box [ 1 ] : box [ 3 ] ] context_crop = caffe . io . resize_image ( context_crop , ( crop_h , crop_w ) ) crop = np . ones ( self . crop_dims , dtype = np . float32 ) * self . crop_mean crop [ pad_y : ( pad_y + crop_h ) , pad_x : ( pad_x + crop_w ) ] = context_crop return crop