def _full_image_crop(image, detections):
    detections = detections.copy()
    height, width = image.shape[0:2]

    max_hw = max(height, width)
    center = [height // 2, width // 2]
    size = [max_hw, max_hw]

    image, border, offset = crop_image(image, center, size)
    detections[:, 0:4:2] += border[2]
    detections[:, 1:4:2] += border[0]
    return image, detections
Exemple #2
0
def make_proof(img, session, size, loose):
    path = image.crop_image(img.jpg, size)
    name = os.path.basename(path[0])
    thumb_name = os.path.basename(path[1])
    proof_path = f"{session.path}/proof/{img.name}/{name}"
    thumb_path = f"{session.path}/proof/{img.name}/{thumb_name}"
    if loose is not True:
        proof_path = f"{session.path}/proof/{img.name}/proof_{name}"
        thumb_path = f"{session.path}/proof/{img.name}/proof_{thumb_name}"
    for p in path:
        p_name = os.path.basename(p)
        p_path = f"{session.path}/proof/{img.name}/{p_name}"
        if loose is not True:
            p_path = f"{session.path}/proof/{img.name}/proof_{p_name}"
        os.rename(p, p_path)
    return name, proof_path, thumb_path
    def __getitem__(self, index):
        img_id = self.images[index]
        image = cv2.imread(
            os.path.join(self.img_dir,
                         self.coco.loadImgs(ids=[img_id])[0]['file_name']))
        height, width = image.shape[0:2]

        out = {}
        for scale in self.test_scales:
            new_height = int(height * scale)
            new_width = int(width * scale)

            in_height = new_height | 127
            in_width = new_width | 127

            fmap_height, fmap_width = (in_height + 1) // self.down_ratio, (
                in_width + 1) // self.down_ratio
            height_ratio = fmap_height / in_height
            width_ratio = fmap_width / in_width

            resized_image = cv2.resize(image, (new_width, new_height))
            resized_image, border, offset = crop_image(
                image=resized_image,
                center=[new_height // 2, new_width // 2],
                new_size=[in_height, in_width])

            resized_image = resized_image / 255.
            resized_image -= self.mean
            resized_image /= self.std
            resized_image = resized_image.transpose(
                (2, 0, 1))[None, :, :, :]  # [H, W, C] to [C, H, W]

            if self.test_flip:
                resized_image = np.concatenate(
                    (resized_image, resized_image[..., ::-1].copy()), axis=0)

            out[scale] = {
                'image': resized_image,
                'border': border,
                'size': [new_height, new_width],
                'fmap_size': [fmap_height, fmap_width],
                'ratio': [height_ratio, width_ratio]
            }

        return img_id, out
    def __getitem__(self, index):
        img_id = self.images[index]
        image = cv2.imread(
            os.path.join(self.img_dir,
                         self.coco.loadImgs(ids=[img_id])[0]['file_name']))
        annotations = self.coco.loadAnns(ids=self.coco.getAnnIds(
            imgIds=[img_id]))

        labels = np.array(
            [self.cat_ids[anno['category_id']] for anno in annotations])
        bboxes = np.array([anno['bbox'] for anno in annotations])
        if len(bboxes) == 0:
            bboxes = np.array([[0., 0., 0., 0.]], dtype=np.float32)
            labels = np.array([0])
        bboxes[:, 2:] += bboxes[:, :2]  # xywh to xyxy

        sorted_inds = np.argsort(labels, axis=0)
        bboxes = bboxes[sorted_inds]
        labels = labels[sorted_inds]

        # random crop (for training) or center crop (for validation)
        if self.split == 'train':
            image, bboxes = random_crop(image,
                                        bboxes,
                                        random_scales=self.rand_scales,
                                        new_size=self.img_size,
                                        padding=self.padding)
        else:
            image, border, offset = crop_image(
                image,
                center=[image.shape[0] // 2, image.shape[1] // 2],
                new_size=[max(image.shape[0:2]),
                          max(image.shape[0:2])])
            bboxes[:, 0::2] += border[2]
            bboxes[:, 1::2] += border[0]

        # resize image and bbox
        height, width = image.shape[:2]
        image = cv2.resize(image, (self.img_size['w'], self.img_size['h']))
        bboxes[:, 0::2] *= self.img_size['w'] / width
        bboxes[:, 1::2] *= self.img_size['h'] / height

        # discard non-valid bboxes
        bboxes[:, 0::2] = np.clip(bboxes[:, 0::2], 0, self.img_size['w'] - 1)
        bboxes[:, 1::2] = np.clip(bboxes[:, 1::2], 0, self.img_size['h'] - 1)
        keep_inds = np.logical_and((bboxes[:, 2] - bboxes[:, 0]) > 0,
                                   (bboxes[:, 3] - bboxes[:, 1]) > 0)
        bboxes = bboxes[keep_inds]
        labels = labels[keep_inds]

        # randomly flip image and bboxes
        if self.split == 'train' and np.random.uniform() > 0.5:
            image[:] = image[:, ::-1, :]
            bboxes[:, [0, 2]] = image.shape[1] - bboxes[:, [2, 0]] - 1

        # # ----------------------------- debug -----------------------------------------
        # plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
        # plt.show()
        # plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
        # for lab, bbox in zip(labels, bboxes):
        #   plt.gca().add_patch(Rectangle(bbox[:2], bbox[2] - bbox[0], bbox[3] - bbox[1],
        #                                 linewidth=1, edgecolor='r', facecolor='none'))
        #   plt.text(bbox[0], bbox[1], self.class_name[lab + 1],
        #            bbox=dict(facecolor='b', alpha=0.5), fontsize=7, color='w')
        # plt.show()
        # # -----------------------------------------------------------------------------

        image = image.astype(np.float32) / 255.

        # randomly change color and lighting
        if self.split == 'train':
            color_jittering_(self.data_rng, image)
            lighting_(self.data_rng, image, 0.1, self.eig_val, self.eig_vec)

        image -= self.mean
        image /= self.std
        image = image.transpose((2, 0, 1))  # [H, W, C] to [C, H, W]

        hmap_tl = np.zeros(
            (self.num_classes, self.fmap_size['h'], self.fmap_size['w']),
            dtype=np.float32)
        hmap_br = np.zeros(
            (self.num_classes, self.fmap_size['h'], self.fmap_size['w']),
            dtype=np.float32)

        regs_tl = np.zeros((self.max_objs, 2), dtype=np.float32)
        regs_br = np.zeros((self.max_objs, 2), dtype=np.float32)

        inds_tl = np.zeros((self.max_objs, ), dtype=np.int64)
        inds_br = np.zeros((self.max_objs, ), dtype=np.int64)

        num_objs = np.array(min(bboxes.shape[0], self.max_objs))
        ind_masks = np.zeros((self.max_objs, ), dtype=np.uint8)
        ind_masks[:num_objs] = 1

        for i, ((xtl, ytl, xbr, ybr), label) in enumerate(zip(bboxes, labels)):
            fxtl = (xtl * self.fmap_size['w'] / self.img_size['w'])
            fytl = (ytl * self.fmap_size['h'] / self.img_size['h'])
            fxbr = (xbr * self.fmap_size['w'] / self.img_size['w'])
            fybr = (ybr * self.fmap_size['h'] / self.img_size['h'])

            ixtl = int(fxtl)
            iytl = int(fytl)
            ixbr = int(fxbr)
            iybr = int(fybr)

            if self.gaussian:
                width = xbr - xtl
                height = ybr - ytl

                width = math.ceil(width * self.fmap_size['w'] /
                                  self.img_size['w'])
                height = math.ceil(height * self.fmap_size['h'] /
                                   self.img_size['h'])

                radius = max(
                    0, int(gaussian_radius((height, width),
                                           self.gaussian_iou)))

                draw_gaussian(hmap_tl[label], [ixtl, iytl], radius)
                draw_gaussian(hmap_br[label], [ixbr, iybr], radius)
            else:
                hmap_tl[label, iytl, ixtl] = 1
                hmap_br[label, iybr, ixbr] = 1

            regs_tl[i, :] = [fxtl - ixtl, fytl - iytl]
            regs_br[i, :] = [fxbr - ixbr, fybr - iybr]
            inds_tl[i] = iytl * self.fmap_size['w'] + ixtl
            inds_br[i] = iybr * self.fmap_size['w'] + ixbr

        return {
            'image': image,
            'hmap_tl': hmap_tl,
            'hmap_br': hmap_br,
            'regs_tl': regs_tl,
            'regs_br': regs_br,
            'inds_tl': inds_tl,
            'inds_br': inds_br,
            'ind_masks': ind_masks
        }
Exemple #5
0
    def __getitem__(self, index):
        img_id = self.img_paths[index]
        img_set, img_vid, img_name = img_id.split("_", 2)
        img_name = img_name.replace("txt", "jpg")
        img_path = os.path.join(self.img_dir, img_set, img_vid)
        img_rgb = cv2.imread(os.path.join(img_path, "visible", img_name),
                             cv2.IMREAD_COLOR)
        img_ir = cv2.imread(os.path.join(img_path, "lwir", img_name),
                            cv2.IMREAD_GRAYSCALE)
        height, width = img_rgb.shape[0:2]

        out = {}
        for scale in self.test_scales:
            new_height = int(height * scale)
            new_width = int(width * scale)

            in_height = new_height | 127
            in_width = new_width | 127

            fmap_height, fmap_width = (in_height + 1) // self.down_ratio, (
                in_width + 1) // self.down_ratio
            height_ratio = fmap_height / in_height
            width_ratio = fmap_width / in_width

            resized_img_rgb = cv2.resize(img_rgb, (new_width, new_height))
            resized_img_rgb, border, offset = crop_image(
                image=resized_img_rgb,
                center=[new_height // 2, new_width // 2],
                channel=3,
                new_size=[in_height, in_width])

            resized_img_rgb = resized_img_rgb / 255.
            resized_img_rgb -= self.mean[0, 0, :3]
            resized_img_rgb /= self.std[0, 0, :3]
            resized_img_rgb = resized_img_rgb.transpose(
                (2, 0, 1))[None, :, :, :]  # [H, W, C] to [C, H, W]

            resized_img_ir = cv2.resize(img_ir, (new_width, new_height))
            resized_img_ir = np.expand_dims(resized_img_ir, axis=2)
            resized_img_ir, border, offset = crop_image(
                image=resized_img_ir,
                center=[new_height // 2, new_width // 2],
                channel=1,
                new_size=[in_height, in_width])
            resized_img_ir = resized_img_ir / 255.
            resized_img_ir -= self.mean[0, 0, 3]
            resized_img_ir /= self.std[0, 0, 3]
            resized_img_ir = resized_img_ir.transpose(
                (2, 0, 1))[None, :, :, :]  # [H, W, C] to [C, H, W]

            if self.test_flip:
                resized_img_rgb = np.concatenate(
                    (resized_img_rgb, resized_img_rgb[..., ::-1].copy()),
                    axis=0)
                resized_img_ir = np.concatenate(
                    (resized_img_ir, resized_img_ir[..., ::-1].copy()), axis=0)

            out[scale] = {
                'img_rgb': resized_img_rgb,
                'img_ir': resized_img_ir,
                'border': border,
                'size': [new_height, new_width],
                'fmap_size': [fmap_height, fmap_width],
                'ratio': [height_ratio, width_ratio]
            }

        return img_id, out
def kp_detection(net, img):
    """
    get detection
    Args:
        net:
        img_file:

    Returns: a dict {img_file: {cls1: }}

    """
    K = 100

    width_scale = img.shape[1] / input_size[1]
    height_scale = img.shape[0] / input_size[0]

    # >> resize
    img = cv2.resize(img, input_size)
    height, width = img.shape[0:2]
    top_bboxes = {}

    detections = []

    for scale in scales:
        new_height = int(height * scale)
        new_width = int(width * scale)
        new_center = np.array([new_height // 2, new_width // 2])

        inp_height = new_height | 127
        inp_width = new_width | 127

        images = np.zeros((1, 3, inp_height, inp_width), dtype=np.float32)
        ratios = np.zeros((1, 2), dtype=np.float32)
        borders = np.zeros((1, 4), dtype=np.float32)
        sizes = np.zeros((1, 2), dtype=np.float32)

        out_height, out_width = (inp_height + 1) // 4, (inp_width + 1) // 4
        height_ratio = out_height / inp_height
        width_ratio = out_width / inp_width

        resized_image = cv2.resize(img, (new_width, new_height))
        resized_image, border, offset = crop_image(resized_image, new_center, [inp_height, inp_width])

        resized_image = resized_image / 255.
        images[0] = resized_image.transpose((2, 0, 1))
        borders[0] = border
        sizes[0] = [int(height * scale), int(width * scale)]
        ratios[0] = [height_ratio, width_ratio]

        images = np.concatenate((images, images[:, :, :, ::-1]), axis=0)
        images = torch.from_numpy(images)
        images = images.cuda()
        dets = kp_decode(net, images, K)
        dets = dets.reshape(2, -1, 8)
        dets[1, :, [0, 2]] = out_width - dets[1, :, [2, 0]]
        dets = dets.reshape(1, -1, 8)

        _rescale_dets(dets, ratios, borders, sizes)
        dets[:, :, 0:4] /= scale
        detections.append(dets)

    detections = np.concatenate(detections, axis=1)

    classes = detections[..., -1]
    classes = classes[0]
    detections = detections[0]

    # reject detections with negative scores
    keep_inds = (detections[:, 4] > -1)
    detections = detections[keep_inds]
    classes = classes[keep_inds]

    for j in range(categories):
        keep_inds = (classes == j)
        top_bboxes[j + 1] = detections[keep_inds][:, 0:7].astype(np.float32)
        soft_nms(top_bboxes[j + 1], Nt=nms_threshold, method=nms_algorithm)
        top_bboxes[j + 1] = top_bboxes[j + 1][:, 0:5]
        top_bboxes[j + 1][:, 0:4:2] *= width_scale
        top_bboxes[j + 1][:, 1:4:2] *= height_scale

        top_bboxes[j + 1] = top_bboxes[j + 1][top_bboxes[j + 1][:, -1] > 0.5]

    return top_bboxes