def __init__(self, n_samples, warp_generator, img, pts, initial_warp, res, feature_obj): self.resx = res[0] self.resy = res[1] self.sift = False self.indx = [] n_points = pts.shape[1] print "Sampling Warps..." self.warps = [np.asmatrix(np.eye(3)) ] + [warp_generator() for i in xrange(n_samples - 1)] print "Sampling Images..." self.images = None self.getBestMatch = self.best_match self.feature_obj = feature_obj for i, w in enumerate(self.warps): sample = self.feature_obj.getFeature(img, pts, initial_warp * w.I) if self.images is None: self.images = np.empty(sample.shape + (n_samples, )) self.images[:, i] = sample # self.images[:,i] = sample_and_normalize(img, apply_to_pts(initial_warp * w.I, pts)) print "Building FLANN Index..." # pyflann.set_distance_type("manhattan") if self.sift == False: self.flann = pyflann.FLANN() # print(self.images.shape) self.flann.build_index(self.images.T, algorithm='kdtree', trees=10) else: desc = self.list2array(self.pixel2sift(self.images)) # --- Building Flann Index --- # self.flann = pyflann.FLANN() # self.flann.build_index(np.asarray(self.images).T, algorithm='linear') #print(type(desc)) #pdb.set_trace() self.flann.build_index(desc.T, algorithm='kdtree', trees=10) print "Done!"
def __init__(self, n_samples, warp_generator, img, pts, initial_warp, res, use_mean, flatten=False, use_hoc=False, histogram=None): if len(img.shape) < 3: raise AssertionError("Error in _WarpIndexVec: The image is not multi channel") if use_hoc and histogram is None: raise SystemExit('Error in _WarpIndexVec:' 'Cannot compute histogram without valid function') self.dim = img.shape[2] self.resx = res[0] self.resy = res[1] self.sift = False self.indx = [] self.use_mean=use_mean self.flatten=flatten n_points = pts.shape[1] print "Sampling Warps..." self.warps = [np.asmatrix(np.eye(3))] + [warp_generator() for i in xrange(n_samples - 1)] print "Sampling Images..." if use_hoc: if self.flatten: self.images = np.empty((256, n_samples)) else: self.images = np.empty((self.dim, 256, n_samples)) else: if self.flatten: self.images = np.empty((self.dim*n_points, n_samples)) else: self.images = np.empty((self.dim, n_points, n_samples)) for i, w in enumerate(self.warps): sample = sample_and_normalize_vec(img, pts, initial_warp * w.I, flatten=self.flatten) if use_hoc: sample=histogram(sample) if self.flatten: self.images[:, i]=sample else: self.images[:, :, i]=sample #self.images[:,i] = sample_and_normalize(img, apply_to_pts(initial_warp * w.I, pts)) print "Building FLANN Index..." self.flann_vec = [] #pyflann.set_distance_type("manhattan") if self.flatten: self.flann = pyflann.FLANN() #print 'self.images.shape:', self.images.shape #print 'self.images.dtype',self.images.dtype self.flann.build_index(self.images.T, algorithm='kdtree', trees=10) else: for i in xrange(self.dim): current_images = self.images[i, :, :] flann = pyflann.FLANN() #print(self.images.shape) flann.build_index(current_images.T, algorithm='kdtree', trees=10) self.flann_vec.append(flann) print "Done!"
def test_pyflann_io(): """ CommandLine: python -m vtool.tests.test_pyflann --test-test_pyflann_io Example: >>> # ENABLE_DOCTEST >>> from vtool.tests.test_pyflann import * # NOQA >>> result = test_pyflann_io() >>> print(result) """ # Create qpts and database data print('Create random qpts and database data') num_neighbors = 3 nPts = 1009 nQPts = 31 qpts = testdata_points(nPts=nQPts) pts = testdata_points(nPts=nPts) # Create flann object print('Create flann object') flann = pyflann.FLANN() # Build kd-tree index over the data print('Build the kd tree') with utool.Timer('Buliding the kd-tree with %d pts' % (len(pts), )): _build_params = flann.build_index(pts) # noqa # Find the closest few points to num_neighbors print('Find nn_index nearest neighbors') indices1, dists1 = flann.nn_index(qpts, num_neighbors=num_neighbors) # Save the data to disk print('Save the data to the disk') np.savez('test_pyflann_ptsdata.npz', pts) npload_pts = np.load('test_pyflann_ptsdata.npz') pts2 = npload_pts['arr_0'] print('Save and delete the FLANN index') flann.save_index('test_pyflann_index.flann') flann.delete_index() print('Reload the data') flann2 = pyflann.FLANN() flann2.load_index('test_pyflann_index.flann', pts2) indices2, dists2 = flann2.nn_index(qpts, num_neighbors=num_neighbors) #print(utool.hz_str('indices2, dists2 = ', indices2, dists2)) print('Find the same nearest neighbors?') if np.all(indices1 == indices2) and np.all(dists1 == dists2): print('...data is the same! SUCCESS!') else: raise AssertionError('...data is the different! FAILURE!')
def __init__(self, eps=0.2, distance_sanity=0.1, min_distance=0.005, base_path = 'OcclusionChallengeICCV2015', objs =['Ape', 'Can', 'Cat', 'Driller', 'Duck', 'Eggbox', 'Glue', 'Holepuncher'], model_partial= 1, K = None, im_size = (640, 480), method="SVD", ver2=False): self.eps = eps self.distance_sanity = distance_sanity self.min_distance = min_distance self.method = method self.ver2 = ver2 ## camera parm if K is not None: self.K = K else: self.K = np.array([[572.41140, 0, 325.26110], [0, 573.57043, 242.04899], [0, 0, 0]]) ## for flann self.flann_search_idx = [] self.flann_search_idx_sphere = [] self.models_pc = [] self.objs = objs pyflann.set_distance_type('euclidean') for obj_name in objs: pc = pypcd.PointCloud.from_path( os.path.join(base_path, 'models_pcd', obj_name + '.pcd')) pc = np.asarray(pc.pc_data.tolist())[:,:3] ## only use xyz # pc = pc * np.array([1, -1, -1])[np.newaxis, :] ## negative z # trans = np.array([[0, -1, 0], [0, 0, -1], [1, 0, 0]]) # pc = np.dot(trans, pc.transpose(1, 0)).transpose(1, 0) search_idx = pyflann.FLANN() search_idx.build_index(pc[0::model_partial, :], algorithm='kmeans', centers_init='kmeanspp', random_seed=1234) self.flann_search_idx.append(search_idx) self.models_pc.append(pc) # sphere model index norm_pc = np.linalg.norm(pc, axis=1, keepdims=True) sphere_pc = pc / norm_pc sphere_pc = np.hstack((sphere_pc, norm_pc)) search_idx_sphere = pyflann.FLANN() search_idx_sphere.build_index(sphere_pc, algorithm='kmeans', centers_init='kmeanspp', random_seed=1234) self.flann_search_idx_sphere.append(search_idx_sphere)
def rematch(C, Q, dbsift): start = time.time() rdr = reader.get_reader(C.params['descriptor']) q = rdr.load_file(Q.siftpath) db = rdr.load_file(dbsift) flann = pyflann.FLANN() results, dists = flann.nn(db['vec'], q['vec'], 1, algorithm='linear') results, dists, q = np.array(results), np.array(dists), np.array(q) idx = np.argsort(dists) results = results[idx] dists = dists[idx] q = q[idx] count = 1000 matches = [] closed = set() for i in range(0, len(results)): if results[i] not in closed and dists[i] < 40000: closed.add(results[i]) atom = { 'db': db[results[i]]['geom'].copy(), 'query': q[i]['geom'].copy() } matches.append(atom) count -= 1 if count == 0: break INFO_TIMING("rematch took %f" % (time.time() - start)) return matches
def get_nearest_patches(hfile_path, modelFile, scaler, n_patches, out_folder): classSupport = np.loadtxt(open(modelFile), skiprows=1) classSupport = classSupport.T.astype("float32") sfile = hfile(hfile_path, "r") im_keys = sfile["keys"][:] iid = sfile["image_index"][:] p7 = sfile["patches7"][:iid.max()].clip(0) pos = sfile["positions"][:iid.max()] p7 = np.hstack([p7, pos.copy()]) p7 = scaler.transform(p7) engine = pyflann.FLANN() engine.build_index(classSupport, algorithm='kdtree', trees=4) (_, dist) = engine.nn_index(p7, num_neighbors=1) bestIdx = dist.argsort()[:n_patches] image_ids = [(idx, np.where(iid <= idx)[0][-1]) for idx in bestIdx if idx != 0] sizes = [32, 64, 128] for num, idx in enumerate(image_ids): image_id = idx[1] # actual image id patch_n = idx[0] - iid[image_id, 0] # patch number relative to image image_pos = pos[iid[image_id, 0]:iid[image_id, 1]] zeroLoc = np.where(np.all(image_pos == [0, 0], axis=1))[0][1:] patch_loc = (image_pos[patch_n, :], sizes[np.where(zeroLoc <= patch_n)[0][-1]] ) # pos and size of the patch save_crop(patch_loc, im_keys[image_id], num, out_folder)
def _load_flann_model(self): if not self._descr_cache and not self._descr_cache_elem.is_empty(): # Load descriptor cache # - is copied on fork, so only need to load here. self._log.debug("Loading cached descriptors") self._descr_cache = \ cPickle.loads(self._descr_cache_elem.get_bytes()) # Params pickle include the build params + our local state params if self._index_param_elem and not self._index_param_elem.is_empty(): state = cPickle.loads(self._index_param_elem.get_bytes()) self._build_autotune = state['b_autotune'] self._build_target_precision = state['b_target_precision'] self._build_sample_frac = state['b_sample_frac'] self._distance_method = state['distance_method'] self._flann_build_params = state['flann_build_params'] # Load the binary index if self._index_elem and not self._index_elem.is_empty(): # make numpy matrix of descriptor vectors for FLANN pts_array = [d.vector() for d in self._descr_cache] pts_array = numpy.array(pts_array, dtype=pts_array[0].dtype) pyflann.set_distance_type(self._distance_method) self._flann = pyflann.FLANN() tmp_fp = self._index_elem.write_temp() self._flann.load_index(tmp_fp, pts_array) self._index_elem.clean_temp() del pts_array, tmp_fp # Set current PID to the current self._pid = multiprocessing.current_process().pid
def Match_one_frame(self, frame1, frame2): feature1 = self._config.LoadFeature(frame1) feature2 = self._config.LoadFeature(frame2) [loc1, des1] = [feature1['location'], feature1['descriptor']] [loc2, des2] = [feature2['location'], feature2['descriptor']] flann = pyflann.FLANN() result, dist = flann.nn(des2, des1, 2, algorithm="kmeans", branching=32, iterations=10, checks=200) index1 = np.arange(loc1.shape[0]) compare = (dist[:, 0].astype(np.float32) / dist[:, 1]) < self._config.Get('flann_threshold') index1 = index1[compare] index2 = result[:, 0][compare] loc1 = loc1[index1, :] loc2 = loc2[index2, :] [F1, M1] = cv2.findFundamentalMat(loc1, loc2, cv2.FM_RANSAC) [F2, M2] = cv2.findFundamentalMat(loc2, loc1, cv2.FM_RANSAC) M = M1 * M2 M = np.reshape(M, [-1]) index1 = index1[M == 1] index2 = index2[M == 1] return np.vstack([index1, index2]).T
def flann_build(codebook, flann_index_file=None, target_precision=0.99, log_level="info"): """ Prepares quantization module based on pyflann @param codebook: loaded codebook in numpy format (row-wise) @param target_precision: amount of exactness of approximation, the lower, the faster @param log_level: log data format to be generatede by flann """ log = logging.getLogger(__name__) flann = pyflann.FLANN() if not flann_index_file or not osp.isfile(flann_index_file): flann.build_index(codebook, target_precision=target_precision, log_level=log_level) if flann_index_file is not None: log.info("Saving generated flann index to: %s", flann_index_file) flann.save_index(flann_index_file) else: log.warning("Constructed new index file " "but did not save it.") else: log.debug("Given existing index file. Loading it.") flann.load_index(flann_index_file, codebook) return flann
def load_pf_flann_index(features, idxpath): """ Loads a FLANN index, implemented with native FLANN library. :param features: detection used to build the index. Must agree with the index; :param idxpath: path to the index file; :return: FLANN index. """ try: print("-- Loading indexing structure") start = get_time() flann_index = pyflann.FLANN() flann_index.load_index(idxpath, features) elapsed = total_time_elapsed(start, get_time()) print('spent time: {0}!'.format(elapsed)) sys.stdout.flush() except Exception: raise Exception("Could not load index!\n") return flann_index
def _load_flann_model(self): if not self._descr_cache and self._descr_cache_filepath: # Load descriptor cache # - is copied on fork, so only need to load here. self._log.debug("Loading cached descriptors") with open(self._descr_cache_filepath, 'rb') as f: self._descr_cache = cPickle.load(f) # Params pickle include the build params + our local state params if self._index_param_filepath: with open(self._index_param_filepath) as f: state = cPickle.load(f) self._build_autotune = state['b_autotune'] self._build_target_precision = state['b_target_precision'] self._build_sample_frac = state['b_sample_frac'] self._distance_method = state['distance_method'] self._flann_build_params = state['flann_build_params'] # Load the binary index if self._index_filepath: # make numpy matrix of descriptor vectors for FLANN pts_array = [d.vector() for d in self._descr_cache] pts_array = numpy.array(pts_array, dtype=pts_array[0].dtype) pyflann.set_distance_type(self._distance_method) self._flann = pyflann.FLANN() self._flann.load_index(self._index_filepath, pts_array) del pts_array # Set current PID to the current self._pid = multiprocessing.current_process().pid
def __init__(self, visualwords, index_filepath=None, algorithm='kdtree', loglevel='WARNING'): """Create or load index of visual words. :param visualwords: 2D array of base vectors :type visualwords: M x len(feature) `numpy.ndarray`, where each element is `numpy.float32` :param index_filepath: If not `None`, index is not created but load from specified file. The file must be created by :func:`BoFMaker.save` :param algorithm: passed to `pyflann.FLANN().build_index` """ self._n_visualwords = visualwords.shape[0] self._flann = pyflann.FLANN() if index_filepath is not None: # load index logger.debug('Loading visual words index from %s ...' % (index_filepath)) t0 = time.time() with open(BoFMaker.meta_filepath(index_filepath)) as f: self._index_param = json.load(f) self._flann.load_index(index_filepath, visualwords) logger.debug('%f sec to load index' % (time.time() - t0)) else: # create index logger.debug('Creating index of visual words...') t0 = time.time() self._index_param = self._flann.build_index(visualwords, algorithm=algorithm) logger.debug('%f sec to create index' % (time.time() - t0))
def __init__(self, n_samples, warp_generator, img, pts, initial_warp, res): self.nodes = None self.resx = res[0] self.resy = res[1] self.gnn = True self.indx = [] n_points = pts.shape[1] print "Sampling Warps..." self.warps = [np.asmatrix(np.eye(3)) ] + [warp_generator() for i in xrange(n_samples - 1)] print "Sampling Images..." self.images = np.empty((n_points, n_samples)) for i, w in enumerate(self.warps): self.images[:, i] = sample_and_normalize(img, pts, initial_warp * w.I) print('Graph based Nearest Neighbour') print('------------------------------') if self.gnn == True: #self.flann = pyflann.FLANN() #print(self.images.shape) #self.flann.build_index(self.images.T, algorithm='kdtree', trees=10) self.images = MA(self.images, 'f8') self.nodes = build_graph(self.images.T, 40) else: desc = self.list2array(self.pixel2sift(self.images)) # --- Building Flann Index --- # self.flann = pyflann.FLANN() print "Done!"
def icp_rotation(src, dst, n_iter=50, thre_precent=95): # input src shape = (3, num) # input dst shape = (3, num) # src = np.array(src, copy=True).astype(np.float32) # dst = np.array(dst, copy=True).astype(np.float32) if len(src) < len(dst): a = src b = dst else: a = dst b = src pyflann.set_distance_type('euclidean') search_idx = pyflann.FLANN() search_idx.build_index(b.transpose(1, 0).astype(np.float64), algorithm='kmeans', centers_init='kmeanspp', random_seed=1234) _R = np.diag((1, 1, 1)) for i in six.moves.range(n_iter): indices, distances = search_idx.nn_index(np.dot(_R, a).transpose(1, 0).astype(np.float64), 1) percentile_thre = np.percentile(distances, thre_precent) inlier_mask = (distances <= percentile_thre) _R = calc_rot_by_svd(b[:, indices[inlier_mask]], a[:, inlier_mask]) if len(src) > len(dst): _R = _R.T return _R
def fit(self, X): self._flann = pyflann.FLANN(target_precision=self._target_precision, algorithm='autotuned', log_level='info') if self._metric == 'angular': X = sklearn.preprocessing.normalize(X, axis=1, norm='l2') self._flann.build_index(X)
def build_flann(database_middle): pyflann.set_distance_type('euclidean') flann = pyflann.FLANN() param_name = os.path.join(args.model_dir, 'params_AE2.pk') flann_name = os.path.join(args.model_dir, 'flann_fix2_AE2') if os.path.exists(param_name): print("==> Load Flann") params = pickle.load(open(param_name, 'rb')) flann.load_index(bytes(flann_name, encoding='utf8'), database_middle) else: # build print("==> Build Flann") params = flann.build_index(database_middle, algorithm='kmeans',\ target_precision=0.9, branching = args.branching, log_level='info') try: pickle.dump(params, open(param_name, 'wb')) flann.save_index(bytes(flann_name, encoding='utf8')) except Exception as e: print('Failed to save flann') print(e) return flann
def get_buildtime_data(**kwargs): flann_params = vt.get_flann_params(**kwargs) print('flann_params = %r' % (ut.dict_str(flann_params), )) data_list = [] num = 1000 print('-----') for count in ut.ProgressIter(itertools.count(), nTotal=-1, freq=1, autoadjust=False): num = int(num * 1.2) print('num = %r' % (num, )) #if num > 1E6: # break data = pool.get_testdata(num) print('object size ' + ut.get_object_size_str(data, 'data')) flann = pyflann.FLANN(**flann_params) with ut.Timer(verbose=False) as t: flann.build_index(data) print('t.ellapsed = %r' % (t.ellapsed, )) if t.ellapsed > 5 or count > 1000: break data_list.append((count, num, t.ellapsed)) print('-----') return data_list, flann_params
class GetNeareastNeighbor: flann = pyflann.FLANN() params = None preRun = False def init(self): if GetNeareastNeighbor.preRun: return GetNeareastNeighbor.preRun = True # index = np.load(settings.INDEX_FILE).item() dataset = np.load(settings.DATASET) GetNeareastNeighbor.flann.load_index(settings.INDEX_FILE, dataset) GetNeareastNeighbor.params = np.load(settings.PARAMS).item() def find(self, image, num): self.init() data = extract_feature([image]) # print data result, dists = GetNeareastNeighbor.flann.nn_index( data, num, checks=GetNeareastNeighbor.params["checks"] ) return result
def get_example(self, example_file, example_index, model, data): points, normals, label = PointSample.getPointCloudNormal( example_file, example_index, self.sample_num) flann = pyflann.FLANN() flann.build_index(points, algorithm='kdtree_simple', leaf_max_size=15) indices = np.empty([self.sample_num, self.neighbor_size]) dists = [] for pt_i, pt in enumerate(points): cur_indices, cur_dists = flann.nn_index(pt, self.neighbor_size + 1) # [1,t+1] cur_indices = np.asarray(cur_indices, dtype=np.int).transpose() # [t+1,1] indices[pt_i] = cur_indices[1:, 0] dists.append(cur_dists[:, 1:]) points -= (np.max(points, axis=0, keepdims=True) + np.min(points, axis=0, keepdims=True)) / 2.0 # centralize if model == 'train': points = rotate(points) # flip normals mask = np.sum(points * normals, axis=1) < 0 normals[mask, :] = -normals[mask, :] # out points dists = np.concatenate(dists, axis=0) # [k,t] out_points = points + normals * np.mean(dists, axis=1, keepdims=True) points = np.concatenate([points, out_points], axis=0) # normalize dist = points[:, 0]**2 + points[:, 1]**2 + points[:, 2]**2 max_dist = np.sqrt(np.max(dist, keepdims=True)) points /= (max_dist + points) data.append((points, indices, label))
def subindexer_time_experiment(): """ builds plot of number of annotations vs indexer build time. TODO: time experiment """ import ibeis import utool as ut import pyflann import plottool as pt ibs = ibeis.opendb(db='PZ_Master0') daid_list = ibs.get_valid_aids() count_list = [] time_list = [] flann_params = ibs.cfg.query_cfg.flann_cfg.get_flann_params() for count in ut.ProgressIter(range(1, 301)): daids_ = daid_list[:] np.random.shuffle(daids_) daids = daids_[0:count] vecs = np.vstack(ibs.get_annot_vecs(daids)) with ut.Timer(verbose=False) as t: flann = pyflann.FLANN() flann.build_index(vecs, **flann_params) count_list.append(count) time_list.append(t.ellapsed) count_arr = np.array(count_list) time_arr = np.array(time_list) pt.plot2(count_arr, time_arr, marker='-', equal_aspect=False, x_label='num_annotations', y_label='FLANN build time')
def compute_normal(points, k=5): assert k >= 5 flann = pyflann.FLANN() flann.build_index(points, algorithm='kdtree_simple', leaf_max_size=3) normals = [] eigvals = [] indices = [] mdists = [] for pt in points: nidxs, ndists = flann.nn_index(pt, k + 1) npts = points[nidxs[0, :], :] # compute normal mean = np.mean(npts, axis=0, keepdims=True) var = (npts - mean).transpose().dot(npts - mean) eigval, eigvec = np.linalg.eigh(var) normals.append(np.expand_dims(eigvec[0], axis=0)) eigvals.append(np.expand_dims(eigval, axis=0)) # only use 5 points indices.append(nidxs[:, 1:6]) mdists.append(np.mean(np.sqrt(ndists[0, 1:6]), axis=0)) normals = np.concatenate(normals, axis=0) eigvals = np.concatenate(eigvals, axis=0) indices = np.concatenate(indices, axis=0) mdists = np.stack(mdists, axis=0) # flip normals masks = np.sum(normals * points, axis=1) < 0 normals[masks] = -normals[masks] return normals, eigvals, indices, mdists
def get_neighbors(name, num=5): """ Find the K nearest neighbors to a user in "behavior space". :param name: The GitHub username. :param num: (optioanl; default: 5) The number of neighbors to find. """ # Get the vector for this user. vector = get_vector(name) # If any of the components are None, bail. if any([v is None for v in vector]): return [] # Parse the vector. vector = parse_vector(vector) # Load the points and user names. with h5py.File(_h5_filename(points_filename), "r") as f: points = f["points"][...] usernames = f["names"][...] # Load the index. flann = pyflann.FLANN() flann.load_index(_h5_filename(index_filename), points) # Find the neighbors. inds, dists = flann.nn_index(vector, num_neighbors=num + 1) inds = inds[0] if usernames[inds[0]] == name: inds = inds[1:] else: inds = inds[:-1] return list(usernames[inds])
def test_add_loop(self): """ Test add_points using a loop when the added data goes out of scope. """ data_dim = 128 num_qpts = 100 num_dpts = 1000 random_seed = 42 rng = np.random.RandomState(0) dataset = rand_vecs(num_dpts, data_dim, rng) testset = rand_vecs(num_qpts, data_dim, rng) # Build determenistic flann object flann = pyflann.FLANN() params = flann.build_index( dataset, algorithm='kdtree', trees=4, random_seed=random_seed ) # Add points in a loop where new_pts goes out of scope num_iters = 100 for count in range(num_iters): new_pts = rand_vecs(200, data_dim, rng) flann.add_points(new_pts, 2) # query to ensure that at least some of the new points are in the results num_extra = 200 num_neighbs = num_dpts + num_extra result1, _ = flann.nn_index(testset, num_neighbs, checks=params['checks']) self.assertTrue( (result1 > num_dpts).sum() >= num_qpts * num_extra, 'at least some of the returned points should be from the added set', )
def rebuild_index(): """ Rebuild the K-nearest neighbors index based on 50000 of the most active users (ignoring the top 500 most active). """ pipe = get_pipeline() usernames = pipe.zrevrange(format_key("user"), 500, 50500).execute()[0] for user in usernames: get_vector(user, pipe=pipe) results = pipe.execute() points = np.zeros([len(usernames), nvector]) for i in range(len(usernames)): points[i, :] = parse_vector(results[8 * i:8 * (i + 1)]) flann = pyflann.FLANN() flann.build_index(points) # Save the index. fn1 = _h5_filename(index_filename) tmp1 = fn1 + ".tmp" flann.save_index(tmp1) # Save the index coordinates. fn2 = _h5_filename(points_filename) tmp2 = fn2 + ".tmp" with h5py.File(tmp2, "w") as f: f["points"] = points f["names"] = usernames # Atomically move the index files into place. shutil.move(tmp1, fn1) shutil.move(tmp2, fn2)
def Visual_Match(self, frame1, frame2): feature1 = self._config.LoadFeature(frame1) feature2 = self._config.LoadFeature(frame2) [loc1, des1] = [feature1['location'], feature1['descriptor']] [loc2, des2] = [feature2['location'], feature2['descriptor']] flann = pyflann.FLANN() result, dist = flann.nn(des2, des1, 2, algorithm="kmeans", branching=32, iterations=10, checks=200) index1 = np.arange(loc1.shape[0]) compare = (dist[:, 0].astype(np.float32) / dist[:, 1]) < self._config.Get('flann_threshold') index1 = index1[compare] index2 = result[:, 0][compare] loc1 = loc1[index1, :] loc2 = loc2[index2, :] [F1, M1] = cv2.findFundamentalMat(loc1, loc2, cv2.FM_RANSAC) [F2, M2] = cv2.findFundamentalMat(loc2, loc1, cv2.FM_RANSAC) M = M1 * M2 M = np.reshape(M, [-1]) loc1 = loc1[M == 1, :] loc2 = loc2[M == 1, :] img1 = cv2.imread(frame1, cv2.IMREAD_COLOR) img2 = cv2.imread(frame2, cv2.IMREAD_COLOR) height = img1.shape[0] width = img1.shape[1] big = np.zeros([height, 2 * width, 3], dtype=np.uint8) big[:, 0:width] = img1 big[:, width:] = img2 # bigger = max(width, height]) #loc1 *= bigger #loc2 *= bigger loc1[:, 0] += width / 2 loc1[:, 1] += height / 2 loc2[:, 0] += width / 2 loc2[:, 1] += height / 2 loc1 = loc1.astype(int) loc2[:, 0] += width loc2 = loc2.astype(int) for i in range(loc1.shape[0]): cv2.line(big, (loc1[i, 0], loc1[i, 1]), (loc2[i, 0], loc2[i, 1]), (0, 255, 0)) cv2.namedWindow('Visualize') cv2.imshow('Visualize', big) cv2.waitKey(0)
def set_current_query(self, qcx, cx2_kpts, cx2_desc, cx2_rchip_size): self.qcx = qcx self.desc1 = cx2_desc[qcx] self.kpts1 = cx2_kpts[qcx] self.vsone_flann = pyflann.FLANN() self.vsone_flann.build_index(self.desc1, **self.flann_params) self.cxs = range(len(cx2_desc)) if self.cxs is None else self.cxs
def assignChargesToNewMesh(new_vertices, old_vertices, old_charges, seeder_opts): dataset = old_vertices testset = new_vertices flann = pyflann.FLANN() new_charges = np.zeros(len(new_vertices)) if seeder_opts["feature_interpolation"]: num_inter = 4 # Number of interpolation features result, dists = flann.nn(dataset, testset, num_inter, algorithm="kdtree") # The size of result is the same as new_vertices for vi_new in range(len(result)): vi_old = result[vi_new] dist_old = dists[vi_new] # If one vertex is right on top, ignore the rest. if dist_old[0] == 0.0: new_charges[vi_new] = old_charges[vi_old[0]] continue total_dist = np.sum(1 / dist_old) for i in range(num_inter): new_charges[vi_new] += (old_charges[vi_old[i]] * (1 / dist_old[i]) / total_dist) else: result, dists = flann.nn(dataset, testset, 1, algorithm="kdtree") new_charges = old_charges[result] return new_charges
def build_geom_neighbor_graph(geoms, n_neighbors): """ Computes the sparse CSR geometrical adjacency matrix gadj Parameters ---------- geoms: (n_pts, d) array, the geometrical info n_neighbors: int, number of neighbors Returns ------- gadj: (n_pts, n_pts) sparse CSR array, the adjacency matrix gadj[i,j] == 1 iff i and j are geometrical neighbors Notes ----- gadj might not be symmetric! """ n_pts = geoms.shape[0] pyflann.set_distance_type('euclidean') # squared euclidean actually fli = pyflann.FLANN() build_params = dict(algorithm='kdtree', num_neighbors=n_neighbors) gneighbs, _ = fli.nn(geoms, geoms, **build_params) data = np.ones((n_pts, n_neighbors), dtype='u1') indptr = np.arange(0, n_pts * n_neighbors + 1, n_neighbors, dtype=int) gadj = sparse.csr_matrix( (data.ravel(), gneighbs.ravel(), indptr), shape=(n_pts, n_pts)) return gadj
def train_flann(x, model_path): samples = x.astype(np.int32) logging.info(f'Indexing samples.shape = {samples.shape}...') model = pyflann.FLANN() model.build_index(samples) logging.info(f'Saving index...') model.save_index(model_path)
def init_support(nnindexer, aid_list, vecs_list, fgws_list, verbose=True): r""" prepares inverted indicies and FLANN data structure """ assert nnindexer.flann is None, 'already initalized' _preptup = prepare_index_data(aid_list, vecs_list, fgws_list, verbose=verbose) (ax2_aid, idx2_vec, idx2_fgw, idx2_ax, idx2_fx) = _preptup nnindexer.flann = pyflann.FLANN() # Approximate search structure nnindexer.ax2_aid = ax2_aid # (A x 1) Mapping to original annot ids nnindexer.idx2_vec = idx2_vec # (M x D) Descriptors to index nnindexer.idx2_fgw = idx2_fgw # (M x 1) Descriptor forground weight nnindexer.idx2_ax = idx2_ax # (M x 1) Index into the aid_list nnindexer.idx2_fx = idx2_fx # (M x 1) Index into the annot's features nnindexer.num_indexed = nnindexer.idx2_vec.shape[0] if nnindexer.idx2_vec.dtype == hstypes.VEC_TYPE: # these are sift descriptors nnindexer.max_distance_sqrd = hstypes.VEC_PSEUDO_MAX_DISTANCE_SQRD else: # FIXME: hacky way to support siam128 descriptors. #raise AssertionError( #'NNindexer should get uint8s right now unless the algorithm has changed') nnindexer.max_distance_sqrd = None