def main(): image = data.astronaut() image = ia.imresize_single_image(image, (HEIGHT, WIDTH)) kps = [] for y in range(NB_ROWS): ycoord = BB_Y1 + int(y * (BB_Y2 - BB_Y1) / (NB_COLS - 1)) for x in range(NB_COLS): xcoord = BB_X1 + int(x * (BB_X2 - BB_X1) / (NB_ROWS - 1)) kp = (xcoord, ycoord) kps.append(kp) kps = set(kps) kps = [ia.Keypoint(x=xcoord, y=ycoord) for (xcoord, ycoord) in kps] kps = ia.KeypointsOnImage(kps, shape=image.shape) bb = ia.BoundingBox(x1=BB_X1, x2=BB_X2, y1=BB_Y1, y2=BB_Y2) bbs = ia.BoundingBoxesOnImage([bb], shape=image.shape) seq = iaa.Affine(rotate=45) seq_det = seq.to_deterministic() image_aug = seq_det.augment_image(image) kps_aug = seq_det.augment_keypoints([kps])[0] bbs_aug = seq_det.augment_bounding_boxes([bbs])[0] image_before = np.copy(image) image_before = kps.draw_on_image(image_before) image_before = bbs.draw_on_image(image_before) image_after = np.copy(image_aug) image_after = kps_aug.draw_on_image(image_after) image_after = bbs_aug.draw_on_image(image_after) misc.imshow(np.hstack([image_before, image_after])) misc.imsave("bb_aug.jpg", np.hstack([image_before, image_after]))
def main(): image = ia.quokka(size=0.5) print(image.shape) kps = [ ia.KeypointsOnImage( [ ia.Keypoint(x=123, y=102), ia.Keypoint(x=182, y=98), ia.Keypoint(x=155, y=134), #ia.Keypoint(x=255, y=213), #ia.Keypoint(x=375, y=205), #ia.Keypoint(x=323, y=279), #ia.Keypoint(x=265, y=223), #ia.Keypoint(x=385, y=215), #ia.Keypoint(x=333, y=289), #ia.Keypoint(x=275, y=233), #ia.Keypoint(x=395, y=225), #ia.Keypoint(x=343, y=299), ia.Keypoint(x=-20, y=20) ], shape=(image.shape[0], image.shape[1]) ) ] #kps[0] = kps[0].on(image.shape) print("image shape:", image.shape) augs = [ #iaa.PiecewiseAffine(scale=0), iaa.PiecewiseAffine(scale=0.05), iaa.PiecewiseAffine(scale=0.1), iaa.PiecewiseAffine(scale=0.2) ] #print("original", image.shape) misc.imshow(kps[0].draw_on_image(image)) print("-----------------") print("Random aug per image") print("-----------------") for aug in augs: images_aug = [] for _ in range(16): aug_det = aug.to_deterministic() img_aug = aug_det.augment_image(image) kps_aug = aug_det.augment_keypoints(kps)[0] #img_aug_kps = kps_aug.draw_on_image(img_aug) img_aug_kps = keypoints_draw_on_image(kps_aug, img_aug) img_aug_kps = np.pad(img_aug_kps, ((1, 1), (1, 1), (0, 0)), mode="constant", constant_values=255) #print(aug.name, img_aug_kps.shape, img_aug_kps.shape[1]/img_aug_kps.shape[0]) images_aug.append(img_aug_kps) #misc.imshow(img_aug_kps) print(aug.name) misc.imshow(ia.draw_grid(images_aug))
def show(self, log=False, size_fig=(10, 5)): 'sowh detecotr image, optioanlly in log scale' fig, ax = plt.subplots(figsize=size_fig) ax.grid(color='white') if log: imshow(np.log(self.img), cmap=plt.cm.cubehelix) if not log: imshow(self.img, cmap=plt.cm.Oranges) plt.colorbar() return fig, ax
def main(): nb_rows = 8 nb_cols = 8 h, w = (128, 128) sample_size = 128 noise_gens = [ iap.SimplexNoise(), iap.FrequencyNoise(exponent=-4, size_px_max=sample_size, upscale_method="cubic"), iap.FrequencyNoise(exponent=-2, size_px_max=sample_size, upscale_method="cubic"), iap.FrequencyNoise(exponent=0, size_px_max=sample_size, upscale_method="cubic"), iap.FrequencyNoise(exponent=2, size_px_max=sample_size, upscale_method="cubic"), iap.FrequencyNoise(exponent=4, size_px_max=sample_size, upscale_method="cubic"), iap.FrequencyNoise(exponent=(-4, 4), size_px_max=(4, sample_size), upscale_method=["nearest", "linear", "cubic"]), iap.IterativeNoiseAggregator( other_param=iap.FrequencyNoise(exponent=(-4, 4), size_px_max=(4, sample_size), upscale_method=["nearest", "linear", "cubic"]), iterations=(1, 3), aggregation_method=["max", "avg"] ), iap.IterativeNoiseAggregator( other_param=iap.Sigmoid( iap.FrequencyNoise(exponent=(-4, 4), size_px_max=(4, sample_size), upscale_method=["nearest", "linear", "cubic"]), threshold=(-10, 10), activated=0.33, mul=20, add=-10 ), iterations=(1, 3), aggregation_method=["max", "avg"] ) ] samples = [[] for _ in range(len(noise_gens))] for _ in range(nb_rows * nb_cols): for i, noise_gen in enumerate(noise_gens): samples[i].append(noise_gen.draw_samples((h, w))) rows = [np.hstack(row) for row in samples] grid = np.vstack(rows) misc.imshow((grid*255).astype(np.uint8)) images = [ia.quokka_square(size=(128, 128)) for _ in range(16)] seqs = [ iaa.SimplexNoiseAlpha(first=iaa.EdgeDetect(1.0)), iaa.SimplexNoiseAlpha(first=iaa.EdgeDetect(1.0), per_channel=True), iaa.FrequencyNoiseAlpha(first=iaa.EdgeDetect(1.0)), iaa.FrequencyNoiseAlpha(first=iaa.EdgeDetect(1.0), per_channel=True) ] images_aug = [] for seq in seqs: images_aug.append(np.hstack(seq.augment_images(images))) images_aug = np.vstack(images_aug) misc.imshow(images_aug)
def show_and_save_montage_of_patches(patches, is_show, is_save, save_filename=None): if is_show or is_save: if patches.ndim == 3: patches_montage = montage2d(patches) else: patches_montage = patches if is_show: imshow(patches_montage) if is_save and (save_filename is not None): save_filename = os.path.join(THIS_FILE_PATH, save_filename) imsave(save_filename, patches_montage)
def quantize_kmeans(img,num_colors=15, verbose=False): height = len(img) width = len(img[0]) # set up KMeans estimator est = KMeans(n_clusters = num_colors,n_jobs=-1) pixels = np.reshape(img, (-1,3)) est.fit(pixels) assign_means = lambda x: est.cluster_centers_[x] labels = copy.deepcopy(est.labels_) img_quantized = np.array(map(assign_means, labels)).reshape(height, width,3) if verbose: misc.imshow(img_quantized) return img_quantized
def display_state_static(state_array, console_print=False): """ Creates a PIL image out of the given state array and displays it. """ assert state_array.ndim == 2, "Need a 2D representations of states!" if console_print: print state_array # Normalise to [0-255.0] anyway regardless of whether the caller did it. # Better safe than sorry. normalise(state_array) imshow(state_array)
def demo_image(): """ misc model in scipy contain some method to operate on image, such as open, save, resize, show; and it will operate image as a numpy array. """ img = misc.imread("image_process/tire.bmp") # img is a numpy array """Resize a image to a big one""" re_img = misc.imresize(img, (600, 600)) """Rotate a image to another one""" ro_img = misc.imrotate(re_img, 45) """Save this image as file""" misc.imsave("test.bmp", ro_img) """Show a image without matplotlib""" misc.imshow(ro_img)
def add_shape(self, f): #Create shape S = Shape(f, self.R, self.SHAPE_R) #Add to shape list S.shape_num = len(self.shape_list) self.shape_list.append(S) row = [] for k in range(len(self.shape_list)): T = self.shape_list[k] ift = real(ipfft(pft_mult(pft_rotate(S.pft, 2.*pi/6.), T.pft), 2*self.SHAPE_R+1,2*self.SHAPE_R+1)) Spad = imrotate(cpad(S.indicator, array([2*self.SHAPE_R+1,2*self.SHAPE_R+1])), 360./6.) Tpad = cpad(T.indicator, array([2*self.SHAPE_R+1,2*self.SHAPE_R+1])) pind = real(fftconvolve(Spad, Tpad, mode='same')) imshow(pind) imshow(ift) obst = to_ind(pind, 0.001) imshow(obst) cutoff = best_cutoff(ift, obst, S.radius + T.radius) print cutoff imshow(to_ind(ift, cutoff)) row.append(cutoff * self.tarea) self.cutoff_matrix.append(row) return S
def vis_test(): model = LoadModel('tests/example.dpm') image = imread('tests/000034.jpg') pyramid = BuildPyramid(image, model=model) filtered_model = model.Filter(pyramid) detections_generator = filtered_model.Parse(-1) nms_generator = NMS(detections_generator, 0.3) detections = [d for i,d in itertools.izip(xrange(2), nms_generator)] print(detections) detection_image = draw_detections (detections, image) imshow(detection_image)
def test_gray(self): cover = read("./test_case/lena_gray.jpg") lsb = LSBAlgorithm(3) watermark = b'Tsinghua University' secret_space = flatten(select_phase(cover, 'gray')) embedded_cover = secret_space.revert( lsb.embed(secret_space, watermark)) imshow(embedded_cover._data) extracted = lsb.extract(flatten(select_phase(embedded_cover, 'gray'))) self.assertEqual(watermark, extracted[:len(watermark)])
def vis_small_test(): model = LoadModel('tests/example.dpm') model.start.rules = model.start.rules[:1] model.start.rules[0].rhs = model.start.rules[0].rhs[1:2] model.start.rules[0].anchor = model.start.rules[0].anchor[1:2] image = imread('tests/000034.jpg') pyramid = BuildPyramid(image, model=model) filtered_model = model.Filter(pyramid) detections = [d for i,d in itertools.izip(xrange(1), filtered_model.Parse(-1))] print(detections) detection_image = draw_detections (detections, image) imshow(detection_image)
def showimage(*images): images = (fig2raster(image) for image in images) if TKPIPE: for image in images: if isinstance(image, ndarray): try: image = pil.fromarray(image) except TypeError: image = pil.fromarray(np.float64(image)) except IndexError: print image raise TKPIPE.writeimage(image) print("") else: for image in images: imshow(image)
def main(): images = [ misc.imresize(ndimage.imread("../quokka.jpg")[0:643, 0:643], (128, 128)), misc.imresize(data.astronaut(), (128, 128)) ] augmenters = [ iaa.Noop(name="Noop"), iaa.Crop(px=(0, 8), name="Crop-px"), iaa.Crop(percent=(0, 0.1), name="Crop-percent"), iaa.Fliplr(0.5, name="Fliplr"), iaa.Flipud(0.5, name="Flipud"), iaa.Grayscale(0.5, name="Grayscale0.5"), iaa.Grayscale(1.0, name="Grayscale1.0"), iaa.GaussianBlur((0, 3.0), name="GaussianBlur"), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.1), name="AdditiveGaussianNoise"), iaa.Dropout((0.0, 0.1), name="Dropout"), iaa.Multiply((0.5, 1.5), name="Multiply"), iaa.ContrastNormalization(alpha=(0.5, 2.0)), iaa.Affine( scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, translate_px={"x": (-16, 16), "y": (-16, 16)}, rotate=(-45, 45), shear=(-16, 16), order=ia.ALL, cval=(0, 1.0), mode=ia.ALL, name="Affine" ), iaa.ElasticTransformation(alpha=(0.5, 8.0), sigma=1.0) ] #for i, aug in enumerate(augmenters): #print(i) #aug.deepcopy() #import copy #copy.deepcopy(aug) seq = iaa.Sequential([aug.copy() for aug in augmenters], name="Sequential") st = iaa.Sometimes(0.5, seq.copy(), name="Sometimes") augmenters.append(seq) augmenters.append(st) for augmenter in augmenters: print("Augmenter: %s" % (augmenter.name,)) grid = augmenter.draw_grid(images, rows=1, cols=16) misc.imshow(grid)
def main(): image = data.astronaut() print("image shape:", image.shape) aug = iaa.WithColorspace( from_colorspace="RGB", to_colorspace="HSV", children=iaa.WithChannels(0, iaa.Add(50)) ) aug_no_colorspace = iaa.WithChannels(0, iaa.Add(50)) img_show = np.hstack([ image, aug.augment_image(image), aug_no_colorspace.augment_image(image) ]) misc.imshow(img_show)
def FindCharacterBboxes(numberedRegions): maxRegion = numberedRegions.max() bboxLi = [] for rn in range(maxRegion+1): regionIm = numberedRegions == rn bbox = BlobBbox(regionIm) bboxLi.append(bbox) #print maxRegion #Try each bounding box as a seed for model fitting #This is similar to ransac but we exhaustively try each starting bbox models = [] maxModelSize = None maxModelSizeInd = None for seedRegionNum in range(maxRegion+1): if 0: regionIm = numberedRegions == seedRegionNum cb = bboxLi[seedRegionNum] im2 = regionIm[cb[0]:cb[1]+1,:] im3 = im2[:,cb[2]:cb[3]+1,] if im3.size > 0: misc.imshow(im3) model = FitBboxModel(seedRegionNum, bboxLi, numberedRegions.shape, numberedRegions) models.append(model) if maxModelSize is None or len(model) > maxModelSize: maxModelSize = len(model) maxModelSizeInd = seedRegionNum #print len(model) #for model in models: # print len(model) #Return biggest model #TODO find a better way to select the best if maxModelSizeInd is None: return None, None return models[maxModelSizeInd]
def main(): img = data.astronaut() img = misc.imresize(img, (64, 64)) aug = iaa.Fliplr(0.5) unseeded1 = aug.draw_grid(img, cols=8, rows=1) unseeded2 = aug.draw_grid(img, cols=8, rows=1) ia.seed(1000) seeded1 = aug.draw_grid(img, cols=8, rows=1) seeded2 = aug.draw_grid(img, cols=8, rows=1) ia.seed(1000) reseeded1 = aug.draw_grid(img, cols=8, rows=1) reseeded2 = aug.draw_grid(img, cols=8, rows=1) ia.seed(1001) reseeded3 = aug.draw_grid(img, cols=8, rows=1) reseeded4 = aug.draw_grid(img, cols=8, rows=1) all_rows = np.vstack([unseeded1, unseeded2, seeded1, seeded2, reseeded1, reseeded2, reseeded3, reseeded4]) misc.imshow(all_rows)
def example_keypoints(): print("Example: Keypoints") import imgaug as ia #from imgaug import augmenters as iaa import augmenters as iaa from scipy import misc import random images = np.random.randint(0, 50, (4, 128, 128, 3), dtype=np.uint8) # Generate random keypoints. # The augmenters expect a list of imgaug.KeypointsOnImage. keypoints_on_images = [] for image in images: height, width = image.shape[0:2] keypoints = [] for _ in range(4): x = random.randint(0, width-1) y = random.randint(0, height-1) keypoints.append(ia.Keypoint(x=x, y=y)) keypoints_on_images.append(ia.KeypointsOnImage(keypoints, shape=image.shape)) seq = iaa.Sequential([iaa.GaussianBlur((0, 3.0)), iaa.Affine(scale=(0.5, 0.7))]) seq_det = seq.to_deterministic() # call this for each batch again, NOT only once at the start # augment keypoints and images images_aug = seq_det.augment_images(images) keypoints_aug = seq_det.augment_keypoints(keypoints_on_images) # Example code to show each image and print the new keypoints coordinates for img_idx, (image_before, image_after, keypoints_before, keypoints_after) in enumerate(zip(images, images_aug, keypoints_on_images, keypoints_aug)): image_before = keypoints_before.draw_on_image(image_before) image_after = keypoints_after.draw_on_image(image_after) misc.imshow(np.concatenate((image_before, image_after), axis=1)) # before and after for kp_idx, keypoint in enumerate(keypoints_after.keypoints): keypoint_old = keypoints_on_images[img_idx].keypoints[kp_idx] x_old, y_old = keypoint_old.x, keypoint_old.y x_new, y_new = keypoint.x, keypoint.y print("[Keypoints for image #%d] before aug: x=%d y=%d | after aug: x=%d y=%d" % (img_idx, x_old, y_old, x_new, y_new))
def example_background_augment_batches(): print("Example: Background Augmentation via augment_batches()") import imgaug as ia from imgaug import augmenters as iaa import numpy as np from skimage import data # Number of batches and batch size for this example nb_batches = 10 batch_size = 32 # Example augmentation sequence to run in the background augseq = iaa.Sequential([ iaa.Fliplr(0.5), iaa.CoarseDropout(p=0.1, size_percent=0.1) ]) # For simplicity, we use the same image here many times astronaut = data.astronaut() astronaut = ia.imresize_single_image(astronaut, (64, 64)) # Make batches out of the example image (here: 10 batches, each 32 times # the example image) batches = [] for _ in range(nb_batches): batches.append( np.array( [astronaut for _ in range(batch_size)], dtype=np.uint8 ) ) # Show the augmented images. # Note that augment_batches() returns a generator. for images_aug in augseq.augment_batches(batches, background=True): misc.imshow(ia.draw_grid(images_aug, cols=8))
def main(): image = ia.quokka(size=0.5) kps = [ia.KeypointsOnImage( [ia.Keypoint(x=245, y=203), ia.Keypoint(x=365, y=195), ia.Keypoint(x=313, y=269)], shape=(image.shape[0]*2, image.shape[1]*2) )] kps[0] = kps[0].on(image.shape) print("image shape:", image.shape) augs = [ iaa.PerspectiveTransform(scale=0.01, name="pt001", keep_size=True), iaa.PerspectiveTransform(scale=0.1, name="pt01", keep_size=True), iaa.PerspectiveTransform(scale=0.2, name="pt02", keep_size=True), iaa.PerspectiveTransform(scale=0.3, name="pt03", keep_size=True), iaa.PerspectiveTransform(scale=(0, 0.3), name="pt00to03", keep_size=True) ] print("original", image.shape) misc.imshow(kps[0].draw_on_image(image)) print("-----------------") print("Random aug per image") print("-----------------") for aug in augs: images_aug = [] for _ in range(16): aug_det = aug.to_deterministic() img_aug = aug_det.augment_image(image) kps_aug = aug_det.augment_keypoints(kps)[0] img_aug_kps = kps_aug.draw_on_image(img_aug) img_aug_kps = np.pad(img_aug_kps, ((1, 1), (1, 1), (0, 0)), mode="constant", constant_values=255) #print(aug.name, img_aug_kps.shape, img_aug_kps.shape[1]/img_aug_kps.shape[0]) images_aug.append(img_aug_kps) #misc.imshow(img_aug_kps) print(aug.name) misc.imshow(ia.draw_grid(images_aug)) print("----------------") print("6 channels") print("----------------") image6 = np.dstack([image, image]) image6_aug = augs[1].augment_image(image6) misc.imshow( np.hstack([image6_aug[..., 0:3], image6_aug[..., 3:6]]) )
import sift import numpy as np from scipy.misc import imread,imshow,imresize import matplotlib from time import time im=imread("test2.jpg") #im = matplotlib.colors.rgb_to_hsv(im) R=im[:,:,0].astype(np.float32) G=im[:,:,1].astype(np.float32) B=im[:,:,2].astype(np.float32) for channel in [R,G,B]: t = time() keypoints, descriptors = sift.descriptors(channel, 30) print time() - t print descriptors.shape for point in keypoints: channel[point[1],point[0]] = 0 channel[point[1],point[0]+1] = 255 imshow(channel)
image_size = 299 num_classes = 1000 with tf.Graph().as_default(): with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): imgPath = '/home/raulgomez/datasets/WebVision/val_images_256/val000800.jpg' testImage_string = tf.gfile.FastGFile(imgPath, 'rb').read() testImage = tf.image.decode_jpeg(testImage_string, channels=3) processed_image = inception_preprocessing.preprocess_image(testImage, image_size, image_size, is_training=False) processed_images = tf.expand_dims(processed_image, 0) logits, _ = inception_resnet_v2.inception_resnet_v2(processed_images, num_classes=num_classes, is_training=False) probabilities = tf.nn.softmax(logits) print(checkpoint_path) checkpoint_path = tf.train.latest_checkpoint(checkpoint_path) print(checkpoint_path) init_fn = slim.assign_from_checkpoint_fn(checkpoint_path, slim.get_model_variables(model_name)) with tf.Session() as sess: init_fn(sess) np_image, probabilities = sess.run([processed_images, probabilities]) probabilities = probabilities[0, 0:] sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x: x[1])] for i in range(1): index = sorted_inds[i] print((probabilities[index], index)) imshow(imread(imgPath))
# $> python npy2tiff.py <filename.npy> [show] import numpy as np import sys import scipy.misc as s a = np.load(sys.argv[1]) if len(sys.argv) > 2: s.imshow(a) else: s.imsave(sys.argv[1].replace('npy', 'tiff'), a)
def show(self): sp.imshow(self.pixel_map)
def show(img, txt=""): misc.imshow(img.astype(np.uint8)) # cv2.imshow(txt, img.astype(np.uint8)) # Display on screen kboard = chr( cv2.waitKey()) # wait until key is pressed or 5 seconds have passed return kboard
#!/usr/bin/env python from scipy.misc import imread, imsave, imresize, imshow img = imread('vim.png') print img.dtype, img.shape img_tinted = img * [1, 0.95, 0.9, 0.9] # here we multiply by 4-dim instead of 3-dim as it has 4 for svg images img_tinted = imresize(img_tinted, (300, 300)) imsave('vim_tinted.png', img_tinted) imshow(img) imshow(img_tinted)
import numpy as np import keras.models from keras.models import model_from_json from scipy.misc import imread, imresize, imshow json_file = open('model.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) loaded_model.load_weights("model.h5") print("Loaded Model from disk") loaded_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) x = imread('output.png', mode='L') x = np.invert(x) x = imresize(x, (28, 28)) imshow(x) x = x.reshape(1, 28, 28, 1) out = loaded_model.predict(x) print(out) print(np.argmax(out, axis=1))
id = {label.id for label in labels if label.name == 'ceiling'} #todo get label and idddddd img = misc.imread('PSPNet-Keras-tensorflow-master/example_images/ade20k2.jpg') #create the neural network pspnet = PSPNet50(nb_classes=150, input_shape=(473, 473), weights='pspnet50_ade20k') #Run over input data class_scores = predict_multi_scale(img, pspnet, EVALUATION_SCALES, sliding, flip) print("Writing results...") class_image = np.argmax(class_scores, axis=2) #Get best classes (highest score!!) pm = np.max(class_scores, axis=2) colored_class_image = utils.color_class_image(class_image, 'ade20k') # colored_class_image is [0.0-1.0] img is [0-255] alpha_blended = 0.5 * colored_class_image + 0.5 * img #filename, ext = splitext('PSPNet-Keras-tensorflow-master/example_results/') #misc.imsave(filename + "_seg" + ext, colored_class_image) #misc.imsave(filename + "_probs" + ext, pm) #misc.imsave(filename + "_seg_blended" + ext, alpha_blended) misc.imshow(colored_class_image)
# Crops image below hood and above horizon where all pixels are of class 0 and therefore not of interest. Speeds up training. label = label[vertical_start:vertical_end, :im_width] label_shape = label.shape label = (np.array(label)).reshape(-1) label = np.eye(13)[label] # Convert label vector to one-hot label = map_labels(label) # Convert labels to 0,1 or 2 pred = sess.run(logits, feed_dict={input_image:image_4d, keep_prob:1.0}) y_pred = np.argmax(pred, axis = 1) label = np.argmax(label, axis = 1) binary_car_result = np.where(y_pred == 2, 1, 0).astype('uint8') binary_road_result = np.where(y_pred == 1, 1, 0).astype('uint8') car_labels = np.where(label == 2, 1, 0).astype('uint8') road_labels = np.where(y_pred == 1, 1, 0).astype('uint8') label_image = restore_image(label, im_size) pred_image = restore_image(y_pred, im_size) car_f1 = f1_score(car_labels, binary_car_result) road_f1 = f1_score(road_labels, binary_road_result) print("Car F1: {0}, Road F1: {1}".format(car_f1, road_f1)) imlbl = np.concatenate((label_image, pred_image), axis=0) misc.imshow(imlbl)
def main(): images = [ misc.imresize( ndimage.imread("../quokka.jpg")[0:643, 0:643], (128, 128)), misc.imresize(data.astronaut(), (128, 128)) ] augmenters = [ iaa.Noop(name="Noop"), iaa.Crop(px=(0, 8), name="Crop-px"), iaa.Crop(percent=(0, 0.1), name="Crop-percent"), iaa.Fliplr(0.5, name="Fliplr"), iaa.Flipud(0.5, name="Flipud"), iaa.Superpixels(p_replace=0.75, n_segments=50, name="Superpixels"), iaa.Grayscale(0.5, name="Grayscale0.5"), iaa.Grayscale(1.0, name="Grayscale1.0"), iaa.GaussianBlur((0, 3.0), name="GaussianBlur"), iaa.Sharpen(alpha=(0.1, 1.0), strength=(0, 2.0), name="Sharpen"), iaa.Emboss(alpha=(0.1, 1.0), strength=(0, 2.0), name="Emboss"), iaa.EdgeDetect(alpha=(0.1, 1.0), name="EdgeDetect"), iaa.DirectedEdgeDetect(alpha=(0.1, 1.0), direction=(0, 1.0), name="DirectedEdgeDetect"), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.1 * 255), name="AdditiveGaussianNoise"), iaa.Dropout((0.0, 0.1), name="Dropout"), iaa.Invert(p=0.5, name="Invert"), iaa.Invert(p=0.5, per_channel=True, name="InvertPerChannel"), iaa.Add((-50, 50), name="Add"), iaa.Add((-50, 50), per_channel=True, name="AddPerChannel"), iaa.AddElementwise((-50, 50), name="AddElementwise"), iaa.Multiply((0.5, 1.5), name="Multiply"), iaa.Multiply((0.5, 1.5), per_channel=True, name="MultiplyPerChannel"), iaa.MultiplyElementwise((0.5, 1.5), name="MultiplyElementwise"), iaa.ContrastNormalization(alpha=(0.5, 2.0), name="ContrastNormalization"), iaa.Affine(scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, translate_px={ "x": (-16, 16), "y": (-16, 16) }, rotate=(-45, 45), shear=(-16, 16), order=ia.ALL, cval=(0, 255), mode=ia.ALL, name="Affine"), iaa.ElasticTransformation(alpha=(0.5, 8.0), sigma=1.0, name="ElasticTransformation") ] #for i, aug in enumerate(augmenters): #print(i) #aug.deepcopy() #import copy #copy.deepcopy(aug) augmenters.append( iaa.Sequential([iaa.Sometimes(0.2, aug.copy()) for aug in augmenters], name="Sequential")) augmenters.append( iaa.Sometimes(0.5, [aug.copy() for aug in augmenters], name="Sometimes")) for augmenter in augmenters: print("Augmenter: %s" % (augmenter.name, )) grid = augmenter.draw_grid(images, rows=1, cols=16) misc.imshow(grid)
def show_grid(images, rows=None, cols=None): grid = draw_grid(images, rows=rows, cols=cols) misc.imshow(grid)
import os import numpy import scipy.misc as misc ImDir = "/scratch/gobi1/seppel/DataSets/COCO_PANOPTIC/PanopticFull/train2017/" PredDir = "/scratch/gobi2/seppel/GeneratedPredictions/51AddClassEquivalent/Pred/" GtDir = "/scratch/gobi2/seppel/GeneratedPredictions/51AddClassEquivalent/GT/" for Name in os.listdir(PredDir): if ".png" in Name: p = misc.imread(PredDir + "/" + Name) g = misc.imread(GtDir + "/" + Name) i = misc.imread(ImDir + "/" + Name[:Name.find("#jpg")] + ".jpg") i = misc.imresize(i, g.shape) i[:, :, 0] *= 1 - p i[:, :, 1] *= 1 - g print(Name) misc.imshow(p * 50 + g * 100) misc.imshow(i)
from scipy import misc misc.imshow(misc.imresize(misc.face(), 50))
def genPoisson(): random_array = np.random.poisson(1, (512, 512)) random_array = random_array / 8 print(random_array) imsave('poisson.png', random_array) imshow(random_array)
fp1.flush() #Comparing fa images and fb images comparision_scores_af = Distance(fa_mgs1,asian_fa_txtnm,fb_mgs1,asian_fb_txtnm,eigenmg1,'AsianFemale') comparision_scores_wm = Distance(fa_mgs2,white_fa_txtnm,fb_mgs2,white_fb_txtnm,eigenmg2,'WhiteMale') choice = 1 while(choice != 0): print 'Choose Ethnic Group:\n1.AsianFemale \n2.WhiteMale\n0.Exit' choice = input('Enter Choice: ') if choice == 1: print 'Total Images Processed in group: '+str(len(fa_mgs1)) print 'Choose the FA Image Number to display the closest FB match:' fach = input('Enter Image # (from 1 to '+str(len(fa_mgs1))+'): ') print (asian_fa_txtnm[fach-1]) smisc.imshow(fa_mgs1[fach-1]) print (comparision_scores_af[fach-1]) #print (asian_fb_txtnm[comparision_scores_af[fach-1]]) closest = int(comparision_scores_af[fach-1][0]) print ('The closest match is: \n ' + asian_fb_txtnm[closest]) smisc.imshow(fb_mgs1[closest]) elif choice == 2: print 'Total Images Processed in group: '+str(len(fa_mgs2)) print 'Choose the FA Image Number to display the closest FB match:' fach = input('Enter Image # (from 1 to '+str(len(fa_mgs2))+'): ') smisc.imshow(fa_mgs2[fach-1]) print (white_fa_txtnm[fach-1]) print (comparision_scores_wm[fach-1]) smisc.imshow(fb_mgs2[fach-1]) #print (white_fb_txtnm[comparision_scores_wm[fach-1]]) closest = int(comparision_scores_wm[fach-1][0])
# Key key = {'x':(0.393,-0.644),'p':21,'q':43,'xy':(-0.236,0.522),'r':16,'t':3,'N':3} # Encrypt print('Encrypting image (huang)...') enc_im = cenc.encrypt(im,key,'huang') # Decrypt print('Decrypting image (huang)...') dec_im = cenc.decrypt(enc_im,key,'huang') # Diff diff = np.array(np.abs((im*1.0) - (dec_im*1.0)), dtype='int') maxdiff = np.max(diff) print('Max diff:', maxdiff) # Show if maxdiff == 0: diff_im = np.zeros(im.shape, dtype='uint8') else: diff_im = np.array((diff - np.min(diff)) / (np.max(diff) - np.min(diff))*255.99, dtype='uint8') print('[ original | encrypted ]') print('[ decrypted | abs(org-dec) ]') imshow(np.concatenate( [np.concatenate((im,enc_im),1), np.concatenate((dec_im,diff_im),1)] ,0))
def main(): # test 2d image misc.imshow(iaa.Scale(64).augment_image(data.camera())) # test many images images = [ia.quokka(size=0.5), ia.quokka(size=0.5)] images_aug = iaa.Scale(64).augment_images(images) misc.imshow(np.hstack(images_aug)) image = ia.quokka(size=0.5) kps = [ia.KeypointsOnImage( [ia.Keypoint(x=245, y=203), ia.Keypoint(x=365, y=195), ia.Keypoint(x=313, y=269)], shape=(image.shape[0]*2, image.shape[1]*2) )] kps[0] = kps[0].on(image.shape) print("image shape:", image.shape) augs = [ iaa.Scale("keep", name="keep"), iaa.Scale(32, name="i32"), iaa.Scale(0.5, name="f05"), iaa.Scale({"height": 32}, name="height32"), iaa.Scale({"width": 32}, name="width32"), iaa.Scale({"height": "keep", "width": 32}, name="keep-width32"), iaa.Scale({"height": 32, "width": "keep"}, name="height32-keep"), iaa.Scale({"height": "keep", "width": "keep"}, name="keep-keep"), iaa.Scale({"height": 32, "width": 64}, name="height32width64"), iaa.Scale({"height": 64, "width": "keep-aspect-ratio"}, name="height64width-kar"), iaa.Scale({"height": "keep-aspect-ratio", "width": 64}, name="height-kar_width64") ] augs_many = [ iaa.Scale((32, 128), name="tuple-32-128"), iaa.Scale([32, 64, 128], name="list-32-64-128"), iaa.Scale({"height": (32, 128), "width": "keep"}, name="height-32-64_width-keep"), iaa.Scale({"height": (32, 128), "width": "keep-aspect-ratio"}, name="height-32-128_width-kar"), iaa.Scale({"height": (32, 128), "width": (32, 128)}, name="height-32-128_width-32-128") ] print("original", image.shape) misc.imshow(kps[0].draw_on_image(image)) print("-----------------") print("Same size per image") print("-----------------") for aug in augs: img_aug = aug.augment_image(image) kps_aug = aug.augment_keypoints(kps)[0] img_aug_kps = kps_aug.draw_on_image(img_aug) print(aug.name, img_aug_kps.shape, img_aug_kps.shape[1]/img_aug_kps.shape[0]) misc.imshow(img_aug_kps) print("-----------------") print("Random per image") print("-----------------") for aug in augs_many: images_aug = [] for _ in range(64): aug_det = aug.to_deterministic() img_aug = aug_det.augment_image(image) kps_aug = aug_det.augment_keypoints(kps)[0] img_aug_kps = kps_aug.draw_on_image(img_aug) #print(aug.name, img_aug_kps.shape, img_aug_kps.shape[1]/img_aug_kps.shape[0]) images_aug.append(img_aug_kps) print(aug.name) misc.imshow(ia.draw_grid(images_aug)) print("nearest/cv2.INTER_NEAREST/cubic") misc.imshow(np.hstack([ iaa.Scale(64, interpolation="nearest").augment_image(image), iaa.Scale(64, interpolation=cv2.INTER_NEAREST).augment_image(image), iaa.Scale(64, interpolation="cubic").augment_image(image) ])) print("random nearest/cubic") iaa.Scale(64, interpolation=["nearest", "cubic"]).show_grid([image], 8, 8)
def train_mnist(): cyclic_names = ['1', '2'] current_name_index = 0 model_path = '/home/osboxes/PycharmProjects/ML_final_project/Models/MNIST' load_model = False save_model = True mnist = input_data.read_data_sets('MNIST_data', one_hot=True) inputSize = (28,28) trainData = mnist.train.images trainLabels = mnist.train.labels testData = mnist.test.images testLabels = mnist.test.labels validateData = mnist.validation.images validateLabels = mnist.validation.labels for i in range(len(trainData)): misc.imshow(trainData[i].reshape((28,28))) # We start best_loss = 0 n_input = 1 for i in inputSize: n_input *= i n_output = 10 x = tf.placeholder(tf.float32, [None, n_input]) y = tf.placeholder(tf.float32, [None, n_output]) x_tensor = tf.reshape(x, [-1, inputSize[0], inputSize[1], 1]) def conv2d(x, W): return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') def max_pool_2x2(x): return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # functions for parameter initialization def weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1) return tf.Variable(initial) def bias_variable(shape): initial = tf.constant(0.1, shape=shape) return tf.Variable(initial) # Weight matrix is [height x width x input_channels x output_channels] # Bias is [output_channels] filter_size = 5 n_filters_1 = 32 n_filters_2 = 64 # parameters W_conv1 = weight_variable([filter_size, filter_size, 1, n_filters_1]) b_conv1 = bias_variable([n_filters_1]) W_conv2 = weight_variable([filter_size, filter_size, n_filters_1, n_filters_2]) b_conv2 = bias_variable([n_filters_2]) # layers h_conv1 = tf.nn.relu(conv2d(x_tensor, W_conv1) + b_conv1, name='conv1') h_pool1 = max_pool_2x2(h_conv1) h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2, name='conv2') h_pool2 = max_pool_2x2(h_conv2) # 7x7 is the size of the image after the convolutional and pooling layers (28x28 -> 14x14 -> 7x7) h_conv2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * n_filters_2]) # %% Create the one fully-connected layer: n_fc = 1024 W_fc1 = weight_variable([7 * 7 * n_filters_2, n_fc]) b_fc1 = bias_variable([n_fc]) h_fc1 = tf.nn.relu(tf.matmul(h_conv2_flat, W_fc1) + b_fc1) keep_prob = tf.placeholder(tf.float32) h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) W_fc2 = weight_variable([n_fc, n_output]) b_fc2 = bias_variable([n_output]) y_pred = tf.matmul(h_fc1_drop, W_fc2) + b_fc2 cross_entropy = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(y_pred, y)) optimizer = tf.train.AdamOptimizer(0.0001).minimize(cross_entropy) # optimizer = tf.train.AdagradOptimizer(0.01).minimize(cross_entropy) correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) # variables: saver = tf.train.Saver() sess = tf.Session() if load_model: saver.restore(sess, model_path + '/saved_model1/my_model') else: sess.run(tf.global_variables_initializer()) # We'll train in minibatches and report accuracy: batch_size = min(50, len(trainData)) n_epochs = 10000 l_loss = list() for epoch_i in xrange(n_epochs): trainData, trainLabels = shuffle_2_arrays(trainData, trainLabels) batch_xs, batch_ys = trainData[:batch_size], trainLabels[:batch_size] sess.run(optimizer, feed_dict={ x: batch_xs, y: batch_ys, keep_prob: 0.5}) loss = sess.run(accuracy, feed_dict={ x: validateData, y: validateLabels, keep_prob: 1.0}) print('Validation accuracy for epoch {} is: {}'.format(epoch_i + 1, loss)) if epoch_i % 10 == 0: l_loss.append(loss) if loss > best_loss and save_model: best_loss = loss saver.save(sess, model_path + '/saved_model' + cyclic_names[current_name_index] + '/my_model') current_name_index = (current_name_index + 1) % len(cyclic_names) ''' plt.title('CNN Acuracy') plt.ylabel('Accuracy') plt.xlabel('Epochs') plt.plot(l_loss, color='m') plt.show() ''' print("Accuracy for test set: {}".format(sess.run(accuracy, feed_dict={ x: testData, y: testLabels, keep_prob: 1.0 }))) f = open(model_path + '/loss', 'w+') for i in l_loss: f.write(str(i) + '\n') f.close()
# Get 3x3 patch localPatch = thresholdedEdges[r - 1:r + 2, c - 1:c + 2] patchMax = localPatch.max() if patchMax == 2: currentPixels.append((r, c)) finalEdges[r, c] = 1 # Extend strong edges based on current pixels while len(currentPixels) > 0: newPix = [] for r, c in currentPixels: for dr in range(-1, 2): for dc in range(-1, 2): if dr == 0 and dc == 0: continue r2 = r + dr c2 = c + dc if thresholdedEdges[r2, c2] == 1 and finalEdges[r2, c2] == 0: # Copy this weak pixel to final result newPix.append((r2, c2)) finalEdges[r2, c2] = 1 currentPixels = newPix return finalEdges if __name__ == "__main__": im = imread("test.jpg", mode="L") # Open image, convert to greyscale finalEdges = CannyEdgeDetector(im) imshow(finalEdges)
# Defining the prediction function prediction_var = lasagne.layers.get_output(net['output'], deterministic=True) pred_and_conf_fn = theano.function( [input_var], [T.argmax(prediction_var, axis=1), T.max(prediction_var, axis=1)]) ### RUN PREDICTIONS ### total_time = 0 # measures the total time spent predicting in seconds print "\nPredictions using %s:" % network_name for X, file_name in zip(input_list, glob.glob('example_images/*.tiff')): start_time = time.time() [prediction, confidence] = pred_and_conf_fn(X) # get the prediction total_time += time.time() - start_time true_label = file_name.split('/')[1].split('.')[0] # True labels are obtained from file name. print " - %s (conf: %.2f, true label: %s)" % ( label_names[prediction[0]], confidence[0], true_label) if display_images: imshow(np.squeeze(X)) print "Average FPS: %.2f" % (float(len(input_list)) / total_time)
img = ds.pixel_array print('Dimensions: %dx%d' % img.shape) mask = imread(mask_filename) if len(mask.shape) == 3: mask = np.sum(mask, 2) # merge all color channels if invert_yaxis: mask = np.flipud(mask) if invert_xaxis: mask = np.fliplr(mask) mask = ndimage.binary_erosion(mask, structure=np.ones( (5, 5))) # erode the boundary of the mask img = np.where(mask > 0, img, 0) # apply mask if show_img: imshow(img) # QuadTree takes the full image and a function that decides whether to split the region or not # The function takes the image data of the current region and returns True when it should be split and False if not def QuadTree(img, f, x=0, y=0): size = img.shape[0] if size == 1 or not f(img): return [{"size": size, "x": x, "y": y}] mid = size / 2 ret = [] ret.extend(QuadTree(img[:mid, :mid], f, x, y)) ret.extend(QuadTree(img[mid:, :mid], f, x + mid, y)) ret.extend(QuadTree(img[:mid, mid:], f, x, y + mid)) ret.extend(QuadTree(img[mid:, mid:], f, x + mid, y + mid))
Dense(units=output_num_units, activation='softmax'), ]) model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy']) model.fit(train_x, train_y, batch_size=batch_size, epochs=epochs, verbose=1, validation_split=0.2) i = random.choice(train.index) print(i) img_name = train.ID[i] abc = img_name.replace('./Train/', '') img = imread(os.path.join(data_dir, 'Train', abc)) pred = model.predict_classes(train_x) pred = pred[i] if (pred == 0): print('Original:', train.Class[i], 'Predicted:MIDDLE') elif (pred == 1): print('Original:', train.Class[i], 'Predicted:OLD') elif (pred == 2): print('Original:', train.Class[i], 'Predicted:YOUNG') imshow(imresize(img, (128, 128)))
initializer=init) return deconv def upscore_layer(self, bottom, out_shape, name, ksize=4, upscale=2): with tf.variable_scope(name): in_channels = bottom.get_shape()[3].value kernel_shape = [ksize, ksize, in_channels, in_channels] deconv_weights = self.get_deconv_filt(kernel_shape) # output shape new_shape = tf.stack( [out_shape[0], out_shape[1], out_shape[2], in_channels]) upscore = tf.nn.conv2d_transpose(bottom, deconv_weights, output_shape=new_shape, strides=[1, upscale, upscale, 1]) return upscore if __name__ == '__main__': # img = misc.imread('/home/sw/Downloads/VOC2007/VOCdevkit/VOC2007/JPEGImages/000039.jpg')/255.0 # img = np.float32(np.expand_dims(img,axis=0)) img = np.random.normal(size=(10, 224, 224, 3)).astype(np.float32) xs = tf.placeholder(tf.float32, shape=[None, None, None, 3]) fcn32 = FCN32() fc8 = fcn32.bulid_model(xs) with tf.Session() as sess: tf.global_variables_initializer().run() output = sess.run(fc8, feed_dict={xs: img}) lab = np.argmax(output, axis=3) misc.imshow(lab[0])
import numpy as np from scipy.misc import imshow from matplotlib.pyplot import imshow import matplotlib.pyplot as plt def generate_image(m, n_H, n_W, n_C): image = np.random.uniform(0, 255, size=[m , n_H, n_W, n_C]).astype('float32') return image if __name__ == "__main__": image = generate_image(10,64,64,3) print(image) imshow(image[0]) plt.show()
def main(): parser = argparse.ArgumentParser(description="Check augmenters visually.") parser.add_argument( '--only', default=None, help= "If this is set, then only the results of an augmenter with this name will be shown.", required=False) args = parser.parse_args() images = [ ia.quokka_square(size=(128, 128)), misc.imresize(data.astronaut(), (128, 128)) ] augmenters = [ iaa.Noop(name="Noop"), iaa.OneOf(children=[ iaa.CoarseDropout(p=0.5, size_percent=0.05), iaa.AdditiveGaussianNoise(scale=0.1 * 255), iaa.Crop(percent=0.1) ], name="OneOf"), iaa.AddToHueAndSaturation((-20, 20), per_channel=True, name="AddHueAndSaturation"), iaa.Crop(px=(0, 8), name="Crop-px"), iaa.Crop(percent=(0, 0.1), name="Crop-percent"), iaa.Fliplr(0.5, name="Fliplr"), iaa.Flipud(0.5, name="Flipud"), iaa.Superpixels(p_replace=0.75, n_segments=50, name="Superpixels"), iaa.Grayscale(0.5, name="Grayscale0.5"), iaa.Grayscale(1.0, name="Grayscale1.0"), iaa.AverageBlur(k=(3, 11), name="AverageBlur"), iaa.GaussianBlur((0, 3.0), name="GaussianBlur"), iaa.MedianBlur(k=(3, 11), name="MedianBlur"), iaa.Sharpen(alpha=(0.1, 1.0), lightness=(0, 2.0), name="Sharpen"), iaa.Emboss(alpha=(0.1, 1.0), strength=(0, 2.0), name="Emboss"), iaa.EdgeDetect(alpha=(0.1, 1.0), name="EdgeDetect"), iaa.DirectedEdgeDetect(alpha=(0.1, 1.0), direction=(0, 1.0), name="DirectedEdgeDetect"), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.1 * 255), name="AdditiveGaussianNoise"), iaa.Dropout((0.0, 0.1), name="Dropout"), iaa.CoarseDropout(p=0.05, size_percent=(0.05, 0.5), name="CoarseDropout"), iaa.Invert(p=0.5, name="Invert"), iaa.Invert(p=0.5, per_channel=True, name="InvertPerChannel"), iaa.Add((-50, 50), name="Add"), iaa.Add((-50, 50), per_channel=True, name="AddPerChannel"), iaa.AddElementwise((-50, 50), name="AddElementwise"), iaa.Multiply((0.5, 1.5), name="Multiply"), iaa.Multiply((0.5, 1.5), per_channel=True, name="MultiplyPerChannel"), iaa.MultiplyElementwise((0.5, 1.5), name="MultiplyElementwise"), iaa.ContrastNormalization(alpha=(0.5, 2.0), name="ContrastNormalization"), iaa.Affine(scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, translate_px={ "x": (-16, 16), "y": (-16, 16) }, rotate=(-45, 45), shear=(-16, 16), order=ia.ALL, cval=(0, 255), mode=ia.ALL, name="Affine"), iaa.PiecewiseAffine(scale=0.03, nb_rows=(2, 6), nb_cols=(2, 6), name="PiecewiseAffine"), iaa.ElasticTransformation(alpha=(0.5, 8.0), sigma=1.0, name="ElasticTransformation") ] augmenters.append( iaa.Sequential([iaa.Sometimes(0.2, aug.copy()) for aug in augmenters], name="Sequential")) augmenters.append( iaa.Sometimes(0.5, [aug.copy() for aug in augmenters], name="Sometimes")) for augmenter in augmenters: if args.only is None or augmenter.name == args.only: print("Augmenter: %s" % (augmenter.name, )) grid = augmenter.draw_grid(images, rows=1, cols=16) misc.imshow(grid)
v_p_1 = int(polar_theta) % filter.shape[1] v_p_2 = math.ceil(polar_theta) % filter.shape[1] # Sampling color = filter[u_p_1, v_p_1] * (1 - u) * (1 - v) + filter[ u_p_1, v_p_2] * (1 - u) * v + filter[u_p_2, v_p_1] * u * ( 1 - v) + filter[u_p_2, v_p_2] * u * v color = (color + 1.0) / 2.0 rgba = np.array(cmap(color)) img_col[x, y, :] = np.int8(rgba[:3] * 255.0) if show_borders: rr, cc = circle_perimeter(mid_x, mid_y, radius) img_col[rr, cc, :] = 0 rr, cc = circle_perimeter(mid_x, mid_y, 1) img_col[rr, cc, :] = 0 for i in range(1, filter.shape[0] - 1): rr, cc = circle_perimeter(mid_x, mid_y, int(i * (radius / (filter.shape[0] - 1)))) img_col[rr, cc, :] = 0 for j in range(filter.shape[1]): theta = j * (2.0 * math.pi / filter.shape[1]) rr, cc = line(mid_x, mid_y, int(mid_x + radius * math.cos(theta)), int(mid_y + radius * math.sin(theta))) img_col[rr, cc, :] = 0 misc.imshow(img_col)
def plotNNfilter(units,N): filters = units.shape[3] for i in xrange(0,N): img=imresize((units[0,:,:,i]),(200,200)) imshow(img)
#!/usr/bin/env python3 ''' img4.py demo for python course at www.jasaplus.com ''' from scipy import misc img = misc.imread('img1.png') misc.imshow(img)
if __name__ == '__main__': img = imread('./RetargetMeAll/boat.png') img = np.stack((img, img, img, img, img, img)) B, H, W, C = img.shape print(H/grid_size) print(W/grid_size) print(img.shape) images = tf.placeholder(tf.float32, shape=(B, H, W, C)) featH = tf.Variable(initial_value=np.random.uniform(low=0.0, high=1.0, size=(B, np.int32(H/grid_size))), dtype=tf.float32) featW = tf.Variable(initial_value=np.random.uniform(low=0.0, high=1.0, size=(B, np.int32(W/grid_size))), dtype=tf.float32) input_size = tf.shape(images) aspect_ratio = tf.constant([np.round(H / 1.0), W / 3]) # featH = tf.nn.sigmoid(featH) # featH = tf.clip_by_value(featH, 0, 1.0) # featW = tf.nn.sigmoid(featW) # featW = tf.clip_by_value(featW, 0, 1.0) ta_final_result = reconstruct_image(images, featH, featW, input_size, aspect_ratio) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) x = sess.run(ta_final_result, feed_dict={images: img}) x = np.array(x) print(x.shape) imshow(x[0,...])
def show_image(image): misc.imshow(image.reshape(28,28,-1))
import fourier_obstacle as obstacle from polar import ipfft, pft_mult import os import pickle db = obstacle.ShapeSet(32, 256) db.add_shape(load_img("shape3.png")) db.add_shape(load_img("shape4.png")) A = db.shape_list[0] B = db.shape_list[1] pconv = ipfft(pft_mult(A.pft, B.pft), 513, 513) imsave("test1.png", pconv) imshow(pconv) #Contstruct sampled convolution field sconv = zeros((513, 513)) for i in range(513): for j in range(513): x = 257. - i y = 257. - j sconv[i, j] = db.potential(A, B, array([0., 0.]), array([x, y]), 0., pi/6.) print x,y, sconv[i,j] imsave("test2.png", sconv) imshow(sconv) dobst = abs(pconv - sconv) imsave("diff.png", dobst)
# (don't forget to optimize only the opt_img variable) optim = tf.train.AdamOptimizer(learning_rate=0.1).minimize( loss,var_list=[opt_img] ) # this clobbers all VGG variables, but we need it to initialize the # adam stuff, so we reload all of the weights... sess.run( tf.initialize_all_variables() ) vgg.load_weights( 'vgg16_weights.npz', sess ) # initialize with the content image print(sess.run([loss, opt_img.assign( content_img )])) lst = [] # --- place your optimization loop here for i in range(400): output = sess.run([loss,optim]) #print(i,output[0],output[2],output[3]) #lst.append(output[0]) stuffs = sess.run(opt_img) imshow(stuffs[0])
def show(self): """Show the image in a window.""" misc.imshow(self.image_arr)
print('Image file missing... Exiting!!') sys.exit(0) if not exists(CHECKPOINT_DIR): print('Checkpoint file missing... Exiting!!') sys.exit(0) model = load_model( CHECKPOINT_DIR, custom_objects={'LocalResponseNormalization': LocalResponseNormalization}) while True: # os.system('streamer -c /dev/video0 -o /media/aman/BE66ECBA66EC75151/Projects/IdeaQuest/images/final_detection/image.ppm') im = imread(IMAGE_DIR) imshow(im) x_shift = 32 y_shift = 54 x_range = [0, 32, 64, 96] y_range = [0, 108] im = Image.fromarray(im) im = im.resize((162, 128)) im = np.array(im) images = [] for x in x_range: for y in y_range: im_ = Image.fromarray(im[x + 2:x + x_shift - 2, y + 2:y + y_shift - 2])
mask = nd.binary_fill_holes(mask) isolatedmgs.append(mask) return isolatedmgs def dispMenu(n): print '\n\nTotal images processed: ' + str(n) num = input( 'Enter the image number to display: ') return int(num-1) directory = 'faces' print('\nReading Images\n') mgfiles = ReadImages(directory) #Isolating the skin colors and face from the image print('\nIsolating Faces in Images\n') isomgs = IsolateFaces(mgfiles) choice = '12' while choice != 0: num = dispMenu(len(isomgs)) if num == -1: break; elif (num < len(isomgs) and num > -1): smisc.imshow(mgfiles[num]) smisc.imshow(isomgs[num]) else: print '\nPlease enter a valid number\n'
#!/usr/bin/env python import numpy import kernel_tuner from scipy import misc #from matplotlib import pyplot #image = misc.imread("../test_small.jpg", "r") image = misc.imread("../test_small.jpg", mode='RGB') misc.imshow(image) print(image.shape) exit() kernel_names = [] """ pipeline overview -- fastnoisefilter -- zeromem dxs zeromem dys convolveHorizontally dxs convolveVertically dys normalize zeromem input convolveHorizontally input convolveVertically input -- zeromeantotalfilter -- computeMeanVertically
for i, chip in enumerate(chips): #cv2.imshow('chip_'+str(i), chip) #imshow(chip) #cv2.waitKey(0) cv2.imwrite('result/chip_' + str(i) + '.png', chip) draw = img.copy() for b in total_boxes: cv2.rectangle(draw, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])), (255, 255, 255)) for p in points: for i in range(5): cv2.circle(draw, (p[i], p[i + 5]), 1, (0, 0, 255), 2) imshow(draw) #cv2.waitKey(0) else: print("no face detected") # -------------- # test on camera # -------------- ''' camera = cv2.VideoCapture(0) cv2.namedWindow("detection result") while True: grab, frame = camera.read() #img = cv2.resize(frame, (320,180)) if frame: img = cv2.resize(frame, (320,180)) #img=frame
vertical = np.ones([1,28]).transpose() * 255 #columna de ceros horizontal = np.ones([29*cant+1]) *255 todo = np.copy(horizontal) for k in range(cant): fila = np.copy(vertical) #inicio la fila for l in range(cant): fila = np.hstack((fila,train_images[k*cant+l,:,:],vertical)) todo = np.vstack((todo,fila,horizontal)) #para que sea un poco más griss (imshow reescala así que no anda) todo /= 2 todo = todo.astype(int) imshow(todo) #%% Reordeno imagenes train_images = train_images.reshape((60000, 28, 28, 1)) #le agrego una columna porque los filtros la necesitan train_images = train_images.astype('float32') / 255 #lo hago un double en [0,1] test_images = test_images.reshape((10000, 28, 28, 1)) #le agrego una columna test_images = test_images.astype('float32') / 255 #lo hago un double en [0,1] train_labels = to_categorical(train_labels) #lo paso a categorico test_labels = to_categorical(test_labels) #lo paso a categorico cant_train = train_labels.shape[0] cant_test = test_labels.shape[0] train_labels_nuevas = np.concatenate((train_labels,np.zeros((cant_train,1))),axis=1) test_labels_nuevas = np.concatenate((test_labels,np.zeros((cant_test,1))),axis=1)
def main(): """ Main function. Does the following step by step: * Load images (from which to extract cat faces) from SOURCE_DIR * Initialize model (as trained via train_cat_face_locator.py) * Prepares images for the model (i.e. shrinks them, squares them) * Lets model locate cat faces in the images * Projects face coordinates onto original images * Squares the face rectangles (as we want to get square images at the end) * Extracts faces from images with some pixels of padding around theM * Augments each face image several times * Removes the padding from each face image * Resizes each face image to OUT_SCALE (height, width) * Saves each face image (unaugmented + augmented images) """ # -------------- # load images # -------------- images, paths = get_images([SOURCE_DIR]) images = images paths = paths # we will use the image filenames when saving the images at the end images_filenames = [path[path.rfind("/")+1:] for path in paths] # -------------- # create model # -------------- #model = create_model_tiny(MODEL_IMAGE_HEIGHT, MODEL_IMAGE_WIDTH, Adam()) model = create_model(MODEL_IMAGE_HEIGHT, MODEL_IMAGE_WIDTH, Adam()) load_weights_seq(model, WEIGHTS_FILEPATH) # -------------- # make all images square with required sizes # and roll color channel to dimension index 1 (required by theano) # -------------- paddings = [] images_padded = np.zeros((len(images), MODEL_IMAGE_HEIGHT, MODEL_IMAGE_WIDTH, 3)) for idx, image in enumerate(images): if idx == 0: print(idx, image.shape, paths[idx]) image_padded, (pad_top, pad_right, pad_bottom, pad_left) = square_image(image) images_padded[idx] = misc.imresize(image_padded, (MODEL_IMAGE_HEIGHT, MODEL_IMAGE_WIDTH)) paddings.append((pad_top, pad_right, pad_bottom, pad_left)) #misc.imshow(images_padded[0]) # roll color channel images_padded = np.rollaxis(images_padded, 3, 1) # project to 0-1 images_padded /= 255 #print(images_padded[0]) # -------------- # predict positions of faces # -------------- coordinates_predictions = predict_on_images(model, images_padded) print("[Predicted positions]", coordinates_predictions[0]) """ for idx, (tl_y, tl_x, br_y, br_x) in enumerate(coordinates_predictions): marked_image = visualize_rectangle(images_padded[idx]*255, tl_x, br_x, tl_y, br_y, \ (255,), channel_is_first_axis=True) misc.imshow(marked_image) """ # -------------- # project coordinates from small padded images to full-sized original images (without padding) # -------------- coordinates_orig = [] for idx, (tl_y, tl_x, br_y, br_x) in enumerate(coordinates_predictions): pad_top, pad_right, pad_bottom, pad_left = paddings[idx] height_full = images[idx].shape[0] + pad_top + pad_bottom width_full = images[idx].shape[1] + pad_right + pad_left height_orig = images[idx].shape[0] width_orig = images[idx].shape[1] tl_y_perc = tl_y / MODEL_IMAGE_HEIGHT tl_x_perc = tl_x / MODEL_IMAGE_WIDTH br_y_perc = br_y / MODEL_IMAGE_HEIGHT br_x_perc = br_x / MODEL_IMAGE_WIDTH # coordinates on full sized squared image version tl_y_full = int(tl_y_perc * height_full) tl_x_full = int(tl_x_perc * width_full) br_y_full = int(br_y_perc * height_full) br_x_full = int(br_x_perc * width_full) # remove paddings to get coordinates on original images tl_y_orig = tl_y_full - pad_top tl_x_orig = tl_x_full - pad_left br_y_orig = br_y_full - pad_top br_x_orig = br_x_full - pad_left # fix broken coordinates # anything below 0 # anything above image height (y) or width (x) # anything where top left >= bottom right tl_y_orig = min(max(tl_y_orig, 0), height_orig) tl_x_orig = min(max(tl_x_orig, 0), width_orig) br_y_orig = min(max(br_y_orig, 0), height_orig) br_x_orig = min(max(br_x_orig, 0), width_orig) if tl_y_orig >= br_y_orig: tl_y_orig = br_y_orig - 1 if tl_x_orig >= br_x_orig: tl_x_orig = br_x_orig - 1 coordinates_orig.append((tl_y_orig, tl_x_orig, br_y_orig, br_x_orig)) """ # project face coordinates to original image sizes coordinates_orig = [] for idx, (tl_y, tl_x, br_y, br_x) in enumerate(coordinates_nopad): height_orig = images[idx].shape[0] width_orig = images[idx].shape[1] tl_y_perc = tl_y / MODEL_IMAGE_HEIGHT tl_x_perc = tl_x / MODEL_IMAGE_WIDTH br_y_perc = br_y / MODEL_IMAGE_HEIGHT br_x_perc = br_x / MODEL_IMAGE_WIDTH tl_y_orig = int(tl_y_perc * height_orig) tl_x_orig = int(tl_x_perc * width_orig) br_y_orig = int(br_y_perc * height_orig) br_x_orig = int(br_x_perc * width_orig) coordinates_orig.append((tl_y_orig, tl_x_orig, br_y_orig, br_x_orig)) print("[Coordinates on original image]", coordinates_orig[0]) # remove padding from predicted face coordinates # tl = top left, br = bottom right coordinates_nopad = [] for idx, (tl_y, tl_x, br_y, br_x) in enumerate(coordinates_predictions): pad_top, pad_right, pad_bottom, pad_left = paddings[idx] tl_y_nopad = tl_y - pad_top tl_x_nopad = tl_x - pad_left br_y_nopad = br_y - pad_top br_x_nopad = br_x - pad_left tpl = (tl_y_nopad, tl_x_nopad, br_y_nopad, br_x_nopad) tpl_fixed = [max(coord, 0) for coord in tpl] if tpl_fixed[0] >= tpl_fixed[2]: tpl_fixed[2] += 1 elif tpl_fixed[1] >= tpl_fixed[3]: tpl_fixed[3] += 1 tpl_fixed = tuple(tpl_fixed) if tpl != tpl_fixed: print("[WARNING] Predicted coordinate below 0 after padding-removel. Bad prediction." \ " (In image %d, coordinates nopad: %s, coordinates pred: %s)" \ % (idx, tpl, coordinates_predictions[idx])) coordinates_nopad.append(tpl_fixed) """ print("[Removed padding from predicted coordinates]", coordinates_orig[0]) # -------------- # square faces # -------------- coordinates_orig_square = [] for idx, (tl_y, tl_x, br_y, br_x) in enumerate(coordinates_orig): height = br_y - tl_y width = br_x - tl_x i = 0 # we remove here instead of adding rows/cols, because that way we wont exceed the # image maximum sizes while height > width: if i % 2 == 0: tl_y += 1 else: br_y -= 1 height -= 1 i += 1 while width > height: if i % 2 == 0: tl_x += 1 else: br_x -= 1 width -= 1 i += 1 print("New height:", (br_y-tl_y), "New width:", (br_x-tl_x)) coordinates_orig_square.append((tl_y, tl_x, br_y, br_x)) print("[Squared face coordinates]", coordinates_orig_square[0]) # -------------- # pad faces # -------------- # extract "padded" faces, where the padding is part of the original image # (N pixels around the face) # After doing that, we can augment the "padded" faces, then remove the padding and have less # augmentation damage (i.e. areas that would otherwise be black will now be filled with parts # of the original image) faces_padded = [] for idx, (tl_y, tl_x, br_y, br_x) in enumerate(coordinates_orig_square): image = images[idx] # we pad the whole image by N pixels so that we can savely extract an area of N pixels # around the face image_padded = np.pad(image, ((AUGMENTATION_PADDING, AUGMENTATION_PADDING), \ (AUGMENTATION_PADDING, AUGMENTATION_PADDING), \ (0, 0)), mode=str("median")) face_padded = image_padded[tl_y:br_y+2*AUGMENTATION_PADDING, \ tl_x:br_x+2*AUGMENTATION_PADDING, \ ...] faces_padded.append(face_padded) print("[Extracted face with padding]") misc.imshow(faces_padded[0]) # -------------- # augment and save images # -------------- for idx, face_padded in enumerate(faces_padded): # these should be the same values for all images image_height = face_padded.shape[0] image_width = face_padded.shape[1] print("[specs of padded face] height", image_height, "width", image_width) # augment the padded images ia = ImageAugmenter(image_width, image_height, channel_is_first_axis=False, hflip=True, vflip=False, scale_to_percent=(0.90, 1.10), scale_axis_equally=True, rotation_deg=45, shear_deg=0, translation_x_px=8, translation_y_px=8) images_aug = np.zeros((AUGMENTATION_ITERATIONS, image_height, image_width, 3), dtype=np.uint8) for i in range(AUGMENTATION_ITERATIONS): images_aug[i, ...] = face_padded print("images_aug.shape", images_aug.shape) images_aug = ia.augment_batch(images_aug) # randomly change brightness of whole images for idx_aug, image_aug in enumerate(images_aug): by_percent = random.uniform(0.90, 1.10) images_aug[idx_aug] = np.clip(image_aug * by_percent, 0.0, 1.0) print("images_aug.shape [0]:", images_aug.shape) # add gaussian noise # skipped, because that could be added easily in torch as a layer #images_aug = images_aug + np.random.normal(0.0, 0.05, images_aug.shape) # remove the padding images_aug = images_aug[:, AUGMENTATION_PADDING:-AUGMENTATION_PADDING, AUGMENTATION_PADDING:-AUGMENTATION_PADDING, ...] print("images_aug.shape [1]:", images_aug.shape) # add the unaugmented image images_aug = np.vstack((images_aug, \ [face_padded[AUGMENTATION_PADDING:-AUGMENTATION_PADDING, \ AUGMENTATION_PADDING:-AUGMENTATION_PADDING, \ ...]])) print("images_aug.shape [2]:", images_aug.shape) # save images for i, image_aug in enumerate(images_aug): if image_aug.shape[0] * image_aug.shape[1] < MINIMUM_AREA: print("Ignoring image %d / %d because it is too small (area of %d vs min. %d)" \ % (idx, i, image_aug.shape[0] * image_aug.shape[1], MINIMUM_AREA)) else: image_resized = misc.imresize(image_aug, (OUT_SCALE, OUT_SCALE)) filename_aug = "%s_%d.jpg" % (images_filenames[idx].replace(".jpg", ""), i) #misc.imshow(image_resized) misc.imsave(os.path.join(TARGET_DIR, filename_aug), image_resized)
import os import dlib import sys from skimage import io detector = dlib.get_frontal_face_detector() def is_pic_file(s): PIC_EXTENTION = '.jpg' if len(s) < len(PIC_EXTENTION) + 1: return False if s[len(s) - len(PIC_EXTENTION):] == PIC_EXTENTION: return True return False files_current_dir = os.listdir('.') pic_files_current_dir = [x for x in files_current_dir if is_pic_file(x)] for pic_file in pic_files_current_dir: img = imread(pic_file) dets = detector(img, 1) print("Number of faces detected: {}".format(len(dets))) for i, d in enumerate(dets): print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format( i, d.left(), d.top(), d.right(), d.bottom())) cv2.rectangle(img, (d.left(), d.top()), (d.right(), d.bottom()), (0, 0, 255), 2) imshow(img)