Example #1
0
    def __call__(self, img, boxes, labels, masks=None):
        if self.color_choose == 0:
            if random.uniform() < self.gray_p:
                gray = mmcv.bgr2gray(img)
                img = mmcv.gray2bgr(gray)
                return img, boxes, labels, masks
            # random brightness
            if random.randint(2):
                delta = random.uniform(-self.brightness_delta,
                                       self.brightness_delta)
                img += delta

            # mode == 0 --> do random contrast first
            # mode == 1 --> do random contrast last
            mode = random.randint(2)
            if mode == 1:
                if random.randint(2):
                    alpha = random.uniform(self.contrast_lower,
                                           self.contrast_upper)
                    img *= alpha

            # convert color from BGR to HSV
            img = mmcv.bgr2hsv(img)

            # random saturation
            if random.randint(2):
                img[..., 1] *= random.uniform(self.saturation_lower,
                                              self.saturation_upper)

            # random hue
            if random.randint(2):
                img[..., 0] += random.uniform(-self.hue_delta, self.hue_delta)
                img[..., 0][img[..., 0] > 360] -= 360
                img[..., 0][img[..., 0] < 0] += 360

            # convert color from HSV to BGR
            img = mmcv.hsv2bgr(img)

            # random contrast
            if mode == 0:
                if random.randint(2):
                    alpha = random.uniform(self.contrast_lower,
                                           self.contrast_upper)
                    img *= alpha

            # randomly swap channels
            if random.randint(2):
                img = img[..., random.permutation(3)]
        else:
            if self.color_choose == 1:
                # random brightness
                if random.randint(2):
                    delta = random.uniform(-self.brightness_delta,
                                           self.brightness_delta)
                    img += delta
            elif self.color_choose == 2:
                # random contrast first
                if random.randint(2):
                    alpha = random.uniform(self.contrast_lower,
                                           self.contrast_upper)
                    img *= alpha
            else:
                # convert color from BGR to HSV
                img = mmcv.bgr2hsv(img)

                if self.color_choose == 3:
                    # random saturation
                    if random.randint(2):
                        img[..., 1] *= random.uniform(self.saturation_lower,
                                                      self.saturation_upper)
                if self.color_choose == 4:
                    # random hue
                    if random.randint(2):
                        img[..., 0] += random.uniform(-self.hue_delta,
                                                      self.hue_delta)
                        img[..., 0][img[..., 0] > 360] -= 360
                        img[..., 0][img[..., 0] < 0] += 360

                # convert color from HSV to BGR
                img = mmcv.hsv2bgr(img)

        return img, boxes, labels, masks
Example #2
0
    def process(self, inputs, outputs, out_dict):
        """
        Args:
            inputs: the inputs to a model.
                It is a list of dict. Each dict corresponds to an image and
                contains keys like "height", "width", "file_name", "image_id", "scene_id".
            outputs: stores time
        """
        cfg = self.cfg
        if cfg.TEST.USE_PNP:
            if cfg.TEST.PNP_TYPE.lower() == "ransac_pnp":
                return self.process_pnp_ransac(inputs, outputs, out_dict)
            elif cfg.TEST.PNP_TYPE.lower() == "net_iter_pnp":
                return self.process_net_and_pnp(inputs,
                                                outputs,
                                                out_dict,
                                                pnp_type="iter")
            elif cfg.TEST.PNP_TYPE.lower() == "net_ransac_pnp":
                return self.process_net_and_pnp(inputs,
                                                outputs,
                                                out_dict,
                                                pnp_type="ransac")
            else:
                raise NotImplementedError

        out_rots = out_dict["rot"].detach().to(self._cpu_device).numpy()
        out_transes = out_dict["trans"].detach().to(self._cpu_device).numpy()

        out_i = -1
        for i, (_input, output) in enumerate(zip(inputs, outputs)):
            json_results = []
            start_process_time = time.perf_counter()
            for inst_i in range(len(_input["roi_img"])):
                out_i += 1  # the index in the flattened output
                scene_im_id_split = _input["scene_im_id"][inst_i].split("/")
                K = _input["cam"][inst_i].cpu().numpy().copy()

                roi_label = _input["roi_cls"][inst_i]  # 0-based label
                score = _input["score"][inst_i]
                roi_label, cls_name = self._maybe_adapt_label_cls_name(
                    roi_label)
                if cls_name is None:
                    continue

                # scene_id = int(scene_im_id_split[0])
                scene_id = scene_im_id_split[0]
                im_id = int(scene_im_id_split[1])
                obj_id = self.data_ref.obj2id[cls_name]

                # get pose
                rot_est = out_rots[out_i]
                trans_est = out_transes[out_i]
                pose_est = np.hstack([rot_est, trans_est.reshape(3, 1)])

                if cfg.DEBUG:  # visualize pose
                    file_name = _input["file_name"][inst_i]

                    if f"{int(scene_id)}/{im_id}" != "9/47":
                        continue

                    im_ori = mmcv.imread(file_name, "color")
                    bbox = _input["bbox_est"][inst_i].cpu().numpy().copy()
                    im_vis = vis_image_bboxes_cv2(im_ori, [bbox],
                                                  [f"{cls_name}_{score}"])

                    self.ren.clear()
                    self.ren.draw_background(
                        mmcv.bgr2gray(im_ori, keepdim=True))
                    self.ren.draw_model(
                        self.ren_models[self.data_ref.objects.index(cls_name)],
                        pose_est)
                    ren_im, _ = self.ren.finish()
                    grid_show(
                        [ren_im[:, :, ::-1], im_vis[:, :, ::-1]],
                        [f"ren_im_{cls_name}", f"{scene_id}/{im_id}_{score}"],
                        row=1,
                        col=2,
                    )

                json_results.extend(
                    self.pose_prediction_to_json(pose_est,
                                                 scene_id,
                                                 im_id,
                                                 obj_id=obj_id,
                                                 score=score,
                                                 pose_time=output["time"],
                                                 K=K))

            output["time"] += time.perf_counter() - start_process_time
            # process time for this image
            for item in json_results:
                item["time"] = output["time"]
            self._predictions.extend(json_results)
def test_color_transform():
    # test assertion for invalid type of magnitude
    with pytest.raises(AssertionError):
        transform = dict(type='ColorTransform', magnitude=None)
        build_from_cfg(transform, PIPELINES)

    # test assertion for invalid value of prob
    with pytest.raises(AssertionError):
        transform = dict(type='ColorTransform', magnitude=0.5, prob=100)
        build_from_cfg(transform, PIPELINES)

    # test assertion for invalid value of random_negative_prob
    with pytest.raises(AssertionError):
        transform = dict(type='ColorTransform',
                         magnitude=0.5,
                         random_negative_prob=100)
        build_from_cfg(transform, PIPELINES)

    # test case when magnitude=0, therefore no color transform
    results = construct_toy_data_photometric()
    transform = dict(type='ColorTransform', magnitude=0., prob=1.)
    pipeline = build_from_cfg(transform, PIPELINES)
    results = pipeline(results)
    assert (results['img'] == results['ori_img']).all()

    # test case when prob=0, therefore no color transform
    results = construct_toy_data_photometric()
    transform = dict(type='ColorTransform', magnitude=1., prob=0.)
    pipeline = build_from_cfg(transform, PIPELINES)
    results = pipeline(results)
    assert (results['img'] == results['ori_img']).all()

    # test case when magnitude=-1, therefore got gray img
    results = construct_toy_data_photometric()
    transform = dict(type='ColorTransform',
                     magnitude=-1.,
                     prob=1.,
                     random_negative_prob=0)
    pipeline = build_from_cfg(transform, PIPELINES)
    results = pipeline(results)
    img_gray = mmcv.bgr2gray(results['ori_img'])
    img_gray = np.stack([img_gray, img_gray, img_gray], axis=-1)
    assert (results['img'] == img_gray).all()

    # test case when magnitude=0.5
    results = construct_toy_data_photometric()
    transform = dict(type='ColorTransform',
                     magnitude=.5,
                     prob=1.,
                     random_negative_prob=0)
    pipeline = build_from_cfg(transform, PIPELINES)
    results = pipeline(results)
    img_r = np.round(
        np.clip((results['ori_img'] * 0.5 + img_gray * 0.5), 0,
                255)).astype(results['ori_img'].dtype)
    assert (results['img'] == img_r).all()
    assert (results['img'] == results['img2']).all()

    # test case when magnitude=0.3, random_negative_prob=1
    results = construct_toy_data_photometric()
    transform = dict(type='ColorTransform',
                     magnitude=.3,
                     prob=1.,
                     random_negative_prob=1.)
    pipeline = build_from_cfg(transform, PIPELINES)
    results = pipeline(results)
    img_r = np.round(
        np.clip((results['ori_img'] * 0.7 + img_gray * 0.3), 0,
                255)).astype(results['ori_img'].dtype)
    assert (results['img'] == img_r).all()
    assert (results['img'] == results['img2']).all()
Example #4
0
# -*- coding: utf-8 -* -
'''
MMCV读取并转换颜色空间
参考:https://mmcv.readthedocs.io/en/latest/image.html#color-space-conversion
'''
import mmcv

img = mmcv.imread("cluo.jpg")
img1 = mmcv.bgr2rgb(img)
img2 = mmcv.bgr2gray(img)
img3 = mmcv.bgr2gray(img)