コード例 #1
0
    def __call__(self, img: np.ndarray, data: dict):
        boxes = data['gt_boxes']
        labels = data['gt_labels']
        scores = data['gt_scores']

        if len(boxes) == 0:
            return img, data
        if not self.constraints:
            self.constraints = [(0.1, 1.0), (0.3, 1.0), (0.5, 1.0), (0.7, 1.0),
                                (0.9, 1.0), (0.0, 1.0)]

        img = PIL.Image.fromarray(img)
        w, h = img.size
        crops = [(0, 0, w, h)]
        for min_iou, max_iou in self.constraints:
            for _ in range(self.max_trial):
                scale = random.uniform(self.scales[0], self.scales[1])
                aspect_ratio = random.uniform(max(1 / self.max_ratio, scale * scale), \
                                              min(self.max_ratio, 1 / scale / scale))
                crop_h = int(h * scale / np.sqrt(aspect_ratio))
                crop_w = int(w * scale * np.sqrt(aspect_ratio))
                crop_x = random.randrange(w - crop_w)
                crop_y = random.randrange(h - crop_h)
                crop_box = np.array([[(crop_x + crop_w / 2.0) / w,
                                      (crop_y + crop_h / 2.0) / h,
                                      crop_w / float(w), crop_h / float(h)]])
                iou = box_iou_xywh(crop_box, boxes)
                if min_iou <= iou.min() and max_iou >= iou.max():
                    crops.append((crop_x, crop_y, crop_w, crop_h))
                    break

        while crops:
            crop = crops.pop(np.random.randint(0, len(crops)))
            crop_boxes, crop_labels, crop_scores, box_num = box_crop(
                boxes, labels, scores, crop, (w, h))

            if box_num < 1:
                continue
            img = img.crop((crop[0], crop[1], crop[0] + crop[2],
                            crop[1] + crop[3])).resize(img.size,
                                                       PIL.Image.LANCZOS)
            img = np.asarray(img)
            data['gt_boxes'] = crop_boxes
            data['gt_labels'] = crop_labels
            data['gt_scores'] = crop_scores
            return img, data

        img = np.asarray(img)
        data['gt_boxes'] = boxes
        data['gt_labels'] = labels
        data['gt_scores'] = scores
        return img, data
コード例 #2
0
ファイル: utils.py プロジェクト: yanzhangnlp/LDGCNs
def crop_resize_image(image: np.ndarray, size) -> np.ndarray:
    """
    Resize the input image.

    :param image: Original image which is a  PIL object.
    :param size: Tuple of height and width to resize the image to.
    :return: Resized image which is a PIL object
    """
    width, height = image.size
    if width > height:
        left = (width - height) / 2
        right = width - left
        top = 0
        bottom = height
    else:
        top = (height - width) / 2
        bottom = height - top
        left = 0
        right = width
    image = image.crop((left, top, right, bottom))
    image = image.resize(size, Image.ANTIALIAS)
    return image
コード例 #3
0
ファイル: utils.py プロジェクト: lagka/sockeye
def crop_resize_image(image: np.ndarray, size) -> np.ndarray:
    """
    Resize the input image.

    :param image: Original image which is a  PIL object.
    :param size: Tuple of height and width to resize the image to.
    :return: Resized image which is a PIL object
    """
    width, height = image.size
    if width > height:
        left = (width - height) / 2
        right = width - left
        top = 0
        bottom = height
    else:
        top = (height - width) / 2
        bottom = height - top
        left = 0
        right = width
    image = image.crop((left, top, right, bottom))
    image = image.resize(size, Image.ANTIALIAS)
    return image
コード例 #4
0
ファイル: image_util.py プロジェクト: stfnwong/lernomatic
def crop(img: np.ndarray, x_pos: int, y_pos: int, size: int) -> np.ndarray:
    ow, oh = img.size
    if (ow > size or oh > size):
        return img.crop((x_pos, y_pos, x_pos + size, y_pos + size))

    return img