def execute_solver(IMAGE_FILE): sample4x4_crop = import_image(IMAGE_FILE) cluster_image = get_clustering_image(sample4x4_crop) cluster_groupings_dict = cluster_grouper(cluster_image).execute() final = pre_process_image(IMAGE_FILE) prediction_dict = clean_prediction_dict(get_predictions(final)) write_puzzle_file(cluster_groupings_dict,prediction_dict) try: solution = solve_puzzle('cv_puzzle.txt',False) except: return 'error' #get image of result fig = plt.figure(figsize=(2, 2), dpi=100,frameon=False) plt.axis('off') plt.imshow(sample4x4_crop, cmap=mpl.cm.Greys_r) for k,v in solution.items(): if v == None: return 'error' plt.annotate('{}'.format(v), xy=(k[0]*50+12,k[1]*50+40), fontsize=14) plt.tight_layout() plt.savefig('static/images/solution.jpg', bbox_inches='tight', dpi=100) #theres an issue with the saved layout, tight_layout #doesn't appear to work so I need to apply my own cropping again resize_final = import_image('static/images/solution.jpg',80) imsave('static/images/solution.jpg',resize_final) return 'good'
def show_cmaps(names): matplotlib.rc('text', usetex=False) a=np.outer(np.arange(0,1,0.01),np.ones(10)) # pseudo image data f=figure(figsize=(10,5)) f.subplots_adjust(top=0.8,bottom=0.05,left=0.01,right=0.99) # get list of all colormap names # this only obtains names of built-in colormaps: maps=[m for m in cm.datad if not m.endswith("_r")] # use undocumented cmap_d dictionary instead maps = [m for m in cm.cmap_d if not m.endswith("_r")] maps.sort() # determine number of subplots to make l=len(maps)+1 if names is not None: l=len(names) # assume all names are correct! # loop over maps and plot the selected ones i=0 for m in maps: if names is None or m in names: i+=1 ax = subplot(1,l,i) ax.axis("off") imshow(a,aspect='auto',cmap=cm.get_cmap(m),origin="lower") title(m,rotation=90,fontsize=10,verticalalignment='bottom') # savefig("colormaps.png",dpi=100,facecolor='gray') show()
def main(): # http://scikit-learn.org/stable/tutorial/basic/tutorial.html#loading-an-example-dataset # "A dataset is a dictionary-like object that holds all the data and some # metadata about the data. This data is stored in the .data member, which # is a n_samples, n_features array. In the case of supervised problem, one # or more response variables are stored in the .target member." # Toy datasets iris = datasets.load_iris() # The iris dataset (classification) digits = datasets.load_digits() # The digits dataset (classification) #boston = datasets.load_boston() # The boston house-prices dataset (regression) #diabetes = datasets.load_diabetes() # The diabetes dataset (regression) #linnerud = datasets.load_linnerud() # The linnerud dataset (multivariate regression) print(iris.feature_names) print(iris.data) print(iris.target_names) print(iris.target) print(digits.images[0]) print(digits.target_names) print(digits.target) plt.imshow(digits.images[0], cmap='gray', interpolation='nearest') plt.show()
def rootmov( numframes, degree, bins, dpi): #Main loop for making frame images for frame in range(1,numframes + 1): realy = list() imagy = list() percent = 1.0 * frame / numframes # Find the roots of all polynomials of given degree as coefficients vary. for group in product(pathcoeff(percent),repeat=degree): rootie = np.roots(group) for rooter in list(rootie): if rooter.imag != 0: realy.append(rooter.real) imagy.append(- rooter.imag) # Make histogram of roots. H, xedges, yedges = np.histogram2d(realy,imagy, bins=bins) H = np.log1p( 1 / (1 + H ) ) # Configure and save an image of the histogram. fig=plt.figure( facecolor='k', edgecolor='k') ax=plt.gca() plt.setp(ax, frame_on=True) plt.setp(ax.get_xticklabels(), visible=False) plt.setp(ax.get_yticklabels(), visible=False) plt.setp(ax.get_xticklines(), visible=False) plt.setp(ax.get_yticklines(), visible=False) plt.imshow(H,interpolation='bicubic',extent=[0,1000,0,600], cmap=dynacm( percent ) ) plt.savefig("root_test{:04}.png".format(frame),dpi=dpi, facecolor='k', edgecolor='k', bbox_inches='tight') ax.clear() plt.close(fig)
def show_overlay(img3d, cc3d, ncc=10, s=85, xyz = 'xy',alpha=.8): """Shows the connected components overlayed over img3d Input ====== img3d -- 3d array cc3d -- 3d array ( preferably of same shape as img3d, use get_3d_cc(...) ) ncc -- where to cut off the color scale s -- slice to show xyz -- which projection to use in {'xy','xz','yz'} """ cc = get_slice(cc3d,s,xyz) img = get_slice(img3d,s,xyz) notcc = np.isnan(cc) incc = np.not_equal(notcc,True) img4 = plt.cm.gray(img/np.nanmax(img)) if ncc is not np.Inf: cc = plt.cm.jet(cc/float(ncc)) else: cc = plt.cm.jet(np.log(cc)/np.log(np.nanmax(cc))) cc[notcc,:]=img4[notcc,:] cc[incc,3] = 1-img[incc]/(2*np.nanmax(img)) plt.imshow(cc)
def filterFunc(): rects = [] hsv_planes = [[[]]] if os.path.isfile(Image_File): BGR=cv2.imread(Image_File) gray = cv2.cvtColor(BGR, cv2.COLOR_BGR2GRAY) img = gray f = np.fft.fft2(img) fshift = np.fft.fftshift(f) magnitude_spectrum = 20*np.log(np.abs(fshift)) plt.subplot(221),plt.imshow(img, cmap = 'gray') plt.title('Input Image'), plt.xticks([]), plt.yticks([]) plt.subplot(222),plt.imshow(magnitude_spectrum, cmap = 'gray') plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([]) FiltzeredFFT = HighPassFilter(fshift, 60) plt.subplot(223),plt.imshow(np.abs(FiltzeredFFT), cmap = 'gray') plt.title('Filtered'), plt.xticks([]), plt.yticks([]) f_ishift = np.fft.ifftshift(FiltzeredFFT) img_back = np.fft.ifft2(f_ishift) img_back = np.abs(img_back) plt.subplot(224),plt.imshow(np.abs(img_back), cmap = 'gray') plt.title('Filtered Image'), plt.xticks([]), plt.yticks([]) plt.show()
def main(): gw = gridworld() a = agent(gw) for epoch in range(20): a.initEpoch() while True: rwd, stat, act = a.takeAction() a.updateQ(rwd, stat, act) if gw.status() == 'Goal': break if mod(a.counter, 10)==0: print(gw.state()) print(gw.field()) print('Finished') print(a.counter) print(gw.state()) print(gw.field()) Q = transpose(a.Q(), (2,0,1)) for i in range(4): plt.subplot(2,2,i) plt.imshow(Q[i], interpolation='nearest') plt.title(a.actions()[i]) plt.colorbar() plt.show()
def vis_result(image, seg, gt, title1='Segmentation', title2='Ground truth', savefile=None): indices = np.where(seg >= 0.5) indices_gt = np.where(gt >= 0.5) im_norm = image / image.max() rgb_image = color.gray2rgb(im_norm) multiplier = [0., 1., 1.] multiplier_gt = [1., 1., 0.] im_seg = rgb_image.copy() im_gt = rgb_image.copy() im_seg[indices[0], indices[1], :] *= multiplier im_gt[indices_gt[0], indices_gt[1], :] *= multiplier_gt fig = plt.figure() a = fig.add_subplot(1, 2, 1) plt.imshow(im_seg) a.set_title(title1) a = fig.add_subplot(1, 2, 2) plt.imshow(im_gt) a.set_title(title2) if savefile is None: plt.show() else: plt.savefig(savefile) plt.close()
def Test(self): test_Dir = "Result"; if not os.path.exists(test_Dir): os.makedirs(test_Dir); test_Label_List = [0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5]; test_Label_Pattern = np.zeros((16, 10)); test_Label_Pattern[np.arange(16), test_Label_List] = 1; feed_Dict = { self.noise_Placeholder: np.random.uniform(-1., 1., size=[16, self.noise_Size]), self.label_for_Fake_Placeholder: test_Label_Pattern, self.is_Training_Placeholder: False }; #Batch is constant in the test. global_Step, mnist_List = self.tf_Session.run(self.test_Tensor_List, feed_dict = feed_Dict); fig = plt.figure(figsize=(4, 4)) gs = gridspec.GridSpec(4, 4) gs.update(wspace=0.05, hspace=0.05) for index, mnist in enumerate(mnist_List): ax = plt.subplot(gs[index]) plt.axis('off') ax.set_xticklabels([]) ax.set_yticklabels([]) ax.set_aspect('equal') plt.imshow(mnist.reshape(28, 28), cmap='Greys_r') plt.savefig('%s/S%d.png' % (test_Dir, global_Step), bbox_inches='tight'); plt.close();
def one_file_features(self, im, demo=False): """ Zde je kontruován vektor příznaků pro klasfikátor """ # color processing fd = np.array([]) img = skimage.color.rgb2gray(im) # graylevel if self.hogFeatures: pass if self.grayLevelFeatures: imr = skimage.transform.resize(img, [9, 9]) glfd = imr.reshape(-1) fd = np.append(fd, glfd) if demo: plt.imshow(imr) plt.show() #fd.append(hsvft[:]) if self.colorFeatures: #fd = np.append(fd, colorft) pass #print hog_image return fd
def export(data, F, k): '''Write data to a png image Arguments --------- data : numpy.ndarray array containing the data to be written as png image F : float feed rate of the current configuration k : float rate constant of the current configuration ''' figsize = tuple(s / 72.0 for s in data.shape) fig = plt.figure(figsize=figsize, dpi=72.0, facecolor='white') fig.add_axes([0, 0, 1, 1], frameon=False) plt.xticks([]) plt.yticks([]) plt.imshow(data, cmap=plt.cm.RdBu_r, interpolation='bicubic') plt.gci().set_clim(0, 1) filename = './study/F{:03d}-k{:03d}.png'.format(int(1000*F), int(1000*k)) plt.savefig(filename, dpi=72.0) plt.close()
def edge_detect(img): BLUR_SIZE = 51 TRUNC_RATIO = 0.75 CLOSING_SIZE = 5 # denoised = cv2.fastNlMeansDenoisingColored(img,None,10,10,7,21) # img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # too_bright=np.logical_and(img[:,:,1]<50, img[:,:,2]>200) # np.set_printoptions(threshold=np.nan) # np.savetxt('conconcon',img[:,:,1],'%i') # img[:,:,1]=np.where(too_bright, np.sqrt(img[:,:,1])+70, img[:,:,1]) # img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) blur = cv2.blur(gray, (BLUR_SIZE, BLUR_SIZE)) edge = np.floor(0.5 * gray + 0.5 * (255 - blur)).astype('uint8') hist,bins = np.histogram(edge.flatten(), 256, [0, 256]) cdf = hist.cumsum() cdf_normalized = cdf * hist.max() / cdf.max() cdf_m = np.ma.masked_equal(cdf, 0) cdf_m = (cdf_m - cdf_m.min()) * 255 / (cdf_m.max() - cdf_m.min()) cdf = np.ma.filled(cdf_m, 0).astype('uint8') equ = cdf[edge] hist,bins = np.histogram(equ.flatten(),256,[0,256]) max_idx = np.argmax(hist); hist_clean = np.where(equ > TRUNC_RATIO * max_idx, 255, equ) kernel = np.ones((CLOSING_SIZE, CLOSING_SIZE), np.uint8) closing = cv2.morphologyEx(hist_clean, cv2.MORPH_CLOSE, kernel) plt.imshow(closing, cmap='Greys_r') plt.show() cv2.waitKey(100)
def heatmap(vals, size=6, aspect=1): """ Plot a heatmap from matrix data """ plt.figure(figsize=(size, size)) plt.imshow(vals, cmap="gray", aspect=aspect, interpolation="none", vmin=0, vmax=1) plt.axis("off")
def plot_jacobian(A, name, cmap= plt.cm.coolwarm, normalize=True, precision=1e-6): """ Customized visualization of jacobian matrices for observing sparsity patterns """ plt.figure() fig, ax = plt.subplots() if normalize is True: plt.imshow(A, interpolation='none', cmap=cmap, norm = mpl.colors.Normalize(vmin=-1.,vmax=1.)) else: plt.imshow(A, interpolation='none', cmap=cmap) plt.colorbar(format=ticker.FuncFormatter(fmt)) ax.spy(A, marker='.', markersize=0, precision=precision) ax.spines['right'].set_visible(True) ax.spines['bottom'].set_visible(True) ax.xaxis.set_ticks_position('top') ax.yaxis.set_ticks_position('left') xlabels = np.linspace(0, A.shape[0], 5, True, dtype=int) ylabels = np.linspace(0, A.shape[1], 5, True, dtype=int) plt.xticks(xlabels) plt.yticks(ylabels) plt.savefig(name, bbox_inches='tight', pad_inches=0.05) plt.close() return
def atest_interpolation_coast(self): """Test interpolation.""" reader = reader_ROMS_native.Reader('/disk2/data/SVIM/ocean_avg_20081201.nc') num_points = 50 np.random.seed(0) # To get the same random numbers each time lons = np.random.uniform(12, 16, num_points) lats = np.random.uniform(68.3, 68.3, num_points) z = np.random.uniform(-100, 0, num_points) x, y = reader.lonlat2xy(lons, lats) variables = ['x_sea_water_velocity', 'y_sea_water_velocity', 'sea_water_temperature'] # Read a block of data covering the points data = reader.get_variables(variables, time=reader.start_time, x=x, y=y, z=z, block=True) import matplotlib.pyplot as plt plt.imshow(data['x_sea_water_velocity'][0,:,:]) plt.colorbar() plt.show() b = ReaderBlock(data, interpolation_horizontal='nearest') env, prof = b.interpolate(x, y, z, variables, profiles=['x_sea_water_velocity'], profiles_depth=[-30, 0])
def plot_bold_nii(data, time): """ Plot all horizontal slices of fMRI image at a given point in time. Parameters: ----------- data : np.ndarray 4D array of fMRI data time : int The index (with respect to time) of the volume to plot Return: ------- Canvas of horizontal slices of the brain at a given time """ assert time <= data.shape[3] length, width, depth, timespan = data.shape len_side = int(np.ceil(np.sqrt(depth))) # Number slices per side of canvas canvas = np.zeros((length * len_side, width * len_side)) depth_i = 0 # The ith slice with respect to depth for row in range(len_side): column = 0 while plot < len_side and depth_i < depth: x_range = np.arange(length * row, width * (column + 1)) y_start = np.arange(width * column, width * (column + 1)) canvas[x_range, y_range] = data[..., depth_i, time] depth_i += 1 column += 1 plt.imshow(canvas, interpolation="nearest", cmap="gray") return None
def plot_bold_nii(filename, timepoint): """Plot all slices of a fMRI image in one plot at a specific time point Parameters: ----------- filename: BOLD.nii.gz timepoint: the time point chose Return: ------- None Note: ----- The function produce a plot """ img = nib.load(filename) data = img.get_data() assert timepoint <= data.shape[-1] plot_per_row = int(np.ceil(np.sqrt(data.shape[2]))) frame = np.zeros((data.shape[0]*plot_per_row, data.shape[1]*plot_per_row)) num_of_plots = 0 for i in range(plot_per_row): j = 0 while j < plot_per_row and num_of_plots < data.shape[2]: frame[data.shape[0]*i:data.shape[0]*(i+1), data.shape[1]*j:data.shape[1]*(j+1)] = data[:,:,num_of_plots,timepoint] num_of_plots+=1 j+=1 plt.imshow(frame, interpolation="nearest",cmap="gray") return None
def make_let_im(let_file, dim = 16, y_lo = 70, y_hi = 220, x_lo = 10, x_hi = 200, edge_pix = 150, plot_let = False): letter = mpimg.imread(let_file) letter = letter[y_lo:y_hi, x_lo:x_hi, 0] for i in range(letter.shape[1]): if letter[0:edge_pix, i].any() == 0: # here is to remove the edge letter[0:edge_pix, i] = 1 plt.imshow(letter, cmap='gray') plt.grid('off') plt.show() x = np.arange(letter.shape[1]) y = np.arange(letter.shape[0]) f2d = interp2d(x, y, letter) x_new = np.linspace(0, letter.shape[1], dim) # dim = 16 y_new = np.linspace(0, letter.shape[0], dim) letter_new = f2d(x_new, y_new) letter_new -= np.mean(letter_new) if plot_let: plt.imshow(letter_new, cmap = 'gray') plt.grid('off') plt.show() letter_flat = letter_new.flatten() # letter_flat is a 1-dimensional array containing 256 elements return letter_new, letter_flat
def vis_square(data): data = (data - data.min()) / (data.max() - data.min()) n = int(np.ceil(np.sqrt(data.shape[0]))) padding = ((0, 0), (0, 0), (1, 1), (1, 1)) data = np.pad(data, padding, mode = 'constant', constant_values = 0) #data = data.squeeze() total_array = np.array([]) for i in range(data.shape[0]): row_array = np.array([]) for j in range(data.shape[1]): if j == 0: row_array = data[i, j, :, :] else: row_array = np.vstack((row_array, data[i, j, :, :])) if i == 0: total_array = row_array else: total_array = np.hstack((total_array, row_array)) print("shape of total_array: {}".format(total_array.shape)) #data = data.reshape((n, n) + data.shape[1:]) #data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:]) plt.imshow(total_array); plt.axis('off') plt.show()
def plot_brights(ax, path, star, regionList, goal=False): ''' Components of this routine: Projected brightness map Please note that this has been modified for use in diagnostic plots, there should really be a way to specify a windowNumber for real data ''' currentWindow = 0 ########################### # Make the brightness map # ########################### img = make_bright_image(star, regionList, currentWindow, goal=goal) plt.imsave(path + "temp.jpg", img, cmap='hot', vmin=0.85, vmax=1.15) plt.imshow(img, cmap='hot') #Create the plot bmap = Basemap(projection='moll', lon_0 = 0, ax=ax) bmap.warpimage(path + "temp.jpg", ax=ax) if goal: ax.set_title("Desired Map") else: ax.set_title("Average Map")
def word_cloud_fre_all(domain_dirs, merged_flag=False): ''' 通过所有domain的wordcloud对象绘制wordcloud图像并保存在相应domain下 :return: ''' for domain_name in listdir(domain_dirs): domain_dir = path.join(domain_dirs, domain_name) wordclouds = word_cloud_frequency(domain_dir, domain_name, merged_flag) if merged_flag: wordcloud_dir = path.join(domain_dir, domain_name + '_merged_wordcloud1/') else: wordcloud_dir = path.join(domain_dir, domain_name + '_wordcloud1/') if not path.exists(wordcloud_dir): makedirs(wordcloud_dir) for topic_id, wordcloud in enumerate(wordclouds): # dump(wordcloud, open(path.join(wordcloud_dir, 'topic' + str(topic_id)), 'wb')) plt.imshow(wordcloud) plt.axis("off") # plt.show() plt.savefig(path.join(wordcloud_dir, 'topic' + str(topic_id))) print(domain_name, ' complete!!!') break
def logLikelihood(self, data): assert self.numImages == data.numImages which = [np.nonzero(data.id == i)[0]\ for i in xrange(0, self.numImages)] # Mean vector m = np.empty(data.t.size) for i in xrange(0, self.numImages): m[which[i]] = self.mag[i] # Covariance matrix [t1, t2] = np.meshgrid(data.t, data.t) lags = np.abs(t2 - t1) plt.imshow(lags) plt.show() ids = np.meshgrid(data.id, data.id) equal = ids[0] == ids[1] ## try: ## L = la.cholesky(C) ## except: ## return -np.inf # y = data.y - m # logDeterminant = 2.0*np.sum(np.log(np.diag(L))) # solution = la.cho_solve((L, True), y) # exponent = np.dot(y, solution) # logL = -0.5*data.t.size*np.log(2.0*np.pi) - 0.5*logDeterminant - 0.5*exponent return 0.
def plot_images(data_list, data_shape="auto", fig_shape="auto"): """ plotting data on current plt object. In default,data_shape and fig_shape are auto. It means considered the data as a sqare structure. """ n_data = len(data_list) if data_shape == "auto": sqr = int(n_data ** 0.5) if sqr * sqr != n_data: data_shape = (sqr + 1, sqr + 1) else: data_shape = (sqr, sqr) plt.figure(figsize=data_shape) for i, data in enumerate(data_list): plt.subplot(data_shape[0], data_shape[1], i + 1) plt.gray() if fig_shape == "auto": fig_size = int(len(data) ** 0.5) if fig_size ** 2 != len(data): fig_shape = (fig_size + 1, fig_size + 1) else: fig_shape = (fig_size, fig_size) Z = data.reshape(fig_shape[0], fig_shape[1]) plt.imshow(Z, interpolation="nearest") plt.tick_params(labelleft="off", labelbottom="off") plt.tick_params(axis="both", which="both", left="off", bottom="off", right="off", top="off") plt.subplots_adjust(hspace=0.05) plt.subplots_adjust(wspace=0.05)
def psf_dl(self, N_OS = None, plate_scale_as_px = None, plotit = False, return_efield = False ): """ Find the PSF of the wavefront at a given wavelength. Parameters ---------- return_efield: boolean Do we return an electric field? If not, return the intensity. """ # Make the field uniform. self.flatten_field() # The PSF is then simply the FFT of the pupil. psf = self.image(N_OS = N_OS, plate_scale_as_px = plate_scale_as_px, return_efield = return_efield) psf /= sum(psf.flatten()) if plotit: axesScale = [0, self.sz*self.m_per_px, 0, self.sz*self.m_per_px] plt.figure() plt.imshow(psf, extent = axesScale) plt.title('PSF of optical system') return psf
def template_matching(): img = cv2.imread('messi.jpg',0) img2 = img.copy() template = cv2.imread('face.png',0) w, h = template.shape[::-1] # All the 6 methods for comparison in a list methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR', 'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED'] for meth in methods: img = img2.copy() method = eval(meth) # Apply template Matching res = cv2.matchTemplate(img,template,method) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]: top_left = min_loc else: top_left = max_loc bottom_right = (top_left[0] + w, top_left[1] + h) cv2.rectangle(img,top_left, bottom_right, 255, 2) plt.subplot(121),plt.imshow(res,cmap = 'gray') plt.title('Matching Result'), plt.xticks([]), plt.yticks([]) plt.subplot(122),plt.imshow(img,cmap = 'gray') plt.title('Detected Point'), plt.xticks([]), plt.yticks([]) plt.suptitle(meth) plt.show()
def subaps_to_grid(subap_frame, plot=False): """ Turn a 1D list of subaperture values to an 11x11 grid See figure from Rudy / Contreras for how we count our subaps. Or just do this: >>> subaps_to_grid(range(97), plot=True) >>> overlay_indices() (n.b. sensible image plotting requires the origin='bottom' keyword argument unless you set it as default in matplotlibrc) """ grid = np.ndarray((11,11)) grid[:,:] = np.nan grid[0][3:8] = subap_frame[0:5] grid[1][2:9] = subap_frame[5:12] grid[2][1:10] = subap_frame[12:21] grid[3] = subap_frame[21:32] grid[4] = subap_frame[32:43] grid[5] = subap_frame[43:54] grid[6] = subap_frame[54:65] grid[7] = subap_frame[65:76] grid[8][1:10] = subap_frame[76:85] grid[9][2:9] = subap_frame[85:92] grid[10][3:8] = subap_frame[92:97] grid = grid.T # we're filling in row-by-row from the top, but numbering starts # in the bottom left with zero and proceeds column-by-column if plot: plt.imshow(grid, origin='bottom') return grid
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues): """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] print("Normalized confusion matrix") else: print('Confusion matrix, without normalization') print(cm) thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label')
def draw_digit(data): size = int(len(data) ** 0.5) Z = data.reshape(size, size) # convert from vector to matrix plt.imshow(Z, interpolation="None") plt.gray() plt.tick_params(labelbottom="off") plt.tick_params(labelleft="off")
def imview(*args, **kwargs): """ A more sensible matplotlib-based image viewer command, a wrapper around `matplotlib.pyplot.imshow`. Parameters are identical to `matplotlib.pyplot.imshow` but this behaves somewhat differently: * By default, it creates a new figure (unless a `figure` keyword argument is supplied. * It modifies the axes of that figure to use the full frame, without ticks or tick labels. * It turns on `nearest` interpolation by default (i.e., it does not antialias pixel data). This can be overridden with the `interpolation` argument as in `imshow`. All other arguments and keyword arguments are passed on to `imshow`.` """ if 'figure' not in kwargs: f = plt.figure() else: f = kwargs['figure'] new_ax = matplotlib.axes.Axes(f, [0, 0, 1, 1], xticks=[], yticks=[], frame_on=False) f.delaxes(f.gca()) f.add_axes(new_ax) if len(args) < 5 and 'interpolation' not in kwargs: kwargs['interpolation'] = 'nearest' plt.imshow(*args, **kwargs)
def draw(): global g_matrix m = np.matrix(np.pad(g_matrix,((1,1),(1,1)), mode='constant', constant_values=0)) plt.imshow(m, cmap = cm.Greys_r, interpolation='nearest') # 横纵坐标 # plt.xticks([]), plt.yticks([]) plt.show()
percentile_threshold = 70 threshold_value = np.percentile(mean_img, percentile_threshold) binary_frame = binarized_frame( movie_frame=mean_img, filled_value=1, threshold_value=None, percentile_threshold=percentile_threshold, with_uint=False, with_4_blocks=True) test_display = True if test_display: print(f"threshold_value {threshold_value}") plt.imshow(binary_frame, cmap=cm.Greys) plt.show() else: # ring analysis session_id = "190127_190207_190208_a003" tiff_file_name = f"/media/julien/Not_today/hne_not_today/data/tom_data/{session_id}_MotCorr.tif" seq_tif = load_tiff_movie(tiff_file_name=tiff_file_name) print(f"seq_tif.shape {seq_tif.shape}") n_frames = seq_tif.shape[0] seq_tif = seq_tif[:, :150, :150] im_mean = np.mean(seq_tif, axis=0).astype(np.uint8) cv2.imwrite(os.path.join(results_path, "im_mean.png"), im_mean)
# 开始训练 x, y = mnist.train.next_batch(5000) model.fit(x, x, batch_size=batch_size, epochs=num_steps) n = 4 canvas_orig = np.empty((28 * n, 28 * n)) canvas_recon = np.empty((28 * n, 28 * n)) batch_x, batch_y = mnist.validation.next_batch(n) test_pred = model.predict(batch_x) for i in range(n): # 展示原始图像 for j in range(n): # Draw the original digits canvas_orig[i * 28:(i + 1) * 28, j * 28:(j + 1) * 28] = batch_x[j].reshape([28, 28]) # 展示重构图像 for j in range(n): # Draw the reconstructed digits canvas_recon[i * 28:(i + 1) * 28, j * 28:(j + 1) * 28] = test_pred[j].reshape([28, 28]) print("原始图像") plt.figure(figsize=(n, n)) plt.imshow(canvas_orig, origin="upper", cmap="gray") plt.show() print("重构图像") plt.figure(figsize=(n, n)) plt.imshow(canvas_recon, origin="upper", cmap="gray") plt.show()
def test(self, data, split='train', attention_visualization=True, save_sampled_captions=True): ''' Args: - data: dictionary with the following keys: - features: Feature vectors of shape (5000, 196, 512) - file_names: Image file names of shape (5000, ) - captions: Captions of shape (24210, 17) - image_idxs: Indices for mapping caption to image of shape (24210, ) - features_to_captions: Mapping feature to captions (5000, 4~5) - split: 'train', 'val' or 'test' - attention_visualization: If True, visualize attention weights with images for each sampled word. (ipthon notebook) - save_sampled_captions: If True, save sampled captions to pkl file for computing BLEU scores. ''' features = data['features'] # build a graph to sample captions alphas, betas, sampled_captions = self.model.build_sampler( max_len=20) # (N, max_len, L), (N, max_len) config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: saver = tf.train.Saver() saver.restore(sess, self.test_model) features_batch, image_files = sample_coco_minibatch( data, self.batch_size) feed_dict = {self.model.features: features_batch} alps, bts, sam_cap = sess.run( [alphas, betas, sampled_captions], feed_dict) # (N, max_len, L), (N, max_len) decoded = decode_captions(sam_cap, self.model.idx_to_word) if attention_visualization: for n in range(10): print "Sampled Caption: %s" % decoded[n] # Plot original image img = ndimage.imread(image_files[n]) plt.subplot(4, 5, 1) plt.imshow(img) plt.axis('off') # Plot images with attention weights words = decoded[n].split(" ") for t in range(len(words)): if t > 18: break plt.subplot(4, 5, t + 2) plt.text(0, 1, '%s(%.2f)' % (words[t], bts[n, t]), color='black', backgroundcolor='white', fontsize=8) plt.imshow(img) alp_curr = alps[n, t, :].reshape(14, 14) alp_img = skimage.transform.pyramid_expand(alp_curr, upscale=16, sigma=20) plt.imshow(alp_img, alpha=0.85) plt.axis('off') plt.show() if save_sampled_captions: all_sam_cap = np.ndarray((features.shape[0], 20)) num_iter = int( np.ceil(float(features.shape[0]) / self.batch_size)) for i in range(num_iter): features_batch = features[i * self.batch_size:(i + 1) * self.batch_size] feed_dict = {self.model.features: features_batch} all_sam_cap[i * self.batch_size:(i + 1) * self.batch_size] = sess.run( sampled_captions, feed_dict) all_decoded = decode_captions(all_sam_cap, self.model.idx_to_word) save_pickle( all_decoded, "./data/%s/%s.candidate.captions.pkl" % (split, split))
def remove_noiseInSpectro(spectro, histo_relative_size=8, window_smoothing=5, N=0.1, dB=False, plot=False): """ Compute a new spectrogram which is "Noise Removed". spectro: spectrogram of the audio signal histo_relative_size: ration between the size of the spectrogram and the size of the histogram window_smoothing: number of points to apply a mean filtering on the histogram and on the background noise curve N: Parameter to set the threshold around the modal intensity dB: If set at True, the spectrogram is converted in decibels plot: if set at True, the function plot the orginal and noise removed spectrograms Output: Noise removed spectrogram Ref: Towsey, Michael W. (2013) Noise removal from wave-forms and spectrograms derived from natural recordings of the environment. Towsey, Michael (2013), Noise Removal from Waveforms and Spectrograms Derived from Natural Recordings of the Environment. Queensland University of Technology, Brisbane. """ low_value = 1.e-07 # Minimum value for the new spectrogram (preferably slightly higher than 0) if dB: spectro = 20 * np.log10(spectro) len_spectro_e = len(spectro[0]) histo_size = int(len_spectro_e / histo_relative_size) background_noise = [] for row in spectro: hist, bin_edges = np.histogram(row, bins=histo_size, density=False) hist_smooth = ([ np.mean(hist[i - int(window_smoothing / 2):i + int(window_smoothing / 2)]) for i in range(int(window_smoothing / 2), len(hist) - int(window_smoothing / 2)) ]) hist_smooth = np.concatenate( (np.zeros(int(window_smoothing / 2)), hist_smooth, np.zeros(int(window_smoothing / 2)))) modal_intensity = np.min([ np.argmax(hist_smooth), 95 * histo_size / 100 ]) # test if modal intensity value is in the top 5% if N > 0: count_thresh = 68 * sum(hist_smooth) / 100 count = hist_smooth[int(modal_intensity)] index_bin = 1 while count < count_thresh: if modal_intensity + index_bin <= (len(hist_smooth) - 1): count = count + hist_smooth[int(modal_intensity + index_bin)] if modal_intensity - index_bin >= 0: count = count + hist_smooth[int(modal_intensity - index_bin)] index_bin += 1 thresh = int(np.min((histo_size, modal_intensity + N * index_bin))) background_noise.append(bin_edges[thresh]) elif N == 0: background_noise.append(bin_edges[modal_intensity]) background_noise_smooth = ([ np.mean(background_noise[i - int(window_smoothing / 2):i + int(window_smoothing / 2)]) for i in range(int(window_smoothing / 2), len(background_noise) - int(window_smoothing / 2)) ]) # keep background noise at the end to avoid last row problem (last bin with old microphones) background_noise_smooth = np.concatenate( (background_noise[0:int(window_smoothing / 2)], background_noise_smooth, background_noise[-int(window_smoothing / 2):])) new_spec = np.array([col - background_noise_smooth for col in spectro.T]).T new_spec = new_spec.clip( min=low_value) # replace negative values by value close to zero #Figure if plot: colormap = "jet" fig = plt.figure() a = fig.add_subplot(1, 2, 1) if dB: plt.imshow(new_spec, origin="lower", aspect="auto", cmap=colormap, interpolation="none") else: plt.imshow(20 * np.log10(new_spec), origin="lower", aspect="auto", cmap=colormap, interpolation="none") a = fig.add_subplot(1, 2, 2) if dB: plt.imshow(new_spec, origin="lower", aspect="auto", cmap=colormap, interpolation="none") else: plt.imshow(20 * np.log10(spectro), origin="lower", aspect="auto", cmap=colormap, interpolation="none") plt.show() return new_spec
# plt.figure() # plt.pcolormesh(gaussian) # plt.show() t=0.0 a=None for _ in range(300): t+=P["dt"] print("np.cos(t) =>", np.cos(t)) J["J_x"][:,:,:]=np.cos(t)*np.expand_dims(gaussian,2) print("np.max(J['J_x'][:,:,1]) =>", np.max(J['J_x'][:,:,1])) plt.figure(1) # image = j["j_x"][:,:,1] image = H["H_z"][:,:,50] if(not a): im = plt.imshow(image) colorbar=plt.colorbar() a=10 else: im.set_clim(vmin=np.min(image), vmax=np.max(image)) im.set_data(image) plt.pause(0.5) E_from_H(E,H,J,P) H_from_E(H,E,P)
def w_CB(change): from scipy.signal import convolve2d from cv2 import resize, INTER_CUBIC, cvtColor, COLOR_RGB2GRAY data = change['new'] if len(data[0]) > 2: # Get strokes information x = np.array(data[0]) y = np.array(data[1]) t = np.array(data[2]) # assuming there is at least 200ms between each stroke line_breaks = np.where(np.diff(t) > 200)[0] # adding end of array line_breaks = np.append(line_breaks, t.shape[0]) # Plot to canvas from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas fig = plt.figure() canvas = FigureCanvas(fig) ax = fig.gca() # plot all strokes plt.plot(x[:line_breaks[0]], y[:line_breaks[0]], color='black', linewidth=4) for i in range(1, len(line_breaks)): plt.plot(x[line_breaks[i - 1] + 1:line_breaks[i]], y[line_breaks[i - 1] + 1:line_breaks[i]], color='black', linewidth=4) plt.xlim(0, 460) plt.ylim(0, 250) plt.axis("off") canvas.draw() # draw the canvas, cache the renderer # convert to numpy array imageflat = np.frombuffer(canvas.tostring_rgb(), dtype='uint8') # not sure why this size... image = np.reshape(imageflat, (288, 432, 3)) # Cut the part containing the writting ind = np.where(image < 255) D0 = ind[0].max() - ind[0].min() D1 = ind[1].max() - ind[1].min() C0 = int(0.5 * (ind[0].max() + ind[0].min())) C1 = int(0.5 * (ind[1].max() + ind[1].min())) if D0 > D1: D = D0 else: D = D1 L = int(D / 2.0) + 20 image = image[C0 - L:C0 + L, C1 - L:C1 + L, :] # Convert to gray image = 255 - cvtColor(image, COLOR_RGB2GRAY) # Low pass filter and resize k = 12 I = convolve2d(image, np.ones((k, k)) / k**2.0, mode="same") # Resize with opencv I = resize(I, dsize=(28, 28), interpolation=INTER_CUBIC) # Clip in [0, 1] I = I / I.max() I = I * 3.0 I = I.clip(0, 1) # Get a feature vector if image_input: X = I[np.newaxis, :, :, np.newaxis] else: X = I.reshape((1, 28 * 28)).astype(np.float64) # Standardization if scaler is not None: X = scaler.transform(X) # Apply the classifier y_prediction = apply_classifier(X) #title = "Prediction: {} ({:.02f})".format(y_prediction, v) title = "Prediction: {}".format(y_prediction) # draw the converted image plt.clf() plt.imshow(I, aspect='equal', cmap=mpl.cm.binary, interpolation='none') plt.title(title) plt.axis("off") plt.show() # To erase after tracing #change['owner'].data = [[], [], []] # Schedule for clearing out.clear_output(wait=True) else: pass
mask = np.zeros_like(gray_image) cv2.drawContours(mask, candidates, 0, (255, 255, 255), -1) #cv2.imshow("mask", mask) out = np.zeros_like(gray_image) out[mask == 255] = gray_image[mask == 255] #cv2.imshow("woo", out) ret, filter_image = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) ret, filter_maskimage = cv2.threshold(out, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) dots = cv2.bilateralFilter(filter_maskimage, 11, 17, 17) _, cnts, _ = cv2.findContours(dots, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) print(len(cnts) - 1) # -1 because the die itself counts try: cv2.imshow("mask", mask) cv2.imshow("woo", out) cv2.imshow("Dice", image) cv2.imshow("Dice - gray", gray_image) cv2.imshow("Dice - filtered", filter_image) cv2.imshow("Dice - masked - filtered", filter_maskimage) cv2.waitKey(0) except: from matplotlib import pyplot as plt plt.imshow(filter_maskimage, cmap='gray') plt.show()
def test_consensus(consensus_caller, data_loader, n_batches=None): if n_batches is None: n_batches = len(data_loader) total_confusion = None total_realigned_confusion = None for b, batch in enumerate(data_loader): sys.stdout.write("\r %.2f%% COMPLETED " % (100 * b / n_batches)) paths, x, y = batch # (n,h,w) shape batch_size, height, width = x.shape for n in range(batch_size): x_n = x[n, :, :].data.numpy() y_n = y[n, :, :].data.numpy() x_n = trim_empty_rows(x_n, background_value=sequence_to_float["-"]) y_predict_n = consensus_caller.call_consensus_as_one_hot(x_n) consensus_sequence = consensus_caller.decode_one_hot_to_string( y_predict_n) reference_sequence = consensus_caller.decode_one_hot_to_string(y_n) # print(consensus_sequence) # print(reference_sequence) if consensus_sequence == '': pyplot.imshow(y_predict_n) pyplot.show() pyplot.close() pyplot.imshow(x_n) pyplot.show() pyplot.close() y_predict_n = torch.FloatTensor(y_predict_n) y_n = torch.FloatTensor(y_n) confusion = sequential_confusion(y_predict=y_predict_n, y=y_n) # realign strings to each other and convert to one hot y_pileup_predict_expanded, y_pileup_expanded = \ realign_consensus_to_reference(consensus_sequence=consensus_sequence, ref_sequence=reference_sequence, print_alignment=False) y_pileup_predict_expanded = torch.FloatTensor( y_pileup_predict_expanded) y_pileup_expanded = torch.FloatTensor(y_pileup_expanded) realigned_confusion = sequential_confusion( y_predict=y_pileup_predict_expanded, y=y_pileup_expanded) # normalized_frequencies = consensus_caller.call_consensus_as_normalized_frequencies(x_n) # plot_consensus_prediction(x=x_n,y=y_n,y_predict=normalized_frequencies) if total_confusion is None: total_confusion = confusion total_realigned_confusion = realigned_confusion else: total_confusion += confusion total_realigned_confusion += realigned_confusion if b == n_batches: break print() plot_confusion(total_confusion) plot_confusion(total_realigned_confusion) # total_confusion = normalize_confusion_matrix(total_confusion) # total_realigned_confusion = normalize_confusion_matrix(total_realigned_confusion) # # plot_confusion(total_confusion) # plot_confusion(total_realigned_confusion) accuracy = calculate_accuracy_from_confusion(total_realigned_confusion) print("Total accuracy", accuracy)
query_image = io.imread(image_path) query_parsed = parser.prepare_document(file_name, query_image) query_doc = {'doc_name': None, 'features': query_parsed[extractor]} results = index.query_index(query_doc, metric, extractor, show_score) results = [x for x in results if x[0] not in blacklist] results = results[:top_k] print 'Results for query image: {}'.format(file_name) if show_score: for result_name, score in results: print '{} -- {}'.format(result_name, score) else: for result_name in results: print '{}'.format(result_name) fig = plt.subplot(1, 3, 1) fig = plt.imshow(io.imread(os.path.join(root, file_name))) fig.axes.get_xaxis().set_visible(False) fig.axes.get_yaxis().set_visible(False) for i in range(4): fig = plt.subplot(2, 6, 3 + i) fig.set_title("result " + str(2*i + 1)) fig = plt.imshow(io.imread(os.path.join('./data/flickr_25k', results[2*i][0]))) fig.axes.get_xaxis().set_visible(False) fig.axes.get_yaxis().set_visible(False) fig = plt.subplot(2, 6, 9 + i) fig.set_title("result " + str(2*i + 2)) fig = plt.imshow(io.imread(os.path.join('./data/flickr_25k', results[2*i+1][ 0]))) fig.axes.get_xaxis().set_visible(False)
from wordcloud import WordCloud, ImageColorGenerator import matplotlib.pyplot as plt import jieba import dy df = dy.con_sql('job') text = '' for line in df['job_welfare']: text += ' '.join(jieba.cut(line, cut_all=False)) backgroud_Image = plt.imread('1.PNG') wc = WordCloud( background_color='white', mask=backgroud_Image, font_path='/Users/goodbyekiller/Library/Fonts/SimHei.ttf', max_words=2000, max_font_size=150, random_state=30, ) wc.generate_from_text(text) img_colors = ImageColorGenerator(backgroud_Image) wc.recolor(color_func=img_colors) plt.imshow(wc) plt.axis('off') wc.to_file("福利.jpg") print('生成词云成功!')
return afin_result f1x, f1y = differential(img1_gray1, h1, w1) f2x, f2y = differential(img2_gray1, h2, w2) s1 = f1x * f1x s2 = f1x * f1y s3 = f1y * f1y s4 = f2x * f2x s5 = f2x * f2y s6 = f2y * f2y s_img1 = Harris(s1, s2, s3, kernel) s_img2 = Harris(s4, s5, s6, kernel) plt.imshow(s_img1) plt.savefig('s_1-1.jpg') plt.imshow(s_img2) plt.savefig('s_1-2.jpg') cv2.imwrite("s_image1-1.jpg", s_img1) cv2.imwrite("s_image1-2.jpg", s_img2) """ ここまでがHarrisの実装 """ """ dx, dy, da, best_error = point_dx_dy(s_img1, s_img2) h, w = img1.shape[:2] img = np.zeros((h+80, w+250, 3), dtype=np.uint8)
import nipy.labs.utils.simul_multisubject_fmri_dataset as simul import nipy.algorithms.statistics.empirical_pvalue as en ############################################################################### # simulate the data shape = (60, 60) pos = 2 * np.array([[6, 7], [10, 10], [15, 10]]) ampli = np.array([3, 4, 4]) dataset = simul.surrogate_2d_dataset(n_subj=1, shape=shape, pos=pos, ampli=ampli, width=10.0).squeeze() fig = plt.figure(figsize=(12, 10)) plt.subplot(3, 3, 1) plt.imshow(dataset, cmap=plt.cm.hot) plt.colorbar() plt.title('Raw data') Beta = dataset.ravel().squeeze() ############################################################################### # fit Beta's histogram with a Gamma-Gaussian mixture gam_gaus_pp = en.gamma_gaussian_fit(Beta, Beta) gam_gaus_pp = np.reshape(gam_gaus_pp, (shape[0], shape[1], 3)) plt.figure(fig.number) plt.subplot(3, 3, 4) plt.imshow(gam_gaus_pp[..., 0], cmap=plt.cm.hot) plt.title('Gamma-Gaussian mixture,\n first component posterior proba.') plt.colorbar()
def main(args): pi = 3.1416 print("Please enter path to folder where you want calibration file saved:") path = raw_input() rospy.init_node('gummiCalibrate', anonymous=True) r = rospy.Rate(60) gummi = Gummi() joint = gummi.elbow numSteps = 9 minAngle = joint.angle.getMin() maxAngle = joint.angle.getMax() print("Using min angle: " + str(minAngle) + ", max angle: " + str(maxAngle) + ", and steps: " + str(numSteps)) anglesToTry = np.linspace(minAngle, maxAngle, numSteps) cocontractionsToTry = np.linspace(1.0, 0.0, numSteps) gummi.setCocontraction(0.6, 0.6, 0.6, 0.6, 0.6) print('WARNING: Moving joints sequentially to equilibrium positions.') gummi.doGradualStartup() print('WARNING: Moving to resting pose, hold arm!') rospy.sleep(1) gummi.goRestingPose(True) for i in range(0,400): gummi.goRestingPose(False) r.sleep() for i in range(0,100): gummi.forearmRoll.servoTo(-pi/2) r.sleep() print("GummiArm is live!") for i in range (0,300): joint.servoTo(0, 0.5) r.sleep() thetas = list() ccs = list() equilibriums = list() for cocont in cocontractionsToTry: for i in range (0,300): joint.servoTo(0, cocont) r.sleep() for angle in anglesToTry: print("Moving joint to angle: " + str(angle) + " and cocontraction: " + str(cocont) + ".") for i in range (0,300): joint.servoTo(angle, cocont) r.sleep() thetas.append(round(joint.angle.getEncoder(), 3)) ccs.append(round(cocont, 3)) equilibriums.append(round(joint.eqModel.dEquilibrium, 3)) for i in range (0,300): joint.servoTo(0, cocont) r.sleep() data = {'thetas': thetas, 'ccs': ccs, 'equilibriums': equilibriums} text = yaml.dump(data, default_flow_style = False, explicit_start = True) print text fileName = path + "/calibration_" + joint.getName() + ".yaml" with open(fileName, 'w') as outfile: outfile.write(text) print("Calibration data written to: " + fileName) jm = InverseModel("test") jm.setCalibration(thetas, ccs, equilibriums) gridX, gridY = np.mgrid[minAngle:maxAngle:0.01, 0:1:0.01] map = jm.getMap(gridX, gridY) plt.imshow(map.T, extent=(minAngle, maxAngle, 0, 1)) plt.show()
import cv2 import matplotlib.pyplot as plt im = cv2.imread('../images/tomare_target.jpg') surf = cv2.xfeatures2d.SURF_create(50) kp, des = surf.detectAndCompute(im, None) print len(kp) # im2 = cv2.drawKeypoints(im, kp, None, (255,0,0), 4) im2 = cv2.drawKeypoints(im, kp, None, color=(255, 0, 0)) plt.imshow(im2), plt.show()
# Sentiment Analysis using Vader FinalResults_Vader = pd.DataFrame() # Creating engine analyzer = SentimentIntensityAnalyzer() # Run Engine for i in range(0, twt.shape[0]): snt = analyzer.polarity_scores(twt.iloc[i,5]) temp = pd.DataFrame({'Tweets': twt.iloc[i,5], 'Polarity': list(snt.items())[3][1]}, index = [0]) FinalResults_Vader = FinalResults_Vader.append(temp) FinalResults_Vader['Sentiment'] = FinalResults_Vader['Polarity'].apply(lambda x: 'Positive' if x>0 else 'Negative' if x<0 else 'Neutral') FinalResults_Vader['Sentiment'].describe() #Results: Most of the tweets are Negative wordcloud = WordCloud(width = 1000, height = 500, stopwords = STOPWORDS, background_color = 'white').generate( ''.join(twt[:6])) plt.figure(figsize = (15,8)) plt.imshow(wordcloud) plt.axis('off') plt.show() wordcloud.to_file("tweets.png")
# cv2.rectangle(img,(i,j),(i+100,j+200),(0,0,255),3) # j += 200 # i += 100 # j = 0 # cv2.imshow("image",img) # # cv2.waitKey(0) # cv2.destroyAllWindows() # # plt.imshow(img) # plt.show() from itertools import islice def window(seq, n=2): "Returns a sliding window (of width n) over data from the iterable" " s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... " it = iter(seq) result = tuple(islice(it, n)) if len(result) == n: yield result for elem in it: result = result[1:] + (elem, ) yield result for i in window(img, 400): plt.imshow(i) plt.show()
#idx = file_list[I-1][idx%len(file_list[I-1])] #print(idx) img = cv2.imread(origindataset.data[idx%20939].replace('simline', 'img').replace('.png', '.jpg')) img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) x1,y1,x2,y2 = rois_ns[order[k]] cropped=img[max(0,y1):min(1170,y2), max(0,x1):min(827,x2),:] results.append(img[y1:y2, x1:x2,:]) cv2.rectangle(img, (x1,y1), (x2,y2),(0,255,0), 3) if simline: cv2.imwrite('%s/Snormalize%d%s_%d.png'%(args.outf, j, tdataset.data[j].split('/')[-1],k), img) else: cv2.imwrite('%s/normalize%d%s_%d.png'%(args.outf, j, tdataset.data[j].split('/')[-1],k), img) fig = plt.figure(figsize=(13, 18)) columns = 5 rows = 8 # ax enables access to manipulate each of subplots ax = [] for i in range(len(results)): img = results[i] sim = nscores[i] # create subplot and append to ax t = fig.add_subplot(rows, columns, i+1) t.title.set_text('%.06f'%sim) t.set_axis_off() ax.append(t) plt.imshow(img) if simline: plt.savefig('%s/ConvSNM%d%s.png'%(args.outf, j, tdataset.data[j].split('/')[-1]), bbox_inches='tight', pad_inches=0.02) else: plt.savefig('%s/Conv%d%s.png'%(args.outf, j, tdataset.data[j].split('/')[-1]), bbox_inches='tight', pad_inches=0.02)
# Anova univariate feature selection followed by BayesianRidge f_regression = mem.cache(feature_selection.f_regression) # caching function anova = feature_selection.SelectPercentile(f_regression) clf = Pipeline([('anova', anova), ('ridge', ridge)]) # Select the optimal percentage of features with grid search clf = GridSearchCV(clf, {'anova__percentile': [5, 10, 20]}, cv=cv) clf.fit(X, y) # set the best parameters coef_ = clf.best_estimator_.steps[-1][1].coef_ coef_ = clf.best_estimator_.steps[0][1].inverse_transform(coef_.reshape(1, -1)) coef_selection_ = coef_.reshape(size, size) # ############################################################################# # Inverse the transformation to plot the results on an image plt.close('all') plt.figure(figsize=(7.3, 2.7)) plt.subplot(1, 3, 1) plt.imshow(coef, interpolation="nearest", cmap=plt.cm.RdBu_r) plt.title("True weights") plt.subplot(1, 3, 2) plt.imshow(coef_selection_, interpolation="nearest", cmap=plt.cm.RdBu_r) plt.title("Feature Selection") plt.subplot(1, 3, 3) plt.imshow(coef_agglomeration_, interpolation="nearest", cmap=plt.cm.RdBu_r) plt.title("Feature Agglomeration") plt.subplots_adjust(0.04, 0.0, 0.98, 0.94, 0.16, 0.26) plt.show() # Attempt to remove the temporary cachedir, but don't worry if it fails shutil.rmtree(cachedir, ignore_errors=True)
pascal_voc_val = pascal.VOCSegmentation(args, split='val') sbd = sbd.SBDSegmentation(args, split=['train', 'val']) pascal_voc_train = pascal.VOCSegmentation(args, split='train') dataset = CombineDBs([pascal_voc_train, sbd], excluded=[pascal_voc_val]) dataloader = torch.utils.data.DataLoader(dataset, batch_size=2, shuffle=True, num_workers=0) for ii, sample in enumerate(dataloader): for jj in range(sample["image"].size()[0]): img = sample['image'].numpy() gt = sample['label'].numpy() tmp = np.array(gt[jj]).astype(np.uint8) segmap = decode_segmap(tmp, dataset='pascal') img_tmp = np.transpose(img[jj], axes=[1, 2, 0]) img_tmp *= (0.229, 0.224, 0.225) img_tmp += (0.485, 0.456, 0.406) img_tmp *= 255.0 img_tmp = img_tmp.astype(np.uint8) plt.figure() plt.title('display') plt.subplot(211) plt.imshow(img_tmp) plt.subplot(212) plt.imshow(segmap) if ii == 1: break plt.show(block=True)
def show_image(img, name): plt.figure(name) plt.imshow(img, cmap='gray', vmin=0, vmax=255)
print(y_train.shape) plt.figure(figsize = ( 16, 16)) i = 1 num = 1 for e in eti : image = x_train[y_train.tolist().index(e)] plt.subplot(10, 10, i) plt.axis('off') #plt.title() i +=1 _=plt.imshow(image, cmap = 'gray') print (num , x_train[y_train.tolist().index(e)]) num +=1 plt.show() i = 1 image = x_train[y_train.tolist().index(3)] plt.subplot(10, 10, i) plt.axis('off') #plt.title() i +=1 _=plt.imshow(image, cmap = 'inferno') plt.show()
def show_images(self, colormap, heightmap): import matplotlib.pyplot as plt # pylint: disable=g-import-not-at-top plt.imshow(colormap) plt.show() plt.imshow(heightmap) plt.show()
def visualize_leaves(images, y, classes, show1, show2): ''' Create a grid of random leaves and/or display all the images of a specified leaf species Displaying the specifed species works for displaying guesses of the test set too Parameters: images - list of images of the leaves y - list of numbers ranging from 0-98 where each number represents a species of leaf classes - list of the names of the species show1 - shows the grid of leaves if true show2 - shows the images of a specified species of leaves if true ''' # For displaying a grid of random leaves if show1: # Setting up our grid num_row = 15 num_col = 15 # Setting up each row for i in range(num_row): # Setting up each image in said row for j in range(num_col): if (j == 0): images1 = images[j + i * num_col,:,:,0] else: temp = images[j + i * num_col,:,:,0] images1 = np.concatenate((images1, temp), axis = 1) # Attach new row below previous one if (i == 0): images2 = images1 else: images2 = np.concatenate((images2, images1), axis = 0) # Displaying the grid a = plt.figure(1) plt.imshow(images2, cmap = 'binary', interpolation = 'none') a.show() # For displaying images of a specified leaf species (Works for guessed leaves in test set) if show2: # Give a list of the id associated with each species for i in range(len(classes)): print i, classes[i] # Ask for what species to display species = input('What leaf species would you like to look at? ') print 'You chose', classes[species] # Find how many images of that species there is hold = [] num_img = 0 # Create a list holding each index that image is in for i in range(len(y)): if y[i] == species: num_img += 1 hold.append(i) ''' Check if we have an odd amount of images (relevant for displaying the test leaves after predictions) ''' if num_img % 2 != 0: print 'Number of images is not even!' # Print the odd image separately c = plt.figure(1) plt.imshow(images[hold[num_img - 1],:,:,0], cmap = 'binary', interpolation = 'none') ax = plt.gca() ax.axes.get_xaxis().set_visible(False) ax.axes.get_yaxis().set_visible(False) c.show() num_img -= 1 # Display the images in 2 rows count = 0 for j in range(len(y)): if y[j] == species: count += 1 # First row if count < (num_img / 2) + 1: if (count == 1): pics = images[j,:,:,0] else: temp = images[j,:,:,0] pics = np.concatenate((pics, temp), axis = 1) # Second row elif count > (num_img / 2) and count < num_img + 1: if (count == (num_img / 2) + 1): pics2 = images[j,:,:,0] else: temp = images[j,:,:,0] pics2 = np.concatenate((pics2, temp), axis = 1) # Display the images al = np.concatenate((pics, pics2), axis = 0) b = plt.figure(num_img) plt.imshow(al, cmap = 'binary', interpolation = 'none') ax = plt.gca() ax.axes.get_xaxis().set_visible(False) ax.axes.get_yaxis().set_visible(False) b.show() # Show multiple images at once if need be if show1 or show2: raw_input() return
plt.legend(windows) plt.title("The smoothing windows") plt.subplot(212) plt.plot(x) plt.plot(xn) for w in windows: plt.plot(smooth(xn,10,w)) l = ['original signal', 'signal with noise'] l.extend(windows) plt.legend(l) plt.title("Smoothing a noisy signal") #plt.show() if __name__=='__main__': # part 1: 1d smooth_demo() # part 2: 2d X, Y = np.mgrid[-70:70, -70:70] Z = np.cos((X**2+Y**2)/200.)+ np.random.normal(size=X.shape) Z2 = blur_image(Z, 3) plt.figure() plt.imshow(Z) plt.figure() plt.imshow(Z2) plt.show()
import numpy as np import matplotlib.pyplot as plt def load_image(fname): return np.array(ndimage.imread(fname, flatten=False)) def classify(image): num_px = 64 my_image = scipy.misc.imresize(image, size=(num_px, num_px)) my_image = my_image.reshape((1, num_px * num_px * 3)).T prediction = predict(data["w"], data["b"], my_image) return np.squeeze(prediction) == 1 if __name__ == "__main__": image = load_image(sys.argv[1]) prediction = classify(image) if prediction: plt.title("cat detected 😸") else: plt.title("no cat detected 🙀") plt.imshow(image) plt.show()
def confusion(y_pred, y, classes, test_ids, num_classes, train_img, thresh): ''' Checks what leaves had a low probability of being chosen and displays them, their guessed species, and the top three probabilities Parameters: y_pred - predictions for each leaf y - 1D array that holds a label for each species ranging from 0 to n - 1 classes - 1D array of the name of each leaf species test_ids - 1D array of the testing set leaves ids num_classes - total number of classes train_img - list of the resized training images thresh - minimum probability needed to not be considered a confused leaf ''' # Find the leaves that have lower probabilities x = [] x_val = [] y_val = [] total = 0 for i in range(len(y_pred)): if (np.amax(y_pred[i]) < thresh): # Index where the highest probability is x = np.append(x, np.argmax(y_pred[i])) # ID of the leaf we are looking at x_val = np.append(x_val, test_ids[i]) # Where the leaf is in the matrix (what row) y_val = np.append(y_val, i) # Tally up the total number of leaves total += 1 print 'Total number of <', thresh, ' predictions: %g' %(total) # Set up an array that ranges from 0 - 98 to set up the class numbers x2 = np.ones(num_classes) for i in range(len(x2)): x2[i] = i most_conf = np.zeros(len(x2)) # Cycle through each leaf that had a low probability for i in range(len(x)): total = 0 # Cycle through each class number for j in range(len(x2)): # Display the leaves with the lower probabilities if (x2[j] == x[i]): # Find what the guess was print 'Species guessed:', classes[j] # Find the id of the corresponding image print 'ID associated with leaf:', int(x_val[i]) # Get top 3 predictions top3_ind = y_pred[int(y_val[i])].argsort()[-3:] top3_species = np.array(classes)[top3_ind] top3_preds = y_pred[int(y_val[i])][top3_ind] for m in range (len(top3_ind)): most_conf[top3_ind[m] - 1] += 1 # Display the top 3 predictions and the actual species print("Top 3 Predicitons:") for k in range(2, -1, -1): print "\t%s (%s): %s" % (top3_species[k], top3_ind[k], top3_preds[k]) # Display the image of the leaf string1 = 'data_provided/unzip_images/images/' + str(int(x_val[i])) + '.jpg' string2 = 'Species guessed: ' + classes[j] + ', ID: ' + str(int(x_val[i])) total += 1 a = plt.figure(total) img = mpimg.imread(string1) plt.title(string2) ax = plt.gca() ax.axes.get_xaxis().set_visible(False) ax.axes.get_yaxis().set_visible(False) plt.imshow(img, cmap = 'binary') a.show() # Display the probabilities for that leaf total += 1 b = plt.figure(total) plt.bar(x2, y_pred[int(y_val[i])]) plt.title('Probability of Each Class for ID: ' + str(int(x_val[i]))) plt.xlabel('Class Number') plt.ylabel('Probability') b.show() # Display the top three guesses images = np.zeros((3, 100, 250)) for z in range(len(top3_ind)): images[z] = display_conf(y, top3_ind[z], train_img, total) all_img = np.concatenate((images[2], images[1], images[0]), axis = 0) total += 1 c = plt.figure(total) plt.imshow(all_img, cmap = 'binary') plt.title('Top 3 Guesses') ax = plt.gca() ax.axes.get_xaxis().set_visible(False) ax.axes.get_yaxis().set_visible(False) c.show() raw_input() plt.close('all') print '\n' print 'Classes confused' total = 0 for i in range(len(x2)): if most_conf[i] > 0: print classes[i] + '(' + str(i + 1) + ')' + ':' + str(most_conf[i]) return
act_matrix = build_matrix(act_x, act_y) list = [] for instant in range(int(tempo_total/delta_t)): act_matrix = solve_matrix(act_matrix, act_x, act_y,instant) list.append(act_matrix) def moments(i): return list[i] img = plt.figure() i = 0 iasd = [range(len(list))] im = plt.imshow(moments(i),animated=True) def updatefig(*args): global i i+=1 im.set_array(moments(i)) return im, ani = animation.FuncAnimation(img,updatefig,interval=5,blit=True) cbar = img.colorbar(im, ticks=[0, 0.5, 1]) cbar.ax.set_yticklabels(['0', '0.5', '> 1']) plt.axis([0, 30, 0, 30]) plt.clim(0,1)
print(predictions) predict_df = pd.DataFrame({'sentiment':predictions,'text':tweetset},columns=['sentiment','text']) print(predict_df) #Create countplot of tweets vs sentiments count_plot = sns.countplot(x='sentiment',data=predict_df) fig = count_plot.get_figure() fig.savefig(url+"_countplot.png") sentiment_dict = {4:'',0:''} for sentiment in sentiment_dict: sentiment_set = predict_df[predict_df['sentiment'] == sentiment] sentiment_dict[sentiment] =' '.join(sentiment_set['text']) wordcloud = WordCloud(max_font_size=50, max_words=100, background_color="white").generate(sentiment_dict[4]) plt.imshow(wordcloud, interpolation='bilinear') plt.axis("off") plt.savefig(url+"_positive.png") try: wordcloud = WordCloud(max_font_size=50, max_words=100, background_color="white").generate(sentiment_dict[0]) plt.imshow(wordcloud, interpolation='bilinear') plt.axis("off") plt.savefig(url+"_negative.png") except ValueError: print("No negative sentiments") ''' #Stream Listener class MyListener(StreamListener):
# In[4]: # Loading the data (cat/non-cat) train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset( ) # We added "_orig" at the end of image datasets (train and test) because we are going to preprocess them. After preprocessing, we will end up with train_set_x and test_set_x (the labels train_set_y and test_set_y don't need any preprocessing). # # Each line of your train_set_x_orig and test_set_x_orig is an array representing an image. You can visualize an example by running the following code. Feel free also to change the `index` value and re-run to see other images. # In[5]: # Example of a picture index = 25 plt.imshow(train_set_x_orig[index]) print("y = " + str(train_set_y[:, index]) + ", it's a '" + classes[np.squeeze(train_set_y[:, index])].decode("utf-8") + "' picture.") # Many software bugs in deep learning come from having matrix/vector dimensions that don't fit. If you can keep your matrix/vector dimensions straight you will go a long way toward eliminating many bugs. # # **Exercise:** Find the values for: # - m_train (number of training examples) # - m_test (number of test examples) # - num_px (= height = width of a training image) # Remember that `train_set_x_orig` is a numpy-array of shape (m_train, num_px, num_px, 3). For instance, you can access `m_train` by writing `train_set_x_orig.shape[0]`. # In[6]: ### START CODE HERE ### (≈ 3 lines of code)
plt.legend() # Loss plotting the loss and the validation loss plt.plot(hist.history['loss'],label = 'train_loss') plt.plot(hist.history['val_loss'],label = 'val_loss') plt.legend() model.save('faces_model_1') dir = '/content/drive/My Drive/tom-hardy-actor-attends-mad-max-fury-road-photocall-th-annual-cannes-film-festival-may-cannes-france-58038178.jpg' from keras_preprocessing import image img = image.load_img(dir, target_size=(224,224)) img = np.asarray(img) plt.imshow(img) training_set.class_indices img = np.expand_dims(img,axis=0) from keras.models import load_model saved_model = load_model('faces_model_1') output = saved_model.predict(img) print(output) print(np.argmax(output)) import cv2 # loading cascades face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')