def get_gist_C_implementation(img, mask=None): """ Extract GIST descriptor of an image. Implemented by C. This implementation cannot change orientation and filter num, but it's really faster than MATLAB. """ _img = transform.resize(img, (224, 224), preserve_range=True).astype('uint8') _mask = transform.resize(mask, (224, 224), preserve_range=True).astype('uint8') if mask is None: return gist.extract(_img) _img[_mask > 0] = 0 # 1440 = 3 * (6 * 5) * 4 * 4 for colored imgs descriptor = gist.extract(_img).reshape((3, 30, 4, 4)) weight = np.zeros((4, 4)).astype('float32') unity, unitx = mask.shape[0] // 4, mask.shape[1] // 4 for _y in range(4): for _x in range(4): weight[_y, _x] = np.sum(mask[_y * unity:(_y + 1) * unity, _x * unitx: (_x + 1) * unitx] == 0) / (unity * unitx) for c in range(3): for i in range(30): descriptor[c, i] *= weight return descriptor.reshape((-1, ))
def gist_extraction(imgFolder,nums = 9,startImg = 0,local = False, **kwargs): ''' Function: extract gist features for all the images in *imgFolder* or for *nums* of images starting from *startImg* Input: imgFolder: <string> file path of the image folder nums: <int> number of images that are gonna be displayed startImg: <int> the index of the first picture local:<bool> whether images are online or saved locally imageList:<list> a specific list of images Output: featureX: <matrix> a matrix of size (nums,960) where each row represents an image feature nameList:<list> a list of image name in accordance to the feature matrix ''' nameList = [] featureX = np.zeros((1,960)) if 'imageList' in kwargs: image_list = kwargs['imageList'] nums = min(nums,len(image_list)-startImg) startImg = 0 else: image_list = os.listdir(datapath) nums = min(nums,len(image_list)-startImg) print("Processing {} images".format(nums)) if local: for i in range(startImg,startImg+nums): print("image {} is in processing".format(i)) image_name = imgFolder+ image_list[i] im = np.array(Image.open(image_name)) feature_for_i = gist.extract(im) featureX = np.concatenate((featureX,feature_for_i.reshape(1,-1)),axis = 0) nameList.append(image_name) else: for i in range(startImg,startImg+nums): print("image {} is in processing".format(i)) image_name = imgFolder + image_list[i] print(image_name) try: response = req.get(image_name) im = np.array(Image.open(BytesIO(response.content))) feature_for_i = gist.extract(im) featureX = np.concatenate((featureX,feature_for_i.reshape(1,-1)),axis = 0) nameList.append(image_name) except: print('{} is error'.format(image_name)) continue return featureX[1:,:], nameList
def gen_gist_features(roi_dir, fea_dir, mode, args): fea_dir = os.path.join(fea_dir, args.model_name, mode) data_dir = os.path.join(roi_dir, mode) img_list = [ele for ele in os.listdir(data_dir) if "png" in ele] for ind, ele in enumerate(img_list): if ind > 0 and ind % 10 == 0: print("processing {}/{}".format(ind, len(img_list))) cur_img_path = os.path.join(data_dir, ele) img_name = os.path.splitext(ele)[0] cur_anno_path = os.path.join(data_dir, img_name + ".json") if not (os.path.exists(cur_img_path) and os.path.exists(cur_anno_path)): print("File not available") img = io.imread(cur_img_path) anno_dict = format.json_to_dict(cur_anno_path) for cur_r in anno_dict: cur_anno = anno_dict[cur_r] region_label = str(label_map[cur_anno['label']]) region_name = "_".join([img_name, 'r' + cur_r]) x_coors, y_coors = cur_anno['w'], cur_anno['h'] cnt_arr = np.zeros((2, len(x_coors)), np.int32) cnt_arr[0], cnt_arr[1] = y_coors, x_coors poly_cnt = poly_transform.np_arr_to_poly(cnt_arr) start_x, start_y = min(x_coors), min(y_coors) cnt_w = max(x_coors) - start_x + 1 cnt_h = max(y_coors) - start_y + 1 coors_arr = patch.wsi_coor_splitting(cnt_h, cnt_w, args.patch_size, overlap_flag=True) Feas, BBoxes = [], [] for cur_h, cur_w in coors_arr: patch_start_w, patch_start_h = cur_w + start_x, cur_h + start_y patch_center = Point(patch_start_w + args.patch_size / 2, patch_start_h + args.patch_size / 2) if patch_center.within(poly_cnt) == True: patch_img = img[patch_start_h:patch_start_h + args.patch_size, patch_start_w:patch_start_w + args.patch_size, :] patch_desp = gist.extract(patch_img) Feas.append(patch_desp) BBoxes.append([ patch_start_h, patch_start_w, args.patch_size, args.patch_size ]) fea_dict = {'feat': np.asarray(Feas), 'bbox': np.asarray(BBoxes)} # save features cat_fea_dir = os.path.join(fea_dir, region_label) if not os.path.exists(cat_fea_dir): os.makedirs(cat_fea_dir) dd.io.save(os.path.join(cat_fea_dir, region_name + ".h5"), fea_dict)
def readImages(paths, str): """ Read All images in paths :param paths: Paths to read all Images :return: """ t1 = time.time() global X global Y global X_test global Y_test cur = 0 prevtime = t1 gist1, gist2 = gist.extract(paths) for k in gist1: X.append(k) for k in range(0, len(gist1)): Y.append([str]) for k in gist2: X_test.append(k) for k in range(0, len(gist2)): Y_test.append([str]) print 'Done' print "X len " , len(X) print "Y len " , len(Y) print "X te len " , len(X_test) print "Y te len " , len(Y_test)
def test_with_nblocks_4_as_keyword_argument(self): arr = self.load_npy('scene.npy') result = gist.extract(arr, nblocks=4) reference = self.load_reference( os.path.join(DATADIR, 'scene.nblocks4.result')) np.testing.assert_allclose(reference, result, rtol=1e-04, atol=1e-04)
def test_with_orientations_per_scale_8_8_8_as_keyword_argument(self): arr = self.load_npy('scene.npy') result = gist.extract(arr, orientations_per_scale=(8, 8, 8)) reference = self.load_reference( os.path.join(DATADIR, 'scene.ops_8_8_8.result')) np.testing.assert_allclose(reference, result, rtol=1e-04, atol=1e-04)
def recognise_gist(self, roi): clf = pickle.load(open("tmodels/svm_model_gist.sav", "rb")) fd = gist.extract(roi) fd = fd[np.newaxis, :] nbr = clf.predict(fd) reg_class = nbr[0] return decode_sym(reg_class)
def test(self): arr = self.load_npy('scene.npy') result = gist.extract(arr) reference = self.load_reference( os.path.join(DATADIR, 'scene.no_arg.result')) np.testing.assert_allclose(reference, result, rtol=1e-04, atol=1e-04)
def __getitem__(self, idx): gist_feature_block = [] point = self.pointlist[idx] _id, cell, lat, lon, attr = point[0], point[1], point[2], point[ 3], point[4] for c in self.camera_views: image_name = str(lat) + '_' + str(lon) + '_' + c + self.ext img = cv2.imread(os.path.join(self.source_path, image_name)) pixels = np.array(cv2.resize(img, self.imgsize), dtype='uint8') desc = gist.extract( pixels, nblocks=self.nblocks, orientations_per_scale=self.orientations_per_scale) gist_feature_block.append(desc) transformed_gist = torch.from_numpy( np.array(gist_feature_block, dtype=np.float)) sample = { 'gist': torch.stack([h for h in transformed_gist]), 'label': torch.from_numpy(np.array([float(attr)])), 'id': _id, 'cell': cell } return sample
def degrade_images_and_save(paths, params, root_folder, category, db_collection, downsample = Image.LANCZOS): size = params['size'] res = params['res'] res_degraded = params['res_degr'] if isinstance(res_degraded, int) or isinstance(res_degraded, float): res_degraded = [res_degraded] for factor in res_degraded: # factor: degraded resolution in meters new_size = round(size/(factor/res)) new_folder = root_folder + "usgs_" + str(size) + "_" + str(factor) + "m/" create_directory(new_folder) new_folder = new_folder + category + "/" create_directory(new_folder) for path in paths: imarray = load_image_as_rgb_array(path) imresize = Image.fromarray(imarray).resize((new_size, new_size), resample = downsample) filename = path.split("/")[-1] new_filename = filename.replace("_res" + str(res) + "m", "_res" + str(factor) + "m") output_path = new_folder + new_filename imresize.save(output_path) gist_vector = gist.extract(np.array(imresize), nblocks=1, orientations_per_scale=(8, 8, 4)).tolist() result = db_collection.update( {"filename": filename}, {"$set": {"gist_"+str(factor).replace(".", "_"): gist_vector}} ) if result["nModified"] == 0: print(filename, "GIST not uploaded to the DB!")
def feature_extraction(img_array, feature_name='all'): ''' Function: extraction multiple features from an image Input: feature_name: <string> 'gist','RGB','nn' img_array: <array> an array converted image Output: array or array-like feature ''' if feature_name == 'gist': return gist.extract(img_array) elif feature_name == 'HSV': return hsv_hist_extract(img_array) elif feature_name == 'all': gist_feature = gist.extract(img_array) hsv_feature = hsv_hist_extract(img_array) return np.concatenate((gist_feature, hsv_feature[:, 0]), axis=0) else: raise ValueError("Wrong feature type!")
def extract_gist_features(images, nblocks=4): """extract gist features from images""" gist_features = [ gist.extract(images[i, :, :, :], nblocks) for i in np.arange(images.shape[0]) ] gist_features = np.stack(gist_features) return gist_features
def feature_extract(path, jpg_pic): jpg_pic_path = path + "/" + jpg_pic pilimg = Image.open(jpg_pic_path) img = np.asarray(pilimg) if len(img.shape) == 2: img = greyToRGB(img) desc = gist.extract(img) feature = np.ndarray.tolist(desc) return (feature)
def gist_worker(file_list, imgdb_dir, gist_list, file_dict): pid = os.getpid() output_gist_file = "./gists_lake_mt" output_img_name_pickle_file = "./gists_lake_mt" idx = -1 segfault = 0 def sig_handler(*args): print("=========================================") print("Segfault in %d" % pid) print("=========================================") idx = idx - 1 segfault = 1 # restart() # os.kill(pid, signal.SIGSEGV) while (True): cur_file = "" if (len(file_list) == 0): break elif (segfault == 0): idx = len(file_list) - 1 # if (idx % 100 == 0): # print(idx) cur_file = file_list.pop() else: segfault == 0 #work on cur_file try: # print(cur_file) if (idx % 20 == 0): print( "Computing gist descriptor for %s, index is %d, in process %d" % (cur_file, idx, pid)) t1 = time() # img_rgb = cv2.imread(imgdb_dir+"/"+cur_file) img_rgb = Image.open(imgdb_dir + "/" + cur_file) descriptor = gist.extract(np.array(img_rgb)) t2 = time() # print("Process time %03f" % (t2-t1)) #Write to list and dict t3 = time() gist_list.append(descriptor) t4 = time() file_dict[idx] = cur_file t5 = time() # print("Process time1 %03f, time2%03f" % ((t4-t3), (t5-t4))) signal.signal(signal.SIGSEGV, sig_handler) except Exception, e: print("Error occurs in thread %d, file %s, index %d: %s" % (pid, cur_file, idx, e)) except KeyboardInterrupt: print("User break") break
def generate_gist(self, size): gist_feature_matrix = np.zeros((self.total_num, 960)) n = 0 with tqdm(total=self.total_num) as pbar: for category in self.categories: for image_name in self.images_name: image_path = os.path.join(self.raw_training_dir, category, image_name) data = cv2.resize(cv2.imread(image_path), (size[0], size[1])) gist_feature = gist.extract(data) pbar.update(1) gist_feature_matrix[n, :] = gist_feature n += 1 return gist_feature_matrix
def runOttawa(n_loc): """ Downloads Google StreetView images of random points in Ottawa :param n_loc: number of locations to download. CAUTION: some points have no images, so it's not the exact number of subdirectories created """ DIRECTORY = "../voteimages" ds = ogr.Open('C:/Users/msawada/Desktop/Urban_RAT/Urban_RAT_inventory.dbf') layer = ds.GetLayer() for i in range(n_loc): print('%.2f' % (i * 100 / n_loc) + " %") index = random.randint(0, len(layer)) feature = layer[index] lon = feature.GetGeometryRef().GetX() lat = feature.GetGeometryRef().GetY() bearing_road = feature.bearing if bearing_road == None: heading = '' else: heading = bearing_road + 90 * np.sign(random.random() - 0.5) folder = DIRECTORY panIds = streetview.panoids(lat, lon) if len(panIds) > 0: for pan in panIds: img = streetview.api_download(pan["panoid"], heading, folder, API_KEY, fov=80, pitch=0, year=pan["year"]) if img != None: full_size = misc.imread(img) resized = misc.imresize(full_size, (64, 64)) desc = gist.extract(resized) np.savetxt(img + '.txt', desc)
def write_feature(db_path, converted_db_path, method='isohashlp', count, vector_size = 64, iter = 200): # A very primitive implementation. Need to refactor the code if # want to make it more versatile to a diversity of methods. if method.lower() == 'isohashlp': db = ply.DB(db_path) # I think we cannot modify in place. Have to create a new database. converted_db = ply.DB(converted_db_path, create_if_missing=True, write_buffer_size=268435456) # Assume we directly use the dimension 960 of gist. Can modify it later gist_dim = 960 gists = np.zeros((gist_dim, count)) i = 0 with converted_db.write_batch() as converted_wb: for key, value in db: img = Image.frombytes('RGB', (224, 224), value) img_data = np.asarray(img) gist = gist.extract(img_data) # The 960-dimensional gists, serving as inputs for isoHash. # TODO: could have better implementation gists[:, i] = gist i += 1 # zero center the data avg = np.sum(gists, axis=1)/count avg = avg.reshape((gist_dim, 1)) gists = gists - avg # This PCA method is said to be inefficient. Can switch. pca = PCA(n_components = vector_size) gist_reduced = (pca.fit_transform(gists.T)).T # Each row is a Principal Component. # gist_reduced is W^T*X Lambda = np.dot(gist_reduced, gist_reduced.T) Q = isoHash_lp(Lambda, iter, vector_size) # TODO: Use sign function to get Y. Y = (Q.T).dot(gist_reduced) Y = (Y >= 0)*1 i = 0 for key, value in db: converted_key = Y[:, i].tobytes() converted_wb.put(converted_key, value) i += 1
def runOttawa(): """ Downloads Google StreetView images of random points in Ottawa :param n_loc: number of locations to download. CAUTION: some points have no images, so it's not the exact number of subdirectories created """ DIRECTORY = "D:\Amaury\Desktop\ottawa_image_db" ds = ogr.Open( 'C:/Users/msawada/Desktop/Urban_RAT/Urban_RAT_inventory_4326.dbf') layer = ds.GetLayer() n_loc = len(layer) #done: range(3000) for i in range(3000, 6000): print('%.2f' % (i * 100 / n_loc) + " %") index = i feature = layer[index] lon = feature.GetGeometryRef().GetX() lat = feature.GetGeometryRef().GetY() folder = DIRECTORY + '/%2.6f,%2.6f' % (lat, lon) panIds = streetview.panoids(lat, lon) if len(panIds) > 0: if not os.path.exists(folder): os.makedirs(folder) for pan in panIds: img = streetview.api_download(pan["panoid"], folder, API_KEY, fov=80, pitch=0, year=pan["year"], month=pan["month"]) if img != None: full_size = misc.imread(img) resized = misc.imresize(full_size, (64, 64)) desc = gist.extract(resized) np.savetxt(img + '64.txt', desc)
def bytes2png(file, width): with open(file, 'rb') as f: bytes = bytearray(f.read()) flatNpArray = np.array(bytes) fileSize = flatNpArray.size hight = int(fileSize / width) + 1 imageSize = (width, hight) needToAddElements = width * hight pad_width = (0, needToAddElements - fileSize) np.pad(flatNpArray, pad_width, 'constant') flatNpArray.resize(hight, width) rgbImag = cv2.cvtColor(flatNpArray, cv2.COLOR_GRAY2RGB) descriptor = gist.extract(rgbImag) return descriptor
def gist_from_df(df): # print('a') count = 0 for index, row in df.iterrows(): if (count % 50 == 0): print('Process {}: {}/{}'.format(multiprocessing.current_process(), count, df.shape[0])) p_img = 'datasets/photonet/imgs/{}.jpg'.format(row['photo_id']) img = cv2.imread(p_img) descriptor = gist.extract(img) for i in range(0, 960): df.at[index, 'gist' + str(i)] = descriptor[i] count += 1 print('{} done'.format(multiprocessing.current_process())) return df
def gist_calculate_and_load(filenames, folder, db_collection): """ Calculates gist vectors of the images and uploads them to the DB :param filenames: list of str :param folder: str :param db_collection: mongodb collection object :return: number of DB documents updated and computed gist vectors """ images_files = load_images_from_gdrive(filenames, folder, return_list=True) gist_uploaded = 0 gist_vectors = [] for image in images_files: gist_vector = gist.extract(image["array"]) gist_vectors.append(gist_vector) result = db_collection.update( {"filename": image["fname"]}, {"$set": {"gist": gist_vector.tolist()}} ) gist_uploaded += result["nModified"] return gist_uploaded, gist_vectors
def save_cropped_images(imcropped, params, input_fname, category, output_folder, db_collection = None): """ :param imcropped: dict Output of get_cropped_images keys are coordinate of image values are image :param params: dict parameter with image properties params['size'] = size params['res'] = resolution :param input_fname: str filename of large image :param category: str name identifier of the image group/category :param output_folder: str :param db_collection: mongodb collection object :return: """ size = params['size'] baseres = params['res'] for coordinate in imcropped.keys(): imarray = imcropped[coordinate] filename_pure = input_fname.split(".")[0] coordinate_string = "_x" + str(coordinate[0]) + "_y" + str(coordinate[1]) size_string = "_size" + str(size) baseres_string = "_baseres" + str(baseres) + "m" filename = filename_pure + coordinate_string + size_string + baseres_string + ".png" output_path = os.path.join(output_folder, filename) Image.fromarray(imarray).save(output_path) if not db_collection is None: gist_vector = gist.extract(imarray, nblocks=1, orientations_per_scale=(8, 8, 4)).tolist() metadata = generate_metadata_usgs( category, filename_pure, filename, coordinate, size, baseres, gist_vector, dataset='usgs' + '_res' + str(baseres) + "m" + '_size' + str(size)) db_collection.replace_one({"filename": metadata["filename"]}, metadata, upsert=True)
filenames = [] apt_list = [] full_filenames = [] counter = 0 featureX = np.zeros((1,960)) logfile_name = '../Logs/gist_feature_extraction_Boston-Massachusetts.txt' feature_folder = '../'+'features/'+ area + 'gist/' if not os.path.exists(feature_folder): os.makedirs(feature_folder) response = req.get(full_path_name) img_temp = Image.open(BytesIO(response.content)) im = np.asarray(img_temp.resize((600,400))) feature_tmp = gist.extract(im) for obj in images_valid[1:]: if counter % 150 == 0 or counter < 10: print("Processing image {} of {}\n ,{}..................".format(counter,len(images_valid),obj.key)) string = obj.key.split('/') try: full_path_name = 'https://s3-us-west-2.amazonaws.com/'\ +bucket_name+'/'+ str(obj.key) response = req.get(full_path_name) img_temp = Image.open(BytesIO(response.content)) im = np.asarray(img_temp.resize((600,400))) feature_tmp = gist.extract(im) except: print('!!!!!!!!!!!!Error at img {}'.format(obj.key))
def test_with_zero_array(self): black_image = np.zeros((480, 640, 3), dtype=np.uint8) gist.extract(black_image)
def test_with_invalid_size_array(self, shape): arr = np.zeros(shape, dtype=np.uint8) with self.assertRaises(ValueError): gist.extract(arr)
import scipy.io from PIL import Image dataset = json.load(open('../data/flickr8k/dataset.json')) image_names = [] for image in dataset['images']: image_names.append(image['filename']) print('number of images' + str(len(image_names))) # create features data feats = {'feats': []} # extract features from images for im_name in image_names: print(im_name) # read image img = Image.open('../data/Flicker8k_Dataset/' + im_name) # convert to array img = np.asarray(img) # get descriptor desc = gist.extract(img) # feats['feats'] = np.append(feats['feats'], flat) feats['feats'].append(desc.tolist()) scipy.io.savemat('../data/Flicker8k_KNN/knn_feats.mat', feats)
continue count += 1 gists = np.array(gists) ##gist parameters number_blocks = 4 orientations_per_scale = [8, 8, 4] #img_size = (600, 800) img_size = (192, 256) ##query image #filename_input = "street_par28.jpg" filename_input = input_image_path img_input_rgb = cv2.imread(filename_input) [h, w, _] = img_input_rgb.shape ##gist for the query image gist_input = gist.extract(img_input_rgb) ##gists of the images in the database #gists = np.load("./gists.npy") # gists = np.load(db_gist_file) ##mask = mask1 + mask2 mask_rgb = io.imread(mask_path) #print(mask_rgb.shape) mask_gray = cv2.cvtColor(mask_rgb, cv2.COLOR_BGR2GRAY) # mask1 = np.zeros((h,w)) # cv2.circle(mask1,(w/4,h/3),50,255,thickness=-1) # mask2 = np.zeros((h,w)) # cv2.circle(mask2,(w/2,int(h/1.5)),60,255,thickness=-1)
input_dir = argv[1] output_dir = argv[2] if (not exists(output_dir)): mkdir(output_dir) imsize = (128, 128) filenames = listdir(input_dir) filenamesSize = len(filenames) i = 1 print("Generate GIST Descriptor") for filename in filenames: print i, " of ", filenamesSize i = i+1 filepath = join(input_dir, filename) try: pilimg = Image.open(filepath) except: continue img = np.asarray(pilimg) img_resized = transform.resize(img, imsize, preserve_range=True).astype(np.uint8) desc = gist.extract(img_resized) file = open(output_dir + "/" + filename.split(".")[0] + ".gist", 'w') file.write(str(desc)) file.close()
if (not exists(output_dir)): mkdir(output_dir) imsize = (128, 128) features = {} filenames = listdir(input_dir) for filename in filenames: filepath = join(input_dir, filename) try: pilimg = Image.open(filepath) except: continue img = np.asarray(pilimg) img_resized = transform.resize(img, imsize, preserve_range=True).astype(np.uint8) desc = gist.extract(img_resized) class_name = filename.split('_')[0] if (class_name in features): features[class_name] = np.vstack((features[class_name], desc)) else: features[class_name] = np.atleast_2d(desc) for class_name, desc_mat in features.items(): np.save(join(output_dir, class_name + '.npy'), desc_mat)
tr = t + param[i, 3] tr = tr + 2 * np.pi * (tr < -np.pi) - 2 * np.pi * (tr > np.pi) gabors[:, :, i] = np.exp( - 10 * param[i, 0] * (fr / n / param[i, 1] - 1) ** 2 - 2 * param[i, 2] * np.pi * tr ** 2) return gabors # ------------------------------------------------------------------------ img = misc.imresize(img, (image_size, image_size)) gabors = create_gabor(orientations, image_size) output = prefilter(img.astype(np.float)) features = get_feature(output, num_blocks, gabors) return features.flatten() # ---------------------------------------------------------------------------- if __name__ == '__main__': import time import gist as g2 img_template = misc.imread('demo1.jpg', mode='L' ) img =np.array(img_template) d1 = gist(img_template) d2 = g2.extract(np.reshape(img,[256,256,1])) ts = time.time() print gist(img_template) print time.time() - ts # ----------------------------------------------------------------------------
def Gist(im): img_resized = ResizeImage(im) desc = gist.extract(img_resized) return list(desc)
#initial works with open('/media/mjia/Data/CNN-fMRI/Summary_info.json') as f: data = json.load(f) sum = 0 for key in data: sum += len(data[key]) #all images from figram will be selected all_files = glob.glob(CROPPED_DIR + '/*/*.jpg', recursive=True) gistf = numpy.ones((960, len(all_files))) correspondingNames = [] filled = 0 for name in all_files: img = Image.open(name) current = gist.extract(numpy.array(img)) gistf[:, filled] = current #save current h5f = h5py.File(name[:-3] + 'h5', 'w') h5f.create_dataset('GistFeatures', data=current) h5f.close() correspondingNames.append(name) print(filled) filled += 1 if filled == 50: break h5f = h5py.File("GistFeatures_all.h5", 'w') h5f.create_dataset('GistFeatures', data=gistf)
def getNonDeepLearningFeatures(self, dataloader, dsetname): daisyfeat = [] lbpfeat = [] gistfeat = [] orbfeat = [] hogfeat = [] Y = [] len_dataset = 0 #descriptor_extractor = ORB(n_keypoints=self.opt['nKeypoints']) print('==> Computing image features for', dsetname, ' ...') for inputs, targets in dataloader: for i in range(inputs.size(0)): len_dataset += 1 img = inputs[i, :, :, :].transpose(0, 2).numpy() img2 = img[:, :, 0] f1, _ = daisy(img2, step=int(img.shape[0] / 2), radius=self.opt['daisyRadius'], rings=self.opt['daisyRings'], histograms=self.opt['daisyHistograms'], orientations=self.opt['daisyOrientations'], visualize=False) daisyfeat.append(f1.flatten()) #lbpfeat.append(mlbp(img2,0,0,self.opt['lbpWidth'],self.opt['lbpHeight'])) lbp = local_binary_pattern(img2, self.opt['lbpP'], self.opt['lbpR'], self.opt['lbpMethod']) lbp_hist, _ = np.histogram(lbp, normed=True, bins=self.opt['lbpP'] + 2, range=(0, self.opt['lbpP'] + 2)) lbpfeat.append(lbp_hist) gistfeat.append(gist.extract((img * 255).astype('uint8'))) hogfeat.append( hog(img2, orientations=self.opt['hogOrientations'], pixels_per_cell=(self.opt['hogCellSize'], self.opt['hogCellSize']), cells_per_block=(self.opt['hogNumCells'], self.opt['hogNumCells']), block_norm=self.opt['hogBlockNorm'], feature_vector=True)) Y.append(targets[i].numpy()) #descriptor_extractor.detect_and_extract((img2*255).astype('uint8')) #orbfeat.append(descriptor_extractor.descriptors.flatten()) if len_dataset >= self.opt['sampleSize']: break self.daisyfeat = np.array(daisyfeat) self.lbpfeat = np.array(lbpfeat) self.gistfeat = np.array(gistfeat) self.hogfeat = np.array(hogfeat) self.Y = np.array(Y) print("Daisy Features: ", self.daisyfeat.shape) print("LBP Features: ", self.lbpfeat.shape) print("GIST Features: ", self.gistfeat.shape) print("HOG Features: ", self.hogfeat.shape) print("Target labels: ", self.Y.shape) return
def fd_gist(image): return gist.extract(image)
def run(img): gist.extract(img) gist.extract(img) gist.extract(img) gist.extract(img) gist.extract(img)
def test_with_None(self): with self.assertRaises(TypeError): self.assertIsNone(gist.extract(None))
def get_GIST_from_image(im): img = read_image(im) descriptors = gist.extract(img) return descriptors
def main(argv): inp_pic = "timages/" try: opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="]) except getopt.GetoptError: print ('test.py -i <inputfile>') sys.exit(2) for opt, arg in opts: if opt == '-h': print ('test.py -i <inputfile>') sys.exit() elif opt in ("-i", "--ifile"): inp_pic += arg # Load the Keras CNN trained model model = load_model('tmodels/gist-crohme-digits-10e-features.h5') # Original image im = cv2.imread(inp_pic) cv2.imshow("Original Image", im) cv2.waitKey() # Read image in grayscale mode img = cv2.imread(inp_pic,0) # Median Blur and Gaussian Blur to remove Noise img = cv2.medianBlur(img,3) img = cv2.GaussianBlur(img, (5, 5), 0) # Adaptive Threshold for handling lightning im_th = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 5) kernel = np.ones((1,1),np.uint8) # Dilation or Thinning of Image im_th = cv2.dilate(im_th,kernel, iterations = 4) cv2.imshow("After", im_th) # cv2.imshow("Threshold Image",im_th) # Find contours in the image _,ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Get rectangles contains each contour rects = [cv2.boundingRect(ctr) for ctr in ctrs] # For each rectangular region, predict using cnn model for rect in rects: # Draw the rectangles cv2.rectangle(im, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 3) # Make the rectangular region around the digit leng = int(rect[3] * 1.6) pt1 = int(rect[1] + rect[3] // 2 - leng // 2) pt2 = int(rect[0] + rect[2] // 2 - leng // 2) roi = im_th[pt1:pt1+leng, pt2:pt2+leng] # Resize the image roi = cv2.resize(roi, (49, 49), interpolation=cv2.INTER_AREA) roi = cv2.cvtColor(roi,cv2.COLOR_GRAY2RGB) fd = gist.extract(roi) fd = fd[np.newaxis,:] # Input for CNN Model # roi = roi[np.newaxis,:,:,np.newaxis] # Input for Feed Forward Model # roi = roi.flatten() # roi = roi[np.newaxis] nbr = model.predict_classes(fd,verbose=0) cv2.putText(im, str(int(nbr[0])), (int(rect[0]), int(rect[1])),cv2.FONT_HERSHEY_DUPLEX, 2, (0, 255, 255), 3) print(str(int(nbr[0]))) cv2.imshow("Resulting Image with Predicted numbers", im) cv2.waitKey()
def predict(self, image): data = cv2.resize(image, (self.pic_size[0], self.pic_size[1])) feature = gist.extract(data) prediction = self.svm.predict(np.atleast_2d(feature)) return prediction