def save_generated_images(generated_images, generator_iteration): # Create the plot plt.figure(figsize=(8, 8), num=1) gs1 = gridspec.GridSpec(8, 8) gs1.update(wspace=0, hspace=0) for i in range(64): ax1 = plt.subplot(gs1[i]) ax1.set_aspect('equal') image = generated_images[i, :, :, :] image += 1 image *= 127.5 fig = plt.imshow(image.astype(np.uint8)) plt.axis('off') fig.axes.get_xaxis().set_visible(False) fig.axes.get_yaxis().set_visible(False) plt.tight_layout() save_name = 'generated images/generatedSamples_genIter' + \ str(generator_iteration + 1) + '.png' plt.savefig(save_name, bbox_inches='tight', pad_inches=0) plt.pause(0.0000000001) plt.show()
def plot_img_with_class_activation_map(image, class_activation_map): image_height = image.shape[0] image_width = image.shape[1] cam_resized = tf.image.resize(class_activation_map, [image_height, image_width]).numpy() fig, ax = plt.subplots() ax.imshow(image.astype(int)) ax.imshow(cam_resized, cmap='jet', alpha=0.5) plt.axis('off') return fig
def HistEqualization(image, number_bins = 256): #implementing the histogram equalization # get the image histogram image_Hist, bins = np.histogram(image.flatten(), number_bins, [0, 256]) cdf = image_Hist.cumsum() # cumulative distribution function cdf = image_Hist.max()*cdf/cdf.max() #normalize cdf_mask = np.ma.masked_equal(cdf, 0) cdf_mask = (cdf_mask - cdf_mask.min())*255/(cdf_mask.max()-cdf_mask.min()) cdf = np.ma.filled(cdf_mask,0).astype('uint8') return cdf[image.astype('uint8')]
def load_image_pixels(filename, shape): image = load_img(filename, target_size=shape) # convert to numpy array image = img_to_array(image) # scale pixel values to [0, 1] image = image.astype('float32') image /= 255.0 # add a dimension so that we have one sample image = expand_dims(image, 0) return image
def preprocess_image(image, target_size): '''function to preprocess input image''' if image.mode != "RGB": image = image.convert("RGB") # if image is not RGB then convert to RGB image = image.resize(target_size) # resize image to defined target size # image = image.img_to_array(image) # convert image to an array of numbers image = asarray(image) image = image.astype(np.float32) # convert from uint8 to float32 image = np.expand_dims(image, axis=0) # expand the dimension of the image return image
def traitement(image_data, model): size = (165,133) image = ImageOps.fit(image_data, size, Image.ANTIALIAS) image=np.asarray(image) image = (image.astype(np.float32) / 255.0) img_reshape = np.expand_dims(image, axis = 0) img_reshape = image[np.newaxis,...,np.newaxis] prediction = model.predict(img_reshape) return prediction
def generate_with_pillow(models, seeds, color_channels, sample=False): if sample: images = sample_images() else: model = keras.models.load_model(models[-1]) # last models is the best images = model(seeds, training=False) img_size = (SIZE * NUM_HEIGHT, SIZE * NUM_WIDTH) if color_channels == 3: img = Image.new('RGB', img_size) for i in range(NUM_HEIGHT): for j in range(NUM_WIDTH): pos = i * NUM_HEIGHT + j image = images[pos] image = np.asarray(image) image = image[:, :, :] * 127.5 + 127.5 # image = image[:, :, :] * 127.5 + 127.5 # print(image.astype(int)) # print(image.shape) # image = image.reshape((SIZE, SIZE)) image = image.astype(np.uint8) im = Image.fromarray(image) img.paste(im, (i * SIZE, j * SIZE)) else: img = Image.new('L', img_size) for i in range(NUM_HEIGHT): for j in range(NUM_WIDTH): pos = i * NUM_HEIGHT + j image = images[pos] image = np.asarray(image) image = image[:, :, 0] * 127.5 + 127.5 image = image.astype(np.uint8) im = Image.fromarray(image) img.paste(im, (i * SIZE, j * SIZE)) img.save('../report/collage_{}x{}_{}.png'.format(NUM_HEIGHT, NUM_WIDTH, FOLDER_NAME))
def load_image_pixels(filename, shape): # load the image to get its shape image = load_img(filename) width, height = image.size # load the image with the required size image = load_img(filename, target_size=shape) # convert to numpy array image = img_to_array(image) # scale pixel values to [0, 1] image = image.astype('float32') image /= 255.0 # add a dimension so that we have one sample image = expand_dims(image, 0) return image, width, height
def face_alignment(image): # implementing face alignment image = np.array(image) image = image.astype(np.uint8) gray_image = image #gray_image = cv2.cvtColor(image ,cv2.COLOR_BGR2GRAY) # convert color image to grayscale image rects = detector(gray_image ,1) # detect faces in the grayscale image if len(rects) > 0: images = [] for (i, rect) in enumerate(rects): shape = predictor(image, rect) shape = shape_to_np(shape) rotated_image = rotate_image(image , shape) images.append(rotated_image) if len(rects) == 1 : return rotated_image else: return images else: #print("Error : number of detected face is zero, so we just return original image") return image