def render_vocab(vocab): """ Renders the average patch of each word. This is a quick visualization of the entire vocabulary. CommandLine: python -m wbia.algo.smk.vocab_indexer render_vocab --show Example: >>> # DISABLE_DOCTEST >>> from wbia.algo.smk.vocab_indexer import * # NOQA >>> vocab = testdata_vocab('PZ_MTEST', num_words=64) >>> all_words = vocab.render_vocab() >>> ut.quit_if_noshow() >>> import wbia.plottool as pt >>> pt.qt4ensure() >>> pt.imshow(all_words) >>> ut.show_if_requested() """ import wbia.plottool as pt wx_list = list(range(len(vocab))) # wx_list = ut.strided_sample(wx_list, 64) wx_list = ut.strided_sample(wx_list, 64) word_patch_list = [] for wx in ut.ProgIter(wx_list, bs=True, lbl='building patches'): word = vocab.wx_to_word[wx] word_patch = vt.inverted_sift_patch(word, 64) word_patch = pt.render_sift_on_patch(word_patch, word) word_patch_list.append(word_patch) all_words = vt.stack_square_images(word_patch_list) return all_words
def render_inverted_vocab(inva, ibs, use_data=False): """ Renders the average patch of each word. This is a visualization of the entire vocabulary. CommandLine: python -m wbia.algo.smk.inverted_index render_inverted_vocab --show python -m wbia.algo.smk.inverted_index render_inverted_vocab --show --use-data python -m wbia.algo.smk.inverted_index render_inverted_vocab --show --debug-depc Example: >>> # DISABLE_DOCTEST >>> from wbia.algo.smk.inverted_index import * # NOQA >>> qreq_, inva = testdata_inva() >>> ibs = qreq_.ibs >>> all_words = inva.render_inverted_vocab(ibs) >>> ut.quit_if_noshow() >>> import wbia.plottool as pt >>> pt.qt4ensure() >>> pt.imshow(all_words) >>> ut.show_if_requested() """ import wbia.plottool as pt # Get words with the most assignments vocab = ibs.depc['vocab'].get_row_data([inva.vocab_rowid], 'words')[0] wx_list = ut.strided_sample(inva.wx_list, 64) word_patch_list = [] for wx in ut.ProgIter(wx_list, bs=True, lbl='building patches'): word = vocab.wx_to_word[wx] word_patch = inva.get_word_patch(wx, ibs) word_patch = pt.render_sift_on_patch(word_patch, word) word_patch_list.append(word_patch) all_words = vt.stack_square_images(word_patch_list) return all_words
def render_inverted_vocab_word(inva, wx, ibs, fnum=None): """ Creates a visualization of a visual word. This includes the average patch, the SIFT-like representation of the centroid, and some of the patches that were assigned to it. CommandLine: python -m ibeis.algo.smk.inverted_index render_inverted_vocab_word --show Example: >>> # DISABLE_DOCTEST >>> from ibeis.algo.smk.inverted_index import * # NOQA >>> import plottool_ibeis as pt >>> qreq_, inva = testdata_inva() >>> ibs = qreq_.ibs >>> wx_list = list(inva.wx_to_aids.keys()) >>> wx = wx_list[0] >>> ut.qtensure() >>> fnum = 2 >>> fnum = pt.ensure_fnum(fnum) >>> # Interactive visualization of many words >>> for wx in ut.InteractiveIter(wx_list): >>> word_img = inva.render_inverted_vocab_word(wx, ibs, fnum) >>> pt.imshow(word_img, fnum=fnum, title='Word %r/%r' % (wx, '?')) >>> pt.update() """ import plottool_ibeis as pt # Create the contributing patch image word_patches = inva.get_patches(wx, ibs) word_patches_ = ut.strided_sample(word_patches, 64) stacked_patches = vt.stack_square_images(word_patches_) # Create the average word image vocab = ibs.depc['vocab'].get_row_data([inva.vocab_rowid], 'words')[0] word = vocab.wx_to_word[wx] average_patch = np.mean(word_patches, axis=0) #vecs = inva.get_vecs(wx) #assert np.allclose(word, vecs.mean(axis=0)) with_sift = True if with_sift: patch_img = pt.render_sift_on_patch(average_patch, word) else: patch_img = average_patch # Stack them together solidbar = np.zeros( (patch_img.shape[0], int(patch_img.shape[1] * .1), 3), dtype=patch_img.dtype) border_color = (100, 10, 10) # bgr, darkblue if ut.is_float(solidbar): solidbar[:, :, :] = (np.array(border_color) / 255)[None, None] else: solidbar[:, :, :] = np.array(border_color)[None, None] patch_img2 = vt.inverted_sift_patch(word) # Fix types patch_img = vt.rectify_to_uint8(patch_img) patch_img2 = vt.rectify_to_uint8(patch_img2) solidbar = vt.rectify_to_uint8(solidbar) stacked_patches = vt.rectify_to_uint8(stacked_patches) # Stack everything together patch_img2, patch_img = vt.make_channels_comparable( patch_img2, patch_img) img_list = [patch_img, solidbar, patch_img2, solidbar, stacked_patches] word_img = vt.stack_image_list(img_list, vert=False, modifysize=True) return word_img
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_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()