def SavingAsNpy(CT_list, PET_list, CT_Tr_path, PET_Tr_path, CT_Ts_path, PET_Ts_path, prefix=""): count_ts = 0 count_tr = 0 for j in range(len(CT_list)): print(j,'/',len(CT_list)-1) #print() if PET_list[0].split('/')[-2] == CT_list[0].split('/')[-2]: #Save ~30% of patients data in the test files if j < len(CT_list)*0.3: CT = sitk.ReadImage(CT_list[j]) dst_CT_name = "CTPET_3D_"+ prefix +'_'+ str(count_ts).zfill(6) + ".nii.gz" dst_CT_path = os.path.join(CT_Ts_path, dst_CT_name) sitk.WriteImage(CT,dst_CT_path ) PET = sitk.ReadImage(PET_list[j]) dst_PET_name = "CTPET_3D_"+ prefix +'_'+ str(count_ts).zfill(6) + ".nii.gz" dst_PET_path = os.path.join(PET_Ts_path, dst_PET_name) sitk.WriteImage(PET,dst_PET_path ) count_ts += 1 else: CT = io.imread(CT_list[j], plugin='simpleitk') #CT = np.array(CT) PET = io.imread(PET_list[j], plugin='simpleitk') #PET = np.array(PET) CT_path = CT_Tr_path PET_path = PET_Tr_path #Saving channel CT & PET images for k in range(CT.shape[0]): CT_k = np.array( CT[k,:,:] ) PET_k = np.array( PET[k,:,:] ) dst_img_name = "CTPET_"+ prefix +'_'+ str(count_tr).zfill(6) + ".npy" dst_img_path = os.path.join(CT_path, dst_img_name) np.save(dst_img_path, CT_k) dst_label_name = "CTPET_"+ prefix +'_'+ str(count_tr).zfill(6) + ".npy" dst_mask_path = os.path.join(PET_path, dst_label_name) np.save(dst_mask_path, PET_k) count_tr += 1 return (count_ts, count_tr)
def load_example(self, img_f): # load the image and the binary mask X = io.imread(os.path.join(self.path, 'images', img_f)) mask = scipy.io.loadmat( os.path.join(self.path, 'images', img_f.replace('.jpg', 'mask.mat')))['BW'] mask = mask[:, :, np.newaxis].astype('float32') img_centers = self.centers[img_f] # reduce the size of image and mask by the given amount H_orig, W_orig = X.shape[0], X.shape[1] if H_orig != self.out_shape[0] or W_orig != self.out_shape[1]: X = SkT.resize(X, self.out_shape, preserve_range=True).astype('uint8') mask = SkT.resize(mask, self.out_shape, preserve_range=True).astype('float32') # compute the density map density = density_map((H_orig, W_orig), img_centers, self.gamma * np.ones((len(img_centers), 2)), out_shape=self.out_shape) density = density[:, :, np.newaxis].astype('float32') return X, mask, density
def mask(self): roifile = self.roi_folder / self.img_file.with_suffix(".png").name self.image_mask = io.imread(roifile) [rows, cols] = np.where(self.image_mask) row1 = min(rows) row2 = max(rows) col1 = min(cols) col2 = max(cols) xdiff = row2 - row1 remainder = self.max_mask_x - xdiff left = remainder // 2 right = remainder - left ydiff = col2 - col1 remainder = self.max_mask_y - ydiff top = remainder // 2 bottom = remainder - top row1 -= left row2 += right col1 -= top col2 += bottom self.img = self.img[row1:row2, col1:col2, :] self.image_mask = self.image_mask[row1:row2, col1:col2]
def load_sequence(self, sequence): """Load a sequence of images/frames. Auxiliary function that loads a sequence of frames with the corresponding ground truth and their filenames. Returns a dict with the images in [0, 1], their corresponding labels, their subset (i.e. category, clip, prefix) and their filenames. """ from skimage import io from PIL import Image import numpy as np X = [] Y = [] F = [] for prefix, frame in sequence: img = io.imread(os.path.join(self.image_path, frame)) img = img.astype(floatX) / 255. # mask = io.imread(os.path.join(self.mask_path, frame)) mask = Image.open(os.path.join(self.mask_path, frame)) mask = np.array(mask) mask = mask.astype('int32') X.append(img) Y.append(mask) F.append(frame) ret = {} ret['data'] = np.array(X) ret['labels'] = np.array(Y) ret['subset'] = prefix ret['filenames'] = np.array(F) return ret
def get_minbatch(self, batch_size, time, type='train', augmentation=True): images = [] labels = [] data_dir_list = os.listdir(self.img_path) data_index = np.copy(self.validation) if type == 'train': data_index = np.copy(self.train) elif type == 'test': data_index = np.copy(self.test) echoe = (len(data_index) - batch_size) // batch_size start_index = (time % echoe) * batch_size np.random.seed((time // echoe) + 1) np.random.shuffle(data_index) for index in range(start_index, start_index + batch_size, 1): img_index = data_index[index] image = io.imread(os.path.join(self.img_path + '/' + data_dir_list[img_index]), as_gray=False) image = resize(image, (self.image_height, self.image_width)) if image.max() > 1: image = image / 255. if augmentation and np.random.randint(0, 10) < 5: image = np.rot90(image, np.random.randint(1, 4)) images.append(image) labels.append(self.labels[img_index]) return np.array(images), np.reshape(labels, (-1, 1))
def changeObjectImage(self, path): im = io.imread(path) self.image = im # TODO: resize image im = np.array(im) image_data = vtk.vtkImageData() image_array = numpy_support.numpy_to_vtk( im.ravel(), deep=True, array_type=vtk.VTK_UNSIGNED_CHAR) image_data.SetDimensions((im.shape[0], im.shape[1], 1)) image_data.SetOrigin([0, 0, 0]) image_data.GetPointData().SetScalars(image_array) image_filter = vtk.vtkImageDataGeometryFilter() image_filter.SetInputData(image_data) image_filter.Update() image_mapper = vtk.vtkPolyDataMapper() image_mapper.SetInputConnection(image_filter.GetOutputPort()) image_actor = vtk.vtkActor() image_actor.SetMapper(image_mapper) image_actor.GetProperty().SetColor(OBJECT_COLOR) self.renderer.RemoveActor(self.object) self.object = image_actor self.renderer.AddActor(self.object)
def Load_RGB(path, h, w): img = io.imread(path) img = color.rgba2rgb(img) # RGB [224, 224, 3] img = imresize(img, (h, w), interp='bilinear') return img / 255.0
def to_numpy_array(data_frame=None, image_shape=(224, 224), data_path=None): """ Converts the images and store them as numpy array Args: image_shape (tuple()): (224,224) or (299,299) for different networks data_frame: dataframe with all the metadata data_path: path to the images Returns: data_numpy_array """ data_numpy_array = np.zeros((len(data_frame), image_shape[0], image_shape[1], 3)) print('Cropping, resizing and saving as a numpy array') for index, row in data_frame.iterrows(): if index % 100 == 0: print(index) image_path = data_path + row['image_name'] img = io.imread(image_path) img_crop = img[int(row['ymin']):int(row['ymax']), int(row['xmin']):int(row['xmax']), :3] img_resize = resize(img_crop, (image_shape[0], image_shape[1])) data_numpy_array[index] = img_resize return data_numpy_array
def crowdAI_show( TRAIN_IMAGES_DIRECTORY=r"D:\work\data\训练数据\遥感目标检测\crowdai\train\train\images", TRAIN_ANNOTATIONS_PATH=r"D:\work\data\训练数据\遥感目标检测\crowdai\train\train\annotation-small.json" ): from pycocotools.coco import COCO import skimage.io as io import matplotlib.pyplot as plt import random import pylab fig = plt.figure() pylab.rcParams['figure.figsize'] = (8.0, 10.0) # TRAIN_ANNOTATIONS_PATH = r"D:\work\data\训练数据\遥感目标检测\crowdai\train\train\annotation.json" # TRAIN_ANNOTATIONS_SMALL_PATH = coco = COCO(TRAIN_ANNOTATIONS_PATH) category_ids = coco.loadCats(coco.getCatIds()) image_ids = coco.getImgIds(catIds=coco.getCatIds()) random_image_id = random.choice(image_ids) img = coco.loadImgs(random_image_id)[0] image_path = os.path.join(TRAIN_IMAGES_DIRECTORY, img["file_name"]) I = io.imread(image_path) annotation_ids = coco.getAnnIds(imgIds=img['id']) annotations = coco.loadAnns(annotation_ids) # load and render the image plt.imshow(I) plt.axis('off') # Render annotations on top of the image coco.showAnns(annotations) plt.savefig(rf'D:\test\draw_pic\{random_image_id}.png', bbox_inches='tight', pad_inches=0)
def load_example(self, img_f): X = io.imread(os.path.join(self.path, img_f)) mask_f = os.path.join(img_f.split(os.sep)[0], img_f.split(os.sep)[1]) + '-msk.npy' mask = np.load(os.path.join(self.path, mask_f)) mask = mask[:, :, np.newaxis].astype('float32') bndboxes = self.bndboxes[img_f] H_orig, W_orig = X.shape[0], X.shape[1] # reduce the size of image and mask by the given amount H_orig, W_orig = X.shape[0], X.shape[1] if H_orig != self.out_shape[0] or W_orig != self.out_shape[1]: X = SkT.resize(X, self.out_shape, preserve_range=True).astype('uint8') mask = SkT.resize(mask, self.out_shape, preserve_range=True).astype('float32') # compute the density map img_centers = [(int((xmin + xmax) / 2.), int((ymin + ymax) / 2.)) for xmin, ymin, xmax, ymax in bndboxes] gammas = self.gamma * np.array( [[1. / np.absolute(xmax - xmin), 1. / np.absolute(ymax - ymin)] for xmin, ymin, xmax, ymax in bndboxes]) # gammas = self.gamma*np.ones((len(bndboxes), 2)) density = density_map((H_orig, W_orig), img_centers, gammas, out_shape=self.out_shape) density = density[:, :, np.newaxis].astype('float32') return X, mask, density
def tiff_to_binary(ops): nplanes = ops['nplanes'] nchannels = ops['nchannels'] ops1 = [] # open all binary files for writing reg_file = [] if nchannels > 1: reg_file_chan2 = [] for j in range(0, nplanes): fpath = os.path.join(ops['save_path0'], 'suite2p', 'plane%d' % j) ops['save_path'] = fpath if ('fast_disk' not in ops) or len(ops['fast_disk']) > 0: ops['fast_disk'] = ops['save_path0'] ops['fast_disk'] = os.path.join(ops['fast_disk'], 'suite2p', 'plane%d' % j) ops['ops_path'] = os.path.join(ops['save_path'], 'ops.npy') ops['reg_file'] = os.path.join(ops['fast_disk'], 'data.bin') if nchannels > 1: ops['reg_file_chan2'] = os.path.join(ops['fast_disk'], 'data_chan2.bin') if not os.path.isdir(ops['fast_disk']): os.makedirs(ops['fast_disk']) if not os.path.isdir(ops['save_path']): os.makedirs(ops['save_path']) ops1.append(ops.copy()) reg_file.append(open(ops['reg_file'], 'wb')) if nchannels > 1: reg_file_chan2.append(open(ops['reg_file_chan2'], 'wb')) fs, ops = get_tif_list(ops) # look for tiffs in all requested folders # loop over all tiffs for ik, file in enumerate(fs): # keep track of the plane identity of the first frame (channel identity is assumed always 0) if ops['first_tiffs'][ik]: iplane = 0 im = io.imread(file) if len(im.shape) < 3: im = np.expand_dims(im, axis=0) nframes = im.shape[0] for j in range(0, nplanes): i0 = nchannels * ((iplane + j) % nplanes) im2write = im[np.arange(int(i0), nframes, nplanes * nchannels), :, :] reg_file[j].write(bytearray(im2write)) if nchannels > 1: im2write = im[ np.arange(int(i0) + 1, nframes, nplanes * nchannels), :, :] reg_file_chan2[j].write(bytearray(im2write)) iplane = (iplane + nframes / nchannels) % nplanes # write ops files for ops in ops1: ops['Ly'] = im.shape[1] ops['Lx'] = im.shape[2] np.save(ops['ops_path'], ops) # close all binary files and write ops files for j in range(0, nplanes): reg_file[j].close() if nchannels > 1: reg_file_chan2[j].close() return ops1
def load_mask(self, idx, label_type=None): """ Load mask image as 1 x height x width integer array of label indices. The leading singleton dimension is required by the loss. """ data = io.imread('{}/full_mask/{}.png'.format(self.dir_, idx)) data = data[np.newaxis, ...] return data.copy()
def processOneImage(inputPath, outputPath): image = io.imread(inputPath) greyImage = rgb2grey(image) threshold = threshold_otsu(greyImage) imgout = closing(greyImage > threshold, square(1)) imgout = crop(imgout) imgout = transform.resize(imgout, (max(imgout.shape), max(imgout.shape))) io.imsave(outputPath, imgout)
def __getitem__(self, ix): # cargar la imágen img = io.imread(self.X[ix]) # aplicar transformaciones if self.trans: img = self.trans(image=img)["image"] return torch.from_numpy(img / 255.).float().permute( 2, 0, 1), torch.tensor(self.y[ix])
def selective_boxes(filename): img = io.imread(filename) img_lbl, regions = selectivesearch.selective_search(img, scale=500, sigma=0.8, min_size=10) # print len(regions) candidates = set() for r in regions: if r['rect'] in candidates: continue if r['size'] < 1000: continue x, y, w, h = r['rect'] if w / h > 1.2 or h / w > 1.2: continue candidates.add(r['rect']) fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6)) ax.imshow(img) filterd_candidates = candidates.copy() for c in candidates: x, y, w, h = c for _x, _y, _w, _h in candidates: if x == _x and y == _y and w == _w and h == _h: continue if abs(x - _x) < 10 and \ abs(y - _y) < 10 and \ w * h - _w * _h> 0: filterd_candidates.discard((_x, _y, _w, _h)) # print "candidates_length is", len(candidates) # print len(filterd_candidates) npboxes = [] for x, y, w, h in filterd_candidates: # print x,y,w,h rect = mpatches.Rectangle((x, y), w, h, fill=False, edgecolor='red', linewidth=1) ax.add_patch(rect) boxes = [x, y, x + w, y + h] npboxes.append(boxes) # fig.savefig('img.png') return npboxes
def ima(filename, n): """ Resizes the image """ ims = io.imread(filename) im = np.float32(ims[n]) / 1024.0 im = transforms.ToPILImage()(torch.tensor(im).unsqueeze_(0)) im = transforms.Resize(resolution)(im) return im
def __getitem__(self, index): img_path = os.path.join(self.root_dir, self.annotations.iloc[index, 0]) image = io.imread(img_path) y_label = torch.tensor(int(self.annotations.iloc[index, 1])) if self.transform: image = self.transform(image) return (image, y_label)
def load_label(self, idx, label_type=None): """ Load label image as 1 x height x width integer array of label indices. The leading singleton dimension is required by the loss. """ data = io.imread('{}/output_depth/depth_sph_corr-{}.png'.format( self.dir_, idx)) data = np.array(data, dtype=np.float32) data = data[np.newaxis, ...] return data
def Load_Mask(path, ray_per_image, random_uv): img = io.imread(path) output = np.array( [img[random_uv[1, i], random_uv[0, i]] for i in range(ray_per_image)], dtype='float32') output[output < 65535.0 - 1e-04] = 0.0 output[output >= 65535.0 - 1e-04] = 1.0 return output
def __getitem__(self, index): image_name = str(self.image_dir / self.data_list[index][0] / (self.data_list[index][1] + ".jpg")) image = io.imread(image_name) landmarks = np.array(self.data_list[index][2]).astype("float") sample = {"image": image, "landmarks": landmarks} if self.transform: sample = self.transform(sample) return sample
def predictImage(img_path, theta_path): brands = ["audi", "benz", "bmw", "chevrolet", "honda", "lexus", "toyota", "volkswagon"] processOneImage(img_path, './temp.jpg') image = io.imread('./temp.jpg') image = transform.resize(image, (400, 400)) features = np.array([hog(image, orientations=8, pixels_per_cell=(20, 20), cells_per_block=(1, 1))]) thetas = np.transpose(np.load(theta_path)) os.remove('./temp.jpg') prediction = predict(thetas, features) return brands[prediction]
def get_saliency_for_deepnet(image_url, sal_url): salnet = SalNet(specfile, modelfile) arr_files = glob.glob(image_url + "*.jpg") for i in range(len(arr_files)): url_image = arr_files[i] img = io.imread(url_image) img = np.asarray(img, dtype='float32') if len(img.shape) == 2: img = to_rgb(img) sal_map = salnet.get_saliency(img) #saliency = misc.imresize(y,(img.shape[0],img.shape[1])) aux = url_image.split("/")[-1].split(".")[0] misc.imsave(sal_url + '/' + aux + '.png', sal_map)
def get_saliency_for_salnet(image_url,sal_url): salnet = SalNet(specfile,modelfile) arr_files = glob.glob(image_url+"*.png") for i in range(len(arr_files)): url_image = arr_files[i] img = io.imread(url_image) img = np.asarray(img, dtype = 'float32') if len(img.shape) == 2: img = to_rgb(img) sal_map = salnet.get_saliency(img) #saliency = misc.imresize(y,(img.shape[0],img.shape[1])) aux = url_image.split("/")[-1].split(".")[0] misc.imsave(sal_url+'/'+aux+'.png', sal_map)
def load_image(self, idx): """ Load input image and preprocess for Caffe: - cast to float - switch channels RGB -> BGR - subtract mean - transpose to channel x height x width order """ data = io.imread('{}/train_image/{}.png'.format(self.dir_, idx)) data = np.array(data, dtype=np.float32) data = data[:, :, ::-1] data -= self.mean data = data.transpose((2, 0, 1)) return data
def image_classification_samples(folder='x_train_play'): ''' Inputs: folder; String type. Folder name of which population you want to pull samples from. Returns: 6x (1x6) plots displaying random sample images from each class of land cover. ''' cols = [0, 1, 2, 3, 4, 5] fig, ax = plt.subplots(1, 6, figsize=(15, 15)) for i in cols: plt.grid(b=None, which='both') building = io.imread('data/{}/0_building/building{}.png'.format( folder, np.random.randint(0, 40))) ax[i].imshow(building) fig.suptitle('building', x=0.03, y=.5) fig, ax = plt.subplots(1, 6, figsize=(15, 15)) for i in cols: barren = io.imread('data/{}/1_barren_land/barren{}.png'.format( folder, np.random.randint(0, 40))) ax[i].imshow(barren) fig.suptitle('barren', x=0.03, y=.5) fig, ax = plt.subplots(1, 6, figsize=(15, 15)) for i in cols: tree = io.imread('data/{}/2_tree/tree{}.png'.format( folder, np.random.randint(0, 40))) ax[i].imshow(tree) fig.suptitle('tree', x=0.03, y=.5) fig, ax = plt.subplots(1, 6, figsize=(15, 15)) for i in cols: grass = io.imread('data/{}/3_grassland/grassland{}.png'.format( folder, np.random.randint(0, 40))) ax[i].imshow(grass) fig.suptitle('grass', x=0.03, y=.5) fig, ax = plt.subplots(1, 6, figsize=(15, 15)) for i in cols: road = io.imread('data/{}/4_road/road{}.png'.format( folder, np.random.randint(0, 40))) ax[i].imshow(road) fig.suptitle('road', x=0.03, y=.5) fig, ax = plt.subplots(1, 6, figsize=(15, 15)) for i in cols: water = io.imread('data/{}/5_water/water{}.png'.format( folder, np.random.randint(0, 40))) ax[i].imshow(water) fig.suptitle('water', x=0.03, y=.5) plt.show()
def prepareImage(imagePath): """ Prepare images before use it. """ print("> Prepare image "+imagePath + ":") imname = ntpath.basename(imagePath) imname = imname.split(imname.split('.')[-1])[0][0:-1] # 记录图片basename img = cv2.imread( imagePath ) # 加载图片 if needCrop: dlib_img = io.imread(imagePath) img2 = cv2.copyMakeBorder(img,0,0,0,0,cv2.BORDER_REPLICATE) detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(FLAGS.predictor_path) # 加载 dlib 检测 dets = detector(img, 1) print("> Number of faces detected: {}".format(len(dets))) if len(dets) == 0: print '> Could not detect the face, skipping the image...' + image_path return None if len(dets) > 1: print "> Process only the first detected face!" # 标记第一个人脸 detected_face = dets[0] cv2.rectangle(img2, (detected_face.left(),detected_face.top()), \ (detected_face.right(),detected_face.bottom()), (0,0,255),2) fileout = open(os.path.join(FLAGS.tmp_detect , imname + ".bbox"),"w") fileout.write("%f %f %f %f\n" % (detected_face.left(),detected_face.top(), \ detected_face.right(),detected_face.bottom())) fileout.close() ## If we are using landmarks to crop if useLM: shape = predictor(dlib_img, detected_face) nLM = shape.num_parts fileout = open(os.path.join(FLAGS.tmp_detect, imname + ".pts" ), "w") for i in range(0, nLM): cv2.circle( img2, (shape.part(i).x, shape.part(i).y), 5, (255,0,0)) fileout.write("%f %f\n" % (shape.part(i).x, shape.part(i).y)) fileout.close() img = utils.cropByLM(img, shape, img2) else: print "> cropByFaceDet " img = utils.cropByFaceDet(img, detected_face, img2) cv2.imwrite(os.path.join( FLAGS.tmp_detect, imname+"_detect.png"), img2) img = cv2.resize(img, (trg_size, trg_size)) cv2.imwrite(os.path.join(FLAGS.tmp_ims, imname + '.png'), img) return img
def load_depth(self, idx): """ Load depth map and preprocess: - resize - cast to float - subtract mean """ idx = self.indices_depth[idx] idx=idx.split()[0] im = io.imread('{}/depth/{}'.format(self.data_dir, idx)) im = Image.fromarray(im) im = im.resize((self.width, self.height), Image.ANTIALIAS) # resize im = np.array(im, dtype=np.float32) # cast to float d = im d -= self.mean_depth # mean subtraction d = d[np.newaxis, ...] return d
def img_load(filename, args): # img_raw = tf.io.read_file(filename) # img = tf.image.decode_image(img_raw) img = io.imread(filename) offset_width = 50 offset_height = 10 target_width = 660 target_height = 470 # imgc = tf.image.crop_to_bounding_box(img, offset_height, offset_width, target_height, target_width) imgc = img[offset_height:target_height, offset_width:target_width] # # args.img_size = 0.25; args.preserve_aspect_ratio = True; args.rand_box = 0.1 imresize_ = np.multiply(imgc.shape[:2], args.img_size) # imgcre = tf.image.resize(imgc, size=imresize_) imgcre = transform.resize(imgc, output_shape=imresize_.astype(np.int), preserve_range=True) return imgcre
def _load_worker(self, sess): while not self.coord.should_stop(): # dequeue one filename from the file name queue idx = sess.run(self.fdx_dequeue) # load the image X = np.ndarray(self.data_shape) y = np.ndarray(self.label_shape) file_path = os.path.join(self.img_dir, self.file_ids[idx] + ".jpg") X = io.imread(file_path) y = self.y[idx] try: sess.run(self.enqueue, feed_dict={ self.X_holder: X, self.y_holder: y, self.idx_holder: idx }) except tf.errors.CancelledError: return
def loadimg(self, path): """ 加载一张图片,将其转化为符合要求的格式 :param path: :return: """ # 读取图片 image = io.imread(path) # 重新设定图片大小 # image = scipy.misc.imresize(image, [IMAGE_HEIGHT, IMAGE_WIDTH]) image = np.array( Image.fromarray(image).resize([IMAGE_HEIGHT, IMAGE_WIDTH])) # 改变数组形状,其实就是把它变成一个batch_size=1的batch image = np.reshape(image, (1, IMAGE_HEIGHT, IMAGE_WIDTH, 3)) # 减去均值,使其数据分布接近0 image = image - IMAGE_MEAN_VALUE return image
def load_image(self, idx): """ Load input image and preprocess: - resize - cast to float - switch channels RGB -> BGR - subtract mean - transpose to channel x height x width order """ idx = self.indices[idx] idx = idx.split()[0] im = io.imread('{}/{}'.format(self.data_dir, idx)) im = Image.fromarray(im) im = im.resize((self.width, self.height), Image.ANTIALIAS) # resize image im = np.array(im, dtype=np.float32) # cast to float im = im[:,:,::-1] # RGB -> BGR im -= self.mean_bgr # mean subtraction im = im.transpose((2,0,1)) return im
def get_saliency_for_juntingnet(image_url,sal_url): arr_files = glob.glob(image_url+"*.png") for i in range(len(arr_files)): url_image = arr_files[i] image = io.imread(url_image) img = misc.imresize(image,(96,96)) img = np.asarray(img, dtype = 'float32') / 255. img = img.transpose(2,0,1).reshape(3, 96, 96) xt = np.zeros((1, 3, 96, 96), dtype='float32') xt[0]=img y = juntingnet.predict(xt) tmp = y.reshape(48,48) blured= ndimage.gaussian_filter(tmp, sigma=3) sal_map = cv2.resize(tmp,(image.shape[1],image.shape[0])) sal_map -= np.min(sal_map) sal_map /= np.max(sal_map) #saliency = misc.imresize(y,(img.shape[0],img.shape[1])) aux = url_image.split("/")[-1].split(".")[0] misc.imsave(sal_url+'/'+aux+'.png', sal_map)
def get_saliency_for_shallownet(image_url, sal_url): arr_files = glob.glob(image_url + "*.jpg") for i in range(len(arr_files)): url_image = arr_files[i] image = io.imread(url_image) img = misc.imresize(image, (96, 96)) img = np.asarray(img, dtype='float32') / 255. img = img.transpose(2, 0, 1).reshape(3, 96, 96) xt = np.zeros((1, 3, 96, 96), dtype='float32') xt[0] = img y = juntingnet.predict(xt) tmp = y.reshape(48, 48) blured = ndimage.gaussian_filter(tmp, sigma=3) sal_map = cv2.resize(tmp, (image.shape[1], image.shape[0])) sal_map -= np.min(sal_map) sal_map /= np.max(sal_map) #saliency = misc.imresize(y,(img.shape[0],img.shape[1])) aux = url_image.split("/")[-1].split(".")[0] misc.imsave(sal_url + '/' + aux + '.png', sal_map)
defaultextension=".htex") if(save_file_name is None or file_name == ""): import sys; print("User quit without selecting a file"); sys.exit(0); else: save_file_name = args.output_file if frozen_status: sys.frozen = frozen_status elif getattr(sys, 'frozen', False): del sys.frozen image = io.imread(file_name)/255.0 if len(image.shape) == 2: image = np.concatenate((image[:,:,None],image[:,:,None],image[:,:,None]),axis=2) if image.shape[2] > 3: image = image[:,:,:3] image_lab = to_color_space(image) relitive_value = max(image.shape[:2]) #Create a tileset from the image, with the user selecting the scale if isinstance(args.base_scale,list): tile_size = max([bsf(relitive_value) for bsf in args.base_scale]) else: tile_size = args.base_scale(relitive_value)#diffution_system.gui.scale_selector(image); print("Base Scale Chosen",tile_size)
image_path = image_path[:-1] print("> Prepare image "+image_path + ":") imname = ntpath.basename(image_path) #imname = imname[:-4] imname = imname.split(imname.split('.')[-1])[0][0:-1] img = cv2.imread(image_path) ## If we have input landmarks if len(landmarkDir) > 0: lms = np.loadtxt(landmarkDir + '/' + imname + '.pts') img2 = cv2.copyMakeBorder(img,0,0,0,0,cv2.BORDER_REPLICATE) nLM = lms.shape[0] for i in range(0,nLM): cv2.circle(img2, (lms[i,0], lms[i,1]), 5, (255,0,0)) img, lms = utils.cropByInputLM(img, lms, img2) else: dlib_img = io.imread(image_path) img2 = cv2.copyMakeBorder(img,0,0,0,0,cv2.BORDER_REPLICATE) dets = detector(img, 1) print("> Number of faces detected: {}".format(len(dets))) if len(dets) == 0: print('> Could not detect the face, skipping the image...' + image_path) continue if len(dets) > 1: print("> Process only the first detected face!") detected_face = dets[0] ## If we are using landmarks to crop shape = predictor(dlib_img, detected_face) nLM = shape.num_parts for i in range(0,nLM): cv2.circle(img2, (shape.part(i).x, shape.part(i).y), 5, (255,0,0)) img, lms = utils.cropByLM(img, shape, img2)
# hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 0.02)) # # ax2.axis('off') # ax2.imshow(hog_image_rescaled, cmap=plt.cm.gray) # ax2.set_title('Histogram of Oriented Gradients') # ax1.set_adjustable('box-forced') # plt.show() # End of hog test # Feature Extraction for NN y = np.repeat(y, 30) features = np.empty([len(y), 3200]) row_count = 0 for image_path in training_sample_paths: image = io.imread(image_path) image = transform.resize(image, (400, 400)) for i in range(10): image_new = image * np.random.normal(1.0, 0.2) + np.random.normal(0, 0.2) image_new = np.maximum(np.minimum(image, 1.0), 0.0) image_new_dilation = morphology.dilation(image, morphology.square(2)) image_new_erosion = morphology.erosion(image, morphology.square(2)) features[row_count, :] = hog(image, orientations=8, pixels_per_cell=(20, 20), cells_per_block=(1, 1)) features[row_count+1, :] = hog(image_new_dilation, orientations=8, pixels_per_cell=(20, 20), cells_per_block=(1, 1)) features[row_count+2, :] = hog(image_new_erosion, orientations=8, pixels_per_cell=(20, 20), cells_per_block=(1, 1)) row_count += 3
from skimage import img_as_float import numpy as np import matplotlib.pyplot as plt import sys #model = '32s' #model = '16s' #model = '8s' model = 'googlenet' ; rgb = [122.67891434, 116.66876762, 104.00698793] filename = '/home/vedaldi/src/deep-seg/data/voc11/JPEGImages/2008_000073.jpg' # read and convert an input image im = io.imread(filename) im = img_as_float(im) * 255 ; im = np.array(im) im = im - np.array(rgb).reshape([1, 1, 3]) im = im[:,:, ::-1] # RGB -> BGR im = np.transpose(im,[2,0,1]) im = im.reshape([1] + list(im.shape)) # load Caffe and network caffe_root = '/home/vedaldi/src/caffe/' sys.path.insert(0, caffe_root + 'python') import caffe if model == '32s': net = caffe.Classifier('data/tmp/fcn/fcn-32s-pascal-deploy.prototxt', 'data/tmp/fcn/fcn-32s-pascal.caffemodel')