Python scipy.misc.imread() Examples
The following are 30
code examples of scipy.misc.imread().
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: utils.py From robust_physical_perturbations with MIT License | 6 votes |
def load_single_image(): ''' Reads the image that FLAGS.attack_srcimg is pointing to, normalizes its pixel values to [0.0,1.0] and returns it in a numpy array of shape (1, img_cols, img_rows, nb_channels) :return: a Numpy array containing the image ''' # get the names of all images in the attack source directory # and filter by extension to only include image files # img = np.float32(cv2.imread(FLAGS.attack_srcimg)) img = np.float32(imread(FLAGS.attack_srcimg)) assert img is not None, "Image at %s not loaded"%full_path #Note that the pixel values are being normalized to [0,1] return [img/255.0]
Example #2
Source File: tracklet_utils_3c.py From TNT with GNU General Public License v3.0 | 6 votes |
def hist_feature_extract(feature_size, num_patch, max_length, patch_folder): fea_mat = np.zeros((num_patch,feature_size-4+2)) tracklet_list = os.listdir(patch_folder) N_tracklet = len(tracklet_list) cnt = 0 for n in range(N_tracklet): tracklet_folder = patch_folder+'/'+tracklet_list[n] patch_list = os.listdir(tracklet_folder) # get patch list, track_id and fr_id, starts from 1 prev_cnt = cnt for m in range(len(patch_list)): # track_id fea_mat[cnt,0] = n+1 # fr_id fea_mat[cnt,1] = int(patch_list[m][-8:-4]) patch_list[m] = tracklet_folder+'/'+patch_list[m] patch_img = imread(patch_list[m]) fea_mat[cnt,2:] = track_lib.extract_hist(patch_img) #import pdb; pdb.set_trace() cnt = cnt+1 return fea_mat
Example #3
Source File: fid_score.py From Face-and-Image-super-resolution with MIT License | 6 votes |
def _compute_statistics_of_path_1(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([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 = (imgs/255)*2-1 m, s = calculate_activation_statistics(imgs, model, batch_size, dims, cuda) return m, s
Example #4
Source File: fid_score.py From Face-and-Image-super-resolution with MIT License | 6 votes |
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 #5
Source File: predictions2html.py From ctw-baseline with MIT License | 6 votes |
def create_pkl(): with open(settings.TEST_CLASSIFICATION) as f: lines = f.read().splitlines() with open(settings.TEST_CLASSIFICATION_GT) as f: gt_lines = f.read().splitlines() assert len(lines) == len(gt_lines) test = [] for i, line in enumerate(lines): anno = json.loads(line.strip()) gt_anno = json.loads(gt_lines[i].strip()) image = misc.imread(os.path.join(settings.TEST_IMAGE_DIR, anno['file_name'])) assert image.shape == (anno['height'], anno['width'], 3) assert len(anno['proposals']) == len(gt_anno['ground_truth']) for proposal, gt in zip(anno['proposals'], gt_anno['ground_truth']): cropped = crop(image, proposal['adjusted_bbox'], 32) test.append([cropped, gt]) if i % 100 == 0: print('test', i, '/', len(lines)) with open(settings.TEST_CLS_CROPPED, 'wb') as f: cPickle.dump(test, f)
Example #6
Source File: ade20k_loader.py From PLARD with MIT License | 6 votes |
def __getitem__(self, index): img_path = self.files[self.split][index].rstrip() lbl_path = img_path[:-4] + '_seg.png' img = m.imread(img_path) img = np.array(img, dtype=np.uint8) lbl = m.imread(lbl_path) lbl = np.array(lbl, dtype=np.int32) if self.augmentations is not None: img, lbl = self.augmentations(img, lbl) if self.is_transform: img, lbl = self.transform(img, lbl) return img, lbl
Example #7
Source File: cityscapes_loader.py From PLARD with MIT License | 6 votes |
def __getitem__(self, index): """__getitem__ :param index: """ img_path = self.files[self.split][index].rstrip() lbl_path = os.path.join(self.annotations_base, img_path.split(os.sep)[-2], os.path.basename(img_path)[:-15] + 'gtFine_labelIds.png') img = m.imread(img_path) img = np.array(img, dtype=np.uint8) lbl = m.imread(lbl_path) lbl = self.encode_segmap(np.array(lbl, dtype=np.uint8)) if self.augmentations is not None: img, lbl = self.augmentations(img, lbl) if self.is_transform: img, lbl = self.transform(img, lbl) return img, lbl
Example #8
Source File: sunrgbd_loader.py From PLARD with MIT License | 6 votes |
def __getitem__(self, index): img_path = self.files[self.split][index].rstrip() lbl_path = self.anno_files[self.split][index].rstrip() # img_number = img_path.split('/')[-1] # lbl_path = os.path.join(self.root, 'annotations', img_number).replace('jpg', 'png') img = m.imread(img_path) img = np.array(img, dtype=np.uint8) lbl = m.imread(lbl_path) lbl = np.array(lbl, dtype=np.uint8) if not (len(img.shape) == 3 and len(lbl.shape) == 2): return self.__getitem__(np.random.randint(0, self.__len__())) if self.augmentations is not None: img, lbl = self.augmentations(img, lbl) if self.is_transform: img, lbl = self.transform(img, lbl) return img, lbl
Example #9
Source File: tracklet_utils_3c.py From TNT with GNU General Public License v3.0 | 6 votes |
def hist_feature_extract(feature_size, num_patch, max_length, patch_folder): fea_mat = np.zeros((num_patch,feature_size-4+2)) tracklet_list = os.listdir(patch_folder) N_tracklet = len(tracklet_list) cnt = 0 for n in range(N_tracklet): tracklet_folder = patch_folder+'/'+tracklet_list[n] patch_list = os.listdir(tracklet_folder) # get patch list, track_id and fr_id, starts from 1 prev_cnt = cnt for m in range(len(patch_list)): # track_id fea_mat[cnt,0] = n+1 # fr_id fea_mat[cnt,1] = int(patch_list[m][-8:-4]) patch_list[m] = tracklet_folder+'/'+patch_list[m] patch_img = imread(patch_list[m]) fea_mat[cnt,2:] = track_lib.extract_hist(patch_img) #import pdb; pdb.set_trace() cnt = cnt+1 return fea_mat
Example #10
Source File: nyuv2_loader.py From PLARD with MIT License | 6 votes |
def __getitem__(self, index): img_path = self.files[self.split][index].rstrip() img_number = img_path.split("_")[-1][:4] lbl_path = os.path.join( self.root, self.split + "_annot", "new_nyu_class13_" + img_number + ".png" ) img = m.imread(img_path) img = np.array(img, dtype=np.uint8) lbl = m.imread(lbl_path) lbl = np.array(lbl, dtype=np.uint8) if not (len(img.shape) == 3 and len(lbl.shape) == 2): return self.__getitem__(np.random.randint(0, self.__len__())) if self.augmentations is not None: img, lbl = self.augmentations(img, lbl) if self.is_transform: img, lbl = self.transform(img, lbl) return img, lbl
Example #11
Source File: mit_sceneparsing_benchmark_loader.py From PLARD with MIT License | 6 votes |
def __getitem__(self, index): """__getitem__ :param index: """ img_path = self.files[self.split][index].rstrip() lbl_path = os.path.join(self.annotations_base, os.path.basename(img_path)[:-4] + '.png') img = m.imread(img_path) img = np.array(img, dtype=np.uint8) lbl = m.imread(lbl_path) lbl = np.array(lbl, dtype=np.uint8) if self.augmentations is not None: img, lbl = self.augmentations(img, lbl) if self.is_transform: img, lbl = self.transform(img, lbl) return img, lbl
Example #12
Source File: create_tfrecords.py From DNA-GAN with MIT License | 6 votes |
def create_tf_example(line, attribute_name, img_dir): info = line.split() img_name = os.path.join(img_dir, info[0]) img = misc.imread(img_name) # from IPython import embed; embed();exit() feature={ 'image/id_name': bytes_feature(info[0]), 'image/height' : int64_feature(img.shape[0]), 'image/width' : int64_feature(img.shape[1]), 'image/encoded': bytes_feature(tf.compat.as_bytes(img.tostring())), } for j, val in enumerate(info[1:]): feature[attribute_name[j]] = int64_feature(int(val)) example = tf.train.Example(features=tf.train.Features(feature=feature)) return example
Example #13
Source File: getImgs.py From crawl-dataset with ISC License | 6 votes |
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 #14
Source File: getImgs.py From crawl-dataset with ISC License | 6 votes |
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 #15
Source File: phoframeTrain.py From videoToVoice with MIT License | 6 votes |
def getInVidsAtFrame(f): arr = np.zeros([1, INVID_HEIGHT,INVID_WIDTH,INVID_DEPTH]) for imageIndex in range(0,29): strIndex = str(f-14+imageIndex) while len(strIndex) < 4: strIndex = "0"+strIndex newImage = misc.imread('3/mouthImages/frame'+strIndex+'.jpg') if newImage.shape[0] > INVID_HEIGHT: extraMargin = (newImage.shape[0]-INVID_HEIGHT)//2 newImage = newImage[extraMargin:extraMargin+INVID_HEIGHT,:,:] if newImage.shape[1] > INVID_WIDTH: extraMargin = (newImage.shape[1]-INVID_WIDTH)//2 newImage = newImage[:,extraMargin:extraMargin+INVID_WIDTH,:] h = newImage.shape[0] w = newImage.shape[1] yStart = (INVID_HEIGHT-h)//2 xStart = (INVID_WIDTH-w)//2 arr[:,yStart:yStart+h,xStart:xStart+w,imageIndex*3:(imageIndex+1)*3] = newImage return np.asarray(arr)/255.0
Example #16
Source File: data_loader.py From BicycleGAN-Tensorflow with MIT License | 6 votes |
def read_image(path): image = imread(path) if len(image.shape) != 3 or image.shape[2] != 3: print('Wrong image {} with shape {}'.format(path, image.shape)) return None # split image h, w, c = image.shape assert w in [256, 512, 1200], 'Image size mismatch ({}, {})'.format(h, w) assert h in [128, 256, 600], 'Image size mismatch ({}, {})'.format(h, w) if 'maps' in path: image_a = image[:, w/2:, :].astype(np.float32) / 255.0 image_b = image[:, :w/2, :].astype(np.float32) / 255.0 else: image_a = image[:, :w/2, :].astype(np.float32) / 255.0 image_b = image[:, w/2:, :].astype(np.float32) / 255.0 # range of pixel values = [-1.0, 1.0] image_a = image_a * 2.0 - 1.0 image_b = image_b * 2.0 - 1.0 return image_a, image_b
Example #17
Source File: phoframeTest.py From videoToVoice with MIT License | 6 votes |
def getInVidsAtFrame(f): arr = np.zeros([1, INVID_HEIGHT,INVID_WIDTH,INVID_DEPTH]) for imageIndex in range(0,29): strIndex = str(f-14+imageIndex) while len(strIndex) < 4: strIndex = "0"+strIndex newImage = misc.imread('3/mouthImages/frame'+strIndex+'.jpg') if newImage.shape[0] > INVID_HEIGHT: extraMargin = (newImage.shape[0]-INVID_HEIGHT)//2 newImage = newImage[extraMargin:extraMargin+INVID_HEIGHT,:,:] if newImage.shape[1] > INVID_WIDTH: extraMargin = (newImage.shape[1]-INVID_WIDTH)//2 newImage = newImage[:,extraMargin:extraMargin+INVID_WIDTH,:] h = newImage.shape[0] w = newImage.shape[1] yStart = (INVID_HEIGHT-h)//2 xStart = (INVID_WIDTH-w)//2 arr[:,yStart:yStart+h,xStart:xStart+w,imageIndex*3:(imageIndex+1)*3] = newImage return np.asarray(arr)/255.0
Example #18
Source File: getImages.py From crawl-dataset with ISC License | 6 votes |
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 #19
Source File: tracklet_utils_3c.py From TNT with GNU General Public License v3.0 | 5 votes |
def convert_frames_to_video(pathIn,pathOut,fps): frame_array = [] files = [f for f in os.listdir(pathIn) if os.path.isfile(os.path.join(pathIn, f))] #for sorting the file names properly #files.sort(key = lambda x: int(x[5:-4])) for i in range(len(files)): filename=pathIn + files[i] #reading each files img = cv2.imread(filename) height, width, layers = img.shape if i==0: size = (width,height) img = cv2.resize(img,size) #print(filename) #inserting the frames into an image array frame_array.append(img) out = cv2.VideoWriter(pathOut,cv2.VideoWriter_fourcc(*'DIVX'), fps, size) for i in range(len(frame_array)): # writing to a image array out.write(frame_array[i]) out.release()
Example #20
Source File: DatasetLoad.py From deepJDOT with MIT License | 5 votes |
def office_31_dataload(dataname='amazon'): from scipy.misc import imread, imresize import matplotlib.pylab as plt import os import numpy as np pathname = os.path.join('/home/damodara/OT/DA/datasets/office31', dataname) images = [] label = [] count = -1 l = -1 files_path = os.path.join(pathname, 'images') img_files = os.listdir(files_path) for imgf in img_files: l = l + 1 img_names = os.listdir(os.path.join(files_path, imgf)) for i in img_names: count = count + 1 tmp = imread(os.path.join(files_path, imgf, i)) if tmp.shape[1] != 300: tmp = imresize(tmp, (300, 300, 3)) images.append(tmp) label.append(l) return np.array(images), label #%% Regression datasets
Example #21
Source File: DatasetLoad.py From deepJDOT with MIT License | 5 votes |
def mnist_m_dataload(): import pickle as pkl import numpy as np import os from scipy.misc import imread img_path = '/home/damodara/OT/DA/datasets/mnist-m' train_path = os.path.join(img_path, 'mnistm_data_keras.pkl') mnist_m_train = pkl.load(open(train_path, 'rb')) Traindata = mnist_m_train['train'] train_label = mnist_m_train['trainlabel'] Testdata = mnist_m_train['test'] test_label = mnist_m_train['testlabel'] # Testdata = mnist_m_train['test'] # test_label = mnist_m_train['testlabel'] # train_path = os.path.join(img_path, 'mnist-m_train.pkl') # mnist_m_train = pkl.load(open(train_path, 'rb')) # Traindata = mnist_m_train['Traindata'] # train_label = mnist_m_train['train_label'] # # test_path = os.path.join(img_path, 'mnist-m_test.pkl') # mnist_m_test = pkl.load(open(test_path, 'rb')) # Testdata = mnist_m_test['Testdata'] # Testdata = np.array(Testdata) # test_label = mnist_m_test['test_label'] return Traindata, train_label, Testdata, test_label # %% Synthetic digits
Example #22
Source File: videofig.py From SiamFC-TensorFlow with MIT License | 5 votes |
def redraw_fn(f, axes): img_file = img_files[f] img = imread(img_file) if not redraw_fn.initialized: redraw_fn.im = axes.imshow(img, animated=True) redraw_fn.initialized = True else: redraw_fn.im.set_array(img)
Example #23
Source File: tracklet_utils_3c.py From TNT with GNU General Public License v3.0 | 5 votes |
def crop_det(tracklet_mat, crop_size, img_folder, crop_det_folder, flag): if not os.path.isdir(crop_det_folder): os.makedirs(crop_det_folder) N_tracklet = tracklet_mat['xmin_mat'].shape[0] T = tracklet_mat['xmin_mat'].shape[1] img_list = os.listdir(img_folder) cnt = 0 for n in range(T): track_ids = np.where(tracklet_mat['xmax_mat'][:,n]!=-1) if len(track_ids)==0: continue track_ids = track_ids[0] img_name = track_lib.file_name(n+1,file_len)+'.jpg' if img_name in img_list: img_path = img_folder+'/'+img_name img = misc.imread(img_path) img_size = img.shape else: continue for m in range(len(track_ids)): if flag==0: xmin = int(max(0,tracklet_mat['xmin_mat'][track_ids[m],n])) xmax = int(min(img.shape[1]-1,tracklet_mat['xmax_mat'][track_ids[m],n])) ymin = int(max(0,tracklet_mat['ymin_mat'][track_ids[m],n])) ymax = int(min(img.shape[0]-1,tracklet_mat['ymax_mat'][track_ids[m],n])) img_patch = img[ymin:ymax,xmin:xmax,:] img_patch = misc.imresize(img_patch, size=[crop_size,crop_size]) class_name = track_lib.file_name(track_ids[m]+1,4) patch_name = class_name+'_'+track_lib.file_name(n+1,4)+'.png' save_path = crop_det_folder+'/'+class_name if not os.path.isdir(save_path): os.makedirs(save_path) save_path = save_path+'/'+patch_name #import pdb; pdb.set_trace() misc.imsave(save_path, img_patch) cnt = cnt+1 return cnt, img_size
Example #24
Source File: facenet.py From TNT with GNU General Public License v3.0 | 5 votes |
def load_data(image_paths, do_random_crop, do_random_flip, image_size, do_prewhiten=True): nrof_samples = len(image_paths) images = np.zeros((nrof_samples, image_size, image_size, 3)) for i in range(nrof_samples): img = misc.imread(image_paths[i]) if img.ndim == 2: img = to_rgb(img) if do_prewhiten: img = prewhiten(img) img = crop(img, do_random_crop, image_size) img = flip(img, do_random_flip) images[i,:,:,:] = img return images
Example #25
Source File: tracklet_utils_2d_online.py From TNT with GNU General Public License v3.0 | 5 votes |
def convert_frames_to_video(pathIn,pathOut,fps): frame_array = [] files = [f for f in os.listdir(pathIn) if os.path.isfile(os.path.join(pathIn, f))] #for sorting the file names properly files.sort(key = lambda x: int(x[5:-4])) for i in range(len(files)): filename=pathIn + files[i] #reading each files img = cv2.imread(filename) height, width, layers = img.shape if i==0: size = (width,height) img = cv2.resize(img,size) #print(filename) #inserting the frames into an image array frame_array.append(img) out = cv2.VideoWriter(pathOut,cv2.VideoWriter_fourcc(*'DIVX'), fps, size) for i in range(len(frame_array)): # writing to a image array out.write(frame_array[i]) out.release()
Example #26
Source File: pose_evaluation_utils.py From SfmLearner-Pytorch with MIT License | 5 votes |
def generator(self): for img_list, pose_list, sample_list in zip(self.img_files, self.poses, self.sample_indices): for snippet_indices in sample_list: imgs = [imread(img_list[i]).astype(np.float32) for i in snippet_indices] poses = np.stack(pose_list[i] for i in snippet_indices) first_pose = poses[0] poses[:,:,-1] -= first_pose[:,-1] compensated_poses = np.linalg.inv(first_pose[:,:3]) @ poses yield {'imgs': imgs, 'path': img_list[0], 'poses': compensated_poses }
Example #27
Source File: preprocessor.py From face_classification with MIT License | 5 votes |
def _imread(image_name): return imread(image_name)
Example #28
Source File: tracklet_utils_3d.py From TNT with GNU General Public License v3.0 | 5 votes |
def convert_frames_to_video(pathIn,pathOut,fps): frame_array = [] files = [f for f in os.listdir(pathIn) if os.path.isfile(os.path.join(pathIn, f))] #for sorting the file names properly #files.sort(key = lambda x: int(x[5:-4])) for i in range(len(files)): filename=pathIn + files[i] #reading each files img = cv2.imread(filename) height, width, layers = img.shape if i==0: size = (width,height) img = cv2.resize(img,size) #print(filename) #inserting the frames into an image array frame_array.append(img) out = cv2.VideoWriter(pathOut,cv2.VideoWriter_fourcc(*'DIVX'), fps, size) for i in range(len(frame_array)): # writing to a image array out.write(frame_array[i]) out.release()
Example #29
Source File: tracklet_utils_3d_online.py From TNT with GNU General Public License v3.0 | 5 votes |
def TC_tracker(): global track_struct init_TC_tracker() # initialize triplet model global triplet_graph global triplet_sess init_triplet_model() # initialize tracklet model global tracklet_graph global tracklet_sess init_tracklet_model() M = track_lib.load_detection(track_struct['file_path']['det_path'], 'KITTI_3d') total_num_fr = int(M[-1,0]-M[0,0]+1) t_pointer = 0 for n in range(total_num_fr): print("fr_idx %d" % n) fr_idx = n idx = np.where(np.logical_and(M[:,0]==fr_idx,M[:,5]>track_struct['track_params']['det_thresh']))[0] if len(idx)>1: choose_idx, _ = track_lib.merge_bbox(M[idx,1:5], 0.3, M[idx,5]) #import pdb; pdb.set_trace() temp_M = M[idx[choose_idx],:] else: temp_M = M[idx,:] img_name = track_lib.file_name(fr_idx,10)+'.png' img_path = track_struct['file_path']['img_folder']+'/'+img_name img = misc.imread(img_path) TC_online(temp_M, img, t_pointer, fr_idx) t_pointer = t_pointer+1 if t_pointer>track_struct['track_params']['num_fr']: t_pointer = track_struct['track_params']['num_fr'] return track_struct
Example #30
Source File: facenet.py From TNT with GNU General Public License v3.0 | 5 votes |
def load_data(image_paths, do_random_crop, do_random_flip, image_size, do_prewhiten=True): nrof_samples = len(image_paths) images = np.zeros((nrof_samples, image_size, image_size, 3)) for i in range(nrof_samples): img = misc.imread(image_paths[i]) if img.ndim == 2: img = to_rgb(img) if do_prewhiten: img = prewhiten(img) img = crop(img, do_random_crop, image_size) img = flip(img, do_random_flip) images[i,:,:,:] = img return images