def testshow_extramargin_info(ibs, aid_list, arg_list, newsize_list, halfoffset_cs_list): #cfpath, gfpath, bbox, theta, new_size, filter_list = tup # TEMP TESTING from vtool import chip as ctool import plottool as pt import vtool as vt from ibeis.viz import viz_chip index = 0 cfpath, gfpath, bbox, theta, new_size, filter_list = arg_list[index] chipBGR = ctool.compute_chip(gfpath, bbox, theta, new_size, filter_list) bbox_cs_list = [ (xo_pcs, yo_pcs, w_pcs, h_pcs) for (w_pcs, h_pcs), (xo_pcs, yo_pcs) in zip(newsize_list, halfoffset_cs_list) ] bbox_pcs = bbox_cs_list[index] aid = aid_list[0] print('new_size = %r' % (new_size,)) print('newsize_list[index] = %r' % (newsize_list[index],)) fnum = 1 viz_chip.show_chip(ibs, aid, pnum=(1, 3, 1), fnum=fnum, annote=False, in_image=True , title_suffix='\noriginal image') viz_chip.show_chip(ibs, aid, pnum=(1, 3, 2), fnum=fnum, annote=False, title_suffix='\noriginal chip') bboxed_chip = vt.draw_verts(chipBGR, vt.scaled_verts_from_bbox(bbox_pcs, theta, 1, 1)) pt.imshow(bboxed_chip, pnum=(1, 3, 3), fnum=fnum, title='scaled chip with expanded margin.\n(orig margin drawn in orange)') pt.show_if_requested()
def detect_feats_main(): import pyhesaff from pyhesaff._pyhesaff import grab_test_imgpath from pyhesaff._pyhesaff import argparse_hesaff_params import cv2 import ubelt as ub img_fpath = grab_test_imgpath(ub.argval('--fname', default='astro.png')) kwargs = argparse_hesaff_params() print('kwargs = %r' % (kwargs, )) (kpts, vecs) = pyhesaff.detect_feats(img_fpath, **kwargs) if ub.argflag('--show'): # Show keypoints imgBGR = cv2.imread(img_fpath) default_showkw = dict(ori=False, ell=True, ell_linewidth=2, ell_alpha=.4, ell_color='distinct') print('default_showkw = %r' % (default_showkw, )) import utool as ut showkw = ut.argparse_dict(default_showkw) import plottool as pt pt.interact_keypoints.ishow_keypoints(imgBGR, kpts, vecs, **showkw) pt.show_if_requested()
def testshow_extramargin_info(gfpath, bbox_gs, theta, new_size, halfoffset_ms, mbbox_gs, margin_size): import plottool as pt import vtool as vt imgBGR = vt.imread(gfpath) chipBGR = compute_chip(gfpath, bbox_gs, theta, new_size, []) mchipBGR = compute_chip(gfpath, mbbox_gs, theta, margin_size, []) #index = 0 w_cs, h_cs = new_size xo_ms, yo_ms = halfoffset_ms bbox_ms = [xo_ms, yo_ms, w_cs, h_cs] verts_gs = vt.scaled_verts_from_bbox(bbox_gs, theta, 1, 1) expanded_verts_gs = vt.scaled_verts_from_bbox(mbbox_gs, theta, 1, 1) expanded_verts_ms = vt.scaled_verts_from_bbox(bbox_ms, 0, 1, 1) # topheavy imgBGR = vt.draw_verts(imgBGR, verts_gs) imgBGR = vt.draw_verts(imgBGR, expanded_verts_gs) mchipBGR = vt.draw_verts(mchipBGR, expanded_verts_ms) fnum = 1 pt.imshow(imgBGR, pnum=(1, 3, 1), fnum=fnum, title='original image') pt.gca().set_xlabel(str(imgBGR.shape)) pt.imshow(chipBGR, pnum=(1, 3, 2), fnum=fnum, title='original chip') pt.gca().set_xlabel(str(chipBGR.shape)) pt.imshow(mchipBGR, pnum=(1, 3, 3), fnum=fnum, title='scaled chip with expanded margin.\n(orig margin drawn in orange)') pt.gca().set_xlabel(str(mchipBGR.shape)) pt.show_if_requested()
def intra_encounter_matching(): import numpy as np from scipy.sparse import coo_matrix, csgraph qreq_, cm_list = testdata_workflow() # qaids = [cm.qaid for cm in cm_list] # top_aids = [cm.get_top_aids(5) for cm in cm_list] aid_pairs = np.array([(cm.qaid, daid) for cm in cm_list for daid in cm.get_top_aids(5)]) top_scores = ut.flatten([cm.get_top_scores(5) for cm in cm_list]) N = aid_pairs.max() + 1 mat = coo_matrix((top_scores, aid_pairs.T), shape=(N, N)) csgraph.connected_components(mat) tree = csgraph.minimum_spanning_tree(mat) # NOQA import plottool as pt dense = mat.todense() pt.imshow(dense / dense.max() * 255) pt.show_if_requested() # baseline jobid import opengm # https://github.com/opengm/opengm/blob/master/src/interfaces/python/examples/tutorial/OpenGM%20tutorial.ipynb numVar = 10 unaries = np.ones([numVar, 3], dtype=opengm.value_type) gm = opengm.gm(np.ones(numVar, dtype=opengm.label_type) * 3) unary_fids = gm.addFunctions(unaries) gm.addFactors(unary_fids, np.arange(numVar)) infParam = opengm.InfParam( workflow=ut.ensure_ascii('(IC)(TTC-I,CC-I)'), ) inf = opengm.inference.Multicut(gm, parameter=infParam) visitor = inf.verboseVisitor(printNth=1, multiline=False) inf.infer(visitor) arg = inf.arg() # gridVariableIndices = opengm.secondOrderGridVis(img.shape[0], img.shape[1]) # fid = gm.addFunction(regularizer) # gm.addFactors(fid, gridVariableIndices) # regularizer = opengm.pottsFunction([3, 3], 0.0, beta) # gridVariableIndices = opengm.secondOrderGridVis(img.shape[0], img.shape[1]) # fid = gm.addFunction(regularizer) # gm.addFactors(fid, gridVariableIndices) unaries = np.random.rand(10, 10, 2) potts = opengm.PottsFunction([2, 2], 0.0, 0.4) gm = opengm.grid2d2Order(unaries=unaries, regularizer=potts) inf = opengm.inference.GraphCut(gm) inf.infer() arg = inf.arg() # NOQA """
def intra_encounter_matching(): import numpy as np from scipy.sparse import coo_matrix, csgraph qreq_, cm_list = testdata_workflow() # qaids = [cm.qaid for cm in cm_list] # top_aids = [cm.get_top_aids(5) for cm in cm_list] aid_pairs = np.array([(cm.qaid, daid) for cm in cm_list for daid in cm.get_top_aids(5)]) top_scores = ut.flatten([cm.get_top_scores(5) for cm in cm_list]) N = aid_pairs.max() + 1 mat = coo_matrix((top_scores, aid_pairs.T), shape=(N, N)) csgraph.connected_components(mat) tree = csgraph.minimum_spanning_tree(mat) # NOQA import plottool as pt dense = mat.todense() pt.imshow(dense / dense.max() * 255) pt.show_if_requested() # baseline jobid import opengm # https://github.com/opengm/opengm/blob/master/src/interfaces/python/examples/tutorial/OpenGM%20tutorial.ipynb numVar = 10 unaries = np.ones([numVar, 3], dtype=opengm.value_type) gm = opengm.gm(np.ones(numVar, dtype=opengm.label_type) * 3) unary_fids = gm.addFunctions(unaries) gm.addFactors(unary_fids, np.arange(numVar)) infParam = opengm.InfParam(workflow=ut.ensure_ascii('(IC)(TTC-I,CC-I)'), ) inf = opengm.inference.Multicut(gm, parameter=infParam) visitor = inf.verboseVisitor(printNth=1, multiline=False) inf.infer(visitor) arg = inf.arg() # gridVariableIndices = opengm.secondOrderGridVis(img.shape[0], img.shape[1]) # fid = gm.addFunction(regularizer) # gm.addFactors(fid, gridVariableIndices) # regularizer = opengm.pottsFunction([3, 3], 0.0, beta) # gridVariableIndices = opengm.secondOrderGridVis(img.shape[0], img.shape[1]) # fid = gm.addFunction(regularizer) # gm.addFactors(fid, gridVariableIndices) unaries = np.random.rand(10, 10, 2) potts = opengm.PottsFunction([2, 2], 0.0, 0.4) gm = opengm.grid2d2Order(unaries=unaries, regularizer=potts) inf = opengm.inference.GraphCut(gm) inf.infer() arg = inf.arg() # NOQA """
def testshow_extramargin_info(ibs, aid_list, arg_list, newsize_list, halfoffset_cs_list): #cfpath, gfpath, bbox, theta, new_size, filter_list = tup # TEMP TESTING from vtool import chip as ctool import plottool as pt import vtool as vt from ibeis.viz import viz_chip index = 0 cfpath, gfpath, expanded_bbox, theta, expanded_new_size, filter_list = arg_list[ index] expanded_chipBGR = ctool.compute_chip(gfpath, expanded_bbox, theta, expanded_new_size, filter_list) bbox_cs_list = [ (xo_pcs, yo_pcs, w_pcs, h_pcs) for (w_pcs, h_pcs), (xo_pcs, yo_pcs) in zip(newsize_list, halfoffset_cs_list) ] bbox_pcs = bbox_cs_list[index] aid = aid_list[0] #print('new_size = %r' % (new_size,)) print('newsize_list[index] = %r' % (newsize_list[index], )) fnum = 1 viz_chip.show_chip(ibs, aid, pnum=(1, 3, 1), fnum=fnum, annote=False, in_image=True, title_suffix='\noriginal image') viz_chip.show_chip(ibs, aid, pnum=(1, 3, 2), fnum=fnum, annote=False, title_suffix='\noriginal chip') bboxed_chip = vt.draw_verts( expanded_chipBGR, vt.scaled_verts_from_bbox(bbox_pcs, theta, 1, 1)) pt.imshow( bboxed_chip, pnum=(1, 3, 3), fnum=fnum, title='scaled chip with expanded margin.\n(orig margin drawn in orange)' ) pt.gca().set_xlabel(str(bboxed_chip.shape)) pt.show_if_requested()
def testshow_colors(rgb_list, gray=ut.get_argflag('--gray')): import plottool as pt import vtool as vt block = np.zeros((5, 5, 3)) block_list = [block + color[0:3] for color in rgb_list] #print(ut.list_str(block_list)) #print(ut.list_str(rgb_list)) stacked_block = vt.stack_image_list(block_list, vert=False) # convert to bgr stacked_block = stacked_block[:, :, ::-1] uint8_img = (255 * stacked_block).astype(np.uint8) if gray: import cv2 uint8_img = cv2.cvtColor(uint8_img, cv2.COLOR_RGB2GRAY) pt.imshow(uint8_img) pt.show_if_requested()
def testshow_extramargin_info(gfpath, bbox_gs, theta, new_size, halfoffset_ms, mbbox_gs, margin_size): import plottool as pt import vtool as vt imgBGR = vt.imread(gfpath) chipBGR = compute_chip(gfpath, bbox_gs, theta, new_size, []) mchipBGR = compute_chip(gfpath, mbbox_gs, theta, margin_size, []) #index = 0 w_cs, h_cs = new_size xo_ms, yo_ms = halfoffset_ms bbox_ms = [xo_ms, yo_ms, w_cs, h_cs] verts_gs = vt.scaled_verts_from_bbox(bbox_gs, theta, 1, 1) expanded_verts_gs = vt.scaled_verts_from_bbox(mbbox_gs, theta, 1, 1) expanded_verts_ms = vt.scaled_verts_from_bbox(bbox_ms, 0, 1, 1) # topheavy imgBGR = vt.draw_verts(imgBGR, verts_gs) imgBGR = vt.draw_verts(imgBGR, expanded_verts_gs) mchipBGR = vt.draw_verts(mchipBGR, expanded_verts_ms) fnum = 1 pt.imshow(imgBGR, pnum=(1, 3, 1), fnum=fnum, title='original image') pt.gca().set_xlabel(str(imgBGR.shape)) pt.imshow(chipBGR, pnum=(1, 3, 2), fnum=fnum, title='original chip') pt.gca().set_xlabel(str(chipBGR.shape)) pt.imshow( mchipBGR, pnum=(1, 3, 3), fnum=fnum, title='scaled chip with expanded margin.\n(orig margin drawn in orange)' ) pt.gca().set_xlabel(str(mchipBGR.shape)) pt.show_if_requested()
def drive_test_script(ibs): r""" Test script where we drive around and take pictures of animals both in a given database and not in a given databse to make sure the system works. CommandLine: python -m ibeis.viz.viz_image --test-drive_test_script python -m ibeis.viz.viz_image --test-drive_test_script --db PZ_MTEST --show python -m ibeis.viz.viz_image --test-drive_test_script --db GIR_Tanya --show python -m ibeis.viz.viz_image --test-drive_test_script --db GIR_Master0 --show python -m ibeis.viz.viz_image --test-drive_test_script --db PZ_Master0 --show python -m ibeis.viz.viz_image --test-drive_test_script --db PZ_FlankHack --show python -m ibeis.viz.viz_image --test-drive_test_script --db PZ_FlankHack --show python -m ibeis.viz.viz_image --test-drive_test_script --dbdir /raid/work2/Turk/GIR_Master --show Example: >>> # DISABLE_DOCTEST >>> from ibeis.viz.viz_image import * # NOQA >>> import ibeis >>> ibs = ibeis.opendb() >>> drive_test_script(ibs) """ import ibeis aid_list = ibeis.testdata_aids(a='default:pername=1') print('Running with (annot) aid_list = %r' % (aid_list)) gid_list = ibs.get_annot_gids(aid_list) print('Running with (image) gid_list = %r' % (gid_list)) avuuid_list = ibs.get_annot_visual_uuids(aid_list) guuid_list = ibs.get_image_uuids(gid_list) print('Running with annot_visual_uuid_list = %s' % (ut.list_str(zip(aid_list, avuuid_list)))) print('Running with image_uuid_list = %s' % (ut.list_str(zip(gid_list, guuid_list)))) for gid, aid in ut.ProgressIter(zip(gid_list, aid_list), lbl='progress '): print('\ngid, aid, nid = %r, %r, %r' % (gid, aid, ibs.get_annot_nids(aid),)) show_image(ibs, gid, annote=False, rich_title=True) pt.show_if_requested()
def fourier_devtest(img): r""" Args: img (ndarray[uint8_t, ndim=2]): image data CommandLine: python -m vtool.quality_classifier --test-fourier_devtest --show References: http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_imgproc/py_transforms/py_fourier_transform/py_fourier_transform.html http://cns-alumni.bu.edu/~slehar/fourier/fourier.html Example: >>> # DISABLE_DOCTEST >>> from vtool.quality_classifier import * # NOQA >>> import vtool as vt >>> # build test data >>> img_fpath = ut.grab_test_imgpath('lena.png') >>> img = vt.imread(img_fpath, grayscale=True) >>> # execute function >>> magnitude_spectrum = fourier_devtest(img) """ import plottool as pt def pad_img(img): rows, cols = img.shape nrows = cv2.getOptimalDFTSize(rows) ncols = cv2.getOptimalDFTSize(cols) right = ncols - cols bottom = nrows - rows bordertype = cv2.BORDER_CONSTANT nimg = cv2.copyMakeBorder(img, 0, bottom, 0, right, bordertype, value=0) return nimg def convert_to_fdomain(img): dft = cv2.dft(img.astype(np.float32), flags=cv2.DFT_COMPLEX_OUTPUT) #dft_shift = np.fft.fftshift(dft) return dft def convert_from_fdomain(dft): img = cv2.idft(dft) img = cv2.magnitude(img[:, :, 0], img[:, :, 1]) img /= img.max() return img * 255.0 def get_fdomain_mag(dft_shift): magnitude_spectrum = np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1])) return magnitude_spectrum def imgstats(img): print('stats:') print(' dtype = %r ' % (img.dtype,)) print(' ' + ut.get_stats_str(img, axis=None)) nimg = pad_img(img) dft = convert_to_fdomain(nimg) #freq_domain = np.fft.fft2(img) #freq_domain_shift = np.fft.fftshift(freq_domain) rows, cols = nimg.shape crow, ccol = rows / 2 , cols / 2 # create a mask first, center square is 1, remaining all zeros mask = np.zeros((rows, cols, 2), np.uint8) mask[crow - 30:crow + 30, ccol - 30:ccol + 30] = 1 dft_mask = np.fft.ifftshift(np.fft.fftshift(dft) * mask) img_back = convert_from_fdomain(dft_mask) imgstats(dft) imgstats(mask) imgstats(nimg) imgstats(nimg) print('nimg.shape = %r' % (nimg.shape,)) print('dft_shift.shape = %r' % (dft.shape,)) if ut.show_was_requested(): #import plottool as pt next_pnum = pt.make_pnum_nextgen(nRows=3, nCols=2) pt.imshow(nimg, pnum=next_pnum(), title='nimg') pt.imshow(20 * get_fdomain_mag(dft), pnum=next_pnum(), title='mag(f)') pt.imshow(20 * get_fdomain_mag(dft_mask), pnum=next_pnum(), title='dft_mask') pt.imshow(img_back, pnum=next_pnum(), title='img_back') pt.show_if_requested()
def test_rot_invar(): r""" CommandLine: python -m pyhesaff test_rot_invar --show --rebuild-hesaff --no-rmbuild python -m pyhesaff test_rot_invar --show --nocpp python -m vtool.tests.dummy testdata_ratio_matches --show --ratio_thresh=1.0 --rotation_invariance --rebuild-hesaff python -m vtool.tests.dummy testdata_ratio_matches --show --ratio_thresh=1.1 --rotation_invariance --rebuild-hesaff Example: >>> # DISABLE_DODCTEST >>> from pyhesaff._pyhesaff import * # NOQA >>> test_rot_invar() """ import cv2 import utool as ut import vtool as vt import plottool as pt TAU = 2 * np.pi fnum = pt.next_fnum() NUM_PTS = 5 # 9 theta_list = np.linspace(0, TAU, NUM_PTS, endpoint=False) nRows, nCols = pt.get_square_row_cols(len(theta_list), fix=True) next_pnum = pt.make_pnum_nextgen(nRows, nCols) # Expand the border a bit around star.png pad_ = 100 img_fpath = ut.grab_test_imgpath('star.png') img_fpath2 = vt.pad_image_ondisk(img_fpath, pad_, value=26) for theta in theta_list: print('-----------------') print('theta = %r' % (theta,)) #theta = ut.get_argval('--theta', type_=float, default=TAU * 3 / 8) img_fpath = vt.rotate_image_ondisk(img_fpath2, theta, borderMode=cv2.BORDER_REPLICATE) if not ut.get_argflag('--nocpp'): (kpts_list_ri, vecs_list2) = detect_feats(img_fpath, rotation_invariance=True) kpts_ri = ut.strided_sample(kpts_list_ri, 2) (kpts_list_gv, vecs_list1) = detect_feats(img_fpath, rotation_invariance=False) kpts_gv = ut.strided_sample(kpts_list_gv, 2) # find_kpts_direction imgBGR = vt.imread(img_fpath) kpts_ripy = vt.find_kpts_direction(imgBGR, kpts_gv, DEBUG_ROTINVAR=False) # Verify results stdout #print('nkpts = %r' % (len(kpts_gv))) #print(vt.kpts_repr(kpts_gv)) #print(vt.kpts_repr(kpts_ri)) #print(vt.kpts_repr(kpts_ripy)) # Verify results plot pt.figure(fnum=fnum, pnum=next_pnum()) pt.imshow(imgBGR) #if len(kpts_gv) > 0: # pt.draw_kpts2(kpts_gv, ori=True, ell_color=pt.BLUE, ell_linewidth=10.5) ell = False rect = True if not ut.get_argflag('--nocpp'): if len(kpts_ri) > 0: pt.draw_kpts2(kpts_ri, rect=rect, ell=ell, ori=True, ell_color=pt.RED, ell_linewidth=5.5) if len(kpts_ripy) > 0: pt.draw_kpts2(kpts_ripy, rect=rect, ell=ell, ori=True, ell_color=pt.GREEN, ell_linewidth=3.5) #print('\n'.join(vt.get_ori_strs(np.vstack([kpts_gv, kpts_ri, kpts_ripy])))) #ut.embed(exec_lines=['pt.update()']) pt.set_figtitle('green=python, red=C++') pt.show_if_requested()
def test_sver_wrapper(): """ Test to ensure cpp and python agree and that cpp is faster CommandLine: python -m vtool.sver_c_wrapper --test-test_sver_wrapper python -m vtool.sver_c_wrapper --test-test_sver_wrapper --rebuild-sver python -m vtool.sver_c_wrapper --test-test_sver_wrapper --show python -m vtool.sver_c_wrapper --test-test_sver_wrapper --show --dummy python -m vtool.sver_c_wrapper --test-test_sver_wrapper --show --fname1=easy1.png --fname2=easy2.png python -m vtool.sver_c_wrapper --test-test_sver_wrapper --show --fname1=easy1.png --fname2=hard3.png python -m vtool.sver_c_wrapper --test-test_sver_wrapper --show --fname1=carl.jpg --fname2=hard3.png Example: >>> # ENABLE_DOCTEST >>> from vtool.sver_c_wrapper import * # NOQA >>> test_sver_wrapper() Ignore: %timeit call_python_version(*args) %timeit get_affine_inliers_cpp(*args) """ import vtool.spatial_verification as sver import vtool.tests.dummy as dummy xy_thresh_sqrd = ktool.KPTS_DTYPE(.4) scale_thresh_sqrd = ktool.KPTS_DTYPE(2.0) ori_thresh = ktool.KPTS_DTYPE(TAU / 4.0) keys = 'xy_thresh_sqrd, scale_thresh_sqrd, ori_thresh'.split(', ') print(ut.dict_str(ut.dict_subset(locals(), keys))) def report_errors(): pass if ut.get_argflag('--dummy'): testtup = dummy.testdata_dummy_matches() (kpts1, kpts2, fm_input, fs_input, rchip1, rchip2) = testtup fm_input = fm_input.astype(fm_dtype) #fm_input = fm_input[0:10].astype(fm_dtype) #fs_input = fs_input[0:10].astype(np.float32) else: fname1 = ut.get_argval('--fname1', type_=str, default='easy1.png') fname2 = ut.get_argval('--fname2', type_=str, default='easy2.png') testtup = dummy.testdata_ratio_matches(fname1, fname2) (kpts1, kpts2, fm_input, fs_input, rchip1, rchip2) = testtup # pack up call to aff hypothesis import vtool as vt import scipy.stats.mstats scales1 = vt.get_scales(kpts1.take(fm_input.T[0], axis=0)) scales2 = vt.get_scales(kpts2.take(fm_input.T[1], axis=0)) #fs_input = 1 / scipy.stats.mstats.gmean(np.vstack((scales1, scales2))) fs_input = scipy.stats.mstats.gmean(np.vstack((scales1, scales2))) print('fs_input = ' + ut.numpy_str(fs_input)) #fs_input[0:-9] = 0 #fs_input = np.ones(len(fm_input), dtype=fs_dtype) #ut.embed() #fs_input = scales1 * scales2 args = (kpts1, kpts2, fm_input, fs_input, xy_thresh_sqrd, scale_thresh_sqrd, ori_thresh) ex_list = [] try: with ut.Indenter('[TEST1] '): inlier_tup = vt.compare_implementations( sver.get_affine_inliers, get_affine_inliers_cpp, args, lbl1='py', lbl2='c', output_lbl=('aff_inliers_list', 'aff_errors_list', 'Aff_mats')) out_inliers, out_errors, out_mats = inlier_tup except AssertionError as ex: ex_list.append(ex) raise try: import functools with ut.Indenter('[TEST2] '): bestinlier_tup = vt.compare_implementations( functools.partial(sver.get_best_affine_inliers, forcepy=True), get_best_affine_inliers_cpp, args, show_output=True, lbl1='py', lbl2='c', output_lbl=('bestinliers', 'besterror', 'bestmat')) bestinliers, besterror, bestmat = bestinlier_tup except AssertionError as ex: ex_list.append(ex) raise if len(ex_list) > 0: raise AssertionError('some tests failed. see previous stdout') #num_inliers_list = np.array(map(len, out_inliers_c)) #best_argx = num_inliers_list.argmax() ##best_inliers_py = out_inliers_py[best_argx] #best_inliers_c = out_inliers_c[best_argx] if ut.show_was_requested(): import plottool as pt fm_output = fm_input.take(bestinliers, axis=0) fnum = pt.next_fnum() pt.figure(fnum=fnum, doclf=True, docla=True) pt.show_chipmatch2(rchip1, rchip2, kpts1, kpts2, fm_input, ell_linewidth=5, fnum=fnum, pnum=(2, 1, 1)) pt.show_chipmatch2(rchip1, rchip2, kpts1, kpts2, fm_output, ell_linewidth=5, fnum=fnum, pnum=(2, 1, 2)) pt.show_if_requested()
def test_sver_wrapper(): """ Test to ensure cpp and python agree and that cpp is faster CommandLine: python -m vtool.sver_c_wrapper --test-test_sver_wrapper python -m vtool.sver_c_wrapper --test-test_sver_wrapper --rebuild-sver python -m vtool.sver_c_wrapper --test-test_sver_wrapper --show python -m vtool.sver_c_wrapper --test-test_sver_wrapper --show --dummy python -m vtool.sver_c_wrapper --test-test_sver_wrapper --show --fname1=easy1.png --fname2=easy2.png python -m vtool.sver_c_wrapper --test-test_sver_wrapper --show --fname1=easy1.png --fname2=hard3.png python -m vtool.sver_c_wrapper --test-test_sver_wrapper --show --fname1=carl.jpg --fname2=hard3.png Example: >>> # ENABLE_DOCTEST >>> from vtool.sver_c_wrapper import * # NOQA >>> test_sver_wrapper() Ignore: %timeit call_python_version(*args) %timeit get_affine_inliers_cpp(*args) """ import vtool.spatial_verification as sver import vtool.tests.dummy as dummy xy_thresh_sqrd = ktool.KPTS_DTYPE(.4) scale_thresh_sqrd = ktool.KPTS_DTYPE(2.0) ori_thresh = ktool.KPTS_DTYPE(TAU / 4.0) keys = 'xy_thresh_sqrd, scale_thresh_sqrd, ori_thresh'.split(', ') print(ut.dict_str(ut.dict_subset(locals(), keys))) def report_errors(): pass if ut.get_argflag('--dummy'): testtup = dummy.testdata_dummy_matches() (kpts1, kpts2, fm_input, fs_input, rchip1, rchip2) = testtup fm_input = fm_input.astype(fm_dtype) #fm_input = fm_input[0:10].astype(fm_dtype) #fs_input = fs_input[0:10].astype(np.float32) else: fname1 = ut.get_argval('--fname1', type_=str, default='easy1.png') fname2 = ut.get_argval('--fname2', type_=str, default='easy2.png') testtup = dummy.testdata_ratio_matches(fname1, fname2) (kpts1, kpts2, fm_input, fs_input, rchip1, rchip2) = testtup # pack up call to aff hypothesis import vtool as vt import scipy.stats.mstats scales1 = vt.get_scales(kpts1.take(fm_input.T[0], axis=0)) scales2 = vt.get_scales(kpts2.take(fm_input.T[1], axis=0)) #fs_input = 1 / scipy.stats.mstats.gmean(np.vstack((scales1, scales2))) fs_input = scipy.stats.mstats.gmean(np.vstack((scales1, scales2))) print('fs_input = ' + ut.numpy_str(fs_input)) #fs_input[0:-9] = 0 #fs_input = np.ones(len(fm_input), dtype=fs_dtype) #ut.embed() #fs_input = scales1 * scales2 args = (kpts1, kpts2, fm_input, fs_input, xy_thresh_sqrd, scale_thresh_sqrd, ori_thresh) ex_list = [] try: with ut.Indenter('[TEST1] '): inlier_tup = vt.compare_implementations( sver.get_affine_inliers, get_affine_inliers_cpp, args, lbl1='py', lbl2='c', output_lbl=('aff_inliers_list', 'aff_errors_list', 'Aff_mats') ) out_inliers, out_errors, out_mats = inlier_tup except AssertionError as ex: ex_list.append(ex) raise try: import functools with ut.Indenter('[TEST2] '): bestinlier_tup = vt.compare_implementations( functools.partial(sver.get_best_affine_inliers, forcepy=True), get_best_affine_inliers_cpp, args, show_output=True, lbl1='py', lbl2='c', output_lbl=('bestinliers', 'besterror', 'bestmat') ) bestinliers, besterror, bestmat = bestinlier_tup except AssertionError as ex: ex_list.append(ex) raise if len(ex_list) > 0: raise AssertionError('some tests failed. see previous stdout') #num_inliers_list = np.array(map(len, out_inliers_c)) #best_argx = num_inliers_list.argmax() ##best_inliers_py = out_inliers_py[best_argx] #best_inliers_c = out_inliers_c[best_argx] if ut.show_was_requested(): import plottool as pt fm_output = fm_input.take(bestinliers, axis=0) fnum = pt.next_fnum() pt.figure(fnum=fnum, doclf=True, docla=True) pt.show_chipmatch2(rchip1, rchip2, kpts1, kpts2, fm_input, ell_linewidth=5, fnum=fnum, pnum=(2, 1, 1)) pt.show_chipmatch2(rchip1, rchip2, kpts1, kpts2, fm_output, ell_linewidth=5, fnum=fnum, pnum=(2, 1, 2)) pt.show_if_requested()
def intra_encounter_matching(): qreq_, cm_list = testdata_workflow() # qaids = [cm.qaid for cm in cm_list] # top_aids = [cm.get_top_aids(5) for cm in cm_list] import numpy as np from scipy.sparse import coo_matrix, csgraph aid_pairs = np.array([(cm.qaid, daid) for cm in cm_list for daid in cm.get_top_aids(5)]) top_scores = ut.flatten([cm.get_top_scores(5) for cm in cm_list]) N = aid_pairs.max() + 1 mat = coo_matrix((top_scores, aid_pairs.T), shape=(N, N)) csgraph.connected_components(mat) tree = csgraph.minimum_spanning_tree(mat) # NOQA import plottool as pt dense = mat.todense() pt.imshow(dense / dense.max() * 255) pt.show_if_requested() # load image and convert to LAB img_fpath = str(ut.grab_test_imgpath(str('lena.png'))) img = vigra.impex.readImage(img_fpath) imgLab = vigra.colors.transform_RGB2Lab(img) superpixelDiameter = 15 # super-pixel size slicWeight = 15.0 # SLIC color - spatial weight labels, nseg = vigra.analysis.slicSuperpixels(imgLab, slicWeight, superpixelDiameter) labels = vigra.analysis.labelImage(labels)-1 # get 2D grid graph and RAG gridGraph = graphs.gridGraph(img.shape[0:2]) rag = graphs.regionAdjacencyGraph(gridGraph, labels) nodeFeatures = rag.accumulateNodeFeatures(imgLab) nodeFeaturesImg = rag.projectNodeFeaturesToGridGraph(nodeFeatures) nodeFeaturesImg = vigra.taggedView(nodeFeaturesImg, "xyc") nodeFeaturesImgRgb = vigra.colors.transform_Lab2RGB(nodeFeaturesImg) #from sklearn.cluster import MiniBatchKMeans, KMeans from sklearn import mixture nCluster = 3 g = mixture.GMM(n_components=nCluster) g.fit(nodeFeatures[:,:]) clusterProb = g.predict_proba(nodeFeatures) import numpy #https://github.com/opengm/opengm/blob/master/src/interfaces/python/examples/tutorial/Irregular%20Factor%20Graphs.ipynb #https://github.com/opengm/opengm/blob/master/src/interfaces/python/examples/tutorial/Hard%20and%20Soft%20Constraints.ipynb clusterProbImg = rag.projectNodeFeaturesToGridGraph(clusterProb.astype(numpy.float32)) clusterProbImg = vigra.taggedView(clusterProbImg, "xyc") # strength of potts regularizer beta = 40.0 # graphical model with as many variables # as superpixels, each has 3 states gm = opengm.gm(numpy.ones(rag.nodeNum,dtype=opengm.label_type)*nCluster) # convert probabilites to energies probs = numpy.clip(clusterProb, 0.00001, 0.99999) costs = -1.0*numpy.log(probs) # add ALL unaries AT ONCE fids = gm.addFunctions(costs) gm.addFactors(fids,numpy.arange(rag.nodeNum)) # add a potts function regularizer = opengm.pottsFunction([nCluster]*2,0.0,beta) fid = gm.addFunction(regularizer) # get variable indices of adjacent superpixels # - or "u" and "v" node id's for edges uvIds = rag.uvIds() uvIds = numpy.sort(uvIds,axis=1) # add all second order factors at once gm.addFactors(fid,uvIds) # get super-pixels with slic on LAB image import opengm # Matching Graph cost_matrix = np.array([ [0.5, 0.6, 0.2, 0.4, 0.1], [0.0, 0.5, 0.2, 0.9, 0.2], [0.0, 0.0, 0.5, 0.1, 0.1], [0.0, 0.0, 0.0, 0.5, 0.1], [0.0, 0.0, 0.0, 0.0, 0.5], ]) cost_matrix += cost_matrix.T number_of_labels = 5 num_annots = 5 cost_matrix = (cost_matrix * 2) - 1 #gm = opengm.gm(number_of_labels) gm = opengm.gm(np.ones(num_annots) * number_of_labels) aids = np.arange(num_annots) aid_pairs = np.array([(a1, a2) for a1, a2 in ut.iprod(aids, aids) if a1 != a2], dtype=np.uint32) aid_pairs.sort(axis=1) # 2nd order function fid = gm.addFunction(cost_matrix) gm.addFactors(fid, aid_pairs) Inf = opengm.inference.BeliefPropagation #Inf = opengm.inference.Multicut parameter = opengm.InfParam(steps=10, damping=0.5, convergenceBound=0.001) parameter = opengm.InfParam() inf = Inf(gm, parameter=parameter) class PyCallback(object): def __init__(self,): self.labels=[] pass def begin(self,inference): print("begin of inference") pass def end(self,inference): self.labels.append(inference.arg()) pass def visit(self,inference): gm=inference.gm() labelVector=inference.arg() print("energy %r" % (gm.evaluate(labelVector),)) self.labels.append(labelVector) pass callback=PyCallback() visitor=inf.pythonVisitor(callback,visitNth=1) inf.infer(visitor) print(callback.labels) # baseline jobid # https://github.com/opengm/opengm/blob/master/src/interfaces/python/examples/tutorial/OpenGM%20tutorial.ipynb numVar = 10 unaries = np.ones([numVar, 3], dtype=opengm.value_type) gm = opengm.gm(np.ones(numVar, dtype=opengm.label_type) * 3) unary_fids = gm.addFunctions(unaries) gm.addFactors(unary_fids, np.arange(numVar)) infParam = opengm.InfParam( workflow=ut.ensure_ascii('(IC)(TTC-I,CC-I)'), ) inf = opengm.inference.Multicut(gm, parameter=infParam) visitor = inf.verboseVisitor(printNth=1, multiline=False) inf.infer(visitor) arg = inf.arg() # gridVariableIndices = opengm.secondOrderGridVis(img.shape[0], img.shape[1]) # fid = gm.addFunction(regularizer) # gm.addFactors(fid, gridVariableIndices) # regularizer = opengm.pottsFunction([3, 3], 0.0, beta) # gridVariableIndices = opengm.secondOrderGridVis(img.shape[0], img.shape[1]) # fid = gm.addFunction(regularizer) # gm.addFactors(fid, gridVariableIndices) unaries = np.random.rand(10, 10, 2) potts = opengm.PottsFunction([2, 2], 0.0, 0.4) gm = opengm.grid2d2Order(unaries=unaries, regularizer=potts) inf = opengm.inference.GraphCut(gm) inf.infer() arg = inf.arg() # NOQA """
def test_featweight_worker(): """ test function python -m ibeis.algo.preproc.preproc_featweight --test-gen_featweight_worker --show --cnn """ import ibeis qreq_ = ibeis.main_helpers.testdata_qreq_(defaultdb='PZ_MTEST', p=['default:fw_detector=cnn'], qaid_override=[1]) ibs = qreq_.ibs 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 = [vt.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 test_rot_invar(): r""" CommandLine: python -m pyhesaff test_rot_invar --show --rebuild-hesaff --no-rmbuild python -m pyhesaff test_rot_invar --show --nocpp python -m vtool.tests.dummy testdata_ratio_matches --show --ratio_thresh=1.0 --rotation_invariance --rebuild-hesaff python -m vtool.tests.dummy testdata_ratio_matches --show --ratio_thresh=1.1 --rotation_invariance --rebuild-hesaff Example: >>> # DISABLE_DODCTEST >>> from pyhesaff._pyhesaff import * # NOQA >>> test_rot_invar() """ import cv2 import vtool as vt import plottool as pt TAU = 2 * np.pi fnum = pt.next_fnum() NUM_PTS = 5 # 9 theta_list = np.linspace(0, TAU, NUM_PTS, endpoint=False) nRows, nCols = pt.get_square_row_cols(len(theta_list), fix=True) next_pnum = pt.make_pnum_nextgen(nRows, nCols) # Expand the border a bit around star.png pad_ = 100 img_fpath = grab_test_imgpath('star.png') img_fpath2 = vt.pad_image_ondisk(img_fpath, pad_, value=26) for theta in theta_list: print('-----------------') print('theta = %r' % (theta, )) img_fpath = vt.rotate_image_ondisk(img_fpath2, theta, border_mode=cv2.BORDER_REPLICATE) if not ub.argflag('--nocpp'): (kpts_list_ri, vecs_list2) = detect_feats(img_fpath, rotation_invariance=True) kpts_ri = kpts_list_ri[0:2] (kpts_list_gv, vecs_list1) = detect_feats(img_fpath, rotation_invariance=False) kpts_gv = kpts_list_gv[0:2] # find_kpts_direction imgBGR = vt.imread(img_fpath) kpts_ripy = vt.find_kpts_direction(imgBGR, kpts_gv, DEBUG_ROTINVAR=False) # Verify results stdout #print('nkpts = %r' % (len(kpts_gv))) #print(vt.kpts_repr(kpts_gv)) #print(vt.kpts_repr(kpts_ri)) #print(vt.kpts_repr(kpts_ripy)) # Verify results plot pt.figure(fnum=fnum, pnum=next_pnum()) pt.imshow(imgBGR) #if len(kpts_gv) > 0: # pt.draw_kpts2(kpts_gv, ori=True, ell_color=pt.BLUE, ell_linewidth=10.5) ell = False rect = True if not ub.argflag('--nocpp'): if len(kpts_ri) > 0: pt.draw_kpts2(kpts_ri, rect=rect, ell=ell, ori=True, ell_color=pt.RED, ell_linewidth=5.5) if len(kpts_ripy) > 0: pt.draw_kpts2(kpts_ripy, rect=rect, ell=ell, ori=True, ell_color=pt.GREEN, ell_linewidth=3.5) pt.set_figtitle('green=python, red=C++') pt.show_if_requested()
def fourier_devtest(img): r""" Args: img (ndarray[uint8_t, ndim=2]): image data CommandLine: python -m vtool.quality_classifier --test-fourier_devtest --show References: http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_imgproc/py_transforms/py_fourier_transform/py_fourier_transform.html http://cns-alumni.bu.edu/~slehar/fourier/fourier.html Example: >>> # DISABLE_DOCTEST >>> from vtool.quality_classifier import * # NOQA >>> import vtool as vt >>> # build test data >>> img_fpath = ut.grab_test_imgpath('lena.png') >>> img = vt.imread(img_fpath, grayscale=True) >>> # execute function >>> magnitude_spectrum = fourier_devtest(img) """ import plottool as pt def pad_img(img): rows, cols = img.shape nrows = cv2.getOptimalDFTSize(rows) ncols = cv2.getOptimalDFTSize(cols) right = ncols - cols bottom = nrows - rows bordertype = cv2.BORDER_CONSTANT nimg = cv2.copyMakeBorder(img, 0, bottom, 0, right, bordertype, value=0) return nimg def convert_to_fdomain(img): dft = cv2.dft(img.astype(np.float32), flags=cv2.DFT_COMPLEX_OUTPUT) #dft_shift = np.fft.fftshift(dft) return dft def convert_from_fdomain(dft): img = cv2.idft(dft) img = cv2.magnitude(img[:, :, 0], img[:, :, 1]) img /= img.max() return img * 255.0 def get_fdomain_mag(dft_shift): magnitude_spectrum = np.log( cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1])) return magnitude_spectrum def imgstats(img): print('stats:') print(' dtype = %r ' % (img.dtype, )) print(' ' + ut.get_stats_str(img, axis=None)) nimg = pad_img(img) dft = convert_to_fdomain(nimg) #freq_domain = np.fft.fft2(img) #freq_domain_shift = np.fft.fftshift(freq_domain) rows, cols = nimg.shape crow, ccol = rows / 2, cols / 2 # create a mask first, center square is 1, remaining all zeros mask = np.zeros((rows, cols, 2), np.uint8) mask[crow - 30:crow + 30, ccol - 30:ccol + 30] = 1 dft_mask = np.fft.ifftshift(np.fft.fftshift(dft) * mask) img_back = convert_from_fdomain(dft_mask) imgstats(dft) imgstats(mask) imgstats(nimg) imgstats(nimg) print('nimg.shape = %r' % (nimg.shape, )) print('dft_shift.shape = %r' % (dft.shape, )) if ut.show_was_requested(): #import plottool as pt next_pnum = pt.make_pnum_nextgen(nRows=3, nCols=2) pt.imshow(nimg, pnum=next_pnum(), title='nimg') pt.imshow(20 * get_fdomain_mag(dft), pnum=next_pnum(), title='mag(f)') pt.imshow(20 * get_fdomain_mag(dft_mask), pnum=next_pnum(), title='dft_mask') pt.imshow(img_back, pnum=next_pnum(), title='img_back') pt.show_if_requested()