Python scipy.misc.imresize() Examples

The following are 30 code examples of scipy.misc.imresize(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module scipy.misc , or try the search function .
Example #1
Source File: BatchDatsetReader.py    From Colorization.tensorflow with MIT License 7 votes vote down vote up
def _transform(self, filename):
        try:
            image = misc.imread(filename)
            if len(image.shape) < 3:  # make sure images are of shape(h,w,3)
                image = np.array([image for i in range(3)])

            if self.image_options.get("resize", False) and self.image_options["resize"]:
                resize_size = int(self.image_options["resize_size"])
                resize_image = misc.imresize(image,
                                             [resize_size, resize_size])
            else:
                resize_image = image

            if self.image_options.get("color", False):
                option = self.image_options['color']
                if option == "LAB":
                    resize_image = color.rgb2lab(resize_image)
                elif option == "HSV":
                    resize_image = color.rgb2hsv(resize_image)
        except:
            print ("Error reading file: %s of shape %s" % (filename, str(image.shape)))
            raise

        return np.array(resize_image) 
Example #2
Source File: imgproc.py    From graph_distillation with Apache License 2.0 6 votes vote down vote up
def resize(video, size, interpolation):
  """
  :param video: ... x h x w x num_channels
  :param size: (h, w)
  :param interpolation: 'bilinear', 'nearest'
  :return:
  """
  shape = video.shape[:-3]
  num_channels = video.shape[-1]
  video = video.reshape((-1, *video.shape[-3:]))
  resized_video = np.zeros((video.shape[0], *size, video.shape[-1]))

  for i in range(video.shape[0]):
    if num_channels == 3:
      resized_video[i] = imresize(video[i], size, interpolation)
    elif num_channels == 2:
      resized_video[i, ..., 0] = imresize(video[i, ..., 0], size, interpolation)
      resized_video[i, ..., 1] = imresize(video[i, ..., 1], size, interpolation)
    elif num_channels == 1:
      resized_video[i, ..., 0] = imresize(video[i, ..., 0], size, interpolation)
    else:
      raise NotImplementedError

  return resized_video.reshape((*shape, *size, video.shape[-1])) 
Example #3
Source File: segmentFinal.py    From Chinese-Character-and-Calligraphic-Image-Processing with MIT License 6 votes vote down vote up
def align_char(char_img, target_h, target_w):
    canvas = np.ones([target_h, target_w], dtype=np.int32) * 255
    img_h, img_w = char_img.shape[0], char_img.shape[1]
    if img_h > img_w:
        new_h = target_h
        new_w = np.int32(img_w * target_h / img_h)
        char_img = misc.imresize(char_img, [new_h, new_w])
        mid_w = target_w // 2
        start = mid_w - new_w // 2
        end = start + new_w
        canvas[:, start:end] = char_img
    if img_h < img_w:
        new_w = target_w
        new_h = np.int32(img_h * target_w / img_w)
        char_img = misc.imresize(char_img, [new_h, new_w])
        mid_h = target_h // 2
        start = mid_h - new_h // 2
        end = start + new_h
        canvas[start:end, :] = char_img
    if img_h == img_w:
        canvas = misc.imresize(char_img, [target_h, target_w])
    return canvas 
Example #4
Source File: test.py    From Chinese-Character-and-Calligraphic-Image-Processing with MIT License 6 votes vote down vote up
def test(self):

        list_ = os.listdir("./maps/val/")
        nums_file = list_.__len__()
        saver = tf.train.Saver(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, "generator"))
        saver.restore(self.sess, "./save_para/model.ckpt")
        rand_select = np.random.randint(0, nums_file)
        INPUTS_CONDITION = np.zeros([1, self.img_h, self.img_w, 3])
        INPUTS = np.zeros([1, self.img_h, self.img_w, 3])
        img = np.array(Image.open(self.path + list_[rand_select]))
        img_h, img_w = img.shape[0], img.shape[1]
        INPUTS_CONDITION[0] = misc.imresize(img[:, img_w//2:], [self.img_h, self.img_w]) / 127.5 - 1.0
        INPUTS[0] = misc.imresize(img[:, :img_w//2], [self.img_h, self.img_w]) / 127.5 - 1.0
        [fake_img] = self.sess.run([self.inputs_fake], feed_dict={self.inputs_condition: INPUTS_CONDITION})
        out_img = np.concatenate((INPUTS_CONDITION[0], fake_img[0], INPUTS[0]), axis=1)
        Image.fromarray(np.uint8((out_img + 1.0)*127.5)).save("./results/1.jpg")
        plt.imshow(np.uint8((out_img + 1.0)*127.5))
        plt.grid("off")
        plt.axis("off")
        plt.show() 
Example #5
Source File: pix2pix.py    From Chinese-Character-and-Calligraphic-Image-Processing with MIT License 6 votes vote down vote up
def train(self):

        list = os.listdir(self.path)
        nums_file = list.__len__()
        saver = tf.train.Saver()
        for i in range(10000):
            rand_select = np.random.randint(0, nums_file, [self.batch_size])
            INPUTS = np.zeros([self.batch_size, self.img_h, self.img_w, 3])
            INPUTS_CONDITION = np.zeros([self.batch_size, self.img_h, self.img_w, 3])
            for j in range(self.batch_size):
                img = np.array(Image.open(self.path + list[rand_select[j]]))
                img_h, img_w = img.shape[0], img.shape[1]
                INPUT_CON = misc.imresize(img[:, :img_w//2], [self.img_h, self.img_w]) / 127.5 - 1.0
                INPUTS_CONDITION[j] = np.dstack((INPUT_CON, INPUT_CON, INPUT_CON))
                INPUT = misc.imresize(img[:, img_w//2:], [self.img_h, self.img_w]) / 127.5 - 1.0
                INPUTS[j] = np.dstack((INPUT, INPUT, INPUT))
            self.sess.run(self.opt_dis, feed_dict={self.inputs: INPUTS, self.inputs_condition: INPUTS_CONDITION})
            self.sess.run(self.opt_gen, feed_dict={self.inputs: INPUTS, self.inputs_condition: INPUTS_CONDITION})
            if i % 10 == 0:
                [G_LOSS, D_LOSS] = self.sess.run([self.g_loss, self.d_loss], feed_dict={self.inputs: INPUTS, self.inputs_condition: INPUTS_CONDITION})
                print("Iteration: %d, d_loss: %f, g_loss: %f"%(i, D_LOSS, G_LOSS))
            if i % 100 == 0:
                saver.save(self.sess, "./save_para//model.ckpt") 
Example #6
Source File: getImgs.py    From crawl-dataset with ISC License 6 votes vote down vote up
def resizeImg(imgPath,img_size):
	try:
		img = imread(imgPath)
		h, w, _ = img.shape
		scale = 1
		if w >= h:
			new_w = img_size
			if w  >= new_w:
				scale = float(new_w) / w
			new_h = int(h * scale)
		else:
			new_h = img_size
			if h >= new_h:
				scale = float(new_h) / h
			new_w = int(w * scale)
		new_img = imresize(img, (new_h, new_w), interp='bilinear')
		imsave(imgPath,new_img)
		print('Img Resized as {}'.format(img_size))
	except Exception as e:
		print(e) 
Example #7
Source File: getImgs.py    From crawl-dataset with ISC License 6 votes vote down vote up
def resizeImg(imgPath,img_size):
    img = imread(imgPath)
    h, w, _ = img.shape
    scale = 1
    if w >= h:
        new_w = img_size
        if w  >= new_w:
            scale = float(new_w) / w
        new_h = int(h * scale)
    else:
        new_h = img_size
        if h >= new_h:
            scale = float(new_h) / h
        new_w = int(w * scale)
    new_img = imresize(img, (new_h, new_w), interp='bilinear')
    imsave(imgPath,new_img)
    print('Img Resized as {}'.format(img_size)) 
Example #8
Source File: getImages.py    From crawl-dataset with ISC License 6 votes vote down vote up
def resizeImg(imgPath,img_size):
    img = imread(imgPath)
    h, w, _ = img.shape
    scale = 1
    if w >= h:
        new_w = img_size
        if w  >= new_w:
            scale = float(new_w) / w
        new_h = int(h * scale)
    else:
        new_h = img_size
        if h >= new_h:
            scale = float(new_h) / h
        new_w = int(w * scale)
    new_img = imresize(img, (new_h, new_w), interp='bilinear')
    imsave(imgPath,new_img)

#Download img
#Later we can do multi thread apply workers to do faster work 
Example #9
Source File: fid_score.py    From Face-and-Image-super-resolution with MIT License 6 votes vote down vote up
def _compute_statistics_of_path(path, model, batch_size, dims, cuda):
    if path.endswith('.npz'):
        f = np.load(path)
        m, s = f['mu'][:], f['sigma'][:]
        f.close()
    else:
        path = pathlib.Path(path)
        files = list(path.glob('*.jpg')) + list(path.glob('*.png'))
        #imgs = np.array([imresize(imread(str(fn)),(64,64)).astype(np.float32) for fn in files])
        imgs = np.array([imread(str(fn)).astype(np.float32) for fn in files])
        # Bring images to shape (B, 3, H, W)
        imgs = imgs.transpose((0, 3, 1, 2))
        # Rescale images to be between 0 and 1
        imgs /= 255
        m, s = calculate_activation_statistics(imgs, model, batch_size,
                                               dims, cuda)

    return m, s 
Example #10
Source File: tracklet_utils_2d_online.py    From TNT with GNU General Public License v3.0 6 votes vote down vote up
def crop_det(det_M, img): 
    global track_struct
    crop_det_folder = track_struct['file_path']['crop_det_folder']
    crop_size = track_struct['track_params']['crop_size']
    if not os.path.isdir(crop_det_folder): 
        os.makedirs(crop_det_folder) 
    
    save_patch_list = []
    for n in range(len(det_M)):
        xmin = int(max(0,det_M[n,1])) 
        xmax = int(min(img.shape[1]-1,det_M[n,1]+det_M[n,3])) 
        ymin = int(max(0,det_M[n,2])) 
        ymax = int(min(img.shape[0]-1,det_M[n,2]+det_M[n,4])) 
        img_patch = img[ymin:ymax,xmin:xmax,:] 
        img_patch = misc.imresize(img_patch, size=[crop_size,crop_size]) 
        patch_name = track_lib.file_name(n,4)+'.png'
        save_path = crop_det_folder+'/'+patch_name 
        misc.imsave(save_path, img_patch)
        save_patch_list.append(save_path)
  
    return save_patch_list 
Example #11
Source File: tracklet_utils_3d_online.py    From TNT with GNU General Public License v3.0 6 votes vote down vote up
def crop_det(det_M, img): 
    global track_struct
    crop_det_folder = track_struct['file_path']['crop_det_folder']
    crop_size = track_struct['track_params']['crop_size']
    if not os.path.isdir(crop_det_folder): 
        os.makedirs(crop_det_folder) 
    
    save_patch_list = []
    for n in range(len(det_M)):
        xmin = int(max(0,det_M[n,1])) 
        xmax = int(min(img.shape[1]-1,det_M[n,1]+det_M[n,3])) 
        ymin = int(max(0,det_M[n,2])) 
        ymax = int(min(img.shape[0]-1,det_M[n,2]+det_M[n,4])) 
        img_patch = img[ymin:ymax,xmin:xmax,:] 
        img_patch = misc.imresize(img_patch, size=[crop_size,crop_size]) 
        patch_name = track_lib.file_name(n,4)+'.png'
        save_path = crop_det_folder+'/'+patch_name 
        misc.imsave(save_path, img_patch)
        save_patch_list.append(save_path)
  
    return save_patch_list 
Example #12
Source File: nyuv2_loader.py    From PLARD with MIT License 6 votes vote down vote up
def transform(self, img, lbl):
        img = m.imresize(img, (self.img_size[0], self.img_size[1]))  # uint8 with RGB mode
        img = img[:, :, ::-1]  # RGB -> BGR
        img = img.astype(np.float64)
        img -= self.mean
        if self.img_norm:
            # Resize scales images from 0 to 255, thus we need
            # to divide by 255.0
            img = img.astype(float) / 255.0
        # NHWC -> NCHW
        img = img.transpose(2, 0, 1)

        classes = np.unique(lbl)
        lbl = lbl.astype(float)
        lbl = m.imresize(lbl, (self.img_size[0], self.img_size[1]), "nearest", mode="F")
        lbl = lbl.astype(int)
        assert np.all(classes == np.unique(lbl))

        img = torch.from_numpy(img).float()
        lbl = torch.from_numpy(lbl).long()
        return img, lbl 
Example #13
Source File: pascal_voc_loader.py    From PLARD with MIT License 6 votes vote down vote up
def transform(self, img, lbl):
        img = m.imresize(img, (self.img_size[0], self.img_size[1])) # uint8 with RGB mode
        img = img[:, :, ::-1] # RGB -> BGR
        img = img.astype(np.float64)
        img -= self.mean
        if self.img_norm:
            # Resize scales images from 0 to 255, thus we need
            # to divide by 255.0
            img = img.astype(float) / 255.0
        # NHWC -> NCHW
        img = img.transpose(2, 0, 1)

        lbl[lbl==255] = 0
        lbl = lbl.astype(float)
        lbl = m.imresize(lbl, (self.img_size[0], self.img_size[1]), 'nearest',
                         mode='F')
        lbl = lbl.astype(int)
        img = torch.from_numpy(img).float()
        lbl = torch.from_numpy(lbl).long()
        return img, lbl 
Example #14
Source File: davis17_online_data.py    From MaskTrack with MIT License 6 votes vote down vote up
def make_img_gt_pair(self, idx):
        """
        Make the image-ground-truth pair
        """
        img = cv2.imread(os.path.join(self.db_root_dir, self.img_list[idx]))
        if self.labels[idx] is not None:
            label = cv2.imread(os.path.join(self.db_root_dir, self.labels[idx]), 0)
        else:
            gt = np.zeros(img.shape[:-1], dtype=np.uint8)

        if self.inputRes is not None:
            img = imresize(img, self.inputRes)
            if self.labels[idx] is not None:
                label = imresize(label, self.inputRes, interp='nearest')

        img = np.array(img, dtype=np.float32)
        img = np.subtract(img, np.array(self.meanval, dtype=np.float32))

        if self.labels[idx] is not None:
                gt = np.array(label, dtype=np.float32)
                gt = gt/np.max([gt.max(), 1e-8])

        return img, gt 
Example #15
Source File: sunrgbd_loader.py    From PLARD with MIT License 6 votes vote down vote up
def transform(self, img, lbl):
        img = m.imresize(img, (self.img_size[0], self.img_size[1])) # uint8 with RGB mode
        img = img[:, :, ::-1] # RGB -> BGR
        img = img.astype(np.float64)
        img -= self.mean
        if self.img_norm:
            # Resize scales images from 0 to 255, thus we need
            # to divide by 255.0
            img = img.astype(float) / 255.0
        # NHWC -> NCHW
        img = img.transpose(2, 0, 1)

        classes = np.unique(lbl)
        lbl = lbl.astype(float)
        lbl = m.imresize(lbl, (self.img_size[0], self.img_size[1]), 'nearest', mode='F')
        lbl = lbl.astype(int)
        assert(np.all(classes == np.unique(lbl)))

        img = torch.from_numpy(img).float()
        lbl = torch.from_numpy(lbl).long()
        return img, lbl 
Example #16
Source File: ade20k_loader.py    From PLARD with MIT License 6 votes vote down vote up
def transform(self, img, lbl):
        img = m.imresize(img, (self.img_size[0], self.img_size[1])) # uint8 with RGB mode
        img = img[:, :, ::-1] # RGB -> BGR
        img = img.astype(np.float64)
        img -= self.mean
        if self.img_norm:
            # Resize scales images from 0 to 255, thus we need
            # to divide by 255.0
            img = img.astype(float) / 255.0
        # NHWC -> NCHW
        img = img.transpose(2, 0, 1)

        lbl = self.encode_segmap(lbl)
        classes = np.unique(lbl)
        lbl = lbl.astype(float)
        lbl = m.imresize(lbl, (self.img_size[0], self.img_size[1]), 'nearest', mode='F')
        lbl = lbl.astype(int)
        assert(np.all(classes == np.unique(lbl)))

        img = torch.from_numpy(img).float()
        lbl = torch.from_numpy(lbl).long()
        return img, lbl 
Example #17
Source File: utility_functions.py    From MaskTrack with MIT License 6 votes vote down vote up
def apply_val_transform_image(image,inputRes=None):
    meanval = (104.00699, 116.66877, 122.67892)

    if inputRes is not None:
        image = sm.imresize(image, inputRes)

    image = np.array(image, dtype=np.float32)
    image = np.subtract(image, np.array(meanval, dtype=np.float32))



    if image.ndim == 2:
        image = image[:, :, np.newaxis]

    # swap color axis because
    # numpy image: H x W x C
    # torch image: C X H X W

    image = image.transpose((2, 0, 1))
    image = torch.from_numpy(image)

    return image 
Example #18
Source File: img_utils.py    From keras-vgg-buddy with MIT License 5 votes vote down vote up
def resize_image(img, img_width, img_height):
    img = models.img_from_vgg(img)
    img = imresize(img, (img_height, img_width), interp='bicubic').astype('float32')
    img = models.img_to_vgg(img)
    return img

# util function to convert a tensor into a valid image 
Example #19
Source File: img_utils.py    From keras-vgg-buddy with MIT License 5 votes vote down vote up
def preprocess_image(img, img_width, img_height):
    img = imresize(img, (img_height, img_width), interp='bicubic').astype('float32')
    img = models.img_to_vgg(img)
    img = np.expand_dims(img, axis=0)
    return img 
Example #20
Source File: img_utils.py    From keras-vgg-buddy with MIT License 5 votes vote down vote up
def deprocess_image(x, contrast_percent=0.0, resize=None):
    x = models.img_from_vgg(x)
    if contrast_percent:
        min_x, max_x = np.percentile(x, (contrast_percent, 100 - contrast_percent))
        x = (x - min_x) * 255.0 / (max_x - min_x)
    x = np.clip(x, 0, 255)
    if resize:
        x = imresize(x, resize, interp='bicubic')
    return x.astype('uint8') 
Example #21
Source File: tf_record.py    From DeepFloorplan with GNU General Public License v3.0 5 votes vote down vote up
def load_seg_raw_images(path):
	paths = path.split('\t')

	image = imread(paths[0], mode='RGB')
	close = imread(paths[2], mode='L')
	room  = imread(paths[3], mode='RGB')
	close_wall = imread(paths[4], mode='L')

	# NOTE: imresize will rescale the image to range [0, 255], also cast data into uint8 or uint32
	image = imresize(image, (512, 512, 3))
	close = imresize(close, (512, 512)) / 255
	close_wall = imresize(close_wall, (512, 512)) / 255
	room = imresize(room, (512, 512, 3))

	room_ind = rgb2ind(room)

	# merge result
	d_ind = (close>0.5).astype(np.uint8)
	cw_ind = (close_wall>0.5).astype(np.uint8)
	room_ind[cw_ind==1] = 10
	room_ind[d_ind==1] = 9

	# make sure the dtype is uint8
	image = image.astype(np.uint8)
	room_ind = room_ind.astype(np.uint8)

	# debug
	# merge = ind2rgb(room_ind, color_map=floorplan_fuse_map)
	# plt.subplot(131)
	# plt.imshow(image)
	# plt.subplot(132)
	# plt.imshow(room_ind)
	# plt.subplot(133)
	# plt.imshow(merge/256.)
	# plt.show()

	return image, room_ind 
Example #22
Source File: tf_record.py    From DeepFloorplan with GNU General Public License v3.0 5 votes vote down vote up
def load_raw_images(path):
	paths = path.split('\t')

	image = imread(paths[0], mode='RGB')
	wall  = imread(paths[1], mode='L')
	close = imread(paths[2], mode='L')
	room  = imread(paths[3], mode='RGB')
	close_wall = imread(paths[4], mode='L')

	# NOTE: imresize will rescale the image to range [0, 255], also cast data into uint8 or uint32
	image = imresize(image, (512, 512, 3))
	wall = imresize(wall, (512, 512))
	close = imresize(close, (512, 512))
	close_wall = imresize(close_wall, (512, 512))
	room = imresize(room, (512, 512, 3))

	room_ind = rgb2ind(room)

	# make sure the dtype is uint8
	image = image.astype(np.uint8)
	wall = wall.astype(np.uint8)
	close = close.astype(np.uint8)
	close_wall = close_wall.astype(np.uint8)
	room_ind = room_ind.astype(np.uint8)

	# debug
	# plt.subplot(231)
	# plt.imshow(image)
	# plt.subplot(233)
	# plt.imshow(wall, cmap='gray')
	# plt.subplot(234)
	# plt.imshow(close, cmap='gray')
	# plt.subplot(235)
	# plt.imshow(room_ind)
	# plt.subplot(236)
	# plt.imshow(close_wall, cmap='gray')
	# plt.show()

	return image, wall, close, room_ind, close_wall 
Example #23
Source File: tf_record.py    From DeepFloorplan with GNU General Public License v3.0 5 votes vote down vote up
def load_bd_rm_images(path):
	paths = path.split('\t')

	image = imread(paths[0], mode='RGB')
	close = imread(paths[2], mode='L')
	room  = imread(paths[3], mode='RGB')
	close_wall = imread(paths[4], mode='L')

	# NOTE: imresize will rescale the image to range [0, 255], also cast data into uint8 or uint32
	image = imresize(image, (512, 512, 3))
	close = imresize(close, (512, 512)) / 255.
	close_wall = imresize(close_wall, (512, 512)) / 255.
	room = imresize(room, (512, 512, 3))

	room_ind = rgb2ind(room)

	# merge result
	d_ind = (close>0.5).astype(np.uint8)
	cw_ind = (close_wall>0.5).astype(np.uint8)

	cw_ind[cw_ind==1] = 2
	cw_ind[d_ind==1] = 1

	# make sure the dtype is uint8
	image = image.astype(np.uint8)
	room_ind = room_ind.astype(np.uint8)
	cw_ind = cw_ind.astype(np.uint8)

	# debugging
	# merge = ind2rgb(room_ind, color_map=floorplan_fuse_map)
	# rm = ind2rgb(room_ind)
	# bd = ind2rgb(cw_ind, color_map=floorplan_boundary_map)
	# plt.subplot(131)
	# plt.imshow(image)
	# plt.subplot(132)
	# plt.imshow(rm/256.)
	# plt.subplot(133)
	# plt.imshow(bd/256.)
	# plt.show()

	return image, cw_ind, room_ind, d_ind 
Example #24
Source File: DnCNN.py    From DnCNN-Denoise-Gaussian-noise-TensorFlow with MIT License 5 votes vote down vote up
def test(self, cleaned_path="./TestingSet//02.png"):
        saver = tf.train.Saver()
        saver.restore(self.sess, "./save_para/DnCNN.ckpt")
        cleaned_img = np.reshape(np.array(misc.imresize(np.array(Image.open(cleaned_path)), [256, 256])), [1, 256, 256, 1])
        noised_img = cleaned_img + np.random.normal(0, SIGMA, cleaned_img.shape)
        [denoised_img] = self.sess.run([self.denoised_img], feed_dict={self.clean_img: cleaned_img, self.noised_img: noised_img, self.train_phase: False})
        compared = np.concatenate((cleaned_img[0, :, :, 0], noised_img[0, :, :, 0], denoised_img[0, :, :, 0]), 1)
        Image.fromarray(np.uint8(compared)).show() 
Example #25
Source File: tools.py    From hart with GNU General Public License v3.0 5 votes vote down vote up
def read_img(path, size=None, dtype=np.float32):
    img = imread(path)
    if size is not None:
        img = imresize(img, size)

    return img.astype(dtype) 
Example #26
Source File: network.py    From pytorch-FPN with MIT License 5 votes vote down vote up
def _add_gt_image(self):
    # add back mean
    image = self._image_gt_summaries['image'] + cfg.PIXEL_MEANS
    image = imresize(image[0], self._im_info[:2] / self._im_info[2])
    # BGR to RGB (opencv uses BGR)
    self._gt_image = image[np.newaxis, :,:,::-1].copy(order='C') 
Example #27
Source File: 6_extract_features.py    From Deep-Learning-for-Computer-Vision with MIT License 5 votes vote down vote up
def load_and_align_data(image_paths,
                        image_size=160,
                        margin=44,
                        gpu_memory_fraction=1.0):
    minsize = 20
    threshold = [0.6, 0.7, 0.7]
    factor = 0.709

    print('Creating networks and loading parameters')
    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=gpu_memory_fraction)
        sess = tf.Session(config=tf.ConfigProto(
            gpu_options=gpu_options, log_device_placement=False))
        with sess.as_default():
            pnet, rnet, onet = align.detect_face.create_mtcnn(sess, None)

    nrof_samples = len(image_paths)
    img_list = [None] * nrof_samples
    for i in range(nrof_samples):
        img = misc.imread(os.path.expanduser(image_paths[i]), mode='RGB')
        img_size = np.asarray(img.shape)[0:2]
        bounding_boxes, _ = align.detect_face.detect_face(
            img, minsize, pnet, rnet, onet, threshold, factor)
        det = np.squeeze(bounding_boxes[0, 0:4])
        bb = np.zeros(4, dtype=np.int32)
        bb[0] = np.maximum(det[0] - margin / 2, 0)
        bb[1] = np.maximum(det[1] - margin / 2, 0)
        bb[2] = np.minimum(det[2] + margin / 2, img_size[1])
        bb[3] = np.minimum(det[3] + margin / 2, img_size[0])
        cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
        aligned = misc.imresize(
            cropped, (image_size, image_size), interp='bilinear')
        prewhitened = prewhiten(aligned)
        img_list[i] = prewhitened
    images = np.stack(img_list)
    return images 
Example #28
Source File: data.py    From udacity-SDC-baseline with MIT License 5 votes vote down vote up
def read_images(image_folder, camera, ids, image_size):
    prefix = path.join(image_folder, camera)
    imgs = []
    for id in ids:
        img = imread(path.join(prefix, '%d.jpg' % id))
        img = imresize(img, size=image_size)
        imgs.append(img)
    img_block = np.stack(imgs, axis=0)
    if K.image_dim_ordering() == 'th':
        img_block = np.transpose(img_block, axes = (0, 3, 1, 2))
    return img_block 
Example #29
Source File: alexnet_fine_tune.py    From Deep-Learning-with-TensorFlow-Second-Edition with MIT License 5 votes vote down vote up
def next_batch(batch_size):
    path = os.getcwd()
    trainPath = path + "/trainDir/"
    files = [f for f in listdir(trainPath) if isfile(join(trainPath, f))]
    files = sample(files, len(files))
    batch_x = np.ndarray([batch_size,227, 227, 3])
    batch_y = np.zeros((batch_size, 2))
    i = 0
    for fname in files:
        img = (imread(join(trainPath, fname))[:,:,:3]).astype(float32)
        img = img - mean(img)
        img = imresize(img, (227,227,3), interp='bilinear', mode=None)
        batch_x[i] = img
        if "cat" in fname:
            batch_y[i][0] = 1
        if "dog" in fname:
            batch_y[i][1] = 1
        i+=1
        if i == batch_size:
            yield (batch_x, batch_y)
            batch_x = np.ndarray([batch_size,227, 227, 3])
            batch_y = np.zeros((batch_size, 3))
            i=0




#Define the output number of classes : dogs and cats 
Example #30
Source File: attack_util.py    From robust_physical_perturbations with MIT License 5 votes vote down vote up
def read_and_resize_image(path, newsize):
    '''
    Wrapper to allow easy substitution of resize function.
    Might be extended to allow for different resize methods
    '''
    img = read_img(path)
    if img.shape[0] != newsize[0] or img.shape[1] != newsize[1]:
        return imresize(img, newsize)
    else:
        return img