def run_foa(sample, gt): # Import image test_image = foai.ImageObject(sample, rgb=False) test_image.ground_truth = gt # Generate Gaussian Blur Prior - Time ~0.0020006 foveation_prior = foac.matlab_style_gauss2D(test_image.modified.shape, 300) # Generate Gamma Kernel k = np.array([1, 9], dtype=float) mu = np.array([0.2, 0.5], dtype=float) kernel = foac.gamma_kernel(mask_size=(14, 14), k=k, mu=mu) # plt.imshow(kernel) # Generate Saliency Map foac.convolution(test_image, kernel, foveation_prior) # Bound and Rank the most Salient Regions of Saliency Map foas.salience_scan(test_image, rank_count=3, bbox_size=(28, 28)) test_image.draw_image_patches() test_image.plot_main() test_image.plot_patches() plt.show() return test_image
def __init__(self, cnn_layers=None, fc_layers=None): super(Model, self).__init__() # Network layer initialization self.cnn_layers = cnn_layers self.fc_layers = fc_layers # Generate Gaussian Blur Prior - Time ~0.0020006 self.prior = foac.matlab_style_gauss2D((60, 60), 300) # Generate Gamma Kernel k = np.array([1, 9], dtype=float) mu = np.array([0.2, 0.5], dtype=float) self.kernel = foac.gamma_kernel(mask_size=(14, 14), k=k, mu=mu)
def run_foa_svhn(input=SVHNImage, tf=int, gt=None, k=None, mu=None): assert (input.image is not None) img = np.array(input.image) x_dim = img.shape[0] y_dim = img.shape[1] print(np.array(img.shape)) # Load Image Object img = foai.ImageObject(img) img.ground_truth = gt # Generate Gaussian Blur Prior - Time ~0.0020006 foveation_prior = foac.matlab_style_gauss2D(img.modified.shape, 300) # Generate Gamma Kernel # k = np.array([1, 26, 1, 25, 1, 19], dtype=float) # mu = np.array([2, 2, 1, 1, 0.5, 0.5], dtype=float) k = np.array([1, 5, 1, 9, 1, 13], dtype=float) mu = np.array([0.8, 0.7, 0.3, 0.5, 0.1, 0.3], dtype=float) kernel = foac.gamma_kernel(img, mask_size=(32, 32), k=k, mu=mu) # Generate Saliency Map start = time.time() foac.convolution(img, kernel, foveation_prior) stop = time.time() print(f"Salience Map Generation: {stop - start} seconds") # Bound and Rank the most Salient Regions of Saliency Map foas.salience_scan(img, rank_count=4, bbox_size=(y_dim // 4, y_dim // 4)) bbt = 2 if (x_dim < 100 and y_dim < 100): bbt = 1 img.draw_image_patches(bbt=bbt) # Threshold img.salience_map = np.where(img.salience_map > tf, img.salience_map, 0) img.plot_main() img.plot_patched_map()
# Open test images as 8-bit RGB values - Time ~0.0778813 file = "./SMW_Test_Image.png" mario = foai.imageObject(file) file = "./AIM/eyetrackingdata/original_images/22.jpg" banana = foai.imageObject(file) file = "./AIM/eyetrackingdata/original_images/120.jpg" corner = foai.imageObject(file) # Test Image testIMG = mario # %% Generate Saliency Map # Generate Gaussian Blur Prior - Time ~0.0020006 foveation_prior = foac.matlab_style_gauss2D(testIMG.modified.shape, 300) # Generate Gamma Kernel kernel = foac.gamma_kernel(testIMG) # Generate Saliency Map start = time.time() foac.foa_convolution(testIMG, kernel, foveation_prior) stop = time.time() print("Salience Map Generation: ", stop - start, " seconds") # Bound and Rank the most Salient Regions of Saliency Map foas.salience_scan(testIMG, rankCount=5) # %% Plot Results
output_patch = file + '_patch_' + '.mp4' # Create temporary image directory os.makedirs(dir_path, exist_ok=True) # Read first frame success, image = movie.read() print("FIRST READ SUCCESS: ", success) # Get image dimensions: Mario - 224x256x3 height, width, channels = image.shape print("H: ", height, "W: ", width, "C: ", channels) start = time.time() # Generate Gaussian Blur Prior - Time ~0.0020006 prior = foac.matlab_style_gauss2D(image.shape, 300) # Generate Gamma Kernel image_curr = foai.ImageObject(image, fc=frame) image_prev = image_curr image_prev.salience_map = np.zeros(image_prev.original.shape[:-1]) kernel = foac.gamma_kernel(image_curr) # Step through each movie frame while success: frame += 1 image_name = str(frame) + ext image_curr = foai.ImageObject(image, fc=frame) # Generate Saliency Map with Gamma Filter foac.convolution(image_curr, kernel, prior)
def salience_scan(image=ImageObject, rank_count=4, bbox_size=(32, 32)): """ Saliency Map Scan Scan through the saliency map with a square region to find the most salient pieces of the image. Done by picking the maximally intense picture and bounding the area around it image - ImageObject being scanned rankCount - Number of objects to acquire before stopping boundLength - Length and width of the square saliency region """ # Copy salience map for processing smap = np.copy(image.salience_map) image.patched_sequence = np.empty((0, smap.shape[0], smap.shape[1])) # Create an inverse Gaussian kernel for removing salient regions inverse_gauss = matlab_style_gauss2D(bbox_size, sigma=28, inverse=True) # Pick out the top 'rankCount' maximally intense regions for i in range(rank_count): # # Copy and Reshape saliency map temp_smap = np.copy(smap) temp_smap = np.reshape(temp_smap, (1, smap.shape[0], smap.shape[1])) # Append modified saliency map image.patched_sequence = np.vstack((image.patched_sequence, temp_smap)) # Grab Maximally Intense Pixel Coordinates (Object Center) indices = np.where(smap == smap.max()) try: R = indices[0][0] # Row C = indices[1][0] # Column except IndexError: if (i == 1): print("Image has no variation, might just be black") R = bbox_size[0] C = bbox_size[1] # Get bounding box coordinates for object coords = build_bounding_box([R, C], temp_smap, bbox_size) # print(f"Coords {i}: {coords}") # Add coordinates to member list on the image object image.bb_coords.append(coords) # "Zero" the maximally intense region to avoid selecting it again R1 = coords["top_left"][0] C1 = coords["top_left"][1] R2 = coords["bottom_right"][0] C2 = coords["bottom_right"][1] # Sum up and find the average intensity of the region pixel_intensity_sum = 0 # Traverse through identified region for j in range(R1, R2): for k in range(C1, C2): x_dim = image.original.shape[0] y_dim = image.original.shape[1] if ((j < x_dim) and (k < y_dim)): pixel_intensity_sum += image.salience_map[j][k] # smap[j][k] = 0 # Zero out pixel smap[j][k] *= inverse_gauss[R2 - j - 1][C2 - k - 1]