def reindex(nnindexer, verbose=True, memtrack=None): r""" indexes all vectors with FLANN. """ num_vecs = nnindexer.num_indexed notify_num = 1e6 verbose_ = ut.VERYVERBOSE or verbose or (not ut.QUIET and num_vecs > notify_num) if verbose_: logger.info( '[nnindex] ...building kdtree over %d points (this may take a sec).' % num_vecs) tt = ut.tic(msg='Building index') idx2_vec = nnindexer.idx2_vec flann_params = nnindexer.flann_params if num_vecs == 0: logger.info( 'WARNING: CANNOT BUILD FLANN INDEX OVER 0 POINTS. THIS MAY BE A SIGN OF A DEEPER ISSUE' ) else: if memtrack is not None: memtrack.report('BEFORE BUILD FLANN INDEX') nnindexer.flann.build_index(idx2_vec, **flann_params) if memtrack is not None: memtrack.report('AFTER BUILD FLANN INDEX') if verbose_: ut.toc(tt)
def reindex(self, verbose=True): num_vecs = len(self.wx2_word) if verbose: print('[nnindex] ...building kdtree over %d points (this may take a sec).' % num_vecs) tt = ut.tic(msg='Building vocab index') if num_vecs == 0: print('WARNING: CANNOT BUILD FLANN INDEX OVER 0 POINTS. THIS MAY BE A SIGN OF A DEEPER ISSUE') else: self.wordflann.build_index(self.wx2_word, **self.flann_params) if verbose: ut.toc(tt)
def reindex(self, verbose=True): num_vecs = len(self.wx2_word) if verbose: print( '[nnindex] ...building kdtree over %d points (this may take a sec).' % num_vecs) tt = ut.tic(msg='Building vocab index') if num_vecs == 0: print( 'WARNING: CANNOT BUILD FLANN INDEX OVER 0 POINTS. THIS MAY BE A SIGN OF A DEEPER ISSUE' ) else: self.wordflann.build_index(self.wx2_word, **self.flann_params) if verbose: ut.toc(tt)
def build(vocab, verbose=True): num_vecs = len(vocab.wx_to_word) if vocab.wordflann is None: flannclass = pickle_flann.PickleFLANN vocab.wordflann = flannclass() if verbose: logger.info(' ...build kdtree with %d points (may take a sec).' % num_vecs) tt = ut.tic(msg='Building vocab index') if num_vecs == 0: logger.info('WARNING: CANNOT BUILD FLANN INDEX OVER 0 POINTS.') logger.info('THIS MAY BE A SIGN OF A DEEPER ISSUE') else: vocab.wordflann.build_index(vocab.wx_to_word, **vocab.flann_params) if verbose: ut.toc(tt)
def reindex(nnindexer, verbose=True, memtrack=None): r""" indexes all vectors with FLANN. """ num_vecs = nnindexer.num_indexed notify_num = 1E6 verbose_ = ut.VERYVERBOSE or verbose or ( not ut.QUIET and num_vecs > notify_num) if verbose_: print('[nnindex] ...building kdtree over %d points (this may take a sec).' % num_vecs) tt = ut.tic(msg='Building index') idx2_vec = nnindexer.idx2_vec flann_params = nnindexer.flann_params if num_vecs == 0: print('WARNING: CANNOT BUILD FLANN INDEX OVER 0 POINTS. THIS MAY BE A SIGN OF A DEEPER ISSUE') else: if memtrack is not None: memtrack.report('BEFORE BUILD FLANN INDEX') nnindexer.flann.build_index(idx2_vec, **flann_params) if memtrack is not None: memtrack.report('AFTER BUILD FLANN INDEX') if verbose_: ut.toc(tt)
def TEST_SQL_NUMPY(): sqldb_fname = 'temp_test_sql_numpy.sqlite3' sqldb_dpath = utool.util_cplat.get_app_resource_dir('ibeis', 'testfiles') utool.ensuredir(sqldb_dpath) utool.util_path.remove_file(join(sqldb_dpath, sqldb_fname), dryrun=False) db = sqldbc.SQLDatabaseController(sqldb_dpath=sqldb_dpath, sqldb_fname=sqldb_fname) db.add_table('temp', [ ('temp_id', 'INTEGER PRIMARY KEY'), ('temp_hash', 'NUMPY'), ]) tt = utool.tic() feats_list = grab_numpy_testdata(shape=(3e3, 128), dtype=np.uint8) print(' * numpy.new time=%r sec' % utool.toc(tt)) print('[TEST] insert numpy arrays') tt = utool.tic() feats_iter = ((feats, ) for feats in feats_list) db.executemany(operation=''' INSERT INTO temp ( temp_hash ) VALUES (?) ''', params_iter=feats_iter) print(' * execute insert time=%r sec' % utool.toc(tt)) print('[TEST] save sql database') tt = utool.tic() #db.cur.commit() db.connection.commit() print(' * commit time=%r sec' % utool.toc(tt)) print('[TEST] read from sql database') tt = utool.tic() db.cur.execute('SELECT temp_hash FROM temp', []) print(' * execute select time=%r sec' % utool.toc(tt)) tt = utool.tic() result_list = _results_gen(db.cur) print(' * iter results time=%r sec' % utool.toc(tt)) print(' * memory(result_list) = %s' % utool.byte_str2(utool.get_object_size(result_list))) del result_list #print('[TEST] result_list=%r' % result_list) print('[TEST] dump sql database') tt = utool.tic() db.dump('temp.dump.txt') print(' * dump time=%r sec' % utool.toc(tt)) #with open('temp.dump.txt') as file_: # print(file_.read()) return locals()
def TEST_SQL_NUMPY(): sqldb_fname = 'temp_test_sql_numpy.sqlite3' sqldb_dpath = utool.util_cplat.get_app_resource_dir('ibeis', 'testfiles') utool.ensuredir(sqldb_dpath) utool.util_path.remove_file(join(sqldb_dpath, sqldb_fname), dryrun=False) db = sqldbc.SQLDatabaseController(sqldb_dpath=sqldb_dpath, sqldb_fname=sqldb_fname) db.schema('temp', [ ('temp_id', 'INTEGER PRIMARY KEY'), ('temp_hash', 'NUMPY'), ]) tt = utool.tic() feats_list = grab_numpy_testdata(shape=(3e3, 128), dtype=np.uint8) print(' * numpy.new time=%r sec' % utool.toc(tt)) print('[TEST] insert numpy arrays') tt = utool.tic() feats_iter = ((feats, ) for feats in feats_list) db.executemany(operation=''' INSERT INTO temp ( temp_hash ) VALUES (?) ''', params_iter=feats_iter) print(' * execute insert time=%r sec' % utool.toc(tt)) print('[TEST] save sql database') tt = utool.tic() #db.cur.commit() db.connection.commit() print(' * commit time=%r sec' % utool.toc(tt)) print('[TEST] read from sql database') tt = utool.tic() db.cur.execute('SELECT temp_hash FROM temp', []) print(' * execute select time=%r sec' % utool.toc(tt)) tt = utool.tic() result_list = _results_gen(db.cur) print(' * iter results time=%r sec' % utool.toc(tt)) print(' * memory(result_list) = %s' % utool.byte_str2(utool.get_object_size(result_list))) del result_list #print('[TEST] result_list=%r' % result_list) print('[TEST] dump sql database') tt = utool.tic() db.dump('temp.dump.txt') print(' * dump time=%r sec' % utool.toc(tt)) #with open('temp.dump.txt') as file_: # print(file_.read()) return locals()
def _check_for_double_click(iqrw, qtindex): threshold = 0.50 # seconds distance = utool.toc(iqrw.tt) print('Pressed %r' % (distance,)) col = qtindex.column() model = qtindex.model() colname = model.get_header_name(col) if distance <= threshold: if colname == 'status': iqrw.view.clicked.emit(qtindex) iqrw._on_click(qtindex) else: #iqrw.view.doubleClicked.emit(qtindex) iqrw._on_doubleclick(qtindex) iqrw.tt = utool.tic()
def _akmeans_iterate(data, centroids, datax2_clusterx_old, max_iters, flann_params, ave_unchanged_thresh, ave_unchanged_iterwin): """ Helper function which continues the iterations of akmeans """ num_data = data.shape[0] num_clusters = centroids.shape[0] # Keep track of how many points have changed in each iteration xx2_unchanged = np.zeros(ave_unchanged_iterwin, dtype=centroids.dtype) + len(data) print('[akmeans] Running akmeans: data.shape=%r ; num_clusters=%r' % (data.shape, num_clusters)) print('[akmeans] * max_iters = %r ' % max_iters) print('[akmeans] * ave_unchanged_iterwin=%r ; ave_unchanged_thresh=%r' % (ave_unchanged_thresh, ave_unchanged_iterwin)) #print('[akmeans] Printing akmeans info in format: time (iterx, ave(#changed), #unchanged)') xx = 0 for xx in range(0, max_iters): tt = ut.tic() ut.print_('...tic') # 1) Find each datapoints nearest cluster center (datax2_clusterx, _dist) = nn.ann_flann_once(centroids, data, 1, flann_params) ellapsed = ut.toc(tt) ut.print_('...toc(%.2fs)' % ellapsed) # 2) Compute new cluster centers centroids = _compute_cluster_centers(num_data, num_clusters, data, centroids, datax2_clusterx) # 3) Check for convergence (no change of cluster index) #ut.print_('+') num_changed = (datax2_clusterx_old != datax2_clusterx).sum() xx2_unchanged[xx % ave_unchanged_iterwin] = num_changed ave_unchanged = xx2_unchanged.mean() #ut.print_(' (%d, %.2f, %d)\n' % (xx, ave_unchanged, num_changed)) if ave_unchanged < ave_unchanged_thresh: break else: # Iterate datax2_clusterx_old = datax2_clusterx #if xx % 5 == 0: # sys.stdout.flush() if xx == max_iters: print('[akmeans] * AKMEANS: converged in %d/%d iters' % (xx + 1, max_iters)) else: print('[akmeans] * AKMEANS: reached the maximum iterations after in %d/%d iters' % (xx + 1, max_iters)) sys.stdout.flush() return (datax2_clusterx, centroids)
def detect_opencv_keypoints(): import cv2 import vtool as vt import numpy as np # NOQA #img_fpath = ut.grab_test_imgpath(ut.get_argval('--fname', default='lena.png')) img_fpath = ut.grab_test_imgpath( ut.get_argval('--fname', default='zebra.png')) imgBGR = vt.imread(img_fpath) imgGray = cv2.cvtColor(imgBGR, cv2.COLOR_BGR2GRAY) def from_cv2_kpts(cv2_kp): kp = (cv2_kp.pt[0], cv2_kp.pt[1], cv2_kp.size, 0, cv2_kp.size, cv2_kp.angle) return kp print('\n'.join(ut.search_module(cv2, 'create', recursive=True))) detect_factory = { #'BLOB': cv2.SimpleBlobDetector_create, #'HARRIS' : HarrisWrapper.create, #'SIFT': cv2.xfeatures2d.SIFT_create, # really DoG 'SURF': cv2.xfeatures2d.SURF_create, # really harris corners 'MSER': cv2.MSER_create, #'StarDetector_create', } extract_factory = { 'SIFT': cv2.xfeatures2d.SIFT_create, 'SURF': cv2.xfeatures2d.SURF_create, #'DAISY': cv2.xfeatures2d.DAISY_create, 'FREAK': cv2.xfeatures2d.FREAK_create, #'LATCH': cv2.xfeatures2d.LATCH_create, #'LUCID': cv2.xfeatures2d.LUCID_create, #'ORB': cv2.ORB_create, } mask = None type_to_kpts = {} type_to_desc = {} key = 'BLOB' key = 'MSER' for key in detect_factory.keys(): factory = detect_factory[key] extractor = factory() # For MSERS need to adapt shape and then convert into a keypoint repr if hasattr(extractor, 'detectRegions'): # bboxes are x,y,w,h regions, bboxes = extractor.detectRegions(imgGray) # ellipse definition from [Fitzgibbon95] # http://www.bmva.org/bmvc/1995/bmvc-95-050.pdf p518 # ell = [c_x, c_y, R_x, R_y, theta] # (cx, cy) = conic center # Rx and Ry = conic radii # theta is the counterclockwise angle fitz_ellipses = [cv2.fitEllipse(mser) for mser in regions] # http://answers.opencv.org/question/19015/how-to-use-mser-in-python/ #hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions] #hull_ells = [cv2.fitEllipse(hull[:, 0]) for hull in hulls] kpts_ = [] for ell in fitz_ellipses: ((cx, cy), (rx, ry), degrees) = ell theta = np.radians(degrees) # opencv lives in radians S = vt.scale_mat3x3(rx, ry) T = vt.translation_mat3x3(cx, cy) R = vt.rotation_mat3x3(theta) #R = np.eye(3) invVR = T.dot(R.dot(S)) kpt = vt.flatten_invV_mats_to_kpts(np.array([invVR]))[0] kpts_.append(kpt) kpts_ = np.array(kpts_) tt = ut.tic('Computing %r keypoints' % (key, )) try: cv2_kpts = extractor.detect(imgGray, mask) except Exception as ex: ut.printex(ex, 'Failed to computed %r keypoints' % (key, ), iswarning=True) pass else: ut.toc(tt) type_to_kpts[key] = cv2_kpts print(list(type_to_kpts.keys())) print(ut.depth_profile(list(type_to_kpts.values()))) print('type_to_kpts = ' + ut.repr3(type_to_kpts, truncate=True)) cv2_kpts = type_to_kpts['MSER'] kp = cv2_kpts[0] # NOQA #cv2.fitEllipse(cv2_kpts[0]) cv2_kpts = type_to_kpts['SURF'] for key in extract_factory.keys(): factory = extract_factory[key] extractor = factory() tt = ut.tic('Computing %r descriptors' % (key, )) try: filtered_cv2_kpts, desc = extractor.compute(imgGray, cv2_kpts) except Exception as ex: ut.printex(ex, 'Failed to computed %r descriptors' % (key, ), iswarning=True) pass else: ut.toc(tt) type_to_desc[key] = desc print(list(type_to_desc.keys())) print(ut.depth_profile(list(type_to_desc.values()))) print('type_to_desc = ' + ut.repr3(type_to_desc, truncate=True))
def create_databse(): def _randstr(size=6, chars=string.ascii_uppercase + string.digits): return ''.join(random.choice(chars) for _ in range(size)) sqldb_fname = 'data_test_qt.sqlite3' sqldb_dpath = utool.util_cplat.get_app_resource_dir('ibeis', 'testfiles') utool.ensuredir(sqldb_dpath) utool.util_path.remove_file(join(sqldb_dpath, sqldb_fname), dryrun=False) db = SQLDatabaseControl.SQLDatabaseController(sqldb_dpath=sqldb_dpath, sqldb_fname=sqldb_fname) encounters = [ ('encounter_id', 'INTEGER PRIMARY KEY'), ('encounter_name', 'TEXT'), ] db.add_table('encounters', encounters) rows = 1 * (10 ** 3) feats_iter = ( (_randstr(), ) for i in range(rows) ) print('[TEST] insert encounters') tt = utool.tic() db.executemany(operation=''' INSERT INTO encounters ( encounter_name ) VALUES (?) ''', params_iter=feats_iter) print(' * execute insert time=%r sec' % utool.toc(tt)) ############################################## headers = [ ('data_id', 'INTEGER PRIMARY KEY'), ('encounter_id', 'INT'), ('data_float', 'FLOAT'), ('data_int', 'INT'), ('data_text', 'TEXT'), ('data_text2', 'TEXT'), ] db.add_table('data', headers) col_name_list = [ column[0] for column in headers ] col_type_list = [ str ] * len(col_name_list) col_edit_list = [ False, True, True, True, True, True ] col_nice_list = [ 'ID', 'Encounter ID', 'TEST Float', 'TEST Int', 'TEST String 1', 'TEST String 2', ] rows = 1 * (10 ** 4) feats_iter = ((random.randint(0, 1000), random.uniform(0.0, 1.0), random.randint(0, 100), _randstr(), _randstr()) for i in range(rows) ) print('[TEST] insert data') tt = utool.tic() db.executemany(operation=''' INSERT INTO data ( encounter_id, data_float, data_int, data_text, data_text2 ) VALUES (?,?,?,?,?) ''', params_iter=feats_iter) print(' * execute insert time=%r sec' % utool.toc(tt)) return col_name_list, col_type_list, col_edit_list, col_nice_list, db
def detect_opencv_keypoints(): import cv2 import vtool as vt import numpy as np # NOQA #img_fpath = ut.grab_test_imgpath(ut.get_argval('--fname', default='lena.png')) img_fpath = ut.grab_test_imgpath(ut.get_argval('--fname', default='zebra.png')) imgBGR = vt.imread(img_fpath) imgGray = cv2.cvtColor(imgBGR, cv2.COLOR_BGR2GRAY) def from_cv2_kpts(cv2_kp): kp = (cv2_kp.pt[0], cv2_kp.pt[1], cv2_kp.size, 0, cv2_kp.size, cv2_kp.angle) return kp print('\n'.join(ut.search_module(cv2, 'create', recursive=True))) detect_factory = { #'BLOB': cv2.SimpleBlobDetector_create, #'HARRIS' : HarrisWrapper.create, #'SIFT': cv2.xfeatures2d.SIFT_create, # really DoG 'SURF': cv2.xfeatures2d.SURF_create, # really harris corners 'MSER': cv2.MSER_create, #'StarDetector_create', } extract_factory = { 'SIFT': cv2.xfeatures2d.SIFT_create, 'SURF': cv2.xfeatures2d.SURF_create, #'DAISY': cv2.xfeatures2d.DAISY_create, 'FREAK': cv2.xfeatures2d.FREAK_create, #'LATCH': cv2.xfeatures2d.LATCH_create, #'LUCID': cv2.xfeatures2d.LUCID_create, #'ORB': cv2.ORB_create, } mask = None type_to_kpts = {} type_to_desc = {} key = 'BLOB' key = 'MSER' for key in detect_factory.keys(): factory = detect_factory[key] extractor = factory() # For MSERS need to adapt shape and then convert into a keypoint repr if hasattr(extractor, 'detectRegions'): # bboxes are x,y,w,h regions, bboxes = extractor.detectRegions(imgGray) # ellipse definition from [Fitzgibbon95] # http://www.bmva.org/bmvc/1995/bmvc-95-050.pdf p518 # ell = [c_x, c_y, R_x, R_y, theta] # (cx, cy) = conic center # Rx and Ry = conic radii # theta is the counterclockwise angle fitz_ellipses = [cv2.fitEllipse(mser) for mser in regions] # http://answers.opencv.org/question/19015/how-to-use-mser-in-python/ #hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions] #hull_ells = [cv2.fitEllipse(hull[:, 0]) for hull in hulls] kpts_ = [] for ell in fitz_ellipses: ((cx, cy), (rx, ry), degrees) = ell theta = np.radians(degrees) # opencv lives in radians S = vt.scale_mat3x3(rx, ry) T = vt.translation_mat3x3(cx, cy) R = vt.rotation_mat3x3(theta) #R = np.eye(3) invVR = T.dot(R.dot(S)) kpt = vt.flatten_invV_mats_to_kpts(np.array([invVR]))[0] kpts_.append(kpt) kpts_ = np.array(kpts_) tt = ut.tic('Computing %r keypoints' % (key,)) try: cv2_kpts = extractor.detect(imgGray, mask) except Exception as ex: ut.printex(ex, 'Failed to computed %r keypoints' % (key,), iswarning=True) pass else: ut.toc(tt) type_to_kpts[key] = cv2_kpts print(list(type_to_kpts.keys())) print(ut.depth_profile(list(type_to_kpts.values()))) print('type_to_kpts = ' + ut.repr3(type_to_kpts, truncate=True)) cv2_kpts = type_to_kpts['MSER'] kp = cv2_kpts[0] # NOQA #cv2.fitEllipse(cv2_kpts[0]) cv2_kpts = type_to_kpts['SURF'] for key in extract_factory.keys(): factory = extract_factory[key] extractor = factory() tt = ut.tic('Computing %r descriptors' % (key,)) try: filtered_cv2_kpts, desc = extractor.compute(imgGray, cv2_kpts) except Exception as ex: ut.printex(ex, 'Failed to computed %r descriptors' % (key,), iswarning=True) pass else: ut.toc(tt) type_to_desc[key] = desc print(list(type_to_desc.keys())) print(ut.depth_profile(list(type_to_desc.values()))) print('type_to_desc = ' + ut.repr3(type_to_desc, truncate=True))
def create_databse(): def _randstr(size=6, chars=string.ascii_uppercase + string.digits): return ''.join(random.choice(chars) for _ in range(size)) sqldb_fname = 'data_test_qt.sqlite3' sqldb_dpath = utool.util_cplat.get_app_resource_dir('ibeis', 'testfiles') utool.ensuredir(sqldb_dpath) utool.util_path.remove_file(join(sqldb_dpath, sqldb_fname), dryrun=False) db = SQLDatabaseControl.SQLDatabaseController(sqldb_dpath=sqldb_dpath, sqldb_fname=sqldb_fname) imagesets = [ ('imageset_id', 'INTEGER PRIMARY KEY'), ('imageset_name', 'TEXT'), ] db.add_table('imagesets', imagesets) rows = 1 * (10**3) feats_iter = ((_randstr(), ) for i in range(rows)) print('[TEST] insert imagesets') tt = utool.tic() db.executemany(operation=''' INSERT INTO imagesets ( imageset_name ) VALUES (?) ''', params_iter=feats_iter) print(' * execute insert time=%r sec' % utool.toc(tt)) ############################################## headers = [ ('data_id', 'INTEGER PRIMARY KEY'), ('imageset_id', 'INT'), ('data_float', 'FLOAT'), ('data_int', 'INT'), ('data_text', 'TEXT'), ('data_text2', 'TEXT'), ] db.add_table('data', headers) col_name_list = [column[0] for column in headers] col_type_list = [str] * len(col_name_list) col_edit_list = [False, True, True, True, True, True] col_nice_list = [ 'ID', 'ImageSet ID', 'TEST Float', 'TEST Int', 'TEST String 1', 'TEST String 2', ] rows = 1 * (10**4) feats_iter = ((random.randint(0, 1000), random.uniform(0.0, 1.0), random.randint(0, 100), _randstr(), _randstr()) for i in range(rows)) print('[TEST] insert data') tt = utool.tic() db.executemany(operation=''' INSERT INTO data ( imageset_id, data_float, data_int, data_text, data_text2 ) VALUES (?,?,?,?,?) ''', params_iter=feats_iter) print(' * execute insert time=%r sec' % utool.toc(tt)) return col_name_list, col_type_list, col_edit_list, col_nice_list, db