def compute_or_read_annotation_chips(ibs, aid_list, ensure=True): """ Reads chips and tries to compute them if they do not exist """ #print('[preproc_chip] compute_or_read_chips') if ensure: try: utool.assert_all_not_None(aid_list, 'aid_list') except AssertionError as ex: utool.printex(ex, key_list=['aid_list']) raise cfpath_list = get_annot_cfpath_list(ibs, aid_list) try: if ensure: chip_list = [gtool.imread(cfpath) for cfpath in cfpath_list] else: chip_list = [None if cfpath is None else gtool.imread(cfpath) for cfpath in cfpath_list] except IOError as ex: if not utool.QUIET: utool.printex(ex, '[preproc_chip] Handing Exception: ') ibs.add_chips(aid_list) try: chip_list = [gtool.imread(cfpath) for cfpath in cfpath_list] except IOError: print('[preproc_chip] cache must have been deleted from disk') compute_and_write_chips_lazy(ibs, aid_list) # Try just one more time chip_list = [gtool.imread(cfpath) for cfpath in cfpath_list] return chip_list
def adaptive_scale(img_fpath, kpts, nScales=4, low=-.5, high=.5, nSamples=16): #imgBGR = cv2.imread(img_fpath, flags=cv2.CV_LOAD_IMAGE_COLOR) imgBGR = gtool.imread(img_fpath) nKp = len(kpts) dtype_ = kpts.dtype # Work with float65 kpts_ = np.array(kpts, dtype=np.float64) # Expand each keypoint into a number of different scales expanded_kpts = expand_scales(kpts_, nScales, low, high) # Sample gradient magnitude around the border border_vals_sum = sample_ell_border_vals(imgBGR, expanded_kpts, nKp, nScales, nSamples) # interpolate maxima subscale_kpts = subscale_peaks(border_vals_sum, kpts_, nScales, low, high) # Make sure that the new shapes are in bounds height, width = imgBGR.shape[0:2] isvalid = check_kpts_in_bounds(subscale_kpts, width, height) # Convert to the original dtype adapted_kpts = np.array(subscale_kpts[isvalid], dtype=dtype_) return adapted_kpts
def show_probability_chip(ibs, aid, species=None, fnum=None, config2_=None, **kwargs): """ TODO: allow species override in controller CommandLine: python -m ibeis.viz.viz_hough --exec-show_probability_chip --cnn --show python -m ibeis.viz.viz_hough --exec-show_probability_chip --cnn --show --db PZ_Master1 python -m ibeis.viz.viz_hough --exec-show_probability_chip --cnn --show --db PZ_Master1 --aid 9970 Example: >>> # SCRIPT >>> from ibeis.viz.viz_hough import * # NOQA >>> import ibeis >>> from ibeis.viz import viz_chip >>> ibs, aid_list, kwargs, config2_ = viz_chip.testdata_showchip() >>> fnum = 1 >>> species = None >>> aid = aid_list[0] >>> fig, ax = show_probability_chip(ibs, aid, species, fnum, **kwargs) >>> ut.show_if_requested() """ fnum = pt.ensure_fnum(fnum) title = 'Probability Chip: ' + ', '.join(vh.get_annot_text(ibs, [aid], True)) hough_cpath = ibs.get_annot_probchip_fpath(aid, config2_=config2_) img = gtool.imread(hough_cpath) fig, ax = viz_image2.show_image(img, title=title, fnum=fnum, **kwargs) return fig, ax
def vtool_adapt_rotation(img_fpath, kpts): """ rotation invariance in python """ import vtool.patch as ptool import vtool.image as gtool imgBGR = gtool.imread(img_fpath) kpts2 = ptool.find_kpts_direction(imgBGR, kpts) vecs2 = extract_vecs(img_fpath, kpts2) return kpts2, vecs2
def gen_detectimg_and_write(tup): """ worker function for parallel generator """ gid, gfpath, new_gfpath, new_size = tup #print('[preproc] writing detectimg: %r' % new_gfpath) img = gtool.imread(gfpath) new_img = gtool.resize(img, new_size) gtool.imwrite(new_gfpath, new_img) return gid, new_gfpath
def testdata_matcher(fname1='easy1.png', fname2='easy2.png'): """" fname1 = 'easy1.png' fname2 = 'hard3.png' python -m vtool.test_constrained_matching --test-visualize_matches --show Args: fname1 (str): (default = 'easy1.png') fname2 (str): (default = 'easy2.png') Returns: ?: testtup CommandLine: python -m vtool.test_constrained_matching --test-testdata_matcher Example: >>> # DISABLE_DOCTEST >>> from vtool.test_constrained_matching import * # NOQA >>> fname1 = 'easy1.png' >>> fname2 = 'easy2.png' >>> testtup = testdata_matcher(fname1, fname2) >>> result = ('testtup = %s' % (str(testtup),)) >>> print(result) """ import utool as ut #import vtool as vt from vtool import image as gtool from vtool import features as feattool fpath1 = ut.grab_test_imgpath(fname1) fpath2 = ut.grab_test_imgpath(fname2) featkw = dict(rotation_invariance=True) kpts1, vecs1 = feattool.extract_features(fpath1, **featkw) kpts2, vecs2 = feattool.extract_features(fpath2, **featkw) #if featkw['rotation_invariance']: # print('ori stats 1 ' + ut.get_stats_str(vt.get_oris(kpts2))) # print('ori stats 2 ' + ut.get_stats_str(vt.get_oris(kpts1))) rchip1 = gtool.imread(fpath1) rchip2 = gtool.imread(fpath2) #chip1_shape = vt.gtool.open_image_size(fpath1) chip2_shape = gtool.open_image_size(fpath2) dlen_sqrd2 = chip2_shape[0] ** 2 + chip2_shape[1] testtup = (rchip1, rchip2, kpts1, vecs1, kpts2, vecs2, dlen_sqrd2) return testtup
def testdata_matcher(fname1='easy1.png', fname2='easy2.png'): """" fname1 = 'easy1.png' fname2 = 'hard3.png' python -m vtool.test_constrained_matching --test-visualize_matches --show Args: fname1 (str): (default = 'easy1.png') fname2 (str): (default = 'easy2.png') Returns: ?: testtup CommandLine: python -m vtool.test_constrained_matching --test-testdata_matcher Example: >>> # DISABLE_DOCTEST >>> from vtool.test_constrained_matching import * # NOQA >>> fname1 = 'easy1.png' >>> fname2 = 'easy2.png' >>> testtup = testdata_matcher(fname1, fname2) >>> result = ('testtup = %s' % (str(testtup),)) >>> print(result) """ import utool as ut #import vtool as vt from vtool import image as gtool from vtool import features as feattool fpath1 = ut.grab_test_imgpath(fname1) fpath2 = ut.grab_test_imgpath(fname2) featkw = dict(rotation_invariance=True) kpts1, vecs1 = feattool.extract_features(fpath1, **featkw) kpts2, vecs2 = feattool.extract_features(fpath2, **featkw) #if featkw['rotation_invariance']: # print('ori stats 1 ' + ut.get_stats_str(vt.get_oris(kpts2))) # print('ori stats 2 ' + ut.get_stats_str(vt.get_oris(kpts1))) rchip1 = gtool.imread(fpath1) rchip2 = gtool.imread(fpath2) #chip1_shape = vt.gtool.open_image_size(fpath1) chip2_shape = gtool.open_image_size(fpath2) dlen_sqrd2 = chip2_shape[0]**2 + chip2_shape[1] testtup = (rchip1, rchip2, kpts1, vecs1, kpts2, vecs2, dlen_sqrd2) return testtup
def extract_chip_from_gpath(gfpath, bbox, theta, new_size, interpolation=cv2.INTER_LANCZOS4): imgBGR = gtool.imread(gfpath) # Read parent image chipBGR = extract_chip_from_img(imgBGR, bbox, theta, new_size, interpolation) return chipBGR
def show_hough(ibs, gid, species, fnum=None, **kwargs): if fnum is None: fnum = df2.next_fnum() title = 'Hough Image: ' + vh.get_image_titles(ibs, gid) print(title) hough_gpath = randomforest.get_image_hough_gpaths(ibs, [gid], species)[0] img = gtool.imread(hough_gpath) fig, ax = viz_image2.show_image(img, title=title, fnum=fnum, **kwargs) return fig, ax
def get_dummy_test_vars1(fname1='easy1.png', fname2='easy2.png'): import utool as ut from vtool import image as gtool from vtool import features as feattool fpath1 = ut.grab_test_imgpath(fname1) fpath2 = ut.grab_test_imgpath(fname2) kpts1, vecs1 = feattool.extract_features(fpath1) kpts2, vecs2 = feattool.extract_features(fpath2) chip1 = gtool.imread(fpath1) chip2 = gtool.imread(fpath2) #chip1_shape = vt.gtool.open_image_size(fpath1) #chip2_shape = gtool.open_image_size(fpath2) #dlen_sqrd2 = chip2_shape[0] ** 2 + chip2_shape[1] #testtup = (rchip1, rchip2, kpts1, vecs1, kpts2, vecs2, dlen_sqrd2) import vtool as vt checks = 800 flann_params = { 'algorithm': 'kdtree', 'trees': 8 } #pseudo_max_dist_sqrd = (np.sqrt(2) * 512) ** 2 pseudo_max_dist_sqrd = 2 * (512 ** 2) flann = vt.flann_cache(vecs1, flann_params=flann_params) import pyflann try: fx2_to_fx1, _fx2_to_dist = flann.nn_index(vecs2, num_neighbors=2, checks=checks) except pyflann.FLANNException: print('vecs1.shape = %r' % (vecs1.shape,)) print('vecs2.shape = %r' % (vecs2.shape,)) print('vecs1.dtype = %r' % (vecs1.dtype,)) print('vecs2.dtype = %r' % (vecs2.dtype,)) raise fx2_to_dist = np.divide(_fx2_to_dist, pseudo_max_dist_sqrd) fx2_to_ratio = np.divide(fx2_to_dist.T[0], fx2_to_dist.T[1]) ratio_thresh = .625 fx2_to_isvalid = fx2_to_ratio < ratio_thresh fx2_m = np.where(fx2_to_isvalid)[0] fx1_m = fx2_to_fx1.T[0].take(fx2_m) #fs_RAT = np.subtract(1.0, fx2_to_ratio.take(fx2_m)) fm_RAT = np.vstack((fx1_m, fx2_m)).T fm = fm_RAT return chip1, chip2, kpts1, kpts2, fm
def get_dummy_test_vars1(fname1='easy1.png', fname2='easy2.png'): import utool as ut from vtool import image as gtool from vtool import features as feattool fpath1 = ut.grab_test_imgpath(fname1) fpath2 = ut.grab_test_imgpath(fname2) kpts1, vecs1 = feattool.extract_features(fpath1) kpts2, vecs2 = feattool.extract_features(fpath2) chip1 = gtool.imread(fpath1) chip2 = gtool.imread(fpath2) #chip1_shape = vt.gtool.open_image_size(fpath1) #chip2_shape = gtool.open_image_size(fpath2) #dlen_sqrd2 = chip2_shape[0] ** 2 + chip2_shape[1] #testtup = (rchip1, rchip2, kpts1, vecs1, kpts2, vecs2, dlen_sqrd2) import vtool as vt checks = 800 flann_params = {'algorithm': 'kdtree', 'trees': 8} #pseudo_max_dist_sqrd = (np.sqrt(2) * 512) ** 2 pseudo_max_dist_sqrd = 2 * (512**2) flann = vt.flann_cache(vecs1, flann_params=flann_params) import pyflann try: fx2_to_fx1, _fx2_to_dist = flann.nn_index(vecs2, num_neighbors=2, checks=checks) except pyflann.FLANNException: print('vecs1.shape = %r' % (vecs1.shape, )) print('vecs2.shape = %r' % (vecs2.shape, )) print('vecs1.dtype = %r' % (vecs1.dtype, )) print('vecs2.dtype = %r' % (vecs2.dtype, )) raise fx2_to_dist = np.divide(_fx2_to_dist, pseudo_max_dist_sqrd) fx2_to_ratio = np.divide(fx2_to_dist.T[0], fx2_to_dist.T[1]) ratio_thresh = .625 fx2_to_isvalid = fx2_to_ratio < ratio_thresh fx2_m = np.where(fx2_to_isvalid)[0] fx1_m = fx2_to_fx1.T[0].take(fx2_m) #fs_RAT = np.subtract(1.0, fx2_to_ratio.take(fx2_m)) fm_RAT = np.vstack((fx1_m, fx2_m)).T fm = fm_RAT return chip1, chip2, kpts1, kpts2, fm
def load(annot): from vtool import image as gtool from vtool import features as feattool kpts, vecs = feattool.extract_features(annot.fpath) annot.kpts = kpts annot.vecs = vecs annot.rchip = gtool.imread(annot.fpath) annot.dstncvs = compute_distinctivness([annot.vecs], annot.species)[0] annot.fgweights = compute_forgroundness(annot.fpath, annot.kpts, annot.species) annot.chipshape = annot.rchip.shape annot.dlen_sqrd = annot.chipshape[0] ** 2 + annot.chipshape[1] ** 2
def load(annot): from vtool import image as gtool from vtool import features as feattool kpts, vecs = feattool.extract_features(annot.fpath) annot.kpts = kpts annot.vecs = vecs annot.rchip = gtool.imread(annot.fpath) annot.dstncvs = compute_distinctivness([annot.vecs], annot.species)[0] annot.fgweights = compute_forgroundness(annot.fpath, annot.kpts, annot.species) annot.chipshape = annot.rchip.shape annot.dlen_sqrd = annot.chipshape[0]**2 + annot.chipshape[1]**2
def testdata_matcher(fname1='easy1.png', fname2='easy2.png'): """" fname1 = 'easy1.png' fname2 = 'hard3.png' annot1 = Annot(fpath1) annot2 = Annot(fpath2) """ import utool as ut from vtool import image as gtool from vtool import features as feattool fpath1 = ut.grab_test_imgpath(fname1) fpath2 = ut.grab_test_imgpath(fname2) kpts1, vecs1 = feattool.extract_features(fpath1) kpts2, vecs2 = feattool.extract_features(fpath2) rchip1 = gtool.imread(fpath1) rchip2 = gtool.imread(fpath2) #chip1_shape = vt.gtool.open_image_size(fpath1) chip2_shape = gtool.open_image_size(fpath2) dlen_sqrd2 = chip2_shape[0]**2 + chip2_shape[1]**2 testtup = (rchip1, rchip2, kpts1, vecs1, kpts2, vecs2, dlen_sqrd2) return testtup
def testdata_matcher(fname1="easy1.png", fname2="easy2.png"): """" fname1 = 'easy1.png' fname2 = 'hard3.png' annot1 = Annot(fpath1) annot2 = Annot(fpath2) """ import utool as ut from vtool import image as gtool from vtool import features as feattool fpath1 = ut.grab_test_imgpath(fname1) fpath2 = ut.grab_test_imgpath(fname2) kpts1, vecs1 = feattool.extract_features(fpath1) kpts2, vecs2 = feattool.extract_features(fpath2) rchip1 = gtool.imread(fpath1) rchip2 = gtool.imread(fpath2) # chip1_shape = vt.gtool.open_image_size(fpath1) chip2_shape = gtool.open_image_size(fpath2) dlen_sqrd2 = chip2_shape[0] ** 2 + chip2_shape[1] ** 2 testtup = (rchip1, rchip2, kpts1, vecs1, kpts2, vecs2, dlen_sqrd2) return testtup
def compute_fgweights(ibs, aid_list, config2_=None): """ Example: >>> # SLOW_DOCTEST >>> from ibeis.algo.preproc.preproc_featweight import * # NOQA >>> import ibeis >>> ibs = ibeis.opendb('testdb1') >>> aid_list = ibs.get_valid_aids()[1:2] >>> config2_ = None >>> featweight_list = compute_fgweights(ibs, aid_list) >>> result = np.array_str(featweight_list[0][0:3], precision=3) >>> print(result) [ 0.125 0.061 0.053] """ nTasks = len(aid_list) print('[preproc_featweight.compute_fgweights] Preparing to compute %d fgweights' % (nTasks,)) probchip_fpath_list = preproc_probchip.compute_and_write_probchip(ibs, aid_list, config2_=config2_) chipsize_list = ibs.get_annot_chip_sizes(aid_list, config2_=config2_) #if ut.DEBUG2: # from PIL import Image # probchip_size_list = [Image.open(fpath).size for fpath in probchip_fpath_list] # #with ut.embed_on_exception_context: # # does not need to happen anymore # assert chipsize_list == probchip_size_list, 'probably need to clear chip or probchip cache' kpts_list = ibs.get_annot_kpts(aid_list, config2_=config2_) # Force grayscale reading of chips probchip_list = [vtimage.imread(fpath, grayscale=True) if exists(fpath) else None for fpath in probchip_fpath_list] print('[preproc_featweight.compute_fgweights] Computing %d fgweights' % (nTasks,)) arg_iter = zip(aid_list, kpts_list, probchip_list, chipsize_list) featweight_gen = utool.generate(gen_featweight_worker, arg_iter, nTasks=nTasks, ordered=True, freq=10) featweight_param_list = list(featweight_gen) #arg_iter = zip(aid_list, kpts_list, probchip_list) #featweight_param_list1 = [gen_featweight_worker((aid, kpts, probchip)) for #aid, kpts, probchip in arg_iter] #featweight_aids = ut.get_list_column(featweight_param_list, 0) featweight_list = ut.get_list_column(featweight_param_list, 1) print('[preproc_featweight.compute_fgweights] Done computing %d fgweights' % (nTasks,)) return featweight_list
def show_hough_image(ibs, gid, species=None, fnum=None, **kwargs): if fnum is None: fnum = pt.next_fnum() title = 'Hough Image: ' + vh.get_image_titles(ibs, gid) print(title) if species is None: species = ibs.cfg.detect_cfg.species_text src_gpath_list = ibs.get_image_detectpaths([gid]) dst_gpath_list = [splitext(gpath)[0] for gpath in src_gpath_list] hough_gpath_list = [gpath + '_' + species + '_hough.png' for gpath in dst_gpath_list] # Detect with hough config = { 'output_gpath_list': hough_gpath_list, } results_list = list(randomforest.detect_gpath_list_with_species(ibs, src_gpath_list, species, **config)) # NOQA # Get path hough_gpath = hough_gpath_list[0] img = gtool.imread(hough_gpath) fig, ax = viz_image2.show_image(img, title=title, fnum=fnum, **kwargs) return fig, ax
def extract_chip_from_gpath_into_square(args): gfpath, bbox, theta, target_size = args imgBGR = gtool.imread(gfpath) # Read parent image return extract_chip_into_square(imgBGR, bbox, theta, target_size)
def test_ori_extract_main(): """ CommandLine: python -m pyhesaff.tests.test_exhaustive_ori_extract --test-test_ori_extract_main python -m pyhesaff.tests.test_exhaustive_ori_extract --test-test_ori_extract_main --show Example: >>> # GUI_DOCTEST >>> from pyhesaff.tests.test_exhaustive_ori_extract import * # NOQA >>> test_ori_extract_main() >>> ut.show_if_requested() """ import pyhesaff from plottool import draw_func2 as df2 from plottool.viz_keypoints import show_keypoints import vtool # NOQA import vtool.image as gtool import vtool.keypoint as ktool np.set_printoptions(threshold=5000, linewidth=5000, precision=3) # Read data print('[rotinvar] loading test data') img_fpath = ut.grab_test_imgpath('jeff.png') imgL = gtool.cvt_BGR2L(gtool.imread(img_fpath)) detect_kw0 = { } detect_kw1 = { 'scale_min': 20, 'scale_max': 100 } detect_kw2 = { 'scale_min': 40, 'scale_max': 60 } detect_kw3 = { 'scale_min': 45, 'scale_max': 49 } # Remove skew and anisotropic scaling def force_isotropic(kpts): kpts_ = kpts.copy() kpts_[:, ktool.SKEW_DIM] = 0 kpts_[:, ktool.SCAX_DIM] = kpts_[:, ktool.SCAY_DIM] vecs_ = pyhesaff.extract_vecs(img_fpath, kpts_) return kpts_, vecs_ def force_ori(kpts, ori): kpts_ = kpts.copy() kpts_[:, ktool.ORI_DIM] = ori vecs_ = pyhesaff.extract_vecs(img_fpath, kpts_) return kpts_, vecs_ def shift_kpts(kpts, x, y): kpts_ = kpts.copy() kpts_[:, ktool.XDIM] += x kpts_[:, ktool.YDIM] += y vecs_ = pyhesaff.extract_vecs(img_fpath, kpts_) return kpts_, vecs_ # --- Experiment --- kpts0, vecs0 = double_detect(img_fpath, **detect_kw0) kpts1, vecs1 = double_detect(img_fpath, **detect_kw1) kpts2, vecs2 = double_detect(img_fpath, **detect_kw2) # kpts3, vecs3 = double_detect(img_fpath, **detect_kw3) kpts4, vecs4 = force_isotropic(kpts3) kpts5, vecs5 = force_ori(kpts3, 1.45) kpts6, vecs6 = shift_kpts(kpts5, -60, -50) kpts7, vecs7 = force_ori(kpts6, 0) kpts8, vecs8 = force_ori(kpts7, 2.40) kpts9, vecs9 = force_ori(kpts8, 5.40) kpts10, vecs10 = force_ori(kpts9, 10.40) # --- Print --- # ---- Draw ---- nRow, nCol = 1, 2 df2.figure(fnum=2, doclf=True, docla=True) df2.figure(fnum=1, doclf=True, docla=True) def show_kpts_(fnum, pnum, kpts, vecs, title): print('--------') print('show_kpts: %r.%r' % (fnum, pnum)) print('kpts = %r' % (kpts,)) print('scales = %r' % ktool.get_scales(kpts)) # FIXME: this exists in ibeis. move to vtool #dev_consistency.check_vecs(vecs3) show_keypoints(imgL, kpts, sifts=vecs, pnum=pnum, rect=True, ori=True, fnum=fnum, title=title, ell_alpha=1) show_kpts_(1, (nRow, nCol, 1), kpts3, vecs3, 'kpts3: original') show_kpts_(1, (nRow, nCol, 2), kpts4, vecs4, 'kpts4: isotropic + redetect') show_kpts_(2, (2, 3, 1), kpts5, vecs5, 'kpts5: force_ori + redetect') show_kpts_(2, (2, 3, 2), kpts6, vecs6, 'kpts6: shift') show_kpts_(2, (2, 3, 3), kpts7, vecs7, 'kpts7: shift + reorient') show_kpts_(2, (2, 3, 4), kpts8, vecs8, 'kpts8: shift + reorient') show_kpts_(2, (2, 3, 5), kpts9, vecs9, 'kpts9: reorient') show_kpts_(2, (2, 3, 6), kpts10, vecs10, 'kpts10: reorient')
def testdata_ratio_matches(fname1='easy1.png', fname2='easy2.png', **kwargs): r""" Runs simple ratio-test matching between two images. Technically this is not dummy data. Args: fname1 (str): fname2 (str): Returns: tuple : matches_testtup CommandLine: python -m vtool.tests.dummy --test-testdata_ratio_matches python -m vtool.tests.dummy --test-testdata_ratio_matches --help python -m vtool.tests.dummy --test-testdata_ratio_matches --show python -m vtool.tests.dummy --test-testdata_ratio_matches --show --ratio_thresh=1.1 --rotation_invariance python -m vtool.tests.dummy --test-testdata_ratio_matches --show --ratio_thresh=.625 --rotation_invariance --fname1 easy1.png --fname2 easy3.png python -m vtool.tests.dummy --test-testdata_ratio_matches --show --ratio_thresh=.625 --no-rotation_invariance --fname1 easy1.png --fname2 easy3.png Example: >>> # ENABLE_DOCTEST >>> from vtool.tests.dummy import * # NOQA >>> import vtool as vt >>> # build test data >>> fname1 = ut.get_argval('--fname1', type_=str, default='easy1.png') >>> fname2 = ut.get_argval('--fname2', type_=str, default='easy2.png') >>> # execute function >>> default_dict = vt.get_extract_features_default_params() >>> default_dict['ratio_thresh'] = .625 >>> kwargs = ut.argparse_dict(default_dict) >>> matches_testtup = testdata_ratio_matches(fname1, fname2, **kwargs) >>> (kpts1, kpts2, fm_RAT, fs_RAT, rchip1, rchip2) = matches_testtup >>> if ut.show_was_requested(): >>> import plottool as pt >>> pt.show_chipmatch2(rchip1, rchip2, kpts1, kpts2, fm_RAT, fs_RAT, ori=True) >>> num_matches = len(fm_RAT) >>> score_sum = sum(fs_RAT) >>> title = 'Simple matches using the Lowe\'s ratio test' >>> title += '\n num_matches=%r, score_sum=%.2f' % (num_matches, score_sum) >>> pt.set_figtitle(title) >>> pt.show_if_requested() """ import utool as ut import vtool as vt from vtool import image as gtool from vtool import features as feattool import pyflann # Get params ratio_thresh = kwargs.get('ratio_thresh', .625) print('ratio_thresh=%r' % (ratio_thresh,)) featkw = vt.get_extract_features_default_params() ut.updateif_haskey(featkw, kwargs) # Read Images fpath1 = ut.grab_test_imgpath(fname1) fpath2 = ut.grab_test_imgpath(fname2) # Extract Features kpts1, vecs1 = feattool.extract_features(fpath1, **featkw) kpts2, vecs2 = feattool.extract_features(fpath2, **featkw) rchip1 = gtool.imread(fpath1) rchip2 = gtool.imread(fpath2) # Run Algorithm def assign_nearest_neighbors(vecs1, vecs2, K=2): checks = 800 flann_params = { 'algorithm': 'kdtree', 'trees': 8 } #pseudo_max_dist_sqrd = (np.sqrt(2) * 512) ** 2 pseudo_max_dist_sqrd = 2 * (512 ** 2) flann = vt.flann_cache(vecs1, flann_params=flann_params) try: fx2_to_fx1, _fx2_to_dist = flann.nn_index(vecs2, num_neighbors=K, checks=checks) except pyflann.FLANNException: print('vecs1.shape = %r' % (vecs1.shape,)) print('vecs2.shape = %r' % (vecs2.shape,)) print('vecs1.dtype = %r' % (vecs1.dtype,)) print('vecs2.dtype = %r' % (vecs2.dtype,)) raise fx2_to_dist = np.divide(_fx2_to_dist, pseudo_max_dist_sqrd) return fx2_to_fx1, fx2_to_dist def ratio_test(fx2_to_fx1, fx2_to_dist, ratio_thresh): fx2_to_ratio = np.divide(fx2_to_dist.T[0], fx2_to_dist.T[1]) fx2_to_isvalid = fx2_to_ratio < ratio_thresh fx2_m = np.where(fx2_to_isvalid)[0] fx1_m = fx2_to_fx1.T[0].take(fx2_m) fs_RAT = np.subtract(1.0, fx2_to_ratio.take(fx2_m)) fm_RAT = np.vstack((fx1_m, fx2_m)).T # return normalizer info as well fx1_m_normalizer = fx2_to_fx1.T[1].take(fx2_m) fm_norm_RAT = np.vstack((fx1_m_normalizer, fx2_m)).T return fm_RAT, fs_RAT, fm_norm_RAT # GET NEAREST NEIGHBORS fx2_to_fx1, fx2_to_dist = assign_nearest_neighbors(vecs1, vecs2, K=2) #fx2_m = np.arange(len(fx2_to_fx1)) #fx1_m = fx2_to_fx1.T[0] #fm_ORIG = np.vstack((fx1_m, fx2_m)).T #fs_ORIG = fx2_to_dist.T[0] #fs_ORIG = 1 - np.divide(fx2_to_dist.T[0], fx2_to_dist.T[1]) #np.ones(len(fm_ORIG)) # APPLY RATIO TEST #ratio_thresh = .625 fm_RAT, fs_RAT, fm_norm_RAT = ratio_test(fx2_to_fx1, fx2_to_dist, ratio_thresh) kpts1 = kpts1.astype(np.float64) kpts2 = kpts2.astype(np.float64) matches_testtup = (kpts1, kpts2, fm_RAT, fs_RAT, rchip1, rchip2) return matches_testtup
def test_featweight_worker(): """ test function python -m ibeis.algo.preproc.preproc_featweight --test-gen_featweight_worker --show --cnn """ from ibeis.algo.hots import _pipeline_helpers as plh ibs, qreq_ = plh.get_pipeline_testdata(defaultdb='PZ_MTEST', qaid_list=[1], #qaid_list='all', preload=False, cfgdict={'featweight_detector': 'cnn'}) config2_ = qreq_.qparams lazy = True aid_list = qreq_.get_external_qaids() #aid_list = ibs.get_valid_aids()[0:30] kpts_list = ibs.get_annot_kpts(aid_list) chipsize_list = ibs.get_annot_chip_sizes(aid_list, config2_=config2_) probchip_fpath_list = preproc_probchip.compute_and_write_probchip(ibs, aid_list, lazy=lazy, config2_=config2_) print('probchip_fpath_list = %r' % (probchip_fpath_list,)) probchip_list = [vtimage.imread(fpath, grayscale=True) if exists(fpath) else None for fpath in probchip_fpath_list] _iter = list(zip(aid_list, kpts_list, probchip_list, chipsize_list)) _iter = ut.InteractiveIter(_iter, enabled=ut.get_argflag('--show')) for aid, kpts, probchip, chipsize in _iter: #kpts = kpts_list[0] #aid = aid_list[0] #probchip = probchip_list[0] #chipsize = chipsize_list[0] tup = (aid, kpts, probchip, chipsize) (aid, weights) = gen_featweight_worker(tup) if aid == 3 and ibs.get_dbname() == 'testdb1': # Run Asserts if not interactive weights_03_test = weights[0:3] print('weights[0:3] = %r' % (weights_03_test,)) #weights_03_target = [ 0.098, 0.155, 0.422] #weights_03_target = [ 0.324, 0.407, 0.688] #weights_thresh = [ 0.09, 0.09, 0.09] #ut.assert_almost_eq(weights_03_test, weights_03_target, weights_thresh) ut.assert_inbounds(weights_03_test, 0, 1) if not ut.show_was_requested(): break if ut.show_was_requested(): import plottool as pt #sfx, sfy = (probchip.shape[1] / chipsize[0], probchip.shape[0] / chipsize[1]) #kpts_ = vt.offset_kpts(kpts, (0, 0), (sfx, sfy)) pnum_ = pt.make_pnum_nextgen(1, 3) # *pt.get_square_row_cols(4)) fnum = 1 pt.figure(fnum=fnum, doclf=True) ### pt.imshow(ibs.get_annot_chips(aid, config2_=config2_), pnum=pnum_(0), fnum=fnum) if ut.get_argflag('--numlbl'): pt.gca().set_xlabel('(1)') ### pt.imshow(probchip, pnum=pnum_(2), fnum=fnum) if ut.get_argflag('--numlbl'): pt.gca().set_xlabel('(2)') #pt.draw_kpts2(kpts_, ell_alpha=.4, color_list=pt.ORANGE) ### #pt.imshow(probchip, pnum=pnum_(3), fnum=fnum) #color_list = pt.draw_kpts2(kpts_, weights=weights, ell_alpha=.7, cmap_='jet') #cb = pt.colorbar(weights, color_list) #cb.set_label('featweights') ### pt.imshow(ibs.get_annot_chips(aid, config2_=qreq_.qparams), pnum=pnum_(1), fnum=fnum) #color_list = pt.draw_kpts2(kpts, weights=weights, ell_alpha=.3, cmap_='jet') color_list = pt.draw_kpts2(kpts, weights=weights, ell_alpha=.3) cb = pt.colorbar(weights, color_list) cb.set_label('featweights') if ut.get_argflag('--numlbl'): pt.gca().set_xlabel('(3)') #pt.draw_kpts2(kpts, ell_alpha=.4) pt.draw() pt.show_if_requested()
def gridsearch_chipextract(): r""" CommandLine: python -m vtool.chip --test-gridsearch_chipextract --show Example: >>> # GRIDSEARCH >>> from vtool.chip import * # NOQA >>> gridsearch_chipextract() >>> ut.show_if_requested() """ import cv2 test_func = extract_chip_from_img if False: gpath = ut.grab_test_imgpath('carl.jpg') bbox = (100, 3, 100, 100) theta = 0.0 new_size = (58, 34) else: gpath = '/media/raid/work/GZ_Master1/_ibsdb/images/1524525d-2131-8770-d27c-3a5f9922e9e9.jpg' bbox = (450, 373, 2062, 1124) theta = 0.0 old_size = bbox[2:4] #target_area = 700 ** 2 target_area = 1200 ** 2 new_size = get_scaled_sizes_with_area(target_area, [old_size])[0] print('old_size = %r' % (old_size,)) print('new_size = %r' % (new_size,)) #new_size = (677, 369) imgBGR = gtool.imread(gpath) args = (imgBGR, bbox, theta, new_size) param_info = ut.ParamInfoList('extract_params', [ ut.ParamInfo('interpolation', cv2.INTER_LANCZOS4, varyvals=[ cv2.INTER_LANCZOS4, cv2.INTER_CUBIC, cv2.INTER_LINEAR, cv2.INTER_NEAREST, #cv2.INTER_AREA ],) ]) show_func = None # Generalize import plottool as pt pt.imshow(imgBGR) # HACK cfgdict_list, cfglbl_list = param_info.get_gridsearch_input(defaultslice=slice(0, 10)) fnum = pt.ensure_fnum(None) if show_func is None: show_func = pt.imshow lbl = ut.get_funcname(test_func) cfgresult_list = [ test_func(*args, **cfgdict) for cfgdict in ut.ProgressIter(cfgdict_list, lbl=lbl) ] onclick_func = None ut.interact_gridsearch_result_images( show_func, cfgdict_list, cfglbl_list, cfgresult_list, fnum=fnum, figtitle=lbl, unpack=False, max_plots=25, onclick_func=onclick_func) pt.iup()
def testdata_ratio_matches(fname1='easy1.png', fname2='easy2.png', **kwargs): r""" Runs simple ratio-test matching between two images. Technically this is not dummy data. Args: fname1 (str): fname2 (str): Returns: tuple : matches_testtup CommandLine: python -m vtool.tests.dummy --test-testdata_ratio_matches python -m vtool.tests.dummy --test-testdata_ratio_matches --help python -m vtool.tests.dummy --test-testdata_ratio_matches --show python -m vtool.tests.dummy --test-testdata_ratio_matches --show --ratio_thresh=1.1 --rotation_invariance python -m vtool.tests.dummy --test-testdata_ratio_matches --show --ratio_thresh=.625 --rotation_invariance --fname1 easy1.png --fname2 easy3.png python -m vtool.tests.dummy --test-testdata_ratio_matches --show --ratio_thresh=.625 --no-rotation_invariance --fname1 easy1.png --fname2 easy3.png Example: >>> # ENABLE_DOCTEST >>> from vtool.tests.dummy import * # NOQA >>> import vtool as vt >>> # build test data >>> fname1 = ut.get_argval('--fname1', type_=str, default='easy1.png') >>> fname2 = ut.get_argval('--fname2', type_=str, default='easy2.png') >>> # execute function >>> default_dict = vt.get_extract_features_default_params() >>> default_dict['ratio_thresh'] = .625 >>> kwargs = ut.argparse_dict(default_dict) >>> matches_testtup = testdata_ratio_matches(fname1, fname2, **kwargs) >>> (kpts1, kpts2, fm_RAT, fs_RAT, rchip1, rchip2) = matches_testtup >>> if ut.show_was_requested(): >>> import plottool as pt >>> pt.show_chipmatch2(rchip1, rchip2, kpts1, kpts2, fm_RAT, fs_RAT, ori=True) >>> num_matches = len(fm_RAT) >>> score_sum = sum(fs_RAT) >>> title = 'Simple matches using the Lowe\'s ratio test' >>> title += '\n num_matches=%r, score_sum=%.2f' % (num_matches, score_sum) >>> pt.set_figtitle(title) >>> pt.show_if_requested() """ import utool as ut import vtool as vt from vtool import image as gtool from vtool import features as feattool import pyflann # Get params ratio_thresh = kwargs.get('ratio_thresh', .625) print('ratio_thresh=%r' % (ratio_thresh, )) featkw = vt.get_extract_features_default_params() ut.updateif_haskey(featkw, kwargs) # Read Images fpath1 = ut.grab_test_imgpath(fname1) fpath2 = ut.grab_test_imgpath(fname2) # Extract Features kpts1, vecs1 = feattool.extract_features(fpath1, **featkw) kpts2, vecs2 = feattool.extract_features(fpath2, **featkw) rchip1 = gtool.imread(fpath1) rchip2 = gtool.imread(fpath2) # Run Algorithm def assign_nearest_neighbors(vecs1, vecs2, K=2): checks = 800 flann_params = {'algorithm': 'kdtree', 'trees': 8} #pseudo_max_dist_sqrd = (np.sqrt(2) * 512) ** 2 pseudo_max_dist_sqrd = 2 * (512**2) flann = vt.flann_cache(vecs1, flann_params=flann_params) try: fx2_to_fx1, _fx2_to_dist = flann.nn_index(vecs2, num_neighbors=K, checks=checks) except pyflann.FLANNException: print('vecs1.shape = %r' % (vecs1.shape, )) print('vecs2.shape = %r' % (vecs2.shape, )) print('vecs1.dtype = %r' % (vecs1.dtype, )) print('vecs2.dtype = %r' % (vecs2.dtype, )) raise fx2_to_dist = np.divide(_fx2_to_dist, pseudo_max_dist_sqrd) return fx2_to_fx1, fx2_to_dist def ratio_test(fx2_to_fx1, fx2_to_dist, ratio_thresh): fx2_to_ratio = np.divide(fx2_to_dist.T[0], fx2_to_dist.T[1]) fx2_to_isvalid = fx2_to_ratio < ratio_thresh fx2_m = np.where(fx2_to_isvalid)[0] fx1_m = fx2_to_fx1.T[0].take(fx2_m) fs_RAT = np.subtract(1.0, fx2_to_ratio.take(fx2_m)) fm_RAT = np.vstack((fx1_m, fx2_m)).T # return normalizer info as well fx1_m_normalizer = fx2_to_fx1.T[1].take(fx2_m) fm_norm_RAT = np.vstack((fx1_m_normalizer, fx2_m)).T return fm_RAT, fs_RAT, fm_norm_RAT # GET NEAREST NEIGHBORS fx2_to_fx1, fx2_to_dist = assign_nearest_neighbors(vecs1, vecs2, K=2) #fx2_m = np.arange(len(fx2_to_fx1)) #fx1_m = fx2_to_fx1.T[0] #fm_ORIG = np.vstack((fx1_m, fx2_m)).T #fs_ORIG = fx2_to_dist.T[0] #fs_ORIG = 1 - np.divide(fx2_to_dist.T[0], fx2_to_dist.T[1]) #np.ones(len(fm_ORIG)) # APPLY RATIO TEST #ratio_thresh = .625 fm_RAT, fs_RAT, fm_norm_RAT = ratio_test(fx2_to_fx1, fx2_to_dist, ratio_thresh) kpts1 = kpts1.astype(np.float64) kpts2 = kpts2.astype(np.float64) matches_testtup = (kpts1, kpts2, fm_RAT, fs_RAT, rchip1, rchip2) return matches_testtup
def make_wordfigures(ibs, metrics, invindex, figdir, wx_sample, wx2_dpath): """ Builds mosaics of patches assigned to words in sample ouptuts them to disk """ from plottool import draw_func2 as df2 import vtool as vt import parse vocabdir = join(figdir, 'vocab_patches2') ut.ensuredir(vocabdir) dump_word_patches(ibs, vocabdir, invindex, wx_sample, metrics) # COLLECTING PART --- collects patches in word folders #vocabdir seldpath = vocabdir + '_selected' ut.ensurepath(seldpath) # stack for show for wx, dpath in ut.progiter(six.iteritems(wx2_dpath), lbl='Dumping Word Images:', num=len(wx2_dpath), freq=1, backspace=False): #df2.rrr() fpath_list = ut.ls(dpath) fname_list = [basename(fpath_) for fpath_ in fpath_list] patch_list = [gtool.imread(fpath_) for fpath_ in fpath_list] # color each patch by nid nid_list = [int(parse.parse('{}_nid={nid}_{}', fname)['nid']) for fname in fname_list] nid_set = set(nid_list) nid_list = np.array(nid_list) if len(nid_list) == len(nid_set): # no duplicate names newpatch_list = patch_list else: # duplicate names. do coloring sortx = nid_list.argsort() patch_list = np.array(patch_list, dtype=object)[sortx] fname_list = np.array(fname_list, dtype=object)[sortx] nid_list = nid_list[sortx] colors = (255 * np.array(df2.distinct_colors(len(nid_set)))).astype(np.int32) color_dict = dict(zip(nid_set, colors)) wpad, hpad = 3, 3 newshape_list = [tuple((np.array(patch.shape) + (wpad * 2, hpad * 2, 0)).tolist()) for patch in patch_list] color_list = [color_dict[nid_] for nid_ in nid_list] newpatch_list = [np.zeros(shape) + color[None, None] for shape, color in zip(newshape_list, color_list)] for patch, newpatch in zip(patch_list, newpatch_list): newpatch[wpad:-wpad, hpad:-hpad, :] = patch #img_list = patch_list #bigpatch = vt.stack_image_recurse(patch_list) #bigpatch = vt.stack_image_list(patch_list, vert=False) bigpatch = vt.stack_square_images(newpatch_list) bigpatch_fpath = join(seldpath, basename(dpath) + '_patches.png') # def _dictstr(dict_): str_ = ut.dict_str(dict_, newlines=False) str_ = str_.replace('\'', '').replace(': ', '=').strip('{},') return str_ figtitle = '\n'.join([ 'wx=%r' % wx, 'stat(pdist): %s' % _dictstr(metrics.wx2_pdist_stats[wx]), 'stat(wdist): %s' % _dictstr(metrics.wx2_wdist_stats[wx]), ]) metrics.wx2_nMembers[wx] df2.figure(fnum=1, doclf=True, docla=True) fig, ax = df2.imshow(bigpatch, figtitle=figtitle) #fig.show() df2.set_figtitle(figtitle) df2.adjust_subplots(top=.878, bottom=0) df2.save_figure(1, bigpatch_fpath)
def test_ori_extract_main(): """ CommandLine: python -m pyhesaff.tests.test_exhaustive_ori_extract --test-test_ori_extract_main python -m pyhesaff.tests.test_exhaustive_ori_extract --test-test_ori_extract_main --show Example: >>> # GUI_DOCTEST >>> from pyhesaff.tests.test_exhaustive_ori_extract import * # NOQA >>> test_ori_extract_main() >>> ut.show_if_requested() """ import pyhesaff from plottool import draw_func2 as df2 from plottool.viz_keypoints import show_keypoints import vtool # NOQA import vtool.image as gtool import vtool.keypoint as ktool np.set_printoptions(threshold=5000, linewidth=5000, precision=3) # Read data print('[rotinvar] loading test data') img_fpath = ut.grab_test_imgpath('jeff.png') imgL = gtool.cvt_BGR2L(gtool.imread(img_fpath)) detect_kw0 = {} detect_kw1 = {'scale_min': 20, 'scale_max': 100} detect_kw2 = {'scale_min': 40, 'scale_max': 60} detect_kw3 = {'scale_min': 45, 'scale_max': 49} # Remove skew and anisotropic scaling def force_isotropic(kpts): kpts_ = kpts.copy() kpts_[:, ktool.SKEW_DIM] = 0 kpts_[:, ktool.SCAX_DIM] = kpts_[:, ktool.SCAY_DIM] vecs_ = pyhesaff.extract_vecs(img_fpath, kpts_) return kpts_, vecs_ def force_ori(kpts, ori): kpts_ = kpts.copy() kpts_[:, ktool.ORI_DIM] = ori vecs_ = pyhesaff.extract_vecs(img_fpath, kpts_) return kpts_, vecs_ def shift_kpts(kpts, x, y): kpts_ = kpts.copy() kpts_[:, ktool.XDIM] += x kpts_[:, ktool.YDIM] += y vecs_ = pyhesaff.extract_vecs(img_fpath, kpts_) return kpts_, vecs_ # --- Experiment --- kpts0, vecs0 = double_detect(img_fpath, **detect_kw0) kpts1, vecs1 = double_detect(img_fpath, **detect_kw1) kpts2, vecs2 = double_detect(img_fpath, **detect_kw2) # kpts3, vecs3 = double_detect(img_fpath, **detect_kw3) kpts4, vecs4 = force_isotropic(kpts3) kpts5, vecs5 = force_ori(kpts3, 1.45) kpts6, vecs6 = shift_kpts(kpts5, -60, -50) kpts7, vecs7 = force_ori(kpts6, 0) kpts8, vecs8 = force_ori(kpts7, 2.40) kpts9, vecs9 = force_ori(kpts8, 5.40) kpts10, vecs10 = force_ori(kpts9, 10.40) # --- Print --- # ---- Draw ---- nRow, nCol = 1, 2 df2.figure(fnum=2, doclf=True, docla=True) df2.figure(fnum=1, doclf=True, docla=True) def show_kpts_(fnum, pnum, kpts, vecs, title): print('--------') print('show_kpts: %r.%r' % (fnum, pnum)) print('kpts = %r' % (kpts, )) print('scales = %r' % ktool.get_scales(kpts)) # FIXME: this exists in ibeis. move to vtool #dev_consistency.check_vecs(vecs3) show_keypoints(imgL, kpts, sifts=vecs, pnum=pnum, rect=True, ori=True, fnum=fnum, title=title, ell_alpha=1) show_kpts_(1, (nRow, nCol, 1), kpts3, vecs3, 'kpts3: original') show_kpts_(1, (nRow, nCol, 2), kpts4, vecs4, 'kpts4: isotropic + redetect') show_kpts_(2, (2, 3, 1), kpts5, vecs5, 'kpts5: force_ori + redetect') show_kpts_(2, (2, 3, 2), kpts6, vecs6, 'kpts6: shift') show_kpts_(2, (2, 3, 3), kpts7, vecs7, 'kpts7: shift + reorient') show_kpts_(2, (2, 3, 4), kpts8, vecs8, 'kpts8: shift + reorient') show_kpts_(2, (2, 3, 5), kpts9, vecs9, 'kpts9: reorient') show_kpts_(2, (2, 3, 6), kpts10, vecs10, 'kpts10: reorient')
def gridsearch_chipextract(): r""" CommandLine: python -m vtool.chip --test-gridsearch_chipextract --show Example: >>> # GRIDSEARCH >>> from vtool.chip import * # NOQA >>> gridsearch_chipextract() >>> ut.show_if_requested() """ import cv2 test_func = extract_chip_from_img if False: gpath = ut.grab_test_imgpath('carl.jpg') bbox = (100, 3, 100, 100) theta = 0.0 new_size = (58, 34) else: gpath = '/media/raid/work/GZ_Master1/_ibsdb/images/1524525d-2131-8770-d27c-3a5f9922e9e9.jpg' bbox = (450, 373, 2062, 1124) theta = 0.0 old_size = bbox[2:4] #target_area = 700 ** 2 target_area = 1200**2 new_size = get_scaled_sizes_with_area(target_area, [old_size])[0] print('old_size = %r' % (old_size, )) print('new_size = %r' % (new_size, )) #new_size = (677, 369) imgBGR = gtool.imread(gpath) args = (imgBGR, bbox, theta, new_size) param_info = ut.ParamInfoList( 'extract_params', [ ut.ParamInfo( 'interpolation', cv2.INTER_LANCZOS4, varyvals=[ cv2.INTER_LANCZOS4, cv2.INTER_CUBIC, cv2.INTER_LINEAR, cv2.INTER_NEAREST, #cv2.INTER_AREA ], ) ]) show_func = None # Generalize import plottool as pt pt.imshow(imgBGR) # HACK cfgdict_list, cfglbl_list = param_info.get_gridsearch_input( defaultslice=slice(0, 10)) fnum = pt.ensure_fnum(None) if show_func is None: show_func = pt.imshow lbl = ut.get_funcname(test_func) cfgresult_list = [ test_func(*args, **cfgdict) for cfgdict in ut.ProgressIter(cfgdict_list, lbl=lbl) ] onclick_func = None ut.interact_gridsearch_result_images(show_func, cfgdict_list, cfglbl_list, cfgresult_list, fnum=fnum, figtitle=lbl, unpack=False, max_plots=25, onclick_func=onclick_func) pt.iup()