Exemple #1
0
def img_to_array(img, data_format=None, dtype=None):
    if data_format is None:
        data_format = backend.image_data_format()
    if 'dtype' in generic_utils.getargspec(image.img_to_array).args:
        if dtype is None:
            dtype = backend.floatx()
        return image.img_to_array(img, data_format=data_format, dtype=dtype)
    return image.img_to_array(img, data_format=data_format)
Exemple #2
0
def getOneStatic(imPath, sampSize, newSize):
    ''' randomly samples an image with no transformations  '''
    im = image.load_img(imPath).resize((newSize, newSize))
    r = int(np.random.rand() * (newSize - sampSize))
    c = int(np.random.rand() * (newSize - sampSize))
    im = im.crop((r, c, r + sampSize, c + sampSize))
    return image.img_to_array(im) / 255.
Exemple #3
0
def data_augumentation(imgs, img_type):
    from image import array_to_img, img_to_array
    import cv2

    tmp = list()

    for x in xrange(imgs.shape[0]):
        img = imgs[x]
        if img_type == 'clabel':
            img = clabel_to_img(img, 128, 128)
        tmp.append(array_to_img(img))

    augmentated_list = rotate_img(tmp)
    augmentated_list = flip_img(augmentated_list)

    total = len(augmentated_list)
    augmentated_imgs = np.zeros([total, 1, 128, 128], dtype='float32')

    for x in xrange(total):
        if img_type == 'clabel':
            data_path = os.path.join('test/retrain/label/img' + str(x) +
                                     '.png')
            augmentated_list[x].save(data_path)
        else:
            data_path = os.path.join('test/retrain/raw/img' + str(x) + '.png')
            augmentated_list[x].save(data_path)

        augmentated_imgs[x] = img_to_array(augmentated_list[x]) / 255

    if img_type == 'raw':
        return augmentated_imgs
    if img_type == 'clabel':
        augmentated_clabels = categorize_label(augmentated_imgs)
        return augmentated_clabels
Exemple #4
0
def get_divided_img_array(tmp_list):
    from image import img_to_array, divide_img

    imgs = [
        img_to_array(patch) / 255 for tmp in tmp_list
        for patch in divide_img(tmp)
    ]

    return np.stack(imgs, axis=0)
Exemple #5
0
def getImage(sampSize, newSize, imPath, _=''):
    ''' randomly samples an image  '''
    f1 = int(np.sign(np.random.rand() - .5))
    f2 = int(np.sign(np.random.rand() - .5))
    im = image.load_img(imPath).resize((newSize, newSize))
    r = int(np.random.rand() * (newSize - sampSize))
    c = int(np.random.rand() * (newSize - sampSize))
    im = im.crop((r, c, r + sampSize, c + sampSize))
    im = image.random_rotation(im, 5)
    im = image.img_to_array(im)[:, ::-f1, ::-f2]
    im = (im - im.min()) / (im.max() - im.min())
    return im
def getOne(batchSize, imPath):
    allIms = np.zeros((batchSize, 3, sampSize, sampSize))
    for i in xrange(batchSize):
        f1 = int(np.sign(np.random.rand() - .5))
        f2 = int(np.sign(np.random.rand() - .5))
        im = image.load_img(imPath).resize((newSize, newSize))
        r = int(np.random.rand() * (newSize - sampSize))
        c = int(np.random.rand() * (newSize - sampSize))
        im = im.crop((r, c, r + sampSize, c + sampSize))
        im = image.random_rotation(im, 5)
        allIms[i, :, :, :] = image.img_to_array(im)
        allIms[i, :, :, :] = allIms[i, :, ::-f1, ::-f2] / 255.
    return allIms
Exemple #7
0
def Gamma_correction(path, image_save_dir, aug_number):
    img = load_img(path, grayscale=True)  # 这是一个PIL图像
    x = img_to_array(img)  # 把PIL图像转换成一个numpy数组
    x = x.reshape(x.shape[0], -1)  # 转成二维数组

    for i in range(aug_number):
        gamma = np.random.uniform(low=0.7, high=1.3)
        adjusted_img = (x / 255)**gamma * 255
        image_name = os.path.basename(path)
        prefix = image_name.split(".")[0]
        save_path = os.path.join(image_save_dir,
                                 prefix + '_aug_gamma_' + str(i) + '.jpg')
        scipy.misc.imsave(save_path, adjusted_img)
Exemple #8
0
def getBatch(batchSize, path):
	allPaths = image.list_pictures(path)
	allIms = np.zeros((batchSize, 3, sampSize, sampSize))
	for i in xrange(batchSize):
		f1 = int(np.sign(np.random.rand() - .5))
		f2 = int(np.sign(np.random.rand() - .5))
		im = image.load_img(allPaths[int(np.random.rand() * len(allPaths))]).resize((newSize, newSize))
		r  = int(np.random.rand() * (newSize - sampSize))
		c  = int(np.random.rand() * (newSize - sampSize))
		im = im.crop((r, c, r + sampSize, c + sampSize))
		im = image.random_rotation(im, 5)
		allIms[i, :, :, :] = image.img_to_array(im)
		allIms[i, :, :, :] = allIms[i, :, ::-f1, ::-f2]
	return allIms/255.
    def process_image(self, img_path, show=False):

        img = image.load_img(img_path, target_size=(150, 150))
        img_tensor = image.img_to_array(img)  # (height, width, channels)
        img_tensor = np.expand_dims(
            img_tensor, axis=0
        )  # (1, height, width, channels), add a dimension because the model expects this shape: (batch_size, height, width, channels)
        img_tensor /= 255.  # imshow expects values in the range [0, 1]

        if show:
            plt.imshow(img_tensor[0])
            plt.axis('off')
            plt.show()

        return self.model.predict(img_tensor)
Exemple #10
0
def Noise_Injection(path, image_save_dir, aug_number):
    img = load_img(path, grayscale=True)  # 这是一个PIL图像
    x = img_to_array(img)  # 把PIL图像转换成一个numpy数组
    x = x.reshape(x.shape[0], -1)  # 转成二维数组
    x_shape = x.shape
    for i in range(aug_number):
        noise = np.random.normal(loc=0.0, scale=0.01, size=x_shape)
        # noise=np.random.random(size=x_shape)
        x_add_noise = np.add(x, noise)
        # 保存图片
        image_dir = os.path.dirname(path)
        image_name = os.path.basename(path)
        prefix = image_name.split(".")[0]
        save_path = os.path.join(image_save_dir,
                                 prefix + '_aug_noise_' + str(i) + '.jpg')
        scipy.misc.imsave(save_path, x_add_noise)
Exemple #11
0
def Scaling(path, image_save_dir, aug_number):
    datagen = ImageDataGenerator(zoom_range=0.3, fill_mode='constant')  # 0.3
    img = load_img(path, grayscale=True)  # 这是一个PIL图像
    x = img_to_array(img)  # 把PIL图像转换成一个numpy数组
    x = x.reshape((1, ) + x.shape)  # 这是一个numpy数组
    image_dir = os.path.dirname(path)
    image_name = os.path.basename(path)
    prefix = image_name.split(".")[0]
    i = 1
    for batch in datagen.flow(x,
                              batch_size=1,
                              save_to_dir=image_save_dir,
                              save_prefix=prefix + "_aug_" + 'Scal',
                              save_format='jpg'):
        i += 1
        if i > aug_number:
            break  # 否则生成器会退出循环
Exemple #12
0
def Random_translation(path, image_save_dir, aug_number):
    datagen = ImageDataGenerator(width_shift_range=0.05,
                                 height_shift_range=0.05)  # 0.2
    img = load_img(path, grayscale=True)  # 这是一个PIL图像
    x = img_to_array(img)  # 把PIL图像转换成一个numpy数组
    x = x.reshape((1, ) + x.shape)  # 这是一个numpy数组
    image_dir = os.path.dirname(path)
    image_name = os.path.basename(path)
    prefix = image_name.split(".")[0]
    i = 1
    for batch in datagen.flow(x,
                              batch_size=1,
                              save_to_dir=image_save_dir,
                              save_prefix=prefix + "_aug_" + 'trans',
                              save_format='jpg'):
        i += 1
        if i > aug_number:
            break  # 否则生成器会退出循环
Exemple #13
0
def Random_affine_transform(path, image_save_dir, aug_number):
    datagen = ImageDataGenerator(
        shear_range=3)  # 水平或垂直投影变换,shear_range是角度范围 #5
    img = load_img(path, grayscale=True)  # 这是一个PIL图像
    x = img_to_array(img)  # 把PIL图像转换成一个numpy数组
    x = x.reshape((1, ) + x.shape)  # 这是一个numpy数组
    image_dir = os.path.dirname(path)
    image_name = os.path.basename(path)
    prefix = image_name.split(".")[0]
    i = 1
    for batch in datagen.flow(x,
                              batch_size=1,
                              save_to_dir=image_save_dir,
                              save_prefix=prefix + "_aug_" + 'aff',
                              save_format='jpg'):
        i += 1
        if i > aug_number:
            break  # 否则生成器会退出循环
Exemple #14
0
def visualize(dir_path, imgs_test_name, imgs, visualize=True):
    from image import combine_img, array_to_img, img_to_array

    tmp_list = list()

    for img_array in imgs:
        img_array *= 255
        img = array_to_img(img_array, scale=False)
        tmp_list.append(img)

    total = imgs_test_name.shape[0]
    combined_imgs = np.zeros([total, 1, imgs.shape[2]*4, imgs.shape[3]*4], dtype = 'uint8')

    for x in xrange(total):
        combined_img = combine_img(tmp_list[16*x:16*(x+1)])
        if visualize == True:
            combined_img.save(os.path.join(dir_path, imgs_test_name[x]))
            print 'save label image:', x+1, '/', total

        combined_imgs[x] = img_to_array(combined_img) / 255

    return combined_imgs
Exemple #15
0
def getBatch(batchSize, allPaths, numClass):
    allIms = np.zeros((batchSize, 3, sampSize, sampSize))
    Y = np.zeros((batchSize, 1)).astype('int')
    rIms = np.zeros((batchSize, 1)).astype('str')
    for i in xrange(batchSize):
        f1 = int(np.sign(np.random.rand() - .5))
        f2 = int(np.sign(np.random.rand() - .5))
        rIm = allPaths[int(np.random.rand() * len(allPaths))]
        rIms[i] = rIm
        im = image.load_img(rIm).resize((newSize, newSize))
        Y[i] = labels[rIm.split('/')[-1]]
        r = int(np.random.rand() * (newSize - sampSize))
        c = int(np.random.rand() * (newSize - sampSize))
        im = im.crop((r, c, r + sampSize, c + sampSize))
        im = image.random_rotation(im, 5)
        allIms[i, :, :, :] = image.img_to_array(im)
        allIms[i, :, :, :] = allIms[i, :, ::-f1, ::-f2]
    if numClass > 2:
        Y2 = np.zeros((batchSize, numClass)).astype('int')
        for i in xrange(batchSize):
            Y2[i, Y[i][0]] = 1
        Y = Y2
    return allIms, Y, rIms
Exemple #16
0
    def post(self):
        image_data = self.request.files['image'][0]
        np_arr = np.fromstring(image_data['body'], np.uint8)
        image_np = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
        image_path = '/tmp/%s.png' % str(uuid.uuid4())
        cv2.imwrite(image_path, image_np)

        image_f = image.load_img(image_path, target_size=(64, 64))
        image_f = image.img_to_array(image_f)
        image_f = np.expand_dims(image_f, axis=0)

        left_or_right = self.application.model['left_or_right']
        straight_layer = self.application.model['straight']

        left_or_right_result = left_or_right.predict(image_f)
        straight_layer_result = straight_layer.predict(image_f)

        os.remove(image_path)

        self.write(
            json.dumps({
                'left_or_right': left_or_right_result,
                'straight': straight_layer_result
            }))
def read_img_test(filepath, size):
    img = T.load_img(os.path.join(test_dir, filepath), target_size=size)
    img = T.img_to_array(img)
    return img