def read_img(img_name, width, height, keep_img_shape=config.keep_img_shape):
    img_ori = tools.read_img(img_name)
    if img_ori is None:
        return None, None
    if not keep_img_shape:
        img = cv2.resize(img_ori, (width, height))
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    else:
        # add by rdc ,follow @Jiachenyin1
        target_h, target_w = height, width
        ori_h, ori_w, _ = img_ori.shape
        scale = min(target_h / ori_h, target_w / ori_w)
        nw, nh = int(scale * ori_w), int(scale * ori_h)
        image_resized = cv2.resize(img_ori, (nw, nh))  ## width and height
        img = np.full(shape=[target_h, target_w, 3],
                      fill_value=0,
                      dtype=np.uint8)
        dh, dw = (target_h - nh) // 2, (target_w - nw) // 2
        img[dh:(nh + dh), dw:(nw + dw), :] = image_resized
        img_ori = img
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    img = img.astype(np.float32)
    img = img / 255.0
    # [416, 416, 3] => [1, 416, 416, 3]
    img = np.expand_dims(img, 0)
    return img, img_ori
Exemple #2
0
 def __read_img(self, img_file):
     '''
     read img_file
     return:img(RGB)
     '''
     img = tools.read_img(img_file)
     if img is None:
         return None
     return img
def read_img(img_name, width, height):
    img_ori = tools.read_img(img_name)
    if img_ori is None:
        return None, None
    img = cv2.resize(img_ori, (width, height))
    # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    img = img.astype(np.float32)
    img = img / 255.0
    # [416, 416, 3] => [1, 416, 416, 3]
    img = np.expand_dims(img, 0)
    return img, img_ori
 def read_img(self, img_file):
     '''
     读取 img_file, 并 resize
     return:img, RGB & float
     '''
     img = tools.read_img(img_file)
     if img is None:
         return None
     img = cv2.resize(img, (self.width, self.height))
     img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
     img = img.astype(np.float32)
     img = img/255.0
     return img
Exemple #5
0
def read_img(img_name, width, height):
    img_ori = tools.read_img(img_name)
    if img_ori is None:
        return None, None
    img = cv2.resize(img_ori, (width, height))
    nw, nh = None, None

    show_img = img
    
    img = img.astype(np.float32)
    img = img/255.0
    # [416, 416, 3] => [1, 416, 416, 3]
    img = np.expand_dims(img, 0)
    return img, nw, nh, img_ori, show_img
Exemple #6
0
    def read_img(self, img_file):
        '''
        读取 img_file, 并 resize
        return:img, RGB & float
        '''
        img = tools.read_img(img_file)
        if img is None:
            return None

        if self.keep_img_shape:
            # keep image shape when we reshape the image
            img, new_w, new_h = data_augment.keep_image_shape_resize(
                img, size=[self.width, self.height])
        else:
            img = cv2.resize(img, (self.width, self.height))
            new_w, new_h = None, None

        if self.flip_img:
            # flip image
            img = data_augment.flip_img(img)

        if self.gray_img and (np.random.random() < 0.2):
            # probility of gray image is 0.2
            img = data_augment.gray_img(img)

        if self.erase_img and (np.random.random() < 0.3):
            # probility of random erase image is 0.3
            img = data_augment.erase_img(img, size_area=[20, 100])

        if self.invert_img and (np.random.random() < 0.1):
            # probility of invert image is 0.1
            img = data_augment.invert_img(img)

        if self.rotate_img:
            # rotation image
            img = data_augment.random_rotate_img(img)

        test_img = img

        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        img = img.astype(np.float32)
        img = img / 255.0
        return img, new_w, new_h, test_img
Exemple #7
0
def read_img(img_name, width, height):
    img_ori = tools.read_img(img_name)
    if img_ori is None:
        return None, None
    if config.keep_img_shape:
        img, nw, nh = data_augment.keep_image_shape_resize(
            img_ori, size=[width, height])
    else:
        img = cv2.resize(img_ori, (width, height))
        nw, nh = None, None

    show_img = img

    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = img.astype(np.float32)
    img = img / 255.0
    # [416, 416, 3] => [1, 416, 416, 3]
    img = np.expand_dims(img, 0)
    return img, nw, nh, img_ori, show_img
Exemple #8
0
    def read_img(self, img_file):
        '''
        read img_file, and resize it
        return:img, RGB & float
        '''
        img = tools.read_img(img_file)
        if img is None:
            return None, None, None, None
        ori_h, ori_w, _ = img.shape
        img = cv2.resize(img, (self.width, self.height))

        # flip image
        if np.random.random() < self.flip_img:
            self.is_flip = True
            img = cv2.flip(img, 1)

        # gray
        if np.random.random() < self.gray_img:
            tmp = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            # convert to 3 channel
            img = cv2.cvtColor(tmp, cv2.COLOR_GRAY2BGR)

        # random erase
        if np.random.random() < self.erase_img:
            erase_w = random.randint(20, 100)
            erase_h = random.randint(20, 100)
            x = random.randint(0, self.width - erase_w)
            y = random.randint(0, self.height - erase_h)
            value = random.randint(0, 255)
            img[y:y + erase_h, x:x + erase_w, :] = value

        test_img = img

        img = img.astype(np.float32)
        img = img / 255.0

        # gasuss noise
        if np.random.random() < self.gasuss:
            noise = np.random.normal(0, 0.01, img.shape)
            img = img + noise
            img = np.clip(img, 0, 1.0)
        return img, ori_w, ori_h, test_img