Exemplo n.º 1
0
def PosterPreProc(image,**kwargs):
    """
    This method takes the image of the foil and creates a smoothed Kuwahara image
    used to make the poster for regional thresholding.
    
        Key-word Arguments:
            Mask = 0 - Assign the Mask here
            kern = 6 - Kernal size for poster opening and closing steps
            KuSize = 17 - Size of Kuwahara Filter
            Gaus1 = 5 - Size of first Gaussian Blur
            Gaus2 = 3 - Size of second Gaussian Blur
            rsize = .1 - Resize value
            Kuw_only = False - Option to only return the Kuwahara filtered image
            ExcludeDirt = True - Option to Exclude dirt 
    """
    Mask = kwargs.get("Mask",0) # Assign the Mask here
    kern = kwargs.get("kern",6) # Kernal size for poster opening and closing steps
    KuSize = kwargs.get("KuSize",17) # Size of Kuwahara Filter
    Gaus1 = kwargs.get("Gaus1",5) # Size of first Gaussian Blur
    Gaus2 = kwargs.get("Gaus2",3) # Size of second Gaussian Blur
    rsize = kwargs.get("rsize",.1) # Resize value
    Kuw_only = kwargs.get("Kuw_only",False) # Option to only return the Kuwahara filtered image
    ExcludeDirt = kwargs.get("ExcludeDirt",True) # Option to Exclude dirt from approximation of shading 
    ExcludePt = kwargs.get("ExcludePt",False) 
    img = np.copy(image).astype(np.uint8)
    
    # Calculate the average Apply mask if provided
    if type(Mask)==np.ndarray and Mask.shape==image.shape:
        if Mask.all() == 0:
            averageColor = 0
        else:
            try: 
                averageColor = int(np.average(img[(Mask!=0)&(img>=10)&(img<=245)]))
            except ValueError:
                averageColor = int(np.average(img[(Mask!=0)]))
    else:
        try: 
            averageColor = int(np.average(img[(img>=10)&(img<=245)]))
        except ValueError:
            averageColor = int(np.average(img))
        
    if ExcludeDirt:
        img[img<=40]=averageColor
    if ExcludePt:
        img[img>=(img.max()-10)]=averageColor
        
    if type(Mask)==np.ndarray and Mask.shape==image.shape:
        img[Mask==0]=averageColor
    
    proc = img.astype(np.uint8)
    proc = misc.imresize(proc,rsize)
    proc = cv2.morphologyEx(proc,cv2.MORPH_ERODE, (kern,kern)) # Eliminates most platinum spots
    proc = cv2.morphologyEx(proc,cv2.MORPH_DILATE,(kern+1,kern+1)) # Eliminates most dirt spots
    proc = cv2.GaussianBlur(proc,(Gaus1,Gaus1),0)
    proc = Kuwahara.Kuwahara(proc,KuSize)
    if Kuw_only:
        return proc
    proc = cv2.GaussianBlur(proc,(Gaus2,Gaus2),0)
    proc = misc.imresize(proc,img.shape)
    return proc
Exemplo n.º 2
0
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
Exemplo n.º 3
0
    def _load_datum(self, id):
        # check cache
        datum = self.db.get(str(id))
        if datum is not None:
            img, label = pickle.loads(datum)
            return img, label
        # there is no cache
        img_file = osp.join(self.pascal_dir, 'JPEGImages', id + '.jpg')
        img = imread(img_file, mode='RGB')
        if img.shape[0] < 224 or img.shape[1] < 224:
            print "------>",img.shape
            img = imresize(img, [max(224, img.shape[0]), max(224, img.shape[1])])
        img = self.img_to_datum(img)

        label_rgb_file = osp.join(self.pascal_dir, 'SegmentationClass', id + '.png')
        label_rgb = imread(label_rgb_file, mode='RGB')
        if label_rgb.shape[0] < 224 or label_rgb.shape[1] < 224:
            label_rgb = imresize(label_rgb, [max(224, label_rgb.shape[0]), max(224, label_rgb.shape[1])], interp='nearest')
        label = self._label_rgb_to_32sc1(label_rgb)
        
        datum = (img, label)
        # save cache
        self.db.put(str(id), pickle.dumps(datum))
        
        return img, label
Exemplo n.º 4
0
    def interpolateXYim(self):

        newWidth = self.image.shape[1]
        
        if newWidth > 1500:
            newWidth = 1500
            newHeight = int(self.image.shape[0]*newWidth/float(self.image.shape[1]))    
            print newWidth, newHeight, 'newWidth, newHeight'
            print self.image.size, 'size'
            
            #if self.image.size == 2:
           
            #    self.image = misc.imresize(self.image, (newHeight, newWidth), mode = 'F')
                
           # elif self.image.size == 3:
           #     print self.image.shape[2], 'shape2'
           #     self.image = misc.imresize(self.image, (newHeight, newWidth, self.image.shape[2]), mode = 'F')
           # else:
            self.image = misc.imresize(self.image, (newHeight, newWidth))
                
        else:
            newHeight = self.image.shape[0]
        
        imX = self.Xmat
        imY = self.Ymat
    
        imX[imX == 0.] = np.nan
        imY[imY == 0.] = np.nan
    
        imX = misc.imresize(imX,(newHeight, newWidth), interp = 'bicubic', mode = 'F')
        imY = misc.imresize(imY,(newHeight, newWidth), interp = 'bicubic', mode = 'F')
        
        self.Xmat = imX
        self.Ymat = imY
Exemplo n.º 5
0
 def get_contents(self, height, width):
     """Returns the contents (pixel values) of both images of the pair as
     one numpy array.
     Args:
         height: Output height of each image.
         width: Output width of each image.
     Returns:
         Numpy array of shape (2, height, width) with dtype uint8.
     """
     img1 = self.image1.get_content()
     img2 = self.image2.get_content()
     if img1.shape[0] != height or img1.shape[1] != width:
         # imresize can only handle (height, width) or (height, width, 3),
         # not (height, width, 1), so squeeze away the last channel
         if IMAGE_CHANNELS == 1:
             img1 = misc.imresize(np.squeeze(img1), (height, width))
             img1 = img1[:, :, np.newaxis]
         else:
             img1 = misc.imresize(img1, (height, width))
     if img2.shape[0] != height or img2.shape[1] != width:
         if IMAGE_CHANNELS == 1:
             img2 = misc.imresize(np.squeeze(img2), (height, width))
             img2 = img2[:, :, np.newaxis]
         else:
             img2 = misc.imresize(img2, (height, width))
     return np.array([img1, img2], dtype=np.uint8)
Exemplo n.º 6
0
def test_bicubic():
    origin = io.imread('baby_GT[gray].bmp')
    im_jor = io.imread('baby_JOR[gray].bmp')
    im_my = io.imread("baby_MY[gray].bmp")

    image = io.imread('baby_GT.bmp')
    shape = origin.shape

    if len(shape) == 3:
        test_img = image[:shape[0]-shape[0] % 3, :shape[1]-shape[1] % 3, :]
    else:
        test_img = image[:shape[0]-shape[0] % 3, :shape[1]-shape[1] % 3]

    lim = imresize(test_img, 1/3.0, 'bicubic')
    mim = imresize(lim, 2.0, 'bicubic')
    rim = imresize(lim, 3.0, 'bicubic')

    lim = np.asarray(tc.rgb2ycbcr(lim)[:, :, 0], dtype=float)
    image = np.asarray(tc.rgb2ycbcr(test_img)[:, :, 0], dtype=float)
    mim = np.asarray(tc.rgb2ycbcr(mim)[:, :, 0], dtype=float)
    rim = np.asarray(tc.rgb2ycbcr(rim)[:, :, 0], dtype=float)

    print psnr(image*1.0, rim*1.0)
    print psnr(image*1.0, im_my[0:504,0:504]*1.0)

    plt.subplot(221)
    plt.imshow(image, interpolation="None", cmap=cm.gray)
    plt.subplot(222)
    plt.imshow(np.abs(rim), cmap=cm.gray)
    plt.subplot(223)
    plt.imshow(np.abs(im_my), interpolation="None", cmap=cm.gray)
    plt.subplot(224)
    plt.imshow(np.abs(im_jor), interpolation="None", cmap=cm.gray)

    plt.show()
def build_rotations(path,pathdir,files,labels,all_count,size):
    train_labels=labels
    barr=len(labels)

    # train_dates={label:[] for label in train_labels}
    train_dates={}
    for label in train_labels:
        '''每一个label都扩充成4倍,
        原来的原来的是原来的label,
        下一轮是rotate 90的,再下一轮是180的,最后一伦施270的'''
        train_dates[int(label)]=[]
        train_dates[int(label)+barr]=[]
        train_dates[int(label)+2*barr]=[]
        train_dates[int(label)+3*barr]=[]

    for file in files:
        label=file[-11:-7]
        if label in train_labels:
            train_dates[int(label)].append(0.001*(255-np.float32(imresize(imread(file,1),size))))
            train_dates[int(label)+barr].append(0.001*(255-np.float32((imresize(imrotate(imread(file,1),90),size)))))
            train_dates[int(label)+2*barr].append(0.001*(255-np.float32((imresize(imrotate(imread(file,1),180),size)))))
            train_dates[int(label)+3*barr].append(0.001*(255-np.float32((imresize(imrotate(imread(file,1),270),size)))))

    if cnn_only:
        return train_dates
Exemplo n.º 8
0
	def preprocess_screen(self, observation):
		screen_width = config.ale_screen_size[0]
		screen_height = config.ale_screen_size[1]
		new_width = config.ale_scaled_screen_size[0]
		new_height = config.ale_scaled_screen_size[1]
		if len(observation.intArray) == 100928: 
			observation = np.asarray(observation.intArray[128:], dtype=np.uint8).reshape((screen_width, screen_height, 3))
			observation = spm.imresize(observation, (new_height, new_width))
			# Clip the pixel value to be between 0 and 1
			if config.ale_screen_channels == 1:
				# Convert RGB to Luminance
				observation = np.dot(observation[:,:,:], [0.299, 0.587, 0.114])
				observation = observation.reshape((new_height, new_width, 1))
			observation = observation.transpose(2, 0, 1) / 255.0
			observation /= (np.max(observation) + 1e-5)
		else:
			# Greyscale
			if config.ale_screen_channels == 3:
				raise Exception("You forgot to add --send_rgb option when you run ALE.")
			observation = np.asarray(observation.intArray[128:]).reshape((screen_width, screen_height))
			observation = spm.imresize(observation, (new_height, new_width))
			# Clip the pixel value to be between 0 and 1
			observation = observation.reshape((1, new_height, new_width)) / 255.0
			observation /= (np.max(observation) + 1e-5)

		observed_screen = observation
		if self.last_observed_screen is not None:
			observed_screen = np.maximum(observation, self.last_observed_screen)

		self.last_observed_screen = observation
		return observed_screen
Exemplo n.º 9
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)
Exemplo n.º 10
0
def load_data(trainingData, trainingLabel,
              testingData, testingLabel,
              resize = False, size = 100, dataset = "IKEA_PAIR"):
    trainingData = os.environ[dataset] + trainingData
    trainingLabel = os.environ[dataset] + trainingLabel
    testingData = os.environ[dataset] + testingData
    testingLabel = os.environ[dataset] + testingLabel

    X_train = np.array(np.load(trainingData),
                       dtype = np.float32)
    Y_train = np.array(np.load(trainingLabel),
                       dtype = np.uint8)
    X_test = np.array(np.load(testingData),
                      dtype = np.float32)
    Y_test = np.array(np.load(testingLabel),
                      dtype = np.uint8)

    print("resizing....")
    if resize:
        X_train = np.array([misc.imresize(X_train[i],
                                          size = (size, size, 3)) /255.0
                            for i in range(X_train.shape[0])], dtype=np.float32)
        X_test = np.array([misc.imresize(X_test[i],
                                         size = (size, size, 3)) /255.0
                           for i in range(X_test.shape[0])], dtype=np.float32)
        np.save(trainingData + "_100.npy", X_train)
        np.save(testingData + "_100.npy", X_test)

    X_train = np.rollaxis(X_train, 3, 1)
    X_test = np.rollaxis(X_test, 3, 1)

    print("downresizing....")


    return X_train, Y_train, X_test, Y_test
    def get_shift_scale_depth(self, shift, scale, framenumber, IM_SZ, show_flag=False):
        """
        Wudi added this method to extract segmented depth frame,
        by a shift and scale

        """
        depth_original = self.getDepth(framenumber)
        mask = numpy.mean(self.getUser(framenumber), axis=2) > 150
        resize_final_out = numpy.zeros((IM_SZ,IM_SZ))
        if mask.sum() < 1000: # Kinect detect nothing
            print "skip "+ str(framenumber)
            flag = False
        else:
            flag = True
            depth_user = depth_original * mask

            depth_user_normalized = depth_user * 1.0 / float(self.data['maxDepth'])
            depth_user_normalized = depth_user_normalized *255 /depth_user_normalized.max()

            rows,cols = depth_user_normalized.shape
            M = numpy.float32([[1,0, shift[1]],[0,1, shift[0]]])
            affine_image = cv2.warpAffine(depth_user_normalized, M,(cols, rows))
            resize_image = imresize(affine_image, scale)
            resize_image_median = cv2.medianBlur(resize_image,5)

            rows, cols = resize_image_median.shape
            image_crop = resize_image_median[rows/2-160:rows/2+160, cols/2-160:cols/2+160]
            resize_final_out = imresize(image_crop, (IM_SZ,IM_SZ))
            if show_flag: # show the segmented images here
                cv2.imshow('image',image_crop)
                cv2.waitKey(10)

        return [resize_final_out, flag]
def downsample(x,p_down):
    size = len(imresize(x[0].reshape(28,28),p_down,mode='F').ravel())
    s_tr = np.zeros((x.shape[0], size))
    for i in xrange(x.shape[0]):
      img = x[i].reshape(28,28)
      s_tr[i] = imresize(img,p_down,mode='F').ravel()
    return s_tr
Exemplo n.º 13
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
Exemplo n.º 14
0
def generate_biomes(data_path):
    if os.path.isfile(data_path + "biomes.pkl"):
        return
    
    moisture = pickle.load(open(data_path+"moisture.pkl", 'rb'))
    moisture = imresize(moisture, (IMAGE_HEIGHT, IMAGE_WIDTH))
    plt.imshow(moisture)
    plt.show()
    moisture = np.digitize(moisture, [0, 100, 170, 230, 255])-1
    moisture[moisture > 4] = 4
    plt.imshow(moisture)
    plt.show()
    temp = pickle.load(open(data_path+"temperature.pkl", 'rb'))
    temp = imresize(temp, (IMAGE_HEIGHT, IMAGE_WIDTH))
    plt.imshow(temp)
    plt.show()
    temp = np.digitize(temp, [0, 90, 130, 255])-1
    temp[temp > 2] = 2
    plt.imshow(temp)
    plt.show()

    biomes = [
        [BARE, TUNDRA, TAIGA, SNOW, OCEAN],
        [GRASSLAND, WOODLAND, TEMPERATE_FOREST, TEMPERATE_RAINFOREST, OCEAN],
        [DESERT, SAVANNAH, TROPICAL_SEASONAL_FOREST, TROPICAL_RAINFOREST, OCEAN]
        ]
    img = np.zeros((IMAGE_HEIGHT, IMAGE_WIDTH))
    for i in range(IMAGE_HEIGHT):
        for j in range(IMAGE_WIDTH):
            img[i,j] = biomes[temp[i,j]][moisture[i,j]]
    elevation = pickle.load(open(data_path+"elevation.pkl", 'rb'))
    img[elevation == 0] = OCEAN
    plt.imshow(img)
    plt.show()
    pickle.dump(img, open(data_path+"biomes.pkl", 'wb'))
Exemplo n.º 15
0
    def setUpClass(cls):
        img = imread(get_path('AS15-M-0298_SML.png'), flatten=True)
        img_coord = (482.09783936, 652.40679932)

        cls.template = sp.clip_roi(img, img_coord, 5)
        cls.template = rotate(cls.template, 90)
        cls.template = imresize(cls.template, 1.)

        cls.search = sp.clip_roi(img, img_coord, 21)
        cls.search = rotate(cls.search, 0)
        cls.search = imresize(cls.search, 1.)

        cls.offset = (1, 1)

        cls.offset_template = sp.clip_roi(img, np.add(img_coord, cls.offset), 5)
        cls.offset_template = rotate(cls.offset_template, 0)
        cls.offset_template = imresize(cls.offset_template, 1.)

        cls.search_center = [math.floor(cls.search.shape[0]/2),
                             math.floor(cls.search.shape[1]/2)]

        cls.upsampling = 10
        cls.alpha = math.pi/2
        cls.cifi_thresh = 90
        cls.rafi_thresh = 90
        cls.tefi_thresh = 100
        cls.use_percentile = True
        cls.radii = list(range(1, 3))

        cls.cifi_number_of_warnings = 2
        cls.rafi_number_of_warnings = 2
Exemplo n.º 16
0
def main():
  scale = 100 #lower this value to make the correlation go faster
  image = imread2("./waldo.png")
  image = imresize(image, scale)
  template = imread2("./template.png")
  template = imresize(template, scale)
  # make grayscale
  image_gray = grayscale(image)
  template = grayscale(template)
  template_w, template_h = template.shape

  gradients_image = gradients(image_gray)
  gradients_image /= np.linalg.norm(gradients_image.flatten())
  gradients_template = gradients(template)
  gradients_template /= np.sum(gradients_template)

  # use cross correlation
  convolved_gradients = correlate2d(gradients_image, gradients_template, mode="same")

  position = np.argmax(convolved_gradients)
  position_x, position_y = np.unravel_index(position, gradients_image.shape)

  #put a big red dot in the middle of where we found our maxima
  dot_rad = 8
  image[position_x-dot_rad:position_x+dot_rad,position_y-dot_rad:position_y+dot_rad,0] = 255
  image[position_x-dot_rad:position_x+dot_rad,position_y-dot_rad:position_y+dot_rad,1:2] = 0

  imsave("./image_matched.png", image )
Exemplo n.º 17
0
def smooth_iter_align(b, g, r, num_downsamples):
    """ A slower alternative (and predecessor) to big_iter_align. Return aligned 
    (i.e., appropriately zero-padded) versions of b, g, and 
    r. Analogously: translate b and g with respect to r such that they come 
    closest to matching it completely. Use downsampling to iteratively 
    approximate alignment.
    """
    # Before aligning the colour-channels, pad them with bottom zero-rows where 
    # necessary to ensure uniform height.
    norm_heights = normalize_heights([b,g,r])
    b, g, r = norm_heights[0], norm_heights[1], norm_heights[2]
    
    # Each downsampling reduces image-size by one half. The smallest versions of
    # the images are approximately aligned first.
    for s in range(num_downsamples + 1)[::-1]:
        size = .5**s
        b_small, g_small, r_small = (imresize(b, size), imresize(g, size), 
                                    imresize(r, size))
        
        trans_range_small = (10 / (num_downsamples + 1)) * (2**s)
        g_r_small_translations = get_translations(b_small, g_small, r_small, 
                                                    trans_range_small)
        
        # Adjust transformation data for application to full-sized patches.
        g_r_translations = g_r_small_translations * (2**s)
        trans_range = trans_range_small * (2**s)
        b, g, r = align_channels(b, g, r, g_r_translations, trans_range)
        
        imsave('testing/'+str(s)+'.jpg', combine_channels(b,g,r))
        
    return b, g, r
    def reshape_images(cls, source_folder, target_folder, height=128, width=128,
                       extensions=('.jpg', '.jpeg', '.png')):
        """ copy images and reshape them"""

        # check source_folder and target_folder:
        cls.check_folder_existance(source_folder, throw_error_if_no_folder=True)
        cls.check_folder_existance(target_folder, display_msg=False)
        if source_folder[-1] == "/":
            source_folder = source_folder[:-1]
        if target_folder[-1] == "/":
            target_folder = target_folder[:-1]

        # read images and reshape:
        print("Resizing '", source_folder, "' images...")
        for filename in os.listdir(source_folder):
            if os.path.isdir(source_folder + '/' + filename):
                cls.reshape_images(source_folder + '/' + filename,
                                   target_folder + '/' + filename,
                                   height, width, extensions=extensions)
            else:
                if extensions == '' and os.path.splitext(filename)[1] == '':
                    copy2(source_folder + "/" + filename,
                          target_folder + "/" + filename)
                    image = ndimage.imread(target_folder + "/" + filename, mode="RGB")
                    image_resized = misc.imresize(image, (height, width))
                    misc.imsave(target_folder + "/" + filename, image_resized)
                else:
                    for extension in extensions:
                        if filename.endswith(extension):
                            copy2(source_folder + "/" + filename,
                                  target_folder + "/" + filename)
                            image = ndimage.imread(target_folder + "/" + filename, mode="RGB")
                            image_resized = misc.imresize(image, (height, width))
                            misc.imsave(target_folder + "/" + filename, image_resized)
Exemplo n.º 19
0
def images2(imgsize=320):
    con = MySQLdb.connect("127.0.0.1", "fsinb", mysql_password, "fsinb")
    cur = con.cursor()
    cur.execute("SET NAMES 'utf8';")
    cur.execute(
        "SELECT DISTINCT w.objectid, p.filename "
        "FROM weights w "
        "LEFT JOIN pictures p ON (p.objectid=w.objectid) "
        "WHERE suitability=2 "
        "ORDER BY w.objectid"
    )

    if not os.path.exists("images"):
        os.makedirs("images")
    for x in cur.fetchall():
        impath = "images/%s" % x[0]
        if not os.path.exists(impath):
            os.makedirs(impath)
        im = imread(path_images + x[1])
        if not os.path.isfile("images/%ssmall.jpg" % x[0]):
            im2 = imresize(im, (120, 120))
            imsave("images/%ssmall.jpg" % x[0], im2)
        if not os.path.isfile("images/%sbig.jpg" % x[0]):
            im2 = imresize(im, (600, 600))
            imsave("images/%sbig.jpg" % x[0], im2)
        im = imresize(im, float(imgsize) / max(im.shape))
        i = 1
        while os.path.isfile("%s/%s%s.jpg" % (impath, x[0], i)):
            i += 1
        imsave("%s/%s%s.jpg" % (impath, x[0], i), im)
    cur.close()
    con.close()
    def transform(self, img, lbl):
        """transform

        :param img:
        :param lbl:
        """
        img = img[:, :, ::-1]
        img = img.astype(np.float64)
        img -= self.mean
        img = m.imresize(img, (self.img_size[0], self.img_size[1]))
        # Resize scales images from 0 to 255, thus we need
        # to divide by 255.0
        img = img.astype(float) / 255.0
        # NHWC -> NCWH
        img = img.transpose(2, 0, 1)
        
        classes = np.unique(lbl)
        lbl = lbl.astype(float)
        lbl = m.imresize(lbl, (self.img_size[0], self.img_size[1]), 'nearest', mode='F')
        lbl = lbl.astype(int)
        

        if not np.all(classes == np.unique(lbl)):
            print("WARN: resizing labels yielded fewer classes")

        if not np.all(np.unique(lbl[lbl!=self.ignore_index]) < self.n_classes):
            print('after det', classes,  np.unique(lbl))
            raise ValueError("Segmentation map contained invalid class values")

        img = torch.from_numpy(img).float()
        lbl = torch.from_numpy(lbl).long()

        return img, lbl
Exemplo n.º 21
0
def crop_im(im, bbox, **kwargs):
	'''
		The bounding box is assumed to be in the form (xmin, ymin, xmax, ymax)
		kwargs:
			imSz: Size of the image required
	'''
	cropType = kwargs['cropType']
	imSz  = kwargs['imSz']
	x1,y1,x2,y2 = bbox
	x1 = max(0, x1)
	y1 = max(0, y1)
	x2 = min(im.shape[1], x2)
	y2 = min(im.shape[0], y2)
	if cropType=='resize':
		imBox = im[y1:y2, x1:x2]
		imBox = scm.imresize(imBox, (imSz, imSz))
	if cropType=='contPad':
		contPad = kwargs['contPad']
		x1 = max(0, x1 - contPad)
		y1 = max(0, y1 - contPad)
		x2 = min(im.shape[1], x2 + contPad)
		y2 = min(im.shape[0], y2 + contPad)	
		imBox = im[y1:y2, x1:x2]
		imBox = scm.imresize(imBox, (imSz, imSz))
	else:
		raise Exception('Unrecognized crop type')
	return imBox		
Exemplo n.º 22
0
def save_frame(depthName=None, depth=None, colorName=None, color=None, userName=None, users=None, maskName=None, mask=None):

	''' Depth '''
	if depthName is not None:
		im = Image.fromarray(depth.astype(np.int32), 'I')
		im = im.resize([320,240])
		im.save(depthName)
		
	'''Mask'''
	if mask is not None and maskName is not None:
			mask = sm.imresize(mask, [240,320], 'nearest')
			sm.imsave(maskName, mask)

	'''Color'''
	if colorName is not None:
		color = sm.imresize(color, [240,320,3], 'nearest')
		# sm.imsave(colorName, color)

	'''User'''
	if userName is not None:
		usersOut = {}
		for k in users.keys():
				usersOut[k] = users[k].toDict()

		with open(userName, 'wb') as outfile:
				pickle.dump(usersOut, outfile, protocol=pickle.HIGHEST_PROTOCOL)
Exemplo n.º 23
0
def recon_gridrec(im1, im2, angles, oversampling):
	"""Reconstruct two sinograms (of the same CT scan) with direct Fourier algorithm.

	Parameters
	----------
	im1 : array_like
		Sinogram image data as numpy array.

	im2 : array_like
		Sinogram image data as numpy array.

	angles : double
		Value in radians representing the number of angles of the input sinogram.

	oversampling : double
		Input sinogram is rescaled to increase the sampling of the Fourier space and
		avoid artifacts. Suggested value in the range [1.2,1.6]. 
	
	"""
	v_angles = linspace(0, angles, im1.shape[0], False).astype(float32)

	# Call C-code for gridrec with oversampling:
	[out1, out2] = paralrecon(im1, im2, v_angles, float(oversampling))  

	# Rescale output (if oversampling used):
	out1 = imresize(out1, (im1.shape[1],im1.shape[1]), interp='bicubic', mode='F')
	out2 = imresize(out2, (im2.shape[1],im2.shape[1]), interp='bicubic', mode='F')

	# Rotate images 90 degrees towards the left:
	out1 = rot90(out1)
	out2 = rot90(out2)

	# Return output:  
	return [out1.astype(float32), out2.astype(float32)]
Exemplo n.º 24
0
def get_image_lena(query):
    """
    get the image
    :param query:
    :type query:
    :return:
    :rtype:
    """

    args = query.split(sep='_')

    if args[2] == 'grey':
        lena = ndimage.imread('lena.jpg', mode='L')
    elif args[2] == 'rgb':
        lena = ndimage.imread('lena.jpg', mode='RGB')
    else:
        raise ValueError('Invalid color type. Allowed rgb or grey')

    if args[3] == 'small':
        lena = misc.imresize(lena, (2048, 2048), interp='bilinear')
    elif args[3] == 'large':
        lena = misc.imresize(lena, (4096, 4096), interp='bilinear')
    else:
        raise ValueError('Invalid size. Allowed small or large')

    if args[4] == 'uint8':
        lena = lena.astype(np.uint8)
    elif args[4] == 'float':
        lena = lena.astype(np.float)
    else:
        raise ValueError('Invalid size. Allowed uint8 or float')

    return lena
Exemplo n.º 25
0
def cropImage(im):
    im2 = np.dstack(im).astype(np.uint8)
    # return centered 128x128 from original 250x250 (40% of area)
    newim = im2[61:189, 61:189]
    sized1 = imresize(newim[:,:,0:3], (feature_width, feature_height), interp="bicubic", mode="RGB")
    sized2 = imresize(newim[:,:,3:6], (feature_width, feature_height), interp="bicubic", mode="RGB")
    return np.asarray([sized1[:,:,0], sized1[:,:,1], sized1[:,:,2], sized2[:,:,0], sized2[:,:,1], sized2[:,:,2]])
Exemplo n.º 26
0
def depth_cluster_2(depth_map, rgb_downsampled, orb_depth, downsample=5, depth_ratio=1.0, rgb_ratio=1.0, position_ratio=0.1, remove_noise=False):
  depth_downsampled = imresize(depth_map, (depth_map.shape[0] / downsample, depth_map.shape[1] / downsample), interp='bicubic')
  rgb_downsampled = imresize(rgb_downsampled, (rgb_downsampled.shape[0] / downsample, rgb_downsampled.shape[1] / downsample), interp='bicubic')
  rgb_r = rgb_downsampled[:, :, 0].reshape((rgb_downsampled.shape[0] * rgb_downsampled.shape[1],))
  rgb_g = rgb_downsampled[:, :, 1].reshape((rgb_downsampled.shape[0] * rgb_downsampled.shape[1],))
  rgb_b = rgb_downsampled[:, :, 2].reshape((rgb_downsampled.shape[0] * rgb_downsampled.shape[1],))
  depth_flatten = depth_downsampled.reshape((depth_downsampled.size,))
  x = np.arange(0, depth_downsampled.shape[1], 1).flatten()
  y = np.arange(0, depth_downsampled.shape[0], 1).flatten()
  xx, yy = np.meshgrid(x, y, sparse=False)
  xx = xx.reshape((xx.size))
  yy = yy.reshape((yy.size))
  fit_data = np.stack((depth_flatten * depth_ratio, xx * position_ratio, yy * position_ratio,
                       rgb_r * rgb_ratio, rgb_g * rgb_ratio, rgb_b * rgb_ratio), axis=-1)
  xx_init, yy_init = np.where(orb_depth > 0.0)
  xx_init /= downsample
  yy_init /= downsample
  depth_init = depth_downsampled[(xx_init, yy_init)]
  rgb_init = rgb_downsampled[(xx_init, yy_init)]
  xx_init = xx_init.reshape((xx_init.size,))
  yy_init = yy_init.reshape((yy_init.size,))
  fit_init = np.stack((depth_init * depth_ratio, xx_init * position_ratio, yy_init * position_ratio,
                       rgb_init[:, 0] * rgb_ratio, rgb_init[:, 1] * rgb_ratio, rgb_init[:, 2] * rgb_ratio), axis=-1)
  clt = KMeans(n_clusters=fit_init.shape[0], init=fit_init)
  clt.fit(fit_data)
  _result = clt.labels_.reshape(depth_downsampled.shape)
  if remove_noise:
    structure = np.ones((3, 3))
    labels = np.unique(clt.labels_)
    for label in labels:
      mask = (_result == label)
      eroded_mask = ndimage.binary_erosion(mask, structure)
      border = (mask != eroded_mask)
      _result[border] = 0
  return imresize(_result, depth_map.shape)
Exemplo n.º 27
0
 def create_mask(self, image, im, blob_list, destination_folder):
   mask_file = np.zeros(shape = (image.shape[0],image.shape[1]))
   if blob_list is not None:
     for bl in blob_list:
       x1 = int(bl[0] - bl[2])
       y1 = int(bl[1] - bl[2])
       x2 = int(bl[0] + bl[2])
       y2 = int(bl[1] + bl[2])
       x1 = np.max([x1, 0])
       y1 = np.max([y1, 0])
       x2 = np.min([x2, int(mask_file.shape[0])])
       y2 = np.min([y2, int(mask_file.shape[1])])
       image1 = image[x1:x2, y1:y2, :]
       im1_sh = image1.shape
       image1 /= 255
       image1_resized = imresize(image1, (128, 128))
       image1_transposed = np.asarray (image1_resized.transpose(2,0,1), dtype = 'float32')
       final_data = np.ndarray(shape = (1,3,128,128))
       final_data[0,:,:,:] = image1_transposed
       y_pred = self.model.predict(final_data, verbose = 0)
       y_pred = np.where(y_pred > 0.5, 1, 0)
       y_pred = np.reshape(y_pred, (64,64))
       y_pred = imresize(y_pred, im1_sh)
       mask_file[x1:x2, y1:y2] += y_pred
       mask_file = np.where(mask_file > 0, 255, 0)
   final_path = destination_folder + '/' + im[:-4] + '-mask.jpg'
   cv2.imwrite(final_path, mask_file)
Exemplo n.º 28
0
def load(data_len, standarize = True, shrink = True, seed = 48):
    import pandas as pd
    from scipy.misc import imread, imsave, imresize
    from sklearn.preprocessing import StandardScaler
    np.random.seed(seed)
    img_fnames = pd.read_csv("./lfw_files.txt").values.ravel()
    Xb = []
    yb = []

    for i in range(data_len):
        image = imread(np.random.choice(img_fnames))
        if shrink:
            size_x = 48
            size_y = 48
        else:
            size_x = image.shape[0]
            size_y = image.shape[1]
        if np.random.random() > 0.5:
            Xb.append(imresize(add_rect(image), (size_x,size_y,3)).swapaxes(0,2).swapaxes(1,2))
            yb.append(0)
        else:
            Xb.append(imresize(add_circle(image), (size_x,size_y,3)).swapaxes(0,2).swapaxes(1,2))
            yb.append(1)
    Xb = np.array(Xb)
    if standarize:
        Xb = np.array(Xb, np.float32)
        n,c,x,y = Xb.shape
        Xb = Xb.reshape((n,x*y*c))
        sc = StandardScaler(with_mean=True, with_std=True)
        Xb = sc.fit_transform(Xb)
        Xb = Xb.reshape((n,c,x,y))
    return Xb, np.array(yb, dtype=np.int32)
def fastguidedfilter(I, p, r, eps, s):
    I_sub = imresize(I, 1.0/s, 'nearest')
    p_sub = imresize(p, 1.0/s, 'nearest')
    r_sub = r / s

    h_sub, w_sub = I_sub.shape[:2]
    N = gf.boxfilter(np.ones((h_sub, w_sub),np.float), r)
    mean_I = gf.boxfilter(I_sub, r_sub) / N
    mean_p = gf.boxfilter(I_sub, r_sub) / N
    mean_Ip = gf.boxfilter(I_sub * p_sub, r_sub) / N
    cov_Ip = mean_Ip - mean_I * mean_p

    mean_II = gf.boxfilter(I_sub * I_sub, r_sub) / N
    var_I = mean_II - mean_I * mean_I

    a = cov_Ip / (var_I + eps)
    b = mean_p - a * mean_I

    mean_a = gf.boxfilter(a, r_sub) / N
    mean_b = gf.boxfilter(b, r_sub) / N

    mean_a = imresize(mean_a, I.shape[:2], 'bilinear')
    mean_b = imresize(mean_b, I.shape[:2], 'bilinear')
    q = mean_a * I + mean_b

    return q
Exemplo n.º 30
0
def makeTestPair(paths, homography, collection, location=".", size=(250,250), scale = 1.0) :
    """ Given a pair of paths to two images and a homography between them,
        this function creates two crops and calculates a new homography.
        input: paths [strings] (paths to images)
               homography [numpy.ndarray] (3 by 3 array homography)
               collection [string] (The name of the testset)
               location [string] (The location (path) of the testset
               size [(int, int)] (The size of an image crop in pixels)
               scale [double] (The scale by which we resize the crops after they've been cropped)
        out:   nothing
    """
    
    # Get width and height
    width, height = size
    
    # Load images in black/white
    images = map(loadImage, paths)
    
    # Crop part of first image and part of second image:
    (top_o, left_o) = (random.randint(0, images[0].shape[0]-height), random.randint(0, images[0].shape[1]-width))
    (top_n, left_n) = (random.randint(0, images[1].shape[0]-height), random.randint(0, images[1].shape[1]-width))
    
    # Get two file names
    c_path = getRandPath("%s/%s/" % (location, collection))
    if not exists(dirname(c_path)) : makedirs(dirname(c_path))
        
    # Make sure we save as gray
    pylab.gray()
    
    im1 = images[0][top_o: top_o + height, left_o: left_o + width]
    im2 = images[1][top_n: top_n + height, left_n: left_n + width]
    im1_scaled = imresize(im1, size=float(scale), interp='bicubic')
    im2_scaled = imresize(im2, size=float(scale), interp='bicubic')
    pylab.imsave(c_path + "_1.jpg", im1_scaled)
    pylab.imsave(c_path + "_2.jpg", im2_scaled)
    
    # Homography for transpose
    T1 = numpy.identity(3)
    T1[0,2] = left_o
    T1[1,2] = top_o
    
    # Homography for transpose back
    T2 = numpy.identity(3)
    T2[0,2] = -1*left_n
    T2[1,2] = -1*top_n
    
    # Homography for scale
    Ts = numpy.identity(3)
    Ts[0,0] = scale
    Ts[1,1] = scale
    
    # Homography for scale back
    Tsinv = numpy.identity(3)
    Tsinv[0,0] = 1.0/scale
    Tsinv[1,1] = 1.0/scale
    
    # Combine homographyies and save
    hom = Ts.dot(T2).dot(homography).dot(T1).dot(Tsinv)
    hom = hom / hom[2,2]
    numpy.savetxt(c_path, hom)
Exemplo n.º 31
0
            logs.close()

            # Save visual results for several test images

            bokeh_crops = sess.run(bokeh_img,
                                   feed_dict={
                                       input_: visual_test_crops,
                                       target_: dslr_images
                                   })

            idx = 0
            for crop in bokeh_crops:
                if idx < 7:
                    before_after = np.hstack((np.float32(
                        misc.imresize(
                            np.reshape(visual_test_crops[idx, :, :, 0:3] * 255,
                                       [PATCH_HEIGHT, PATCH_WIDTH, 3]),
                            [TARGET_HEIGHT, TARGET_WIDTH])) / 255.0, crop,
                                              np.reshape(
                                                  visual_target_crops[idx], [
                                                      TARGET_HEIGHT,
                                                      TARGET_WIDTH,
                                                      TARGET_DEPTH
                                                  ])))
                    misc.imsave(
                        "results/pynet_img_" + str(idx) + "_level_" +
                        str(LEVEL) + "_iter_" + str(i) + ".jpg", before_after)
                idx += 1

            training_loss = 0.0

            # Saving the model that corresponds to the current iteration
Exemplo n.º 32
0
# Compare your output to ours; difference should be around e-8
print('Testing conv_forward_naive')
print('difference: ', rel_error(out, correct_out))

# In[33]:

from scipy.misc import imread, imresize

kitten, puppy = imread('kitten.jpg'), imread('puppy.jpg')
# kitten is wide, and puppy is already square
d = kitten.shape[1] - kitten.shape[0]
kitten_cropped = kitten[:, d // 2:-d // 2, :]

img_size = 200  # Make this smaller if it runs too slow
x = np.zeros((2, 3, img_size, img_size))
x[0, :, :, :] = imresize(puppy, (img_size, img_size)).transpose((2, 0, 1))
x[1, :, :, :] = imresize(kitten_cropped, (img_size, img_size)).transpose(
    (2, 0, 1))

# Set up a convolutional weights holding 2 filters, each 3x3
w = np.zeros((2, 3, 3, 3))

# The first filter converts the image to grayscale.
# Set up the red, green, and blue channels of the filter.
w[0, 0, :, :] = [[0, 0, 0], [0, 0.3, 0], [0, 0, 0]]
w[0, 1, :, :] = [[0, 0, 0], [0, 0.6, 0], [0, 0, 0]]
w[0, 2, :, :] = [[0, 0, 0], [0, 0.1, 0], [0, 0, 0]]

# Second filter detects horizontal edges in the blue channel.
w[1, 2, :, :] = [[1, 2, 1], [0, 0, 0], [-1, -2, -1]]
Exemplo n.º 33
0
def main():

    # user defined variables
    IMG_SIZE = 32
    BATCH_SIZE = 16
    if (len(sys.argv) > 0):
        print(len(sys.argv))
        DIRECTORY_PATH = str(sys.argv[1])
    else:
        DIRECTORY_PATH = "one"

    DATASET_DIR = '/home/grupo07/mcv/datasets/MIT_split'
    ACTIVATIONS = [
        'softmax', 'elu', 'selu', 'softplus', 'softsign', 'relu', 'tanh',
        'sigmoid', 'hard_sigmoid', 'exponential', 'linear'
    ]
    for ACTIVATION in ACTIVATIONS:
        RELATIVE_PATH = '/home/grupo07/week3/out/' + DIRECTORY_PATH + '/' + ACTIVATION + '/'

        for UNIT in range(7, 12):
            UNIT_POW = 2**UNIT

            if not os.path.exists(DATASET_DIR):
                print(
                    stylize(
                        'ERROR: dataset directory ' + DATASET_DIR +
                        ' do not exists!\n', fg('blue')))
                quit()

            print('Building MLP model...\n')

            # Build the Multi Layer Perceptron model
            model = Sequential()
            model.add(
                Reshape((IMG_SIZE * IMG_SIZE * 3, ),
                        input_shape=(IMG_SIZE, IMG_SIZE, 3),
                        name='first'))
            model.add(
                Dense(units=UNIT_POW, activation=ACTIVATION, name='second'))
            if (DIRECTORY_PATH == 'two'):
                model.add(Dense(units=UNIT_POW, activation=ACTIVATION))
            elif (DIRECTORY_PATH == 'three'):
                model.add(Dense(units=UNIT_POW, activation=ACTIVATION))
                model.add(Dense(units=UNIT_POW, activation=ACTIVATION))

            model.add(Dense(units=8, activation=ACTIVATION))
            model.compile(loss='categorical_crossentropy',
                          optimizer='sgd',
                          metrics=['accuracy'])

            print(model.summary())
            UNIT_POW = str(UNIT_POW)
            MODEL_FNAME = RELATIVE_PATH + ACTIVATION + '_' + UNIT_POW + '_mlp.h5'
            plot_model(model,
                       to_file=(RELATIVE_PATH + ACTIVATION + '_' + UNIT_POW +
                                'modelMLP.png'),
                       show_shapes=True,
                       show_layer_names=True)

            print('Done!\n')

            if os.path.exists(MODEL_FNAME):
                print('WARNING: model file ' + MODEL_FNAME +
                      ' exists and will be overwritten!\n')

            print('Start training...\n')

            # this is the dataset configuration we will use for training
            # only rescaling
            train_datagen = ImageDataGenerator(rescale=1. / 255,
                                               horizontal_flip=True)

            # this is the dataset configuration we will use for testing:
            # only rescaling
            test_datagen = ImageDataGenerator(rescale=1. / 255)

            # this is a generator that will read pictures found in
            # subfolers of 'data/train', and indefinitely generate
            # batches of augmented image data
            train_generator = train_datagen.flow_from_directory(
                DATASET_DIR + '/train',  # this is the target directory
                target_size=(
                    IMG_SIZE, IMG_SIZE
                ),  # all images will be resized to IMG_SIZExIMG_SIZE
                batch_size=BATCH_SIZE,
                classes=[
                    'coast', 'forest', 'highway', 'inside_city', 'mountain',
                    'Opencountry', 'street', 'tallbuilding'
                ],
                class_mode='categorical'
            )  # since we use binary_crossentropy loss, we need categorical labels

            # this is a similar generator, for validation data
            validation_generator = test_datagen.flow_from_directory(
                DATASET_DIR + '/test',
                target_size=(IMG_SIZE, IMG_SIZE),
                batch_size=BATCH_SIZE,
                classes=[
                    'coast', 'forest', 'highway', 'inside_city', 'mountain',
                    'Opencountry', 'street', 'tallbuilding'
                ],
                class_mode='categorical')

            history = model.fit_generator(train_generator,
                                          steps_per_epoch=1881 // BATCH_SIZE,
                                          epochs=50,
                                          validation_data=validation_generator,
                                          validation_steps=807 // BATCH_SIZE)

            print('Done!\n')
            print('Saving the model into ' + MODEL_FNAME + ' \n')
            model.save_weights(
                MODEL_FNAME
            )  # always save your weights after training or during training
            print('Done!\n')

            # summarize history for accuracy
            plt.plot(history.history['acc'])
            plt.plot(history.history['val_acc'])
            plt.title('model accuracy')
            plt.ylabel('accuracy')
            plt.xlabel('epoch')
            plt.legend(['train', 'validation'], loc='upper left')
            plt.savefig(RELATIVE_PATH + ACTIVATION + '_' + UNIT_POW +
                        '_accuracy.jpg')
            plt.close()
            # summarize history for loss
            plt.plot(history.history['loss'])
            plt.plot(history.history['val_loss'])
            plt.title('model loss')
            plt.ylabel('loss')
            plt.xlabel('epoch')
            plt.legend(['train', 'validation'], loc='upper left')
            plt.savefig(RELATIVE_PATH + ACTIVATION + '_' + UNIT_POW +
                        '_loss.jpg')

            # to get the output of a given layer
            # crop the model up to a certain layer
            model_layer = Model(inputs=model.input,
                                outputs=model.get_layer('second').output)

            # get the features from images
            directory = DATASET_DIR + '/test/coast'
            x = np.asarray(
                Image.open(os.path.join(directory,
                                        os.listdir(directory)[0])))
            x = np.expand_dims(imresize(x, (IMG_SIZE, IMG_SIZE, 3)), axis=0)
            print('prediction for image ' +
                  os.path.join(directory,
                               os.listdir(directory)[0]))
            features = model_layer.predict(x / 255.0)
            print(features)
            print('Done!')
Exemplo n.º 34
0
def preprocess_image(img):
    img = imresize(img[60:150, :, :], (200, 66))
    return normalize(img)
Exemplo n.º 35
0
def load_pascal(data_dir, split='train'):
    """
    Function to read images from PASCAL data folder.
    Args:
        data_dir (str): Path to the VOC2007 directory.
        split (str): train/val/trainval split to use.
    Returns:
        images (np.ndarray): Return a np.float32 array of
            shape (N, H, W, 3), where H, W are 224px each,
            and each image is in RGB format.
        labels (np.ndarray): An array of shape (N, 20) of
            type np.int32, with 0s and 1s; 1s for classes that
            are active in that image.
    """
    # Wrote this function
    img_dir = data_dir + 'JPEGImages/'
    label_dir = data_dir + 'ImageSets/Main/'

    # read images
    label_path = label_dir + 'aeroplane_' + split + '.txt'
    file = open(label_path, 'r')
    lines = file.readlines()
    img_num = len(lines)
    first_flag = True

    for line in lines:
        line = line.split(' ')[0]
        img_name = img_dir + line + '.jpg'
        img = sci.imread(img_name)
        img = sci.imresize(img, (256, 256, 3))
        img = np.expand_dims(img, axis=0)
        if first_flag == True:
            img_list = img
            first_flag = False
        else:
            img_list = np.concatenate((img_list, img), axis=0)
    file.close()
    print("finish loading images")
    print(img_list.shape)

    # read labels
    label_list = np.zeros((img_num, 20))
    weight_list = np.zeros((img_num, 20))
    cls_pos = 0
    for class_name in CLASS_NAMES:
        img_pos = 0
        label_path = label_dir + class_name + '_' + split + '.txt'
        # load images
        file = open(label_path, 'r')
        lines = file.readlines()
        for line in lines:
            label = line.split()[1]
            label = int(label)
            if label == 1:
                label_list[img_pos, cls_pos] = 1
                weight_list[img_pos, cls_pos] = 1
            elif label == 0:
                label_list[img_pos, cls_pos] = 1
            else:
                weight_list[img_pos, cls_pos] = 1
            img_pos += 1
        cls_pos += 1
        file.close()
    print("finish loading label")

    img_list = img_list.astype(np.float32)
    label_list = label_list.astype(np.int32)
    weight_list = weight_list.astype(np.int32)
    return img_list, label_list, weight_list
    def collect_data(self):
        output_dir = os.path.expanduser(self.output_datadir)
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)

        dataset = facenet.get_dataset(self.input_datadir)
        with tf.Graph().as_default():
            gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)
            sess = tf.Session(config=tf.ConfigProto(
                gpu_options=gpu_options, log_device_placement=False))
            with sess.as_default():
                pnet, rnet, onet = detect_face.create_mtcnn(sess, './npy')

        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)

        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: %s" % image_path)
                    if not os.path.exists(output_filename):
                        try:
                            img = misc.imread(image_path)
                        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]

                            bounding_boxes, _ = detect_face.detect_face(
                                img, minsize, pnet, rnet, onet, threshold,
                                factor)
                            nrof_faces = bounding_boxes.shape[0]
                            print('No of 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:
                                    continue
                            else:
                                print('Unable to align "%s"' % image_path)
                                text_file.write('%s\n' % (output_filename))

        return (nrof_images_total, nrof_successfully_aligned)
Exemplo n.º 37
0
def main(argv):
    parser = argparse.ArgumentParser(
        'Collect data from images using text files')

    parser.add_argument("data_location", help="directory of image data")
    parser.add_argument("text", help="text file containing list of images")
    parser.add_argument("--train",
                        help="For training data",
                        default=False,
                        action="store_true")
    parser.add_argument("--test",
                        help="For testing data",
                        default=False,
                        action="store_true")
    parser.add_argument("--validation",
                        help="For validation data",
                        default=False,
                        action="store_true")
    parser.add_argument("--random",
                        help="Distribute whole data randomly",
                        default=False,
                        action="store_true")

    args = parser.parse_args()

    f = open(args.text, 'r')

    X = []
    y = []

    for i in f.readlines():
        im = imresize(imread(os.path.join(args.data_location,
                                          i.split()[0])),
                      (224, 224)).astype(np.float32)
        if im.ndim == 2:
            im = gray2rgb(im)

        im = im.transpose(2, 0, 1)  #(channel, width, height)
        cls = i.split()[1]

        X.append(im)
        y.append(cls)
        print("currnet no. of images: {}".format(len(X)))

    X = np.array(X)
    y = np.array(y)

    if args.train:
        pickle.dump((X, y), open("train_data.p", "wb"))
    elif args.test:
        pickle.dump((X, y), open("test_data.p", "wb"))
    elif args.validation:
        pickle.dump((X, y), open("valid_data.p", "wb"))
    elif args.random:
        indx = dice(X.shape[0])  #select train_ratio, test_ratio, valide_ratio
        pickle.dump(((X[indx == 0][:][:], y[indx == 0]),
                     (X[indx == 1][:][:], y[indx == 1]),
                     (X[indx == 2][:][:], y[indx == 2])),
                    open("all_data.p", "wb"))
    else:
        assert 1, 'Choose mode'
Exemplo n.º 38
0
def upsample(im, orig_size):
    return misc.imresize(im, orig_size)
Exemplo n.º 39
0
def preprocessing(img_rows, img_cols, data_path):
    # Define data path
    data_dir_list = os.listdir(
        data_path
    )  #MacOS creates automatically '.DS_Store' file in each folder
    if data_dir_list[0] == '.DS_Store':
        data_dir_list = os.listdir(data_path)[1:]
    print(data_dir_list)

    #list of all the images
    img_data_list = []

    #total number of images
    num_samples = 0

    #image preprocessing
    for dataset in data_dir_list:
        img_list = os.listdir(data_path + '\\' + dataset)
        if img_list[0] == '.DS_Store':
            img_list = os.listdir(data_path + '\\' + dataset)[1:]
        num_samples += len(img_list)
        print('Loaded the images of dataset-' + '{}\n'.format(dataset))

        for img in img_list:
            input_img = imread(data_path + '\\' + dataset + '\\' + img,
                               mode=img_mode)
            input_img_resize = imresize(input_img, (img_rows, img_cols))
            img_data_list.append(input_img_resize)

    print("The total number of images: " + str(num_samples))

    #array of all the images
    img_data = np.array(img_data_list, dtype='float32')
    print(img_data.shape)
    img_data = img_data.astype('float32')
    img_data /= 255
    print(img_data.shape)

    if num_channel == 1:
        if K.image_dim_ordering() == 'th':
            img_data = np.expand_dims(img_data, axis=1)
            print(img_data.shape)
        else:
            img_data = np.expand_dims(img_data, axis=4)
            print(img_data.shape)

    else:
        if K.image_dim_ordering() == 'th':
            img_data = np.rollaxis(img_data, 3, 1)
            print(img_data.shape)

    # create the array of labels
    label = np.ones((num_samples, ), dtype=int)
    count1 = 0
    count2 = 0
    for dirs in data_dir_list:
        img_list = os.listdir(data_path + '\\' + dirs)[1:]
        count1, count2 = count2, count2 + len(img_list)
        label[count1:count2] = dirs[1:2]

    #list of labels
    poi_list = data_dir_list

    # convert class labels to on-hot encoding
    Y = np_utils.to_categorical(label, num_classes)

    #Shuffle the dataset
    x, y = shuffle(img_data, Y, random_state=2)

    # Split the dataset
    X_train, X_test, Y_train, Y_test = train_test_split(x,
                                                        y,
                                                        test_size=0.2,
                                                        random_state=2)
    return X_train, X_test, Y_train, Y_test, poi_list
Exemplo n.º 40
0
def atari_preprocessing(raw_image, width, height):
    gray_image = np.dot(raw_image[..., :3], [0.299, 0.587, 0.114]) / 255
    resized_image = imresize(gray_image, [width, height])
    return resized_image
images_and_prediction = list(zip(digits.images[n_sample // 2:], predictedY))
for index, [image, prediction] in enumerate(images_and_prediction[:5]):
    plt.subplot(2, 5, index + 6)
    plt.axis('on')
    plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
    plt.title("predict. : %i" % prediction)

print("original values : ", digits.target[n_sample // 2:(n_sample // 2) + 6])
# plt.show()

# Install pillow library

from scipy.misc import imread, imresize, bytescale
img = imread("seven2.jpeg")
print('image shape = ', img.shape)
img = imresize(img, (8, 8))
classifier = svm.SVC(gamma=0.001)
classifier.fit(imageData[:], digits.target[:])
img = img.astype(digits.images.dtype)
img = bytescale(img, high=16.0, low=0)
x_testData = []
for c in img:
    for r in c:
        x_testData.append(sum(r) / 3.0)
print("x_testData : \n", x_testData)
print("x_testData len : \n", len(x_testData))
x_testData = [x_testData]
print("Machine Output = ", classifier.predict(x_testData))
plt.show()
Exemplo n.º 42
0
def downsample(im, percent):
    """
    percent: percent of image 
    """
    return misc.imresize(im, percent)
Exemplo n.º 43
0
def s2l():

    #Randomly initialize critic,actor,target critic, target actor network  and replay buffer
    num_states = feature_size  #num_states = env.observation_space.shape[0]
    num_actions = env.action_space.shape[0]
    print("Number of States:", num_states)
    print("Number of Actions:", num_actions)

    agent = Saved_Policy(num_states, num_actions)
    exploration_noise = OUNoise(env.action_space.shape[0])
    counter = 0
    total_reward = 0

    print("Number of Rollouts per episode:", num_rollouts)
    print("Number of Steps per roll out:", steps)
    reward_st = np.array([0])  #saving reward
    reward_st_all = np.array([0])  #saving reward after every step
    eval_metric_st = np.array([0])  #saving evalutation metric

    activity_obj = Vid_Feature()
    demo_vid_array = demo_array_extractor(demo_folder)
    demo_features = activity_obj.feature_extractor(demo_vid_array)

    frame_obj = Frame_Feature()

    for episode in range(num_episodes):
        print("==== Starting episode no:", episode, "====", "\n")
        env.reset()  # Reset env in the begining of each episode
        env.render()
        obs_img = env.render(mode='rgb_array')  # Get the observation
        obs_img = np.array(misc.imresize(obs_img, [112, 112, 3]))
        observation = np.array(frame_obj.frame_feature_extractor(obs_img))
        observation = observation.reshape(-1)
        reward_per_episode = 0

        for t in range(num_rollouts):

            reward_per_rollout = 0
            vid_robo_ = []

            for i in range(steps):

                x = observation

                action = agent.get_action(np.reshape(x, [1, num_states]))
                noise = exploration_noise.noise()
                action = action[
                    0] + noise  #Select action according to current policy and exploration noise
                print('Action at episode-', episode, 'rollout-', t, 'step-', i,
                      " :", action)

                _, _, done, info = env.step(action)
                env.render()
                obs_robo_ = env.render(mode='rgb_array')  # Get the observation
                obs_robo = misc.imresize(obs_robo_, [112, 112, 3])
                vid_robo_.append(obs_robo)
                observation = np.array(
                    frame_obj.frame_feature_extractor(np.array(obs_robo)))
                observation = observation.reshape(-1)
                #pasue()

            # Printing eval_metric after every rollout
            eval_metric = np.array(env.get_eval())
            eval_metric = eval_metric.reshape(-1)
            print('Distance to goal:', eval_metric)
            eval_metric_st = np.append(eval_metric_st, eval_metric)
            np.savetxt('test_eval_metric_per_rollout.txt',
                       eval_metric_st,
                       newline="\n")
Exemplo n.º 44
0
    print('Top-3 accuracy:', score[2])

    # Computing the error rates
    error = 1 - score[1]
    top3error = 1 - score[2]

    print('Error: ', error)
    print('Top-3 error: ', top3error)

    #Testing the random image from internet
    from skimage import io
    #just insert any direct link to the image
    url = 'http://www.baviere-quebec.org/imperia/md/quebec/tourismus/neuschwanstein_bild.jpeg'
    test_image = io.imread(url)

    test_image = imresize(test_image, (img_rows, img_cols))
    test_image = np.array(test_image)
    test_image = test_image.astype('float32')
    test_image /= 255

    if num_channel == 1:
        if K.image_dim_ordering() == 'th':
            test_image = np.expand_dims(test_image, axis=0)
            test_image = np.expand_dims(test_image, axis=0)
        else:
            test_image = np.expand_dims(test_image, axis=3)
            test_image = np.expand_dims(test_image, axis=0)

    else:
        if K.image_dim_ordering() == 'th':
            test_image = np.rollaxis(test_image, 2, 0)
Exemplo n.º 45
0
    def agent_step(self, reward, observation):

        # Preproces
        tmp = np.bitwise_and(
            np.asarray(observation.intArray[128:]).reshape([210, 160]),
            0b0001111)  # Get Intensity from the observation
        obs_array = (spm.imresize(tmp, (110, 84)))[110 - 84 - 8:110 -
                                                   8, :]  # Scaling
        obs_processed = np.maximum(
            obs_array, self.last_observation)  # Take maximum from two frames

        # Compose State : 4-step sequential observation
        self.state = np.asanyarray(
            [self.state[1], self.state[2], self.state[3], obs_processed],
            dtype=np.uint8)
        state_ = cuda.to_gpu(
            np.asanyarray(self.state.reshape(1, 4, 84, 84), dtype=np.float32))

        # Exploration decays along the time sequence
        if self.policyFrozen is False:  # Learning ON/OFF
            if self.DQN.initial_exploration < self.time:
                self.epsilon -= 1.0 / 10**6
                if self.epsilon < 0.1:
                    self.epsilon = 0.1
                eps = self.epsilon
            else:  # Initial Exploation Phase
                print("Initial Exploration : %d/%d steps" %
                      (self.time, self.DQN.initial_exploration))
                eps = 1.0
        else:  # Evaluation
            print("Policy is Frozen")
            eps = 0.05

        # Generate an Action by e-greedy action selection
        returnAction = Action()
        action, Q_now = self.DQN.e_greedy(state_, eps)
        returnAction.intArray = [action]

        # Learning Phase
        if self.policyFrozen is False:  # Learning ON/OFF
            self.DQN.stockExperience(self.time, self.last_state,
                                     self.lastAction.intArray[0], reward,
                                     self.state, False)
            self.DQN.experienceReplay(self.time)

        # Target model update
        if self.DQN.initial_exploration < self.time and np.mod(
                self.time, self.DQN.target_model_update_freq) == 0:
            print("########### MODEL UPDATED ######################")
            self.DQN.target_model_update()

        # Simple text based visualization
        print(
            ' Time Step %d /   ACTION  %d  /   REWARD %.1f   / EPSILON  %.6f  /   Q_max  %3f'
            % (self.time, self.DQN.action_to_index(action), np.sign(reward),
               eps, np.max(Q_now.get())))

        # Updates for next step
        self.last_observation = obs_array

        if self.policyFrozen is False:
            self.lastAction = copy.deepcopy(returnAction)
            self.last_state = self.state.copy()
            self.time += 1

        return returnAction
Exemplo n.º 46
0
def test():
    data_path = os.path.join(os.pardir, 'test_data', 'example_data_np_array.npy')
    images = np.load(data_path)
    num_images = images.shape[2]
    bandlimit_ratio = 1.0
    truncation_parameter = 1
    resolutions = [16, 32, 64]
    images_multiplier = 100
    n = images_multiplier * num_images

    for resolution in resolutions:

        # testing with odd grid
        scaled_images = np.zeros((2 * resolution + 1, 2 * resolution + 1, num_images))
        for j in range(num_images):
            scaled_images[:, :, j] = imresize(images[:, :, j], (2 * resolution + 1, 2 * resolution + 1))
        scaled_images = np.repeat(scaled_images, images_multiplier, axis=2)

        print("testing images of size {}".format(scaled_images.shape[0]))
        tic1 = time.clock()
        converter = Converter(scaled_images.shape[0], truncation_parameter, beta=bandlimit_ratio)
        tic2 = time.clock()
        converter.init_direct()
        tic3 = time.clock()
        print("finished initializing the model in {} sec, the PSWF2D took {} seconds".format(tic3 - tic1, tic2 - tic1))

        # test if coefficients are the same
        tic = time.clock()
        coefficients = converter.direct_forward(scaled_images)
        toc = time.clock()
        t = toc - tic
        tpi = t/n
        print("finished forwarding {} images in {} seconds, average of {} seconds per image".format(n, t, tpi))

        # test reconstruction error
        tic = time.clock()
        reconstructed_images = converter.direct_backward(coefficients)
        toc = time.clock()
        t = toc - tic
        tpi = t / n
        print("finished backward of {} images in {} seconds, average of {} seconds per image".format(n, t, tpi))

        x_1d_grid = range(-resolution, resolution + 1)
        x_2d_grid, y_2d_grid = np.meshgrid(x_1d_grid, x_1d_grid)
        r_2d_grid = np.sqrt(np.square(x_2d_grid) + np.square(y_2d_grid))
        points_inside_the_circle = r_2d_grid <= resolution

        err = reconstructed_images - scaled_images
        e = np.mean(np.square(np.absolute(err)), axis=2)
        e = np.sum(e[points_inside_the_circle])

        p = np.mean(np.square(np.absolute(scaled_images)), axis=2)
        p = np.sum(p[points_inside_the_circle])

        print("odd images with resolution {} direct coefficients reconstructed error: {}\n".format(resolution, e / p))

        # testing with even grid
        scaled_images = np.zeros((2 * resolution, 2 * resolution, num_images))
        for j in range(num_images):
            scaled_images[:, :, j] = imresize(images[:, :, j], (2 * resolution, 2 * resolution))
        scaled_images = np.repeat(scaled_images, images_multiplier, axis=2)

        print("testing images of size {}".format(scaled_images.shape[0]))
        tic1 = time.clock()
        converter = Converter(scaled_images.shape[0], truncation_parameter, beta=bandlimit_ratio)
        tic2 = time.clock()
        converter.init_direct()
        tic3 = time.clock()
        print("finished initializing the model in {} sec, the PSWF2D took {} seconds".format(tic3 - tic1, tic2 - tic1))
        # test if coefficients are the same
        tic = time.clock()
        coefficients = converter.direct_forward(scaled_images)
        toc = time.clock()
        t = toc - tic
        tpi = t/n
        print("finished forwarding {} images in {} seconds, average of {} seconds per image".format(n, t, tpi))

        # test reconstruction error
        tic = time.clock()
        reconstructed_images = converter.direct_backward(coefficients)
        toc = time.clock()
        t = toc - tic
        tpi = t / n
        print("finished backward of {} images in {} seconds, average of {} seconds per image".format(n, t, tpi))

        x_1d_grid = range(-resolution, resolution)
        x_2d_grid, y_2d_grid = np.meshgrid(x_1d_grid, x_1d_grid)
        r_2d_grid = np.sqrt(np.square(x_2d_grid) + np.square(y_2d_grid))
        points_inside_the_circle = r_2d_grid <= resolution

        err = reconstructed_images - scaled_images
        e = np.mean(np.square(np.absolute(err)), axis=2)
        e = np.sum(e[points_inside_the_circle])

        p = np.mean(np.square(np.absolute(scaled_images)), axis=2)
        p = np.sum(p[points_inside_the_circle])

        print("even images with resolution {} direct coefficients reconstructed error: {}\n".format(resolution, e / p))
Exemplo n.º 47
0
def test(args):
    model_file_name = os.path.split(args.model_path)[1]
    model_name = model_file_name[:model_file_name.find('_')] + '_two_heads'

    # Setup image
    print("Read Input Image from : {}".format(args.img_path))
    # img = misc.imread(args.img_path)
    img = cv2.imread(args.img_path)

    data_loader = get_loader(args.dataset)
    data_path = get_data_path(args.dataset)
    loader = data_loader(data_path, is_transform=True, img_norm=args.img_norm)
    n_classes = loader.n_classes

    if ASPECT_AWARE_SCALING:
        im_shape = img.shape
        im_size_min = np.min(im_shape[0:2])
        im_size_max = np.max(im_shape[0:2])
        im_scale = float(TARGET_SIZE) / float(im_size_min)
        # Prevent the biggest axis from being more than MAX_SIZE
        if np.round(im_scale * im_size_max) > MAX_SIZE:
            im_scale = float(MAX_SIZE) / float(im_size_max)
        resized_img = cv2.resize(img,
                                 None,
                                 None,
                                 fx=im_scale,
                                 fy=im_scale,
                                 interpolation=cv2.INTER_CUBIC)
    else:
        resized_img = misc.imresize(img,
                                    (loader.img_size[0], loader.img_size[1]),
                                    interp='bicubic')

    orig_size = img.shape[:-1]
    if model_name in ['pspnet', 'icnet', 'icnetBN']:
        if ASPECT_AWARE_SCALING:
            # Make sure size attributes are even numbers
            out_shape = im_shape[0:2] * im_scale
            out_shape = out_shape // 2 * 2 + 1
            img = cv2.resize(img,
                             out_shape,
                             None,
                             interpolation=cv2.INTER_LINEAR)
        else:
            img = misc.imresize(
                img, (orig_size[0] // 2 * 2 + 1, orig_size[1] // 2 * 2 + 1)
            )  # uint8 with RGB mode, resize width and height which are odd numbers
    else:
        if ASPECT_AWARE_SCALING:
            img = cv2.resize(img,
                             None,
                             None,
                             fx=im_scale,
                             fy=im_scale,
                             interpolation=cv2.INTER_LINEAR)
        else:
            img = misc.imresize(img, (loader.img_size[0], loader.img_size[1]))

    # img = img[:, :, ::-1] # RGB -> BRG
    img = img.astype(np.float64)
    img -= loader.mean
    if args.img_norm:
        img = img.astype(float) / 255.0
    # NHWC -> NCHW
    img = img.transpose(2, 0, 1)
    img = np.expand_dims(img, 0)
    img = torch.from_numpy(img).float()

    # Setup Model
    model = get_model(model_name, n_classes, version=args.dataset)
    state = convert_state_dict(torch.load(args.model_path)['model_state'])
    model.load_state_dict(state)
    model.eval()

    with torch.no_grad():
        if torch.cuda.is_available():
            model.cuda(0)
            images = Variable(img.cuda(0))
        else:
            images = Variable(img)

        output_first_head, output_second_head = model(images)
        #outputs = F.softmax(outputs, dim=1)

    if args.dcrf:
        for idx, out in enumerate([output_first_head, output_second_head]):
            unary = out.data.cpu().numpy()
            unary = np.squeeze(unary, 0)
            unary = -np.log(unary)
            unary = unary.transpose(2, 1, 0)
            w, h, c = unary.shape
            unary = unary.transpose(2, 0, 1).reshape(loader.n_classes, -1)
            unary = np.ascontiguousarray(unary)

            resized_img = np.ascontiguousarray(resized_img)

            d = dcrf.DenseCRF2D(w, h, loader.n_classes)
            d.setUnaryEnergy(unary)
            d.addPairwiseBilateral(sxy=5, srgb=3, rgbim=resized_img, compat=1)

            q = d.inference(50)
            mask = np.argmax(q, axis=0).reshape(w, h).transpose(1, 0)
            decoded_crf = loader.decode_segmap(np.array(mask, dtype=np.uint8))
            dcrf_path = args.out_path + 'out_drf_' + str(idx) + '.png'
            misc.imsave(dcrf_path, decoded_crf)
            print("Dense CRF Processed Mask Saved at: {}".format(dcrf_path))

    pred_first_head = np.squeeze(
        output_first_head.data.max(1)[1].cpu().numpy(), axis=0)
    pred_second_head = np.squeeze(
        output_second_head.data.max(1)[1].cpu().numpy(), axis=0)
    if model_name in ['pspnet', 'icnet', 'icnetBN']:
        pred_first_head = pred_first_head.astype(np.float32)
        pred_first_head = misc.imresize(
            pred_first_head, orig_size, 'nearest',
            mode='F')  # float32 with F mode, resize back to orig_size
        pred_second_head = pred_second_head.astype(np.float32)
        pred_second_head = misc.imresize(
            pred_second_head, orig_size, 'nearest',
            mode='F')  # float32 with F mode, resize back to orig_size

    decoded_first_head = loader.decode_segmap(pred_first_head)
    decoded_second_head = loader.decode_segmap(pred_second_head)
    print('Classes found (first head):', np.unique(pred_first_head),
          ' | Classes found (second head):', np.unique(pred_second_head))
    misc.imsave(args.out_path + 'out_0.png', decoded_first_head)
    misc.imsave(args.out_path + 'out_1.png', decoded_second_head)
    print("Segmentation Mask Saved at: {}".format(args.out_path))
Exemplo n.º 48
0
oshape = img_raw.shape
w_original, h_original = oshape[1], oshape[0]

max_w = (2048 * (int(quality))) / 20
max_h = (2048 * (int(quality))) / 20

hh, ww = h_original, w_original
if h_original > max_h:
    hh = max_h

if w_original > max_w:
    ww = max_w

w, h = int(ww), int(hh)
img = imresize(img_raw, (h, w))

t_scale = (float(w) / oshape[1], float(h) / oshape[0])

img3 = None


def segmentation_processing():
    global img3, proper_polylines, w, h

    GC = DGC.GraphCutter()
    my_options = {
        'file': './temp.tif',
        'image': None,
        'resize': True,
        'w': w,
Exemplo n.º 49
0
def compression(image):
    image = imresize(imread(image), (224, 224)) / 255.0 - 0.5
    return image
Exemplo n.º 50
0
            fc3w = tf.Variable(tf.truncated_normal([4096, 1000],
                                                   dtype=tf.float32,
                                                   stddev=1e-1),
                               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())
        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('amby2.png', 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])
Exemplo n.º 51
0
    def fetch(self, cropSz=None, procSz=None):
        seqLen = int(self.mnSeqLen_ + self.rand_.rand() *
                     (self.mxSeqLen_ - self.mnSeqLen_))
        self.seqLen_ = seqLen
        model, f, ballPos, walls = self.generate_model()
        force = np.zeros((2 * self.numBalls_, self.seqLen_)).astype(np.float32)
        position = np.zeros(
            (2 * self.numBalls_, self.seqLen_)).astype(np.float32)
        velocity = np.zeros(
            (2 * self.numBalls_, self.seqLen_)).astype(np.float32)
        imList = []
        imBalls = []
        for b in range(self.numBalls_):
            imBalls.append([])
            fb = f[b]
            st, en = 2 * b, 2 * b + 1
            force[st, 0], force[en, 0] = fb.x(), fb.y()
        #Previous position
        pPos = np.nan * np.zeros((self.numBalls_, 2))
        for i in range(self.seqLen_):
            model.step()
            im = model.generate_image()
            vx, vy = None, None
            for j in range(self.numBalls_):
                ballName = 'ball-%d' % j
                ball = model.get_object(ballName)
                pos = ball.get_position()
                position[2 * j, i] = pos.x()
                position[2 * j + 1, i] = pos.y()
                #Speed should not be predicted, instead we should just predict
                #delta in position. The difference between the two is critical
                #due to collisions.
                if not np.isnan(pPos[j][0]):
                    vx = pos.x() - pPos[j][0]
                    vy = pos.y() - pPos[j][1]
                pPos[j][0], pPos[j][1] = pos.x(), pos.y()
                xMid, yMid = round(pos.x()), round(pos.y())
                if cropSz is not None:
                    imBall = 255 * np.ones(
                        (cropSz, cropSz, 3)).astype(np.uint8)
                    #Cropping coordinates in the original image
                    x1, x2 = max(0, xMid - cropSz / 2.0), min(
                        self.xSz_, xMid + cropSz / 2.0)
                    y1, y2 = max(0, yMid - cropSz / 2.0), min(
                        self.ySz_, yMid + cropSz / 2.0)
                    #Coordinates in the cropped image centerd at the ball
                    imX1 = int(round(cropSz / 2.0 - (xMid - x1)))
                    imX2 = int(round(cropSz / 2.0 + (x2 - xMid)))
                    imY1 = int(round(cropSz / 2.0 - (yMid - y1)))
                    imY2 = int(round(cropSz / 2.0 + (y2 - yMid)))
                    x1, x2 = int(round(x1)), int(round(x2))
                    y1, y2 = int(round(y1)), int(round(y2))
                    imBall[imY1:imY2, imX1:imX2, :] = im[y1:y2, x1:x2, 0:3]
                    position[2 * j, i] = position[2 * j, i] - x1 + imX1
                    position[2 * j + 1, i] = position[2 * j + 1, i] - y1 + imY1

                    if procSz is not None:
                        posScale = float(procSz) / float(cropSz)
                        imBall = scm.imresize(imBall, (procSz, procSz))
                        position[2 * j, i] = position[2 * j, i] * posScale
                        position[2 * j + 1,
                                 i] = position[2 * j + 1, i] * posScale
                        if vx is not None:
                            velocity[2 * j, i - 1] = vx * posScale
                            velocity[2 * j + 1, i - 1] = vy * posScale
                    imBalls[j].append(imBall)
            imList.append(im)
        if cropSz is None:
            return imList
        else:
            return imBalls, force[:, 0:self.seqLen_],\
                velocity[:, 0:self.seqLen_],\
                position[:, 0:self.seqLen_]
Exemplo n.º 52
0
]

classes = []
features = []

#Read Images
for char_class in character_classes:
    i = 0
    for filename in glob.glob(image_dataset_file + "/" + char_class +
                              "/*.jpg"):
        i = i + 1
        print(str(i))
        if i > 3000:
            break
        im = imread(filename, "L")
        im = imresize(im, (28, 28))
        classes.append(char_class)
        features.append(im)

#Create HOG of images
i = 0
features_hog = []
for feature in features:
    print(i)
    i = i + 1
    fd = hog(feature,
             orientations=9,
             pixels_per_cell=(2, 2),
             cells_per_block=(1, 1),
             visualise=False)
    features_hog.append(fd)
Exemplo n.º 53
0
vibot = color.rgb2grey(io.imread('images/vibot-color.jpg'))
# Get height and width of image
mlena, nlena = lena.shape
mvibot, nvibot = vibot.shape

#---------------------------------------------------------------
# Show original image
plt.figure()
io.imshow(lena)
plt.axis('off')
plt.suptitle('Lena')

#---------------------------------------------------------------
# Image Resize: Nearest Interpolation
scale = 2
nearest_lena = imresize(lena, (mlena * scale, nlena * scale), interp='nearest')
plt.figure()
io.imshow(nearest_lena)
plt.axis('off')
plt.suptitle('Nearest Interpolation with Scale=2')

#---------------------------------------------------------------
# Image Resize: Bilinear Interpolation
scale = 3
bilinear_lena = imresize(lena, (mlena * scale, nlena * scale),
                         interp='bilinear')
plt.figure()
io.imshow(bilinear_lena)
plt.axis('off')
plt.suptitle('Bilinear Interpolation with Scale=3')
Exemplo n.º 54
0
 def _to_84x84_grayscale(observation):
     resized_observation = imresize(observation, [110, 84], interp="nearest")[17:101]
     # resized_observation = imresize(observation, [84, 84], interp="nearest")
     resized_grayscale_observation = rgb2gray(resized_observation)
     return resized_grayscale_observation
Exemplo n.º 55
0
def get_img(data_path):
    # Getting image array from path:
    img = imread(data_path, flatten=grayscale_images)
    img = imresize(img, (img_size, img_size, 1 if grayscale_images else 3))
    return img
Exemplo n.º 56
0
def resizeImageToBlend(im1, im2):
    m_im1, n_im1 = im1.shape
    scaled_img2 = imresize(im2, (m_im1, n_im1), interp='bilinear')
    return scaled_img2
Exemplo n.º 57
0
imgcnt = 0
for i, relpath in zip(range(nclass), paths):
    path = cwd + "/" + relpath
    flist = os.listdir(path)
    for f in flist:
        if os.path.splitext(f)[1].lower() not in valid_exts:
            continue
        fullpath = os.path.join(path, f)
        currimg = imread(fullpath)
        # Convert to grayscale
        if use_gray:
            grayimg = rgb2gray(currimg)
        else:
            grayimg = currimg
        # Reshape
        graysmall = imresize(grayimg, [imgsize[0], imgsize[1]]) / 255.
        grayvec = np.reshape(graysmall, (1, -1))
        # Save
        curr_label = np.eye(nclass, nclass)[i:i + 1, :]
        if imgcnt is 0:
            totalimg = grayvec
            totallabel = curr_label
        else:
            totalimg = np.concatenate((totalimg, grayvec), axis=0)
            totallabel = np.concatenate((totallabel, curr_label), axis=0)
        imgcnt = imgcnt + 1
print("Total %d images loaded." % (imgcnt))

# # DIVIDE TOTAL DATA INTO TRAINING AND TEST SET

# In[5]:
	file_fc6=open(rootPathSave + videoList[video] + '/fc6.txt', 'w')
	#file_conv5_3=open(rootPathSave + videoList[video] + '/conv5_3.txt', 'w')
	#file_conv5_1=open(rootPathSave + videoList[video] + '/conv5_1.txt', 'w')
	#file_conv4_3=open(rootPathSave + videoList[video] + '/conv4_3.txt', 'w')
	file_pool5=open(rootPathSave + videoList[video] + '/pool5.txt', 'w')
	#file_pool4=open(rootPathSave + videoList[video] + '/pool4.txt', 'w')
	
	nFrames=0;

	for file in os.listdir(rootRawFrames + videoList[video] + '/x_flow/'):
    		if file.endswith(".jpg"):
          		if nFrames<stackFrames:
          			x_temp=misc.imread(rootRawFrames + videoList[video] + '/x_flow/' + file)
          			y_temp=misc.imread(rootRawFrames + videoList[video] + '/y_flow/' + file)
				
          			x_temp=misc.imresize(x_temp, [sizeImputNet, sizeImputNet])
          			y_temp=misc.imresize(y_temp, [sizeImputNet, sizeImputNet])

          			arrayStackFrames[2*nFrames]=x_temp
          			arrayStackFrames[2*nFrames+1]=y_temp
          			nFrames=nFrames+1

          		

          			if nFrames==stackFrames:
          				net.blobs['data'].data[0]=arrayStackFrames - mean_flow
          				net.forward()

          				#extract fc8 features from the network and save them in a .txt file
		              		#feat_fc8 = net.blobs['fc8'].data[0]
		              		#np.savetxt(file_fc8, feat_fc8, fmt='%s', newline=' ', delimiter=',')
def get_images(filename):
    x = misc.imread(filename)
    x = misc.imresize(x, size=[299, 299])
    return x
Exemplo n.º 60
0
def imgporcessing(img):
    img = greyscale(img)
    img = img[25:, :]  #crop out irrelevant pixels
    img = misc.imresize(img, [80, 80, 1])  #resize to 80X80
    #img = np.reshape(img,[np.prod(img.shape)])
    return img