Example #1
0
def fond(premiere, derniere, pas=1):
    image = misc.imread(fichiers[premiere])
    fond = numpy.zeros(image.shape)
    for n in range(premiere,derniere,pas):
        image = misc.imread(fichiers[n])
        fond = (image < fond)*fond + (image >= fond)*image 
    return Image.fromarray(fond).convert('L')
def build(path,pathdir,files,labels,all_count,ratio,size):

    if not label_fixed:
        train_labels=random.sample(labels,int(ratio*len(labels)))
    else:
        train_labels=labels[:int(ratio*len(labels))]
        print train_labels[:20]
    for i in train_labels:
        labels.remove(i)
    test_labels=labels
    assert len(train_labels)+len(test_labels)==all_count

    train_dates={label:[] for label in train_labels}
    test_dates={label:[] for label in test_labels}
    for file in files:
        label=file[-11:-7]
        if label in train_labels:
            train_dates[label].append(0.001*(255-np.float32(imresize(imread(file,1),size))))
        else:
            test_dates[label].append(0.001*(255-np.float32(imresize(imread(file,1),size))))

    train_rank_dates={}
    for i in range(len(train_dates)):
        train_rank_dates[i]=train_dates[train_dates.keys()[i]]
    if cnn_only:
        return train_rank_dates
    x_train,y_train=get_sequence_images(train_rank_dates,train_labels,path_length,total_labels_per_seq,size,total_roads)

    # x_train,y_train=get_sequence_images(train_dates,train_labels,path_length,total_labels_per_seq,size,total_roads)
    x_test,y_test=get_sequence_images_cover(train_dates,test_dates,train_labels,test_labels,path_length,total_labels_per_seq,size,total_roads)
    return x_train,y_train,x_test,y_test,train_labels,test_labels
Example #3
0
 def _load_datum(self, index, type):
     """Get inputs with global index (global means self.ids[index] works)"""
     max_size = 500 * 500
     roi = self.rois[index]
     img = imread(self.img_files[index], mode='RGB')
     if roi is not None:
         (y_min, x_min), (y_max, x_max) = roi
         img = img[y_min:y_max, x_min:x_max]
     img, _ = fcn.util.resize_img_with_max_size(img, max_size=max_size)
     label = np.zeros(img.shape[:2], dtype=np.int32)  # bg_label is 0
     height, width = img.shape[:2]
     translation = (int(0.1 * np.random.random() * height),
                    int(0.1 * np.random.random() * width))
     tform = skimage.transform.SimilarityTransform(translation=translation)
     img = skimage.transform.warp(
         img, tform, mode='constant', preserve_range=True).astype(np.uint8)
     for label_value, mask_file in enumerate(self.mask_files[index]):
         if mask_file is None:
             continue
         mask = imread(mask_file, mode='L')
         if roi is not None:
             (y_min, x_min), (y_max, x_max) = roi
             mask = mask[y_min:y_max, x_min:x_max]
         mask, _ = fcn.util.resize_img_with_max_size(mask, max_size)
         mask = skimage.transform.warp(
             mask, tform, mode='constant',
             preserve_range=True).astype(np.uint8)
         label[mask == 255] = label_value
     return img, label
Example #4
0
def batch_predict(filenames, net):
    """
    Get the features for all images from filenames using a network

    Inputs:
    filenames: a list of names of image files

    Returns:
    an array of feature vectors for the list of files given
    """

    N, C, H, W = net.blobs[net.inputs[0]].data.shape
    F = net.blobs[net.outputs[0]].data.shape[1]
    Nf = len(filenames)
    Hi, Wi, _ = imread(filenames[0]).shape
    allftrs = np.zeros((Nf, F))
    for i in range(0, Nf, N):
        in_data = np.zeros((N, C, H, W), dtype=np.float32)

        batch_range = range(i, min(i+N, Nf))
        batch_filenames = [filenames[j] for j in batch_range]
        Nb = len(batch_range)

        batch_images = np.zeros((Nb, 3, H, W))
        for j,fname in enumerate(batch_filenames):
            im = imread(fname)
            # change grayscale to RGB
            if len(im.shape) == 2:
                im = np.tile(im[:,:,np.newaxis], (1,1,3))

            # lots of steps here to convert a scipy imread image to
            # the dimensions and channel ordering that caffe expects
            # Briefly, scipy.io.imread returns a HxWxC array in RGB channel order, 
            # whereas we need a CxHxW array in BGR order and subtract the mean
            
            # RGB -> BGR
    #        im = im[:,:,(2,1,0)]
            # mean subtraction

            #im = im - np.array([103.939, 116.779, 123.68])
            # resize
            im = imresize(im, (H, W))
            MEAN_FILE = '/afs/cs.stanford.edu/u/anenberg/scr/CS231N/examples/UCFff/mean.npy'
		# get channel in correct dimension
            im = np.transpose(im, (2, 0, 1))
            im = im - np.load(MEAN_FILE)
            batch_images[j,:,:,:] = im

        # insert into correct place
        in_data[0:len(batch_range), :, :, :] = batch_images

        # predict features
        ftrs = predict(in_data, net)

        for j in range(len(batch_range)):
            allftrs[i+j,:] = ftrs[j,:]

        print 'Done %d/%d files' % (i+len(batch_range), len(filenames))

    return allftrs
Example #5
0
def computeDiff(file1, file2):
    # read images as 2D arrays (convert to grayscale for simplicity)
    img1 = to_grayscale(imread(file1).astype(float))
    img2 = to_grayscale(imread(file2).astype(float))
    # compare
    n_m, n_0 = compare_images(img1, img2)
    return n_m*1.0/img1.size
Example #6
0
def main():

    image_file_name_0 = '/local/data/2014_07/TXM_commissioning/test/rotation_axis_twicking/Pin_0deg.tif'
    image_file_name_180 = '/local/data/2014_07/TXM_commissioning/test/rotation_axis_twicking/Pin_180deg.tif'

#    image_0 = read_tiff(image_file_name_0)
#    image_180 = read_tiff(image_file_name_180)
    image_0 = misc.imread(image_file_name_0)
    image_180 = misc.imread(image_file_name_180)
    image_180 = np.fliplr(image_180)
    
    image_0 = image_0[400:700, 300:1000]
    image_180 = image_180[400:700, 300:1000]

    print image_180.shape

    im2, scale, angle, t = similarity(image_0, image_180)
    print "Scale: ", scale, "Angle: ", angle, "Transformation Matrix: ", t

    rot_axis_shift_x = -t[0]/2.0
    rot_axis_tilt = -t[1]/1.0
    
    print "Rotation Axis Shift (x, y):", "(", rot_axis_shift_x, ",", rot_axis_tilt,")"

    plt.subplot(2,2,1)
    plt.imshow(image_0, cmap=plt.cm.hot)
    plt.title('0^o image'), plt.colorbar()
    plt.subplot(2,2,2)
    plt.imshow(image_180, cmap=plt.cm.hot)
    plt.title('180^o image flipped left - right'), plt.colorbar()
    plt.subplot(2,2,3)
    plt.imshow(im2, cmap=plt.cm.hot)
    plt.title('Im2 shifted'), plt.colorbar()
    plt.show()
Example #7
0
File: hw8.py Project: yigong/AY250
def searchPic():
    searchTerm = kwords.get()
    searchTerm = searchTerm.replace(' ','%20')
    url = ('https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=' +
           searchTerm+ '&userip=INSERT-USER-IP')
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)    
    # Process the JSON string.
    results = simplejson.load(response)
    imgURL = results['responseData']['results'][0]['unescapedUrl']
    imgName = imgURL.split('/')[-1]
    img_temp = urllib.URLopener().retrieve(imgURL, imgName)
    img_array = imread(img_temp[0])
    url_label.config(text=imgURL)
    
    imgLF.image = imread(imgName, flatten=True)
    imgLF.image_clr = imread(imgName)
    ax1.imshow(imgLF.image, cmap=plt.cm.gray)
    canvas.show()
    
    refresh.config(state=NORMAL, command=refresh_func)
    blur.config(state=NORMAL, command=blur_func)
    sharpen.config(state=NORMAL, command=sharpen_func)
    smooth.config(state=NORMAL, command=smooth_func)
    edge.config(state=NORMAL, command=edge_func)
    #contrast.config(state=NORMAL, command=contrast_func)
    color.config(state=NORMAL, command=color_func)
    decolor.config(state=NORMAL, command=decolor_func)
    brighten.config(state=NORMAL, command=brighten_func)
    darken.config(state=NORMAL, command=darken_func)
Example #8
0
def fond(premiere, derniere, pas=1):
    image = misc.imread(fichiers[premiere])
    fond = numpy.ones(image.shape)*255
    for n in range(premiere,derniere,pas):
        image = misc.imread(fichiers[n])
        fond = (image > fond)*fond + (image <= fond)*image
    return fond
Example #9
0
def copy_incorrect(in_folder, out_folder, incorrect_files="snapshotVGG1-5-test.txt"):
    from scipy.misc import imread, imsave, imrotate
    print(incorrect_files)
    if os.path.exists(incorrect_files):
        f = open(incorrect_files, "r")
        print("File found")
    else:
        f = open(os.path.join(in_folder, "stats", incorrect_files), "r")
    page = f.read()

    sources = page.split('\n')
    print(sources)
    print(len(sources))
    count = 0
    for source in sources:
        if source.find("jpg") >= 0:
            fileinfo = source
            if source.find(",") >= 0:
                fileinfo = source.split(", ")[0]
                rotation = source.split(", ")[1]
                image = imread(fileinfo)
                image = imrotate(image, int(rotation))
            else:
                image = imread(fileinfo)
            if count == 0:
                print(fileinfo)
            count += 1
            destination = os.path.split(fileinfo.replace(in_folder, out_folder))[0]
            if not os.path.exists(destination):
                os.makedirs(destination)
            filename = os.path.split(fileinfo)[1]
            # print(os.path.join(destination, filename))
            imsave(os.path.join(destination, filename), image)
    print("Moved " + str(count) + " files")
Example #10
0
def run_example( size = 64 ):
    """
    Run this file and result will be saved as 'rsult.jpg'
    Buttle neck: map 
    """
    modes = """
        normal
        add            substract      multiply       divide     
        dissolve       overlay        screen         pin_light
        linear_light   soft_light     vivid_light    hard_light    
        linear_dodge   color_dodge    linear_burn    color_burn
        light_only     dark_only      lighten        darken    
        lighter_color  darker_color   
        """
    top = misc.imresize( misc.imread('./imgs/top.png')[:,:,:-1], (size,size,3) )
    base = misc.imresize( misc.imread('./imgs/base.png')[:,:,:-1], (size,size,3) )
    modes = modes.split()
    num_of_mode = len( modes )
    result = np.zeros( [ size*2, size*(num_of_mode//2+2), 3 ])
    result[:size:,:size:,:] = top
    result[size:size*2:,:size:,:] = base
    for index in xrange( num_of_mode ):
        y = index // 2 + 1
        x = index % 2
        tmp= blends.blend( top, base, modes[index] )
        result[ x*size:(x+1)*size, y*size:(y+1)*size, : ] = tmp 
    # random blend
    result[-size::,-size::,:] = blends.random_blend( top, base )
    misc.imsave('result.jpg',result)
def getHaar (filepath, row, col, Npos, Nneg):
	Nimg = Npos + Nneg
	Nfeatures = 295936 #change this number if you need to use more/less features 
	features = np.zeros((Nfeatures, Nimg))
	
	files = glob.glob(filepath+ "faces/*.jpg")
	for i in xrange (Npos):
		print "\nComputing Haar Face ",i
		imgGray = misc.imread (files[i], flatten=1) #array of floats, gray scale image
		if (i < Npos):
			# convert to integral image
			intImg = np.zeros((row+1,col+1))
			intImg [1:row+1,1:col+1] = np.cumsum(cumsum(imgGray,axis=0),axis=1)	
			# compute features
			features [:,i] = computeFeature(intImg,row,col,Nfeatures) 

			
	files = glob.glob(filepath+ "background/*.jpg")
	for i in xrange (Nneg):
			print "\nComputing Haar Background ",i

			imgGray = misc.imread (files[i], flatten=1) #array of floats, gray scale image
			if (i < Nneg):
				# convert to integral image
				intImg = np.zeros((row+1,col+1))
				intImg [1:row+1,1:col+1] = np.cumsum(cumsum(imgGray,axis=0),axis=1)
				#	print intImg.shape 
				#	import pdb pdb.set_trace()
				# compute features
				features[:,i+Npos] = computeFeature(intImg,row,col,Nfeatures)
			
	# print "feat ", features[1000,:]
	return features
def resize_and_save(df, img_name, true_idx, size='80x50', fraction=0.125):
    '''
    INPUT:  (1) Pandas DF
            (2) string: image name
            (3) integer: the true index in the df of the image
            (4) string: to append to filename
            (5) float: fraction to scale images by
    OUTPUT: None

    Resize and save the images in a new directory.
    Try to read the image. If it fails, download it to the raw data directory.
    Finally, read in the full size image and resize it.
    '''
    try:
        img = imread(img_name)
    except:
        cardinal_dir = img_name[-5:-4]
        cardinal_translation = {'N': 0, 'E': 90, 'S': 180, 'W': 270}
        coord = (df.ix[true_idx]['lat'], df.ix[true_idx]['lng'])
        print 'Saving new image...'
        print coord, cardinal_dir, cardinal_translation[cardinal_dir]
        save_image(coord, cardinal_translation[cardinal_dir], loc='newdata')
    finally:
        img_name_to_write = ('newdata_' + size + '/' +
                             img_name[8:-4] + size + '.png')
        if os.path.isfile(img_name_to_write) == False:
            img = imread(img_name)
            resized = imresize(img, fraction)
            print 'Writing file...'
            imsave(img_name_to_write, resized)
Example #13
0
    def __init__(self, folder, im_type='tif'):
        """
        Load a previously acquired series of projections for analysis and
        reconstruction.

        # folder:    Path to folder where projections are stored. Reconstructed
                     slices will also be saved here.
        """
        self.folder = os.path.split(os.path.abspath(folder))[0]
        self.p0 = 0
        self.cor_offset = 0
        self.crop = None, None, None, None
        self.num_images = None
        self.angles = None

        files = [f for f in os.listdir(folder) if f[-4:] == '.%s' % im_type]
        im_shape = imread(os.path.join(folder, files[0])).shape
        self.im_stack = np.zeros(im_shape + (len(files), ))
        for idx, fname in enumerate(files):
                sys.stdout.write('\rProgress: [{0:20s}] {1:.0f}%'.format('#' *
                                 int(20*(idx + 1) / len(files)),
                                 100*((idx + 1)/len(files))))
                sys.stdout.flush()
                f = os.path.join(folder, fname)
                self.im_stack[:, :, idx] = imread(f)

        self.height = self.im_stack.shape[0]
        self.width = self.im_stack.shape[1]
Example #14
0
def example():
   
   
 
   # todo: read the example image from the papers source directory   
   image1=imread(wrapper.source_directory+'/examples/gobelet.png') 
   image2=imread(wrapper.source_directory+'/examples/gobelet2.png')
   
   # todo: call the wrapped function(s) with a valid set of arguments
   #keys1=wrapper.
   keys1=wrapper.extract_surf(image1)
   keys2=wrapper.extract_surf(image2)
   matches=wrapper.match_surf(keys1,keys2)

   # todo: display the results
   fig = plt.figure(figsize=(10,5))
   ax1 = fig.add_subplot(121)
   plt.imshow(image1) 
   ax2 = fig.add_subplot(122)
   plt.imshow(image2)     
  
 
   for x1,y1,x2,y2 in matches:
      con = ConnectionPatch(xyA=(x2, y2), xyB=(x1,y1),  coordsA="data", coordsB="data"  ,
                         axesA=ax2, axesB=ax1,
                         shrinkB=5,color='r')
      ax2.add_artist(con)
   plt.draw()
   plt.show()
 
   
   
   print 'done' 
def play_color_likeness(df):
    '''
    INPUT:  (1) Pandas DF
    OUTPUT: None

    Pull 20 images and show the most similar images
    as found by euclidean distance from their color histograms.
    '''
    some_random_files = [random_file_pull(df, yield_all_info=True).next()
                         for _ in range(20)]
    for img_info_tuple in some_random_files:
        source_idx = img_info_tuple[0]
        direction = img_info_tuple[1]
        filename = img_info_tuple[2]
        column_direction = 'nearest_10_neighbors_euclidean_' + direction
        nearest_image_idx = df[column_direction][source_idx][0]
        nearest_image = (df['base_filename'][nearest_image_idx] +
                         direction + '.png')
        print "original image location is {}, {}".format(
                df['lat'][source_idx], df['lng'][source_idx])
        print "new image location is {}, {}".format(
                df['lat'][nearest_image_idx], df['lng'][nearest_image_idx])
        print "the indices in the df are {} and {}".format(
                source_idx, nearest_image_idx)
        print "\n"
        fig = plt.figure(figsize=(16, 8))

        # Show search image and most similar image in database #
        ax = fig.add_subplot(2, 2, 1)
        ax.imshow(imread(filename))
        ax2 = fig.add_subplot(2, 2, 2)
        ax2.imshow(imread(nearest_image))
        ax.set_xticks([])
        ax.set_yticks([])
        ax2.set_xticks([])
        ax2.set_yticks([])

        # Plot scatter of nearest 10 and a sampling of all datapoints #
        # See plot_nearest_10 for a better visualization #
        ax3 = fig.add_subplot(2, 2, 3)
        nearest_10 = find_locations_nearest_10(source_idx, direction)
        true_loc = df['lat'][source_idx], df['lng'][source_idx]
        ax3.scatter(nearest_10[2:, 1], nearest_10[2:, 0])
        ax3.scatter(true_loc[1], true_loc[0], color='#33FFFF',
                    label='true loc', s=30)
        ax3.scatter(nearest_10[0][1], nearest_10[0][0],
                    color='#00FF00', label='best guess', s=30)
        ax3.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=2,
                   mode="expand", borderaxespad=0.)
        ax4 = fig.add_subplot(2, 2, 4)
        ax4.scatter(df['lng'][::10], df['lat'][::10])
        ax3.set_xlabel("Longitude")
        ax4.set_xlabel("Longitude")
        ax4.set_ylabel("Latitude")
        ax3.set_ylabel("Latitude")
        ax3.set_xlim(-109.5, -102.5)
        ax4.set_xlim(-109.5, -102.5)
        ax4.set_ylim(37, 41)
        ax3.set_ylim(37, 41)
        plt.show()
Example #16
0
File: aam.py Project: dfdx/masque
def run_build_model():
    import glob
    from scipy import io, misc
    DATA_DIR = '../others/icaam/examples/cootes'
    training_files = sorted(glob.glob(DATA_DIR + '/*.bmp.mat'))
    fst_im = misc.imread(training_files[0][:-4])
    ni, nj, nc = fst_im.shape
    ns = len(training_files)
    np, nd = io.loadmat(training_files[0])['annotations'].shape
    appearances = zeros((ns, ni, nj, nc))
    shapes = zeros((ns, np, nd))
    for i in range(ns):
        name = training_files[i]
        app = misc.imread(name[:-4])
        appearances[i, :, :, :] = app
        d = io.loadmat(name)
        annotations = d['annotations']
        shapes[i, :, :] = xy2ij(annotations, ni)
    AAM = build_model(shapes, appearances)
    
    
    
        
        
        
    
Example #17
0
def match(pic1,pic2):
 
    file1 = Image.open(pic1)
    file2 = Image.open(pic2)
    
   
    #img1 = file1.crop((100, 0, 500, 550))
    img1 = file1.crop((327, 219, 615, 576))
    img1.save("img1.jpg")
   
   # w = file2.size
    #h = file2.size
    #img2 = file2.crop((100, 0, 500, 550))
    img2 = file2.crop((327, 219, 615, 576))
    img2.save("img2.jpg")
   
    # read images as 2D arrays (convert to grayscale for simplicity)
    img1 = to_grayscale(imread("img1.jpg").astype(float))
    img2 = to_grayscale(imread("img2.jpg").astype(float))
    #img1 = to_grayscale(img1.astype(float))
    #img2 = to_grayscale(img2.astype(float))
    
   
    # compare
    n_m, n_0 = compare_images(img1, img2)
    if n_m/img1.size <= 45 and n_0*1.0/img1.size <=45:
        return True
    else:
        return False
Example #18
0
def main():
    k = 0.5
    pan = scm.imread('assets/scale/taipei_pan.jpg')
    mul =  scm.imread('assets/scale/taipei_mul.jpg')

    
    r = mul[:, :, 0]
    g = mul[:, :, 1]
    b = mul[:, :, 2]


    time_start = time.time()

    i = (r*0.171 +g*0.2+b*0.171)/0.632
    kx__pan_minus_iii = k*(pan-i)
    coe = pan/(i+kx__pan_minus_iii)
    nr        = coe * (r+kx__pan_minus_iii)
    ng     = coe * (g+kx__pan_minus_iii)
    nb       = coe * (b+kx__pan_minus_iii)

    finish_time = time.time() - time_start
    print("finish time:", finish_time, " s")

    
    output_img = np.empty_like(mul)
    output_img[:, :, 0] = nr
    output_img[:, :, 1] = ng
    output_img[:, :, 2] = nb

    scm.imsave("output.jpg", output_img)
Example #19
0
def file_reader(filename, **kwds):
    '''Read data from any format supported by PIL.
    
    Parameters
    ----------
    filename: str
        By using '*' it is possible to load a collection of images of the same 
        size into a three dimensional dataset.
    '''
    if '*' in filename:
        from glob import glob
        flist=glob(filename)
        flist.sort()
        imsample = imread(flist[0])
        w=imsample.shape[0]
        h=imsample.shape[1]
        d=len(flist)
        dc=np.zeros((d,w,h))
        for i in xrange(d):
            dc[i,:,:] = imread(flist[i])
    else:
        dc = imread(filename)
    dt = 'image'    
    return [{'data':dc, 
             'mapped_parameters': {
                'original_filename' : filename,
                'record_by': dt,
                'signal_type' : None,
                }
             }]
Example #20
0
def build(path,pathdir,files,files_eval,labels,labels_eval,all_count,size):
    train_labels=labels
    test_labels=labels_eval
    assert len(train_labels)+len(test_labels)==all_count

    train_dates={label:[] for label in train_labels}
    test_dates={label:[] for label in test_labels}
    for file in (files+files_eval):
        label=file[-11:-7]
        if label in train_labels:
            train_dates[label].append(0.001*(255-np.float32(imresize(imread(file,1),size))))
        else:
            test_dates[label].append(0.001*(255-np.float32(imresize(imread(file,1),size))))

    train_rank_dates={}
    for i in range(len(train_dates)):
        train_rank_dates[i]=train_dates[train_dates.keys()[i]]
    if cnn_only:
        return train_rank_dates
    print '\ntrain keys:',train_dates.keys()
    # print train_rank_dates.keys()
    print 'test keys:',test_dates.keys(),'\n'


    x_train,y_train=get_sequence_images(train_rank_dates,train_labels,path_length,total_labels_per_seq,size,total_roads)

    # x_train,y_train=get_sequence_images(train_dates,train_labels,path_length,total_labels_per_seq,size,total_roads)
    x_test,y_test=get_sequence_images(test_dates,test_labels,path_length,total_labels_per_seq,size,total_roads)
    return x_train,y_train,x_test,y_test
Example #21
0
def example():
   
   from matplotlib import pyplot as plt
   import numpy as np 

   # todo: read the example image from the papers source directory   
   image1=imread(os.path.join(wrapper.source_directory,'data','tsukuba0.png')) 
   image2=imread(os.path.join(wrapper.source_directory,'data','tsukuba1.png')) 
  

   output=wrapper.stereoGuidedFilter(image1,image2,dmin=-15,dmax=0)
   print 'found disparities :'
   v,c=np.unique(output['disparity'],return_counts=True)
   plt.plot(v,c)
   plt.title('nb pixel per disparity')
   # todo: display the results
   plt.figure()
   plt.subplot(2,2,1)   
   plt.imshow(output['disparity'].astype(np.float),cmap='Greys_r')
   plt.subplot(2,2,2)   
   plt.imshow(output['occlusion'].astype(np.float),cmap='Greys_r')
   plt.subplot(2,2,3)   
   plt.imshow(output['occlusion_filled'].astype(np.float),cmap='Greys_r')
   plt.subplot(2,2,4)   
   plt.imshow(output['occlusion_filled_smoothed'].astype(np.float),cmap='Greys_r')
   plt.show()
   print 'done' 
Example #22
0
def distTest(net1):

    myImage1 = '/Users/shkejriwal/Documents/personal/data/myPics/small_full_face.jpg'
    myImage2 = '/Users/shkejriwal/Documents/personal/data/myPics/small_full_face_no_glass.jpg'
    #myImage2 = myImage1

    img1 = scipyMisc.imread(myImage1)
    #X1 = prepare1DImage(img1)
    X1 = prepare2DImage(img1)

    y1 = net1.predict(X1)

    
    img2 = scipyMisc.imread(myImage2)
    #X2 = prepare1DImage(img2)
    X2 = prepare2DImage(img2)

    #X2 = load2d(test=True)[0][6:7]
    #X2 = load2d(test=True)[0][15]
    
    y2 = net1.predict(X2)

    #dist = HausdorffDist(y1[0],y2[0])
    dist = distanceBetweenCurves(y1[0],y2[0])
    print dist
	def initialize(self, filename):
		# read filename
		with open(filename) as f:
			for line in f:
				cline = re.split('\s',line)
				# tuple (rgb_filename, gt_filename)
				self.flist.append((cline[0], cline[1]))
		self.N = len(self.flist)

		# get sizes
		(rgbname, gtname) = self.flist[0]
		rgb = misc.imread(rgbname)

		if(gtname.endswith('.png')):
			gt = misc.imread(gtname)
		else:
			gt = np.loadtxt(gtname)
		(self.H, self.W, self.CH) = rgb.shape
		gtshape = gt.shape
		self.HL = gtshape[0]
		self.WL = gtshape[1]
		if(len(gtshape) == 2):
			self.CHL = 1
		else:
			self.CHL = gtshape[2]

		# just retrieve the new sizes
		if( (self.data_transformer is not None) and self.data_transformer.isResized() ):
			(self.H, self.W) = self.data_transformer.getNewDims()
			self.HL = self.H
			self.WL = self.W
Example #24
0
def image_compare(df, IMAGES_DIR='/home/ryan/asi_images/'):
    '''
    takes a list of n image ids and returns sum(n..n-1) n comparisons of r2 difference, r2(fft) difference, and average number of thresholded pixels
    '''
    img_buffer = {}
    return_list = []
    artdf = df[['_id', 'images']].copy()
    artdf.images = artdf.images.apply(getpath) 
    paths = artdf[['_id','images']].dropna()
    paths.index = paths._id
    paths = paths.images
    if paths.shape[0] < 2:
        return DataFrame([])
    for id_pair in combinations(paths.index, 2):
        if id_pair[0] in img_buffer:
            img1 = img_buffer[id_pair[0]]
        else:
            img_buffer[id_pair[0]] = img_as_float(rgb2gray(resize(imread(IMAGES_DIR + paths[id_pair[0]]), (300,300))))
            img1 = img_buffer[id_pair[0]]
        
        if id_pair[1] in img_buffer:
            img2 = img_buffer[id_pair[1]]
        else:
            img_buffer[id_pair[1]] = img_as_float(rgb2gray(resize(imread(IMAGES_DIR + paths[id_pair[1]]), (300,300))))
            img2 = img_buffer[id_pair[1]]
        return_list.append(
                [id_pair[0], id_pair[1], \
                    norm(img1 - img2), \
                    norm(fft2(img1) - fft2(img2)), \
                    #mean([sum(img1 > threshold_otsu(img1)), sum(img2 > threshold_otsu(img2))])]
                    #mean([sum(img1 > 0.9), sum(img2 > 0.9)])] 
                    std(img1)+std(img2)/2.]
       )
    return DataFrame(return_list, columns=['id1','id2','r2diff', 'fftdiff', 'stdavg'])
	def getBatch_(self, indices):
		# format NxCHxWxH
		batchRGB = np.zeros((len(indices), self.CH, self.W, self.H), dtype='float32')
		batchLabel = np.zeros((len(indices), self.W, self.H), dtype='int32')

		k = 0
		for i in indices:
			(rgbname, gtname) = self.flist[i]

			# format: HxWxCH
			rgb =  misc.imread(rgbname)

			if(gtname.endswith('.png')):
				gt = misc.imread(gtname)
			else:
				gt = np.loadtxt(gtname)
			gt = gt.astype('uint8')
		
			if(self.data_transformer is not None):
				rgb = self.data_transformer.transformData(rgb)
				gt = self.data_transformer.transformLabel(gt)
			#^ data_transformer outputs in format HxWxCH

			# convertion from HxWxCH to CHxWxH
			batchRGB[k,:,:,:] = rgb.astype(np.float32).transpose((2,1,0))
			batchLabel[k,:,:] = gt.astype(np.int32).transpose((1,0))

			k += 1

			#ipdb.set_trace()

		if(self.weights_classes_flag):
			return (batchRGB, batchLabel, self.weights_classes)
		else:
			return (batchRGB, batchLabel)
Example #26
0
    def test_imsave(self):
        picdir = os.path.join(datapath, "data")
        for png in glob.iglob(picdir + "/*.png"):
            with suppress_warnings() as sup:
                # PIL causes a Py3k ResourceWarning
                sup.filter(message="unclosed file")
                img = misc.imread(png)
            tmpdir = tempfile.mkdtemp()
            try:
                fn1 = os.path.join(tmpdir, 'test.png')
                fn2 = os.path.join(tmpdir, 'testimg')
                with suppress_warnings() as sup:
                    # PIL causes a Py3k ResourceWarning
                    sup.filter(message="unclosed file")
                    misc.imsave(fn1, img)
                    misc.imsave(fn2, img, 'PNG')

                with suppress_warnings() as sup:
                    # PIL causes a Py3k ResourceWarning
                    sup.filter(message="unclosed file")
                    data1 = misc.imread(fn1)
                    data2 = misc.imread(fn2)
                assert_allclose(data1, img)
                assert_allclose(data2, img)
                assert_equal(data1.shape, img.shape)
                assert_equal(data2.shape, img.shape)
            finally:
                shutil.rmtree(tmpdir)
Example #27
0
def repeated_sales(df, artistname, artname, r2thresh=7000, fftr2thresh=10000, IMAGES_DIR='/home/ryan/asi_images/'):
    """
        Takes a dataframe, artistname and artname and tries to decide, via image matching, if there is a repeat sale. Returns a dict of lot_ids, each entry a list of repeat sales
    """
    artdf = df[(df['artistID']==artistname) & (df['artTitle']==artname)]

    artdf.images = artdf.images.apply(getpath)
    paths = artdf[['_id','images']].dropna()
    id_dict = {}
    img_buffer = {}
    already_ordered = []
    for i, path_i in paths.values:
        id_dict[i] = []
        img_buffer[i] = img_as_float(rgb2gray(resize(imread(IMAGES_DIR + path_i), (300,300))))
        for j, path_j in paths[paths._id != i].values:
            if j > i and j not in already_ordered:
                if j not in img_buffer.keys():
                    img_buffer[j] = img_as_float(rgb2gray(resize(imread(IMAGES_DIR + path_j), (300,300))))
                if norm(img_buffer[i] - img_buffer[j]) < r2thresh and\
                        norm(fft2(img_buffer[i]) - fft2(img_buffer[j])) < fftr2thresh:
                    id_dict[i].append(j)
                    already_ordered.append(j)
    for key in id_dict.keys():
        if id_dict[key] == []:
            id_dict.pop(key)
    return id_dict
Example #28
0
def doMyTest(net1):

    #myImage = '/Users/shkejriwal/Documents/personal/data/myPics/small_no_glasses.jpg'
    #myImage = '/Users/shkejriwal/Documents/personal/data/myPics/small.jpg'
    #myImage = '/Users/shkejriwal/Documents/personal/data/myPics/small_sk_closeup.jpg'
    myImage1 = '/Users/shkejriwal/Documents/personal/data/myPics/small_full_face.jpg'
    myImage2 = '/Users/shkejriwal/Documents/personal/data/myPics/small_full_face_no_glass.jpg'

    img1 = scipyMisc.imread(myImage1)
    #X1 = prepare1DImage(img1)
    X1 = prepare2DImage(img1)

    sample1 = load(test=True)[0][6:7]

    img2 = scipyMisc.imread(myImage2)
    #X2 = prepare1DImage(img2)
    X2 = prepare2DImage(img2)
    
    y1 = net1.predict(X1)
    y2 = net1.predict(X2)

    fig = pyplot.figure(figsize=(6, 3))
    ax = fig.add_subplot(1, 2, 1, xticks=[], yticks=[])
    plot_sample(X1[0], y1[0],ax)
    ax = fig.add_subplot(1, 2, 2, xticks=[], yticks=[])
    plot_sample(X2[0], y2[0],ax)

    pyplot.show()
Example #29
0
def testsift(image1, image2, maxd=1000, distthresh=0.4):
    im1 = imread(image1)
    print im1[:, :]
    im2 = imread(image2)
    print im2.shape
    kdt = [[], [], []]
    d1 = [[], [], []]
    d2 = [[], [], []]
    C = 3
    for channel in range(C):
        if C == 1:
            _, d1[channel] = sift.descriptors(im1, maxd)
        else:
            _, d1[channel] = sift.descriptors(im1[:, :, channel], maxd)
        print channel, d1[channel].shape
        if C == 1:
            _, d2[channel] = sift.descriptors(im2, maxd)
        else:
            _, d2[channel] = sift.descriptors(im2[:, :, channel], maxd)
        print channel, d2[channel].shape
        kdt[channel] = KDTree(d1[channel])
        for desc in d2[channel]:
            dist, index = kdt[channel].query(desc)
            if distthresh >= dist:
                print dist, index
Example #30
0
def example():
   
   from matplotlib import pyplot as plt
   import numpy as np 

   # todo: read the example image from the papers source directory   
   image1=imread(wrapper.source_directory+'/adam1.png') 
   image2=imread(wrapper.source_directory+'/adam2.png')
   
   # todo: call the wrapped function(s) with a valid set of arguments
   matches,keys1,keys2=wrapper.asift(image1,image2)

   # todo: display the results
   fig = plt.figure(figsize=(10,5))
   ax1 = fig.add_subplot(121)
   plt.imshow(image1,cmap='Greys_r') 
   ax2 = fig.add_subplot(122)
   plt.imshow(image2,cmap='Greys_r')   


   for x1,y1,x2,y2 in matches[::1,:]:
      con = ConnectionPatch(xyA=(x2, y2), xyB=(x1,y1),  coordsA="data", coordsB="data"  ,
                            axesA=ax2, axesB=ax1,
                            shrinkB=5,color='r')
      ax2.add_artist(con)
   plt.draw()
   plt.show()

   print 'done' 
Example #31
0
def align_data(input_dir='./input_dir/', output_dir='./out_dir'):
    output_dir_path = output_dir
    output_dir = os.path.expanduser(output_dir_path)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    datadir = input_dir
    dataset = facenet.get_dataset(datadir)
    startTime = datetime.now()
    print('Creating networks and loading parameters')
    with tf.Graph().as_default():

        #        sess = tf.Session(config=tf.ConfigProto(log_device_placement=False))    #Removed gpu_options since commented out above
        #        with sess.as_default():
        pnet, rnet, onet = detect_face.create_mtcnn(
            None)  # From https://github.com/davidsandberg/facenet/issues/412

    minsize = 20  # minimum size of face
    threshold = [0.6, 0.7, 0.7]  # three steps's threshold
    factor = 0.709  # scale factor
    margin = 44
    image_size = 182

    # Add a random key to the filename to allow alignment using multiple processes
    random_key = np.random.randint(0, high=99999)
    bounding_boxes_filename = os.path.join(
        output_dir, 'bounding_boxes_%05d.txt' % random_key)
    print('Goodluck')

    with open(bounding_boxes_filename, "w") as text_file:
        nrof_images_total = 0
        nrof_successfully_aligned = 0
        for cls in dataset:
            output_class_dir = os.path.join(output_dir, cls.name)
            if not os.path.exists(output_class_dir):
                os.makedirs(output_class_dir)
            for image_path in cls.image_paths:
                nrof_images_total += 1
                filename = os.path.splitext(os.path.split(image_path)[1])[0]
                output_filename = os.path.join(output_class_dir,
                                               filename + '.png')
                print(image_path)
                if not os.path.exists(output_filename):
                    try:
                        img = misc.imread(image_path)
                        print('read data dimension: ', img.ndim)
                    except (IOError, ValueError, IndexError) as e:
                        errorMessage = '{}: {}'.format(image_path, e)
                        print(errorMessage)
                    else:
                        if img.ndim < 2:
                            print('Unable to align "%s"' % image_path)
                            # text_file.write('%s\n' % (output_filename))
                            continue
                        if img.ndim == 2:
                            img = facenet.to_rgb(img)
                            print('to_rgb data dimension: ', img.ndim)
                        img = img[:, :, 0:3]
                        print('after data dimension: ', img.ndim)

                        bounding_boxes, _ = detect_face.detect_face(
                            img, minsize, pnet, rnet, onet, threshold, factor)
                        nrof_faces = bounding_boxes.shape[0]
                        print('detected_face: %d' % nrof_faces)
                        if nrof_faces > 0:
                            det = bounding_boxes[:, 0:4]
                            img_size = np.asarray(img.shape)[0:2]
                            if nrof_faces > 1:
                                bounding_box_size = (det[:, 2] - det[:, 0]) * (
                                    det[:, 3] - det[:, 1])
                                img_center = img_size / 2
                                offsets = np.vstack([
                                    (det[:, 0] + det[:, 2]) / 2 -
                                    img_center[1],
                                    (det[:, 1] + det[:, 3]) / 2 - img_center[0]
                                ])
                                offset_dist_squared = np.sum(
                                    np.power(offsets, 2.0), 0)
                                index = np.argmax(
                                    bounding_box_size - offset_dist_squared *
                                    2.0)  # some extra weight on the centering
                                det = det[index, :]
                            det = np.squeeze(det)
                            bb_temp = np.zeros(4, dtype=np.int32)

                            bb_temp[0] = det[0]
                            bb_temp[1] = det[1]
                            bb_temp[2] = det[2]
                            bb_temp[3] = det[3]
                            try:
                                cropped_temp = img[bb_temp[1]:bb_temp[3],
                                                   bb_temp[0]:bb_temp[2], :]
                                scaled_temp = misc.imresize(
                                    cropped_temp, (image_size, image_size),
                                    interp='bilinear')
                                nrof_successfully_aligned += 1
                                misc.imsave(output_filename, scaled_temp)
                            # text_file.write('%s %d %d %d %d\n' % (output_filename, bb_temp[0], bb_temp[1], bb_temp[2], bb_temp[3]))
                            except Exception as e:
                                os.remove(image_path)

                        else:
                            print('Unable to align "%s"' % image_path)
                            os.remove(image_path)
                            print('removed file %s' % image_path)
                            # text_file.write('%s\n' % (output_filename))
    print('Total number of images: %d' % nrof_images_total)
    print('Number of successfully aligned images: %d' %
          nrof_successfully_aligned)
    os.remove(bounding_boxes_filename)
    print(datetime.now() - startTime)
Example #32
0
import matplotlib.pyplot as plt
import itertools

#################
#load validation images
#################
ValidationPath = 'E:/Eslam/Data Science/Nile University/CIT-690-A Selected Topics in Machine Learning/Assignment 1 - Perceptron/Validation'
os.chdir(ValidationPath)
Validation_Lables = np.loadtxt('Validation Labels.txt')
filesvalid = os.listdir(ValidationPath)
filesvalid.pop()
filesvalid = sorted(filesvalid, key=lambda x: int(os.path.splitext(x)[0]))

all_validation = []
for i in filesvalid:
    imgvalidation = misc.imread(i)
    type(imgvalidation)
    imgvalidation.shape
    #change dimention to 1 dimentional array instead of (28x28)
    imgvalidation = imgvalidation.reshape(784, )
    imgvalidation = np.append(imgvalidation, 1)
    all_validation.append(imgvalidation)

############################
#Load Wieght Vector per eta
############################

Weightpath = 'E:/Eslam/Data Science/Nile University/CIT-690-A Selected Topics in Machine Learning/Assignment 1 - Perceptron/Training Wieght Vectors'
os.chdir(Weightpath)
#Wx is wieght matrix (10X785) computed for different eta
W0 = np.loadtxt('w_eta_0.txt')
Example #33
0
import matplotlib.pyplot as plt  #importing matplotlib
from scipy import misc
from skimage import transform
imsize = 512
img = misc.imread("contents/content7.jpg", mode='RGB').astype(float)
img = transform.resize(img, (imsize, imsize))
print(img)
plt.figure()
plt.hist(img.ravel(), bins=256, range=(0, 256), fc='k', ec='k')
plt.savefig("content_hist.jpg")

img = misc.imread("chol.jpg", mode='RGB')
plt.figure()
plt.hist(img.ravel(), bins=256, range=(0, 256), fc='k', ec='k')
plt.savefig("chol_hist.jpg")

img = misc.imread("chol.jpg", mode='RGB')
plt.figure()
plt.hist(img.ravel(), bins=256, range=(0, 256), fc='k', ec='k')
plt.savefig("pca_hist.jpg")
from scipy.misc import imread
from caffe_classes import class_names
from alexnet import AlexNet

x = tf.placeholder(tf.float32, (None, 32, 32, 3))

resized = tf.image.resize_images(x, size=(227, 227))

probs = AlexNet(resized)

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

# Read Images
im1 = imread("construction.jpg").astype(np.float32)
im1 = im1 - np.mean(im1)

im2 = imread("stop.jpg").astype(np.float32)
im2 = im2 - np.mean(im2)

# Run Inference
t = time.time()
output = sess.run(probs, feed_dict={x: [im1, im2]})

# Print Output
for input_im_ind in range(output.shape[0]):
    inds = np.argsort(output)[input_im_ind, :]
    print("Image", input_im_ind)
    for i in range(5):
        print("%s: %.3f" %
Example #35
0
from scipy.misc import imread, imsave
import numpy as np


def ycbcr2rgb(im):
    xform = np.array([[1, 0, 1.402], [1, -0.34414, -.71414], [1, 1.772, 0]])
    rgb = im.astype(np.float)
    rgb[:, :, [1, 2]] -= 128
    return rgb.dot(xform.T)


gt = imread('butterfly_GT.bmp')
gt = ycbcr2rgb(gt)
gt = gt[:, :, 0]
gt = gt[6:231, 6:231, :]
imsave('ground_true_Y.png', gt)

test = imread('sr.bmp')
test = ycbcr2rgb(test)
test = test[:, :, 0]
#imsave('test.png', test)
Example #36
0
File: vgg16.py Project: svats2k/iml
                               name='weights')
            fc3b = tf.Variable(tf.constant(1.0, shape=[1000],
                                           dtype=tf.float32),
                               trainable=True,
                               name='biases')
            self.fc3l = tf.nn.bias_add(tf.matmul(self.fc2, fc3w), fc3b)
            self.parameters += [fc3w, fc3b]

    def load_weights(self, weight_file, sess):
        weights = np.load(weight_file)
        keys = sorted(weights.keys())
        self.layers = keys[:-6]
        self.layers = [x[:-2] for x in self.layers]
        for i, k in enumerate(keys):
            print i, k, np.shape(weights[k])
            sess.run(self.parameters[i].assign(weights[k]))


if __name__ == '__main__':
    sess = tf.Session()
    imgs = tf.placeholder(tf.float32, [None, 224, 224, 3])
    vgg = vgg16(imgs, 'vgg16_weights.npz', sess)

    img1 = imread('boston_terrier.jpg', mode='RGB')
    img1 = imresize(img1, (224, 224))

    prob = sess.run(vgg.probs, feed_dict={vgg.imgs: [img1]})[0]
    preds = (np.argsort(prob)[::-1])[0:5]
    for p in preds:
        print class_names[p], prob[p]
Example #37
0
def loadImg(arg):
    return misc.imread(arg)
Example #38
0
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))
Example #39
0
    return x


caffe.set_mode_gpu()
caffe.set_device(0)
print "Load net..."
net = caffe.Net('/your path/test.prototxt',
                '/your path/pdb-convlstm.caffemodel', caffe.TEST)

with open('/your path/davis_path.txt') as f:
    lines = f.readlines()

for idx in range(len(lines) / 5):
    print "Run net..."
    net.forward()
    for i in xrange(5):
        all = net.blobs['conv7_sm'].data
        out = all[i][0]
        line = lines[idx * 5 + i].replace(" 0\n", "")
        dir, file = os.path.split(line)
        file = file.replace(".jpg", '.png')
        video = dir.split("/")[-1]
        img = misc.imread(line)
        out = misc.imresize(out, img.shape)
        out = out.astype('float')
        out = MaxMinNormalization(out, out.max(), out.min())
        savePath = '/your save path/' + video + '/'
        if not os.path.exists(savePath):
            os.makedirs(savePath)
        misc.imsave(savePath + file, out)
        print 'Save the ' + str(idx * 5 + i) + ' Image: ' + savePath + file
Example #40
0
def create_data(contour, translation, img_path_0, img_path_1, osvos_model, k):
    """Returns a torch_geometric.data object.
    
    The data object consists of:
    * x: Node feature matrix of shape [num_nodes, num_node_features]. The feature 
         of each node are the concatenated OSVOS feature vectors of the current 
         and the next frame.
    * edge_index: Graph connectivity in COO format of shape (2, num_edges) and type torch.long
                  Each node should be connected to its K nearest neighbours
    * edge_attr: Edge feature matrix with shape [num_edges, num_edge_features]
                 The feature of each edge is the inverse distance between the two nodes it connects
    * y, optianl: The target of each node is the displacement of the node between the current 
                  and the next frame

    Parameters
    ----------
    contour : ndarray 
        Array of shape (num_contour_points, 2) containing contour points
    translation : ndarray of shape (num_contour_points, 2)
        Array of shape (num_contour_points, 2) containing translations from current to next contour point
    img_path_0 : str
        Path to current image
    img_path_0 : str
        Path to next image
    osvos_model : torch.nn.Sequential
        OSVOS model from which feature vectors can be extracted
    k : int
        Number of neighbours to compute KNN graph of contour points

    Returns
    -------
    data : torch_geometric.data
        Data object containing consisting of x, edge_index, edge_attr, and y
    """

    contour = torch.from_numpy(contour)

    img_0 = np.moveaxis(imread(img_path_0), 2, 0).astype(np.float64)
    img_0 = np.expand_dims(img_0, axis=0)
    img_0 = torch.from_numpy(img_0)

    img_1 = np.moveaxis(imread(img_path_1), 2, 0).astype(np.float64)
    img_1 = np.expand_dims(img_1, axis=0)
    img_1 = torch.from_numpy(img_1)

    # x: Node feature matrix
    x_1 = get_OSVOS_feature_vectors(contour, img_0, osvos_model)
    x_2 = get_OSVOS_feature_vectors(contour, img_1, osvos_model)
    x = torch.cat((x_1, x_2), 1)

    # edge_index: Graph connectivity in COO format
    edge_index = knn_graph(contour, k)
    edge_index = to_undirected(edge_index)

    # edge_attr: Edge feature matrix
    edge_attr = get_edge_attribute(contour, edge_index)

    # Create data object
    if translation is None:
        data = Data(x=x,
                    edge_index=edge_index,
                    edge_attr=edge_attr,
                    contour=contour)
    else:
        # The target of each node is the displacement of the node between the current and the next frame
        y = torch.from_numpy(translation.astype(np.float64))
        data = Data(x=x,
                    edge_index=edge_index,
                    edge_attr=edge_attr,
                    y=y,
                    contour=contour)

    return data
Example #41
0
def TPS_image(**kwargs):
    """
        Application of the 'g' function on an image. This function applies
        the reverting-methodology: an estimation of the reverse function
        (backward projection) is estimated using a grid, which is projected
        by the 'g' function. This projected grid is then used as source,
        and the coordinates on the original image as destination. The
        TPSCy.image can not be called with a forward 'g' TPS function!
        
        Required:
        
        :param infile: URI to the input file
        :type infile: python string
        
        :param inimg: numpy array with the input image
        :type inimg: numpy.array
        
        .. note::
            `infile` and `inimg` are mutially exclusive. `inimg` has prioritary
        
        :param gfile: URI to the TPS parameters file
        :type gfile: python string
        
        :param g: TPS parameters
        :type g: python dictionary
        
        .. note::
            `gile` and `g` are mutially exclusive. `gfile` has prioritary
        
        Optional:
        
        :param res: Resolution of the input and output image
        :type res: float
        
        :param outfile: URI to the output image file
        :type outfile: string
        
        :param reverseFullGrid: Use the full-grid to revert the 'g' function (SLOW !)
        :type reverseFullGrid: boolean
        
        :param useWeights: Use the weights to selects the optimal grid to revert the 'g' function
        :type useWeights: boolean
        
        :param gridSize: Size of the grid to caculate the revert function
        :type gridSize: float
        
        :param cx: x coordinate of the center
        :type cx: float
        
        :param cy: y coordinate of the center
        :type cy: float
        
        :param ncores: Number of cores used to do the projection of the input image.
        :type ncores: int
        
        Return:
        
        :return: Distorted image or successuly-written image
        :rtype: numpy.array or bool
    """
    if TPSModule.lang() == "Python":
        raise NotImplementedError

    else:
        infile = kwargs.pop("infile", None)
        inimg = kwargs.pop("inimg", None)

        gfile = kwargs.pop("gfile", None)
        g = kwargs.pop("g", None)
        g2 = deepcopy(g)

        reverseFullGrid = kwargs.pop("reverseFullGrid", False)

        gridSize = kwargs.pop("gridSize", CONF_gridSize)

        cx = kwargs.pop("cx", 0)
        cy = kwargs.pop("cy", 0)
        res = kwargs.pop("res", CONF_res)

        outfile = kwargs.pop("outfile", None)

        ncores = kwargs.pop("ncores", CONF_ncores)

        ############################################################################
        #    Image loading and conversion
        #        Loading the image in 'image coordinate' (top left) and flip in
        #        'ANSI/NIST 2007 coordinates' (bottom left)
        ############################################################################

        if infile != None:
            indata = misc.imread(infile)
        elif inimg != None:
            indata = np.asarray(inimg)
        else:
            raise Exception("No input data (infile or image object)")

        indata = indata.astype(np.int32)
        indata = np.flipud(indata)
        indata = indata.T

        ############################################################################
        #    Distorsion parameters
        #        The g file must be calculated in milimeters and not in pixels !
        #        The center is in "NIST coordinates" (mm from the bottom left)
        ############################################################################

        if gfile != None:
            g2 = TPS_loadFromFile(gfile)
        elif g2 != None:
            pass
        else:
            raise Exception("No TPS parameters of file")

        if cx != 0 and cy != 0:
            g2 = TPS_recenter(g=g2, cx=cx, cy=cy)

        maxx, maxy = indata.shape
        maxx = maxx / float(res) * 25.4
        maxy = maxy / float(res) * 25.4

        ############################################################################
        #    Range calculation
        #        Because the borders could be in negative coordinate
        ############################################################################

        r = kwargs.get('r', None)

        if r == None:
            r = TPSModule.r(g2, 0, int(maxx), 0, int(maxy))

        size = [
            int(float(res) / 25.4 * (r['maxx'] - r['minx'])),
            int(float(res) / 25.4 * (r['maxy'] - r['miny']))
        ]

        outimg = np.ones(size, dtype=np.int32)

        ############################################################################
        #    Calculation of the "inverse" function (approximation)
        #        The real inverse function of g is not calculable (the g function
        #        is not bijective). My approximation here is to project a grid of
        #        size gridSize with the g, and use this projected grid as source for
        #        the g2 projection function. This function is used by the
        #        TPSCy.image function.
        ############################################################################

        if reverseFullGrid:
            g2 = TPSModule.revert(g2, 0, maxx, 0, maxy, gridSize)
        else:
            g2 = TPS_revertDownSampling(g=g2,
                                        minx=0,
                                        maxx=maxx,
                                        miny=0,
                                        maxy=maxy,
                                        gridSize=gridSize,
                                        **kwargs)

        ############################################################################
        #    Distorsion of the image
        #        Reverse calculation of the distorted image. The g2 function allow
        #        the TPSCy.image function to calculate the x, y coordinates on the
        #        original image. This methodoloy allow to have a continuous image.
        ############################################################################

        TPSModule.image(indata, g2, r, float(res), outimg, ncores)

        ############################################################################
        #    Preparation of the output image (file or image object return)
        ############################################################################

        outimg = np.flipud(outimg.T)
        outimg = misc.toimage(outimg, cmin=0, cmax=255)

        if outfile != None:
            outimg.save(outfile, dpi=(int(res), int(res)))
            return os.path.isfile(outfile)
        else:
            return outimg
Example #42
0
# plt.axis('off')
# plt.imshow(noisy)
# plt.show()

wavelet_family_label_list = [
    'haar',
    'db2',
    'db3',
    'db4',
    'dmey',
    'sym2',
    'sym3',
    'bior1.3',
]

image1 = imread('C:\img\zz.jpg')
image_noise = imread('C:\img\zz_noise.jpg')
img = img_as_float(image1)
img_noise = img_as_float(image_noise)

mse_noise = compare_mse(img, img_noise) * 255
psnr_noise = compare_psnr(img, img_noise)
print(mse_noise, psnr_noise)
#sigma = estimate_sigma(image_noise)
print('threshloding mode', 'Wavelet', 'sigma', 'MSE', 'PSNR', 'delta_MSE_%',
      'delta_PSNR_%')
for x in wavelet_family_label_list:
    #mse = compare_mse(img, img_as_float(denoising_image('C:\img\lena_noise.png', x, 'hard')[0]))*255
    #psnr = compare_psnr(img, img_as_float(denoising_image('C:\img\lena_noise.png', x, 'hard')[0]))
    sigma_est = estimate_sigma(
        img_noise, multichannel=True, average_sigmas=True) * 255
def get_car_notcar_scaled_feature_vectors(cars_dataset, notcars_dataset, cspace, bin_params, color_params, hog_params,
                                          display_sample=False):

    car_features = extract_features_imgs(cars_dataset, cspace=cspace, bin_params=bin_params,
                                         color_params=color_params, hog_params=hog_params)
    notcar_features = extract_features_imgs(notcars_dataset, cspace=cspace, bin_params=bin_params,
                                            color_params=color_params, hog_params=hog_params)
    example_fig_filename = 'im_feature_norm.png'
    # Normalize the features
    if len(car_features) > 0:
        # Create an array stack of feature vectors
        X = np.vstack((car_features, notcar_features)).astype(np.float64)
        # Fit a per-column scaler
        X_scaler = StandardScaler().fit(X)
        # Apply the scaler to X
        scaled_X = X_scaler.transform(X)

        ### For display
        if display_sample:
            car_ind = np.random.randint(0, len(cars_dataset))
            notcar_ind = np.random.randint(0, len(notcars_dataset))
            img = imread(cars_dataset[car_ind])
            notcar_img = imread(notcars_dataset[notcar_ind])
            # Plot an example of raw and scaled features
            fig = plt.figure(figsize=(12, 4))
            plt.subplot(131)
            plt.imshow(img)
            plt.title('Original Image')
            plt.subplot(132)
            plt.plot(X[car_ind])
            plt.title('Raw Features')
            plt.subplot(133)
            plt.plot(scaled_X[car_ind])
            plt.title('Normalized Features')
            fig.tight_layout()
            plt.savefig(example_fig_filename)

            # show corresponding HoG image
            vis_hog_params = hog_params
            vis_hog_params['visualize'] = True
            vis_hog_params['feature_vector'] = False
            _, hog_img = hog_features(img, **vis_hog_params)
            plt.figure()
            plt.imshow(hog_img)
            plt.gray()
            plt.title('HoG Features')
            plt.savefig('hog_image.png')

            plt.figure()
            plt.subplot(121)
            plt.imshow(img)
            plt.title('Car')
            plt.subplot(122)
            plt.imshow(notcar_img)
            plt.title('Not Car')
            plt.savefig('car_notcar.png')

    else:
        ValueError('Empty feature vector')

    return {'X': X, 'scaled_X': scaled_X, 'X_scaler': X_scaler,
            'car_features': car_features, 'notcar_features': notcar_features}
import matplotlib.pyplot as plt
import numpy as np
from scipy.misc import imread
a = imread("placa.png")
#a.shape
#plt.imshow(a, cmap= "gray")
#plt.imshow(a[:,:,0])
#plt.show()
#plt.imshow(a[:,:,1])
#plt.show()
#plt.imshow(a[:,:,1], cmap= "gray")
#plt.show()
#b=a[:,:,1]
#plt.imshow(b, cmap= "gray")
#plt.show()
#b.shape
#c=b.reshape([b.shape[0]*b.shape[1]])
#c.shape
#plt.imshow(c)
Example #45
0

if "content" in args.init_image or "gray" in args.init_image:
    x = preprocess_image(base_image_path, True, read_mode=read_mode)
elif "noise" in args.init_image:
    x = np.random.uniform(0, 255, (1, img_width, img_height, 3)) - 128.

    if K.image_dim_ordering() == "th":
        x = x.transpose((0, 3, 1, 2))
else:
    print("Using initial image : ", args.init_image)
    x = preprocess_image(args.init_image, read_mode=read_mode)

# We require original image if we are to preserve color in YCbCr mode
if preserve_color:
    content = imread(base_image_path, mode="YCbCr")
    content = imresize(content, (img_width, img_height))

    if color_mask_present:
        if K.image_dim_ordering() == "th":
            color_mask_shape = (None, None, img_width, img_height)
        else:
            color_mask_shape = (None, img_width, img_height, None)

        color_mask = load_mask(args.color_mask, color_mask_shape, return_mask_img=True)
    else:
        color_mask = None
else:
    color_mask = None

num_iter = args.num_iter
Example #46
0
    def get_labels(self, files):
        """Process and save label images."""
        labels = {}
        new_files = {}
        for k, images in files.iteritems():
            # Replace extension and path with labels
            label_vec = []
            file_vec = []
            fold = images[0].split(os.path.sep)[-2]

            # New label dir
            proc_dir = os.path.join(images[0].split(fold)[0], fold,
                                    self.processed_labels)
            py_utils.make_dir(proc_dir)

            # New image dir
            proc_image_dir = os.path.join(self.config.data_root, self.name,
                                          self.images_dir, fold,
                                          self.processed_images)
            py_utils.make_dir(proc_image_dir)
            ims = []
            for im in tqdm(images, total=len(images), desc=k):
                it_label = im.split(os.path.sep)[-1]
                it_label_path = '%s%s' % (im.split('.')[0], self.lab_extension)
                it_label_path = it_label_path.replace(self.images_dir,
                                                      self.labels_dir)

                # Process every label and duplicate images for each
                label_data = io.loadmat(it_label_path)['groundTruth'].reshape(
                    -1)
                im_data = misc.imread(im)
                transpose_labels = False
                if not np.all(self.im_size == list(im_data.shape)):
                    im_data = np.swapaxes(im_data, 0, 1)
                    # im_data = im_proc.pad_square(im_data)
                    transpose_labels = True
                assert np.all(
                    self.im_size == list(im_data.shape)), \
                    'Mismatched dimensions.'
                ims += [im_data]

                if self.fold_options[k] == 'duplicate':
                    # Loop through all labels
                    for idx, lab in enumerate(label_data):

                        # Process labels
                        ip_lab = lab.item()[1].astype(np.float32)
                        if transpose_labels:
                            ip_lab = np.swapaxes(ip_lab, 0, 1)
                            # ip_lab = im_proc.pad_square(ip_lab)
                        it_im_name = 'image_%s_%s.npy' % (idx, it_label)
                        it_lab_name = 'label_%s.npy' % it_im_name.split('.')[0]
                        out_lab = os.path.join(proc_dir, it_lab_name)
                        np.save(out_lab, ip_lab)
                        label_vec += [out_lab]

                        # Process images
                        proc_im = os.path.join(proc_image_dir, it_im_name)
                        np.save(proc_im, im_data)
                        file_vec += [proc_im]
                elif self.fold_options[k] == 'mean':
                    mean_labs = []
                    for idx, lab in enumerate(label_data):

                        # Process labels
                        ip_lab = lab.item()[1].astype(np.float32)
                        if transpose_labels:
                            ip_lab = np.swapaxes(ip_lab, 0, 1)
                            # ip_lab = im_proc.pad_square(ip_lab)
                        mean_labs += [ip_lab]
                    mean_lab = np.asarray(mean_labs).mean(0)
                    out_lab = os.path.join(
                        proc_dir, 'label_%s.npy' % it_label.split('.')[0])
                    np.save(out_lab, mean_lab)
                    label_vec += [out_lab]

                    # Process images
                    proc_im = os.path.join(proc_image_dir, it_label)
                    np.save(proc_im, im_data)
                    # misc.imsave(proc_im, im_data)
                    file_vec += ['%s.npy' % proc_im]
                else:
                    raise NotImplementedError
            labels[k] = label_vec
            new_files[k] = file_vec
            ims = np.array(ims)
            np.savez(os.path.join(self.config.data_root, self.name,
                                  self.images_dir, fold, 'file_paths'),
                     labels=labels,
                     files=new_files,
                     mean=np.mean(ims, axis=(0, 1, 2), keepdims=True),
                     stds=np.std(ims, axis=(0, 1, 2), keepdims=True))
        return labels, new_files
Example #47
0
    SUPPORT_SIZE = 320
    POOL = ['85.jpg', '44.jpg', '61.jpg', '73.jpg', '94.jpg']
    CWD = os.getcwd()

    for TEST_SHOT in [1, 3, 5]:
        model_dir = os.path.join(CWD,
                                 'models/simu_finetuned/train/checkpoints')
        load_path = os.path.join(
            model_dir, 'faster_rcnn_{}_{}_{}.pth'.format(1, 27, 1248))
        model = get_model(load_path, TEST_SHOT)

        query_name = '002468.jpg'
        file_path = os.path.join(
            CWD, 'data/simulated/images/simulated2020/' + query_name)
        im_data = imread(file_path)[:, :, ::-1].copy()  # rgb -> bgr
        im2show = copy.deepcopy(im_data)
        target_size = cfg.TRAIN.SCALES[0]
        im_data, im_scale = prep_im_for_blob(im_data, cfg.PIXEL_MEANS,
                                             target_size, cfg.TRAIN.MAX_SIZE)
        im_data = torch.from_numpy(im_data)
        info = np.array([[im_data.shape[0], im_data.shape[1], im_scale]],
                        dtype=np.float32)
        info = torch.from_numpy(info)
        dets = torch.from_numpy(np.array([0]))
        n_boxes = torch.from_numpy(np.array([0]))
        query = im_data.permute(2, 0, 1).contiguous().unsqueeze(0)

        support_data_all = np.zeros((TEST_SHOT, 3, SUPPORT_SIZE, SUPPORT_SIZE),
                                    dtype=np.float32)
        for i in range(TEST_SHOT):
# # Collect metadata

subdirs = list(os.walk(dir_train))[1:]

# collect train metadata
train_metadata = []

for dir_path, _, files in tqdm(subdirs):

    dir_name = dir_path.split('/')[-1]

    for file_name in files:
        if not file_name.startswith('.'):
            # read image
            temp = misc.imread(os.path.join(dir_path, file_name))
            # collect image metadata
            image_metadata = []
            image_metadata.extend([dir_name, file_name])
            image_metadata.extend(
                list(temp.shape) if len(temp.shape) ==
                3 else [temp.shape[0], temp.shape[1], 1])
            image_metadata.extend([temp.nbytes, temp.dtype])
            # append image metadata to list
            train_metadata.append(image_metadata)

# # Explore metadata

M = pd.DataFrame(train_metadata)
M.columns = [
    'directory', 'img_name', 'height', 'width', 'channels', 'byte_size',
Example #49
0
    dist_2 = [int(raw_input("Y dist: "))]
    dist_3 = [int(raw_input("Z dist: "))]
    compute = raw_input(
        "Compute? Y/n   :"
    )  ##Enter distance in mellimeters, result = coordinates
    if compute.lower() != 'y':
        break
    X = rf_xx.predict(
        np.array([dist_1, dist_2, dist_3]).reshape(1, -1)
    )  ##Random Forest predictiion from datasets rfc_x.sav, rfc_y.sav,rfc_z.sav
    Y = rf_yy.predict(np.array([dist_1, dist_2, dist_3]).reshape(1, -1))
    Z = rf_zz.predict(np.array([dist_1, dist_2, dist_3]).reshape(1, -1))
    print(X, Y, Z)
    datafile = cbook.get_sample_data(
        'C:\python\map_1.jpg')  ##Load from file bitmap on plot
    img = imread(datafile)
    ax.set_xlim([0, 25])
    ax.set_ylim([0, 50])

    plt.imshow(img, zorder=0, extent=[0, 25, 0, 50])  ##Show picture centered

    plt.scatter(X, Y, c=Z, s=50)  ##Point options
    plt.clim(0, 10)  ##Colorbar range (0-10)
    plt.gray()  ##Depends where it can be placed(color of point may vary)

    plt.grid()  ##Show grid

    plt.colorbar()  ##Show colorbar
    #z_mass[X,Y] = Z
    #plt.colorbar(Z)
    ax.set_xlabel("X label")
# -*- coding: utf-8 -*-

import os, sys
import scipy.misc as misc
import cv2
import matplotlib.pyplot as plt

from pycontour import DATA_DIR
from pycontour import cv_cnt_to_np_arr, np_arr_to_cv_cnt

from .extract_cv2_cnts import extract_cnt_using_cv2

def test_cv_np_transfom(tmp_cnt):
    # convert cv2 contour to numpy array
    np_arr = cv_cnt_to_np_arr(tmp_cnt)
    # convert numpy arrary to cv2 contour
    cv_cnt = np_arr_to_cv_cnt(np_arr)

    return cv_cnt


if __name__ == "__main__":
    img_path = os.path.join(DATA_DIR, "images", "brain.jpg")
    img = misc.imread(img_path)
    cnts = extract_cnt_using_cv2(img_path)
    test_cnt = cnts[1]
    cv_cnt = test_cv_np_transfom(test_cnt)
    draw_img = cv2.drawContours(img, [cv_cnt], 0, 255, 3)
    entrada = 'img_entrada.tif'

try:
    saida = sys.argv[2]
except IndexError:
    saida = 'img_saida.tif'

try:
    T_ini = sys.argv[3]
except IndexError:
    T_ini = 0.5

T_ini = float(T_ini)

# Faz a leitura da imagem
img_entrada = misc.imread(entrada)

# Converte os pixels em float, com valores entre 0 e 1
img_entrada = img_as_float(img_entrada.astype(np.uint8))

# Chama a função para cálculo do limiar global iterativo
valor_T = limiar_global_simples(img_entrada, T_ini)

# Segmenta a imagem com o limiar T
img_saida = img_entrada > valor_T
misc.imsave(saida, img_saida.astype(np.uint8))

# Organiza o plote das imagens
plt.figure()
plt.subplot(221)
plt.imshow(img_entrada, cmap='gray', interpolation='nearest')
def read_style_image(filename, batch_size=64):
	img = imread(filename)
Example #53
0
    def procimage(self, picture, threadid):
        filelist = {}
        max = 60
        if not self.options.silent: print "[", threadid, "] open: ", picture
        for i in range(max):
            try:
                image = misc.imread(picture)
            except KeyboardInterrupt:
                return
            except IOError as e:
                try:
                    print "cannot open ", picture, ", lets wait.", max - i, " s"
                    print e.message, sys.exc_info()[0]
                    time.sleep(1)
                    continue
                except KeyboardInterrupt:
                    return
            except:
                print "############"
                print sys.exc_info()
                continue
            if image.shape == tuple(
                    self.cals[0].config["Geometry"]["Imagesize"]):
                break
            print "cannot open ", picture, ", lets wait.", max - i, " s"
            time.sleep(1)

        else:
            print "image ", picture, " has wrong format"
            return

        if self.options.outdir != "":
            basename = self.options.outdir + os.sep + ('_'.join(
                picture.replace('./', '').split(os.sep))[:-3]).replace(
                    '/', "_")
            basename = basename.replace(':', '').replace('.', '')
        else:
            reldir = os.path.join(os.path.dirname(picture),
                                  self.options.relpath)
            if not os.path.isdir(reldir):
                os.mkdir(reldir)
            basename = os.path.join(reldir, os.path.basename(picture)[:-4])
        data = []
        for calnum, cal in enumerate(self.cals):
            filename = basename + "_c" + cal.kind[0] + str(calnum)
            chifilename = filename + ".chi"
            filelist[cal.kind + str(calnum)] = chifilename
            if not self.options.resume or not os.path.isfile(chifilename):
                result = cal.integratechi(image, chifilename, picture)
                result["Image"] = picture
                data.append(result)
                if threadid == 0 and self.options.plotwindow:
                    # this is a hack it really schould be a proper GUI

                    cal.plot(image, fig=self.fig)
                    plt.draw()

            if self.options.writesvg:

                if not self.options.resume or not os.path.isfile(filename +
                                                                 '.svg'):
                    cal.plot(image, filename + ".svg", fig=self.fig)
            if self.options.writepng:
                if not self.options.resume or not os.path.isfile(filename +
                                                                 '.svg'):
                    misc.imsave(filename + ".png", image)
            if self.options.silent:
                if np.mod(self.allp.value, 100) == 0:
                    print "[", threadid, "] ", self.allp.value
            else:
                print "[", threadid, "] write: ", filename + ".chi"
        with self.allp.get_lock():
            self.allp.value += 1

        json.dump(data, open(basename + ".json", "w"))
        filelist["JSON"] = basename + ".json"
        self.histqueue.put({
            "Time": time.time(),
            "FileList": filelist,
            "BaseName": basename
        })

        return basename, data
def caption_image_beam_search(encoder,
                              decoder,
                              image_path,
                              word_map,
                              gt_word,
                              beam_size=1):
    """
    Reads an image and captions it with beam search.

    :param encoder: encoder model
    :param decoder: decoder model
    :param image_path: path to image
    :param word_map: word map
    :param beam_size: number of sequences to consider at each decode-step
    :return: caption, weights for visualization
    """

    k = beam_size
    vocab_size = len(word_map)
    # Keep track of the raw score for each predicted words
    img_scores = []
    # Read image and process
    img = imread(image_path)
    if len(img.shape) == 2:
        img = img[:, :, np.newaxis]
        img = np.concatenate([img, img, img], axis=2)
    img = imresize(img, (256, 256))
    img = img.transpose(2, 0, 1)
    img = img / 255.
    img = torch.FloatTensor(img).to(device)
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])
    transform = transforms.Compose([normalize])
    image = transform(img)  # (3, 256, 256)

    # Encode
    image = image.unsqueeze(0)  # (1, 3, 256, 256)
    encoder_out = encoder(
        image)  # (1, enc_image_size, enc_image_size, encoder_dim)
    enc_image_size = encoder_out.size(1)
    encoder_dim = encoder_out.size(3)

    # Flatten encoding
    encoder_out = encoder_out.view(1, -1,
                                   encoder_dim)  # (1, num_pixels, encoder_dim)
    num_pixels = encoder_out.size(1)

    # We'll treat the problem as having a batch size of k
    encoder_out = encoder_out.expand(
        k, num_pixels, encoder_dim)  # (k, num_pixels, encoder_dim)

    # Tensor to store top k previous words at each step; now they're just <start>
    k_prev_words = torch.LongTensor([[word_map['<start>']]] * k).to(
        device)  # (k, 1)

    # Tensor to store top k sequences; now they're just <start>
    seqs = k_prev_words  # (k, 1)

    # Tensor to store top k sequences' scores; now they're just 0
    top_k_scores = torch.zeros(k, 1).to(device)  # (k, 1)

    # Tensor to store top k sequences' alphas; now they're just 1s
    seqs_alpha = torch.ones(k, 1, enc_image_size, enc_image_size).to(
        device)  # (k, 1, enc_image_size, enc_image_size)

    # Lists to store incompleted sequences, their alphas and scores
    complete_seqs = list()
    complete_seqs_alpha = list()
    complete_seqs_scores = list()
    incomplete_seqs = list()
    incomplete_seqs_alpha = list()
    incomplete_seqs_scores = list()

    # Start decoding
    step = 1
    h, c = decoder.init_hidden_state(encoder_out)

    # s is a number less than or equal to k, because sequences are removed from this process once they hit <end>
    while True:

        embeddings = decoder.embedding(k_prev_words).squeeze(
            1)  # (s, embed_dim)

        awe, alpha = decoder.attention(encoder_out,
                                       h)  # (s, encoder_dim), (s, num_pixels)

        alpha = alpha.view(
            -1, enc_image_size,
            enc_image_size)  # (s, enc_image_size, enc_image_size)

        gate = decoder.sigmoid(
            decoder.f_beta(h))  # gating scalar, (s, encoder_dim)
        awe = gate * awe

        # Hidden and cell state
        h, c = decoder.decode_step(torch.cat([embeddings, awe], dim=1),
                                   (h, c))  # (s, decoder_dim)

        # Probability for each token
        scores = decoder.fc(h)  # (s, vocab_size)
        scores = F.softmax(scores, dim=1)
        # Add
        #scores = top_k_scores.expand_as(scores) + scores  # (s, vocab_size)
        img_scores.append(scores.cpu().detach().numpy())
        # For the first step, all k points will have the same scores (since same k previous words, h, c)
        if step == 1:
            top_k_scores, top_k_words = scores[0].topk(k, 0, True, True)  # (s)
        else:
            # Unroll and find top scores, and their unrolled indices
            top_k_scores, top_k_words = scores.view(-1).topk(k, 0, True,
                                                             True)  # (s)
        # Convert unrolled indices to actual indices of scores
        prev_word_inds = top_k_words / vocab_size  # (s)
        next_word_inds = top_k_words % vocab_size  # (s)

        # Add new words to sequences, alphas
        seqs = torch.cat([seqs[prev_word_inds],
                          next_word_inds.unsqueeze(1)],
                         dim=1)  # (s, step+1)
        seqs_alpha = torch.cat(
            [seqs_alpha[prev_word_inds], alpha[prev_word_inds].unsqueeze(1)],
            dim=1)  # (s, step+1, enc_image_size, enc_image_size)
        # Break if things have been going on too long
        if step > len(gt_word.split(" ")):
            break
        step += 1

    seq = seqs[0].tolist()
    alphas = seqs_alpha[0].tolist()
    return img_scores, seq, alphas
Example #55
0
# _*_ coding:utf-8 _*_
__author__ = 'Xbc'

import matplotlib as mpl
mpl.use('TkAgg')

from os import path
from scipy.misc import imread
import matplotlib.pyplot as plt
import jieba
from wordcloud import WordCloud

f = open('./jd_comment.txt', 'rb')
text = f.read()

cut_text = ''.join(jieba.lcut(text))
print(cut_text)
color_mask = imread("iphone.jpg")
cloud = WordCloud(
    font_path='ziti.TTF',  # 字体最好放在与脚本相同的目录下,而且必须设置
    background_color='white',
    mask=color_mask,
    max_words=500,
    max_font_size=3000)
word_cloud = cloud.generate(cut_text)
plt.imshow(word_cloud)
plt.axis('off')
plt.show()
Example #56
0
from glob import glob
from scipy.misc import imread, imsave
from skimage.filters import threshold_otsu
from skimage.color import rgb2gray
from skimage.morphology import square, erosion

files = glob('*.jpg')

for i in files:
    image = rgb2gray(imread(i))
    try:
        global_thresh = threshold_otsu(image)
        binary_global = image < global_thresh
        binary_global = erosion(binary_global, square(3))
    except Exception as e:
        binary_global = image
        print e, i
    imsave('bw/' + i[:-4] + '__' + '.jpg', binary_global)
    print i, i[:-4] + '__' + '.jpg'
Example #57
0
    def test_seg_accuracy(self,
                          image_type='.jpg',
                          seg_type='.png',
                          bbox_type='.xml',
                          use_bbox_gt=True):
        test_file = self.test_file
        image_folder = self.image_folder
        bbox_folder = self.bbox_folder
        seg_folder = self.seg_folder

        f = open(test_file, 'r')
        lines = f.readlines()

        intersection_class_compare = np.zeros(voc_classes.shape)
        union_class_compare = np.zeros(voc_classes.shape)

        for line in lines:
            name = line.split('\n')[0]
            image_file = image_folder + name + image_type
            seg_file = seg_folder + name + seg_type
            bbox_file = bbox_folder + name + bbox_type
            object_names, object_attr = parseXmlVoc(bbox_file)
            im = imread(image_file)
            im_shape = im.shape
            if use_bbox_gt:
                detect = []
                for i in range(len(object_attr)):
                    attr = object_attr[i]
                    object_name = object_names[i]
                    label = np.argwhere(voc_classes == object_name)[0][0] + 1
                    xmin = float(attr['xmin']) / im_shape[1]
                    ymin = float(attr['ymin']) / im_shape[0]
                    xmax = float(attr['xmax']) / im_shape[1]
                    ymax = float(attr['ymax']) / im_shape[0]

                    detect.append(
                        [xmin, ymin, xmax, ymax, label, 1, object_name])
            else:
                detect = self.detect(image_file)
                # print len(detect)
            num_detect = len(detect)

            if num_detect > 0:
                bbox, all_diff_cls_labels = self.detect2bbox(detect)

                seg_prob = self.seg(bbox,
                                    all_diff_cls_labels,
                                    image_file,
                                    key_type='label',
                                    nocls=True)

                mask = self.merge_seg_prob(seg_prob, bbox, all_diff_cls_labels)

                seg_label = imread(seg_file)

                # object_index = list(set(mask[np.where(mask!=0)].flatten()))
                object_index = list(
                    set(seg_label[np.where(seg_label != 0)].flatten()))
                for idx_c in object_index:
                    if idx_c == 255:
                        continue
                    seg_anno_c = np.single(seg_label == idx_c)
                    seg_mask_c = np.single(mask == idx_c)
                    # erase the ignore label
                    # seg_mask_c[np.where(seg_label == 255)] = 0
                    intersection_class_compare[idx_c - 1] += len(
                        np.argwhere((seg_anno_c * seg_mask_c) > 0))
                    union_class_compare[idx_c - 1] += len(
                        np.argwhere((seg_anno_c + seg_mask_c) > 0))
                if True:
                    plt.figure(1)
                    plt.imshow(im)
                    ax = plt.gca()

                    for item in detect:
                        xmin = int(round(item[0] * im_shape[1]))
                        ymin = int(round(item[1] * im_shape[0]))
                        xmax = int(round(item[2] * im_shape[1]))
                        ymax = int(round(item[3] * im_shape[0]))

                        bbox_weight = xmax - xmin
                        bbox_height = ymax - ymin

                        coords = (xmin, ymin), bbox_weight, bbox_height
                        ax.add_patch(
                            plt.Rectangle(*coords,
                                          fill=False,
                                          edgecolor='g',
                                          linewidth=3))

                        ax.text(xmin,
                                ymin,
                                item[-1] + str(item[-2]),
                                bbox={
                                    'facecolor': 'b',
                                    'alpha': 0.5
                                })

                    color_map = np.random.randint(0, 256,
                                                  [256, 3]).astype(np.uint8)
                    mask_color = color_map[mask]
                    seg_label_color = color_map[seg_label]
                    plt.figure(666)
                    plt.imshow(mask_color)
                    plt.figure(888)
                    plt.imshow(seg_label_color)

                    plt.show()
                    pass
            else:
                object_index = list(
                    set(seg_label[np.where(seg_label != 0)].flatten()))
                for idx_c in object_index:
                    if idx_c == 255:
                        continue
                    seg_anno_c = np.single(seg_label == idx_c)
                    intersection_class_compare[idx_c - 1] += 0
                    union_class_compare[idx_c - 1] += len(
                        np.argwhere((seg_anno_c) > 0))
        print intersection_class_compare / union_class_compare
        print(intersection_class_compare / union_class_compare).mean()
Example #58
0
words_df = pandas.DataFrame({'segment': segment})
words_df.head()
stopwords = pandas.read_csv("/hwj/dorahacks/stopwords.txt",
                            index_col=False,
                            quoting=3,
                            sep="\t",
                            names=['stopword'],
                            encoding="utf8")
#去掉我们不需要的高频语气词等
words_df = words_df[~words_df.segment.isin(stopwords.stopword)]

words_stat = words_df.groupby(by=['segment'])['segment'].agg(
    {"计数": numpy.size})
words_stat = words_stat.reset_index().sort_values(by="计数", ascending=False)

from scipy.misc import imread
import matplotlib.pyplot as plt
from wordcloud import WordCloud, ImageColorGenerator
#%matplotlib inline
bimg = imread('/hwj/dorahacks/1.jpg')
wordcloud = WordCloud(background_color="black",
                      mask=bimg,
                      font_path='/hwj/dorahacks/STFANGSO.ttf')
#wordcloud=wordcloud.fit_words(words_stat.head(4000).itertuples(index=False))
words = words_stat.set_index("segment").to_dict()
wordcloud = wordcloud.fit_words(words["计数"])
bimgColors = ImageColorGenerator(bimg)
plt.axis("off")
plt.imshow(wordcloud.recolor(color_func=bimgColors))
plt.show()
Example #59
0
def main(
        image_direc='image',
        nPixels_on_side=15,
        i_std=15,  # std dev for color Gaussian
        imgs_of_the_same_size=None):
    if image_direc is None:
        raise ValueError("image_direc cannot be None")
    image_direc = os.path.expanduser(image_direc)
    image_direc = os.path.abspath(image_direc)
    FilesDirs.raise_if_dir_does_not_exist(image_direc)
    imgs = get_list_of_all_imgs_dir(image_direc)
    if not imgs:
        raise Exception(
            "\n\nDirectory \n{}\ncontains no images.\n".format(image_direc))

    # Results will be saved in the /results folder under input directory. You can change it.
    save_path_root = os.path.join(image_direc, 'result')
    print "I am going to save results into", save_path_root
    FilesDirs.mkdirs_if_needed(save_path_root)

    # Part 1: Specify the parameters:
    prior_count_const = 5  # the weight of Inverse-Wishart prior of space covariance(ex:1,5,10)
    use_hex = True  # toggle between hexagons and squares (for the init)
    prior_weight = 0.5  # in the segmentation,
    # we do argmax w * log_prior + (1-w) *log_likelihood.
    # Keeping w (i.e., prior_weight) at 0.5 means we are trying
    # to maximize the true posterior.
    # We keep the paramter here in case the user will like
    # to tweak it.

    calc_s_cov = True  # If this is False, then we avoid estimating the spatial cov.
    num_EM_iters = nPixels_on_side
    num_inner_iters = 10
    sp_size = nPixels_on_side * nPixels_on_side

    for i, img_filename in enumerate(imgs):

        # Part 2 : prepare for segmentation
        print img_filename
        fullfilename = os.path.join(image_direc, img_filename)
        FilesDirs.raise_if_file_does_not_exist(fullfilename)
        img = misc.imread(fullfilename)

        dimx = img.shape[1]
        dimy = img.shape[0]

        if not ((img.ndim in [1, 3]) and (img.shape[2] in [1, 3])):
            raise ValueError(
                "\n\nProblem with {0}\n\nI was expecting 3 channels, but img.shape = {1}\n\n"
                .format(fullfilename, img.shape))

        tic = time.clock()
        if (imgs_of_the_same_size and i != 0):
            sw.initialize_seg()
            # you can use the same SuperpixelsWrapper object with different imgs and/or,
            # i_std, s_std, prior_count.
            # Just call sw.set_img(new_img), sw.initialize_seg(), and/or
            # sw.set_superpixels(i_std=..., s_std = ..., prior_count = ...)
            # again and recompute the seg.
        else:
            sw = SuperpixelsWrapper(dimy=dimy,
                                    dimx=dimx,
                                    nPixels_in_square_side=nPixels_on_side,
                                    i_std=i_std,
                                    s_std=nPixels_on_side,
                                    prior_count=prior_count_const * sp_size,
                                    use_hex=use_hex)

        toc = time.clock()
        print 'init time = ', toc - tic
        print 'nSuperpixels =', sw.nSuperpixels
        sw.set_img(img)

        # Part 3: Do the superpixel segmentation

        tic = time.clock()
        #actual work
        sw.calc_seg(nEMIters=num_EM_iters,
                    nItersInner=num_inner_iters,
                    calc_s_cov=calc_s_cov,
                    prior_weight=prior_weight)
        # Copy the parameters from gpu to cpu
        sw.gpu2cpu()
        toc = time.clock()
        print 'superpixel calculation time = ', toc - tic

        # Part 4: Save the mean/boundary image and the resulting parameters

        img_overlaid = sw.get_img_overlaid()  # get the boundary image
        img_cartoon = sw.get_cartoon()  # get the cartoon image
        grid = ['square', 'hex'][sw.use_hex]

        root_slash_img_num = os.path.splitext(img_filename)[0]
        img_num = os.path.split(root_slash_img_num)[1]
        #fname_res_border = '_'.join([img_num , 'std', str(i_std), 'border', grid+'.png'])
        #fname_res_cartoon = '_'.join([img_num , 'std', str(i_std), 'mean', grid+'.png'])

        fname_res_border = '_'.join([
            img_num, 'n', '{0:03}'.format(nPixels_on_side), 'std',
            '{0:02}'.format(i_std), 'border', grid + '.png'
        ])
        fname_res_cartoon = '_'.join([
            img_num, 'n', '{0:03}'.format(nPixels_on_side), 'std',
            '{0:02}'.format(i_std), 'mean', grid + '.png'
        ])

        fname_res_border = os.path.join(save_path_root, fname_res_border)
        fname_res_cartoon = os.path.join(save_path_root, fname_res_cartoon)

        print 'saving', fname_res_border
        misc.imsave(fname_res_border, img_overlaid)
        print 'saving', fname_res_cartoon
        misc.imsave(fname_res_cartoon, img_cartoon)

        #save the resulting parameters to MATLAB .mat file
        #mat_filename = os.path.join(save_path_root , img_num + '_std_'+str(i_std)+'.mat')
        mat_filename = os.path.join(
            save_path_root  #, img_num + '_std_'+str(i_std)+'.mat')
            ,
            '_'.join([
                img_num, 'n', '{0:03}'.format(nPixels_on_side), 'std',
                '{0:02}'.format(i_std) + '.mat'
            ]))
        print 'Saving params to ', mat_filename

        pm = sw.superpixels.params
        sio.savemat(
            mat_filename, {
                'pixel_to_super': sw.seg.cpu,
                'count': pm.counts.cpu,
                'mu_i': pm.mu_i.cpu,
                'mu_s': pm.mu_s.cpu,
                'sigma_s': pm.Sigma_s.cpu,
                'sigma_i': pm.Sigma_i.cpu
            })
        if (not imgs_of_the_same_size):
            del sw
Example #60
0
#
# TODO: Write a for-loop that iterates over the images in the
# Module4/Datasets/ALOI/32/ folder, appending each of them to
# your list. Each .PNG image should first be loaded into a
# temporary NDArray, just as shown in the Feature
# Representation reading.
#
# Optional: Resample the image down by a factor of two if you
# have a slower computer. You can also convert the image from
# 0-255  to  0.0-1.0  if you'd like, but that will have no
# effect on the algorithm's results.
#
# .. your code here .. 
path = 'Datasets/ALOI/32/'
for filename in os.listdir(path):
    samples.append((misc.imread(path + filename)[::2,::2]/ 255.0).reshape(-1, 3))
#print(samples)
#print(misc.imread('Datasets/ALOI/32/32_r0.png'))
#print('---')
#print(misc.imread('Datasets/ALOI/32/32_r0.png').reshape(-1, 3))


#print((misc.imread('Datasets/ALOI/32/32_r0.png')/ 255.0).reshape(-1, 3))
#
# TODO: Once you're done answering the first three questions,
# right before you converted your list to a dataframe, add in
# additional code which also appends to your list the images
# in the Module4/Datasets/ALOI/32_i directory. Re-run your
# assignment and answer the final question below.
#
# .. your code here ..