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. Retu
(self, im, window)
| 123 | return self.detect_windows(zip(image_fnames, windows_list)) |
| 124 | |
| 125 | def crop(self, im, window): |
| 126 | """ |
| 127 | Crop a window from the image for detection. Include surrounding context |
| 128 | according to the `context_pad` configuration. |
| 129 | |
| 130 | Parameters |
| 131 | ---------- |
| 132 | im: H x W x K image ndarray to crop. |
| 133 | window: bounding box coordinates as ymin, xmin, ymax, xmax. |
| 134 | |
| 135 | Returns |
| 136 | ------- |
| 137 | crop: cropped window. |
| 138 | """ |
| 139 | # Crop window from the image. |
| 140 | crop = im[window[0]:window[2], window[1]:window[3]] |
| 141 | |
| 142 | if self.context_pad: |
| 143 | box = window.copy() |
| 144 | crop_size = self.blobs[self.inputs[0]].width # assumes square |
| 145 | scale = crop_size / (1. * crop_size - self.context_pad * 2) |
| 146 | # Crop a box + surrounding context. |
| 147 | half_h = (box[2] - box[0] + 1) / 2. |
| 148 | half_w = (box[3] - box[1] + 1) / 2. |
| 149 | center = (box[0] + half_h, box[1] + half_w) |
| 150 | scaled_dims = scale * np.array((-half_h, -half_w, half_h, half_w)) |
| 151 | box = np.round(np.tile(center, 2) + scaled_dims) |
| 152 | full_h = box[2] - box[0] + 1 |
| 153 | full_w = box[3] - box[1] + 1 |
| 154 | scale_h = crop_size / full_h |
| 155 | scale_w = crop_size / full_w |
| 156 | pad_y = round(max(0, -box[0]) * scale_h) # amount out-of-bounds |
| 157 | pad_x = round(max(0, -box[1]) * scale_w) |
| 158 | |
| 159 | # Clip box to image dimensions. |
| 160 | im_h, im_w = im.shape[:2] |
| 161 | box = np.clip(box, 0., [im_h, im_w, im_h, im_w]) |
| 162 | clip_h = box[2] - box[0] + 1 |
| 163 | clip_w = box[3] - box[1] + 1 |
| 164 | assert(clip_h > 0 and clip_w > 0) |
| 165 | crop_h = round(clip_h * scale_h) |
| 166 | crop_w = round(clip_w * scale_w) |
| 167 | if pad_y + crop_h > crop_size: |
| 168 | crop_h = crop_size - pad_y |
| 169 | if pad_x + crop_w > crop_size: |
| 170 | crop_w = crop_size - pad_x |
| 171 | |
| 172 | # collect with context padding and place in input |
| 173 | # with mean padding |
| 174 | context_crop = im[box[0]:box[2], box[1]:box[3]] |
| 175 | context_crop = caffe.io.resize_image(context_crop, (crop_h, crop_w)) |
| 176 | crop = np.ones(self.crop_dims, dtype=np.float32) * self.crop_mean |
| 177 | crop[pad_y:(pad_y + crop_h), pad_x:(pad_x + crop_w)] = context_crop |
| 178 | |
| 179 | return crop |
| 180 | |
| 181 | def configure_crop(self, context_pad): |
| 182 | """ |
no test coverage detected