Exemplo n.º 1
0
    def __call__(self, results):
        """Call functions to add image meta information.

        Args:
            results (dict): Result dict with Webcam read image in
                ``results['img']``.

        Returns:
            dict: The dict contains loaded image and meta information.
        """
        assert results['img'].dtype == 'uint8'

        img = results['img']
        if self.color_type == 'grayscale' and img.shape[2] == 3:
            img = mmcv.bgr2gray(img, keepdim=True)
        if self.color_type == 'color' and img.shape[2] == 1:
            img = mmcv.gray2bgr(img)
        if self.to_float32:
            img = img.astype(np.float32)

        results['filename'] = None
        results['ori_filename'] = None
        results['img'] = img
        results['img_shape'] = img.shape
        results['ori_shape'] = img.shape
        results['img_fields'] = ['img']
        return results
Exemplo n.º 2
0
 def test_gray2bgr(self):
     in_img = np.random.rand(10, 10).astype(np.float32)
     out_img = mmcv.gray2bgr(in_img)
     assert out_img.shape == (10, 10, 3)
     for i in range(3):
         assert_array_almost_equal(out_img[..., i], in_img, decimal=4)
Exemplo n.º 3
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