def TEST_QUERY_COMP(ibs): r""" CommandLine: python -m ibeis.tests.test_ibs_query_components --test-TEST_QUERY_COMP Example: >>> # ENABLE_DOCTEST >>> from ibeis.tests.test_ibs_query_components import * # NOQA >>> import ibeis >>> # build test data >>> ibs = ibeis.opendb('testdb1') >>> # execute function >>> TEST_QUERY_COMP(ibs) """ print('[TEST_QUERY_COMP]') aids = ibs.get_valid_aids() index = 0 index = utool.get_argval('--index', type_=int, default=index) qaid_list = utool.safe_slice(aids, index, index + 1) print('[TEST_QUERY_COMP] len(qaid_list)=%r' % (qaid_list)) try: comp_locals_ = query_helpers.get_query_components(ibs, qaid_list) qres_dict = OrderedDict([ ('ORIG', comp_locals_['qres_ORIG']), ('FILT', comp_locals_['qres_FILT']), ('SVER', comp_locals_['qres_SVER']), ]) top_aids = qres_dict['SVER'].get_top_aids() aid2 = top_aids[0] except Exception as ex: if 'qres_dict' in vars(): for name, qres in qres_dict.items(): print(name) print(qres.get_inspect_str()) utool.printex(ex, keys=['qaid_list'], pad_stdout=True) raise for px, (lbl, qres) in enumerate(six.iteritems(qres_dict)): print(lbl) fnum = df2.next_fnum() df2.figure(fnum=fnum, doclf=True) qres.ishow_top(ibs, fnum=fnum, top_aids=top_aids, ensure=False) df2.set_figtitle(lbl) df2.adjust_subplots_safe(top=.8) fnum = df2.next_fnum() qaid2_svtups = comp_locals_['qaid2_svtups'] qaid2_chipmatch_FILT = comp_locals_['qaid2_chipmatch_FILT'] aid1 = qaid = comp_locals_['qaid'] aid2_svtup = qaid2_svtups[aid1] chipmatch_FILT = qaid2_chipmatch_FILT[aid1] viz.show_sver(ibs, aid1, aid2, chipmatch_FILT, aid2_svtup, fnum=fnum) return locals()
def _click_matches_click(event): print_('[inter] clicked matches') if event is None: return button = event.button is_right_click = button == 3 if is_right_click: return (x, y, ax) = (event.xdata, event.ydata, event.inaxes) # Out of axes click if None in [x, y, ax]: print('... out of axis') _chipmatch_view() viz.draw() return viztype = vh.get_ibsdat(ax, 'viztype', '') print_('[ir] viztype=%r ' % viztype) key = '' if event.key is None else event.key print_('key=%r ' % key) ctrl_down = key.find('control') == 0 # Click in match axes if viztype == 'matches' and ctrl_down: # Ctrl-Click print('.. control click') return _sv_view(aid) elif viztype == 'matches': if len(fm) == 0: print('[inter] no feature matches to click') else: # Normal Click # Select nearest feature match to the click kpts1, kpts2 = ibs.get_annot_kpts([qaid, aid]) kpts1_m = kpts1[fm[:, 0]] kpts2_m = kpts2[fm[:, 1]] x2, y2, w2, h2 = xywh2_ptr[0] _mx1, _dist1 = utool.nearest_point(x, y, kpts1_m) _mx2, _dist2 = utool.nearest_point(x - x2, y - y2, kpts2_m) mx = _mx1 if _dist1 < _dist2 else _mx2 print('... clicked mx=%r' % mx) _select_ith_match(mx, qaid, aid) elif viztype in ['warped', 'unwarped']: hs_aid = ax.__dict__.get('_hs_aid', None) hs_fx = ax.__dict__.get('_hs_fx', None) if hs_aid is not None and viztype == 'unwarped': ishow_chip(ibs, hs_aid, fx=hs_fx, fnum=df2.next_fnum()) elif hs_aid is not None and viztype == 'warped': viz.show_keypoint_gradient_orientations(ibs, hs_aid, hs_fx, fnum=df2.next_fnum()) else: print('...Unknown viztype: %r' % viztype) viz.draw()
def sver_aids(ibs, qaid_list): qaid2_qres = ibs.query_all(qaid_list) for qaid in qaid_list: qres = qaid2_qres[qaid] aid2 = qres.get_top_aids()[0] interact.ishow_sver(ibs, qaid, aid2, fnum=df2.next_fnum(), annote_mode=1) return qaid2_qres
def __init__(self, **kwargs): self.debug = DEBUG if self.debug: print('[pt.a] create interaction') self.fnum = kwargs.get('fnum', None) if self.fnum is None: self.fnum = df2.next_fnum() self.interaction_name = kwargs.get('interaction_name', 'AbstractInteraction') # Careful this might cause memory leaks self.scope = [] # for keeping those widgets alive! self.is_down = {} self.is_drag = {} self.is_running = False self.pan_event_list = [] self.zoom_event_list = [] self.fig = getattr(self, 'fig', None) for button in self.MOUSE_BUTTONS.values(): self.is_down[button] = None self.is_drag[button] = None autostart = kwargs.get('autostart', False) if autostart: self.start()
def on_ctrl_clicked_match(self, aid1, aid2): """ HELPER: Executed when a result ANNOTATION is control-clicked """ fnum_ = df2.next_fnum() ishow_sver(self.ibs, aid1, aid2, fnum=fnum_) fig = df2.gcf() fig.canvas.draw() df2.bring_to_front(fig)
def TEST_QUERY(ibs): print('[TEST_QUERY]') valid_aids = ibs.get_valid_aids() print('[TEST_QUERY] len(valid_aids)=%r' % (len(valid_aids))) qaid_list = valid_aids[0:1] print('[TEST_QUERY] len(qaid_list)=%r' % (len(qaid_list))) ibs._init_query_requestor() qreq = ibs.qreq #query_helpers.find_matchable_chips(ibs) aids = ibs.get_recognition_database_aids() qres_dict = ibs.query_all(qaid_list) for qaid in qaid_list: qres = qres_dict[qaid] top_aids = qres.get_top_aids(ibs) top_aids = utool.safe_slice(top_aids, 3) aid2 = top_aids[0] fnum = df2.next_fnum() df2.figure(fnum=fnum, doclf=True) #viz_matches.show_matches(ibs, qres, aid2, fnum=fnum, in_image=True) #viz.show_qres(ibs, qres, fnum=fnum, top_aids=top_aids, ensure=False) interact.ishow_qres(ibs, qres, fnum=fnum, top_aids=top_aids, ensure=False, annote_mode=1) df2.set_figtitle('Query Result') df2.adjust_subplots_safe(top=.8) return locals()
def on_clicked_match(self, aid1, aid2): """ HELPER: Executed when a result ANNOTATION is clicked """ fnum_ = df2.next_fnum() qres = self.qaid2_qres[aid1] qres.ishow_matches(self.ibs, aid2, fnum=fnum_) fig = df2.gcf() fig.canvas.draw() df2.bring_to_front(fig)
def query_last_feature(self): ibs = self.ibs qaid = self.qaid viz.show_nearest_descriptors(ibs, qaid, self.last_fx, df2.next_fnum(), qreq_=self.qreq_, draw_chip=True) fig3 = df2.gcf() ih.connect_callback(fig3, 'button_press_event', self.on_click) viz.draw()
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 TEST_QUERY_COMP(ibs): print('[TEST_QUERY_COMP]') qaid_list = ibs.get_valid_aids()[0:1] print('[TEST_QUERY_COMP] len(qaid_list)=%r' % (qaid_list)) ibs._init_query_requestor() qreq = ibs.qreq #query_helpers.find_matchable_chips(ibs) aids = ibs.get_recognition_database_aids() index = 0 index = utool.get_arg('--index', type_=int, default=index) qaid_list = utool.safe_slice(aids, index, index + 1) comp_locals_ = query_helpers.get_query_components(ibs, qaid_list) qres_dict = OrderedDict([ ('ORIG', comp_locals_['qres_ORIG']), ('FILT', comp_locals_['qres_FILT']), ('SVER', comp_locals_['qres_SVER']), ]) top_aids = qres_dict['SVER'].get_top_aids(ibs) top_aids = utool.safe_slice(top_aids, 3) aid2 = top_aids[0] for px, (lbl, qres) in enumerate(six.iteritems(qres_dict)): print(lbl) fnum = df2.next_fnum() df2.figure(fnum=fnum, doclf=True) #viz_matches.show_matches(ibs, qres, aid2, fnum=fnum, in_image=True) #viz.show_qres(ibs, qres, fnum=fnum, top_aids=top_aids, ensure=False) interact.ishow_qres(ibs, qres, fnum=fnum, top_aids=top_aids, ensure=False) df2.set_figtitle(lbl) df2.adjust_subplots_safe(top=.8) fnum = df2.next_fnum() qaid2_svtups = comp_locals_['qaid2_svtups'] qaid2_chipmatch_FILT = comp_locals_['qaid2_chipmatch_FILT'] aid1 = qaid = comp_locals_['qaid'] aid2_svtup = qaid2_svtups[aid1] chipmatch_FILT = qaid2_chipmatch_FILT[aid1] viz.show_sver(ibs, aid1, aid2, chipmatch_FILT, aid2_svtup, fnum=fnum) return locals()
def ishow_image(ibs, gid, sel_aids=[], fnum=None, select_callback=None, **kwargs): if ut.VERBOSE: print(ut.get_caller_name(range(9))) print('[interact_image] gid=%r fnum=%r' % (gid, fnum,)) if fnum is None: fnum = df2.next_fnum() # TODO: change to class based structure self = ut.DynStruct() self.fnum = fnum fig = ih.begin_interaction('image', fnum) #printDBG(utool.func_str(interact_image, [], locals())) kwargs['draw_lbls'] = kwargs.get('draw_lbls', True) def _image_view(sel_aids=sel_aids, **_kwargs): try: viz.show_image(ibs, gid, sel_aids=sel_aids, fnum=self.fnum, **_kwargs) df2.set_figtitle('Image View') except TypeError as ex: ut.printex(ex, ut.dict_str(_kwargs)) raise # Create callback wrapper def _on_image_click(event): printDBG('[inter] clicked image') if ih.clicked_outside_axis(event): # Toggle draw lbls kwargs['draw_lbls'] = not kwargs.get('draw_lbls', True) _image_view(**kwargs) else: ax = event.inaxes viztype = vh.get_ibsdat(ax, 'viztype') annotation_centers = vh.get_ibsdat(ax, 'annotation_centers', default=[]) printDBG(' annotation_centers=%r' % annotation_centers) printDBG(' viztype=%r' % viztype) if len(annotation_centers) == 0: print(' ...no chips exist to click') return x, y = event.xdata, event.ydata # Find ANNOTATION center nearest to the clicked point aid_list = vh.get_ibsdat(ax, 'aid_list', default=[]) centx, _dist = ut.nearest_point(x, y, annotation_centers) aid = aid_list[centx] print(' ...clicked aid=%r' % aid) if select_callback is not None: # HACK, should just implement this correctly here select_callback(gid, sel_aids=[aid], fnum=self.fnum) else: _image_view(sel_aids=[aid]) viz.draw() _image_view(**kwargs) viz.draw() ih.connect_callback(fig, 'button_press_event', _on_image_click)
def show_keypoint_gradient_orientations(ibs, aid, fx, fnum=None, pnum=None, config2_=None): # Draw the gradient vectors of a patch overlaying the keypoint if fnum is None: fnum = df2.next_fnum() rchip = ibs.get_annot_chips(aid, config2_=config2_) kp = ibs.get_annot_kpts(aid, config2_=config2_)[fx] sift = ibs.get_annot_vecs(aid, config2_=config2_)[fx] df2.draw_keypoint_gradient_orientations(rchip, kp, sift=sift, mode='vec', fnum=fnum, pnum=pnum) df2.set_title('Gradient orientation\n %s, fx=%d' % (get_aidstrs(aid), fx))
def sver_aids(ibs, qaid_list, daid_list=None): """ CommandLine: python dev.py -w --show -t sver --db PZ_MTEST --qaid 72 python dev.py -w --show -t sver --db PZ_MTEST --qaid 1 """ if daid_list is None: daid_list = ibs.get_valid_aids() qres_list = ibs.query_chips(qaid_list, daid_list) for qres in qres_list: aid2 = qres.get_top_aids()[0] interact.ishow_sver(ibs, qres.qaid, aid2, fnum=df2.next_fnum(), annot_mode=1)
def show_image(img, bbox_list=[], title='', theta_list=None, text_list=None, sel_list=None, draw_lbls=True, fnum=None, annote=True, **kwargs): """ Driver function to show images """ # Shows an image with annotations if fnum is None: fnum = df2.next_fnum() fig, ax = df2.imshow(img, title=title, fnum=fnum, docla=True, **kwargs) df2.remove_patches(ax) if annote: draw_image_overlay(ax, bbox_list, theta_list, text_list, sel_list, draw_lbls) return fig, ax
def query_aids(ibs, qaid_list, daid_list=None): """ CommandLine: python dev.py -w --show -t query --db PZ_MTEST --qaid 72 """ import ibeis if daid_list is None: daid_list = ibs.get_valid_aids() qres_list = ibs.query_chips(qaid_list, daid_list) for qres in qres_list: assert isinstance(qres, ibeis.algo.hots.hots_query_result.QueryResult) qres.ishow_top(ibs, fnum=df2.next_fnum(), annot_mode=1, make_figtitle=True)
def TEST_QUERY(ibs): r""" CommandLine: python -m ibeis.tests.test_ibs_query --test-TEST_QUERY python -m ibeis.tests.test_ibs_query --test-TEST_QUERY --show Example: >>> # ENABLE_DOCTEST >>> from ibeis.tests.test_ibs_query import * # NOQA >>> import plottool as pt >>> import ibeis >>> ibs = ibeis.opendb('testdb1') >>> TEST_QUERY(ibs) >>> pt.show_if_requested() """ print('[TEST_QUERY]') daid_list = ibs.get_valid_aids() print('[TEST_QUERY] len(daid_list)=%r' % (len(daid_list))) qaid_list = daid_list[0:1] print('[TEST_QUERY] len(qaid_list)=%r' % (len(qaid_list))) qres_list = ibs.query_chips(qaid_list, daid_list, use_cache=False, use_bigcache=False) qres_list_ = ibs.query_chips(qaid_list, daid_list) try: vals1 = qres_list vals2 = qres_list_ assert len(vals1) == 1, 'expected 1 qres in result' assert len(vals2) == 1, 'expected 1 qres in result' #assert list(qres_dict.keys()) == list(qres_dict_.keys()), 'qres cache doesnt work. key error' qres1 = vals1[0] qres2 = vals2[0] inspect_str1 = qres1.get_inspect_str(ibs) inspect_str2 = qres2.get_inspect_str(ibs) print(inspect_str1) assert inspect_str1 == inspect_str2, 'qres cache inconsistency' assert vals1 == vals2, 'qres cache doesnt work. val error' except AssertionError as ex: utool.printex(ex, key_list=list(locals().keys())) raise if ut.show_was_requested(): for qres in qres_list: top_aids = qres.get_top_aids() #top_aids = utool.safe_slice(top_aids, 3) aid2 = top_aids[0] fnum = df2.next_fnum() df2.figure(fnum=fnum, doclf=True) qres.ishow_top(ibs, fnum=fnum, top_aids=top_aids, ensure=False, annot_mode=1) df2.set_figtitle('Query Result') df2.adjust_subplots_safe(top=.8) return locals()
def _on_chip_click(event): print('[inter] clicked chip') ax, x, y = event.inaxes, event.xdata, event.ydata if ih.clicked_outside_axis(event): if not ischild: print('... out of axis') mode_ptr[0] = (mode_ptr[0] + 1) % 3 _chip_view(**kwargs) else: if event.button == 3: # right-click import guitool #from ibeis.viz.interact import interact_chip height = fig.canvas.geometry().height() qpoint = guitool.newQPoint(event.x, height - event.y) refresh_func = partial(_chip_view, **kwargs) callback_list = build_annot_context_options( ibs, aid, refresh_func=refresh_func, with_interact_chip=False, config2_=config2_) qwin = fig.canvas guitool.popup_menu(qwin, qpoint, callback_list) #interact_chip.show_annot_context_menu( # ibs, aid, fig.canvas, qpoint, refresh_func=refresh_func, # with_interact_chip=False, config2_=config2_) else: viztype = vh.get_ibsdat(ax, 'viztype') print('[ic] viztype=%r' % viztype) if viztype == 'chip' and event.key == 'shift': _chip_view(**kwargs) ih.disconnect_callback(fig, 'button_press_event') elif viztype == 'chip': kpts = ibs.get_annot_kpts(aid, config2_=config2_) if len(kpts) > 0: fx = vt.nearest_point( x, y, kpts, conflict_mode='next')[0] print('... clicked fx=%r' % fx) _select_fxth_kpt(fx) else: print('... len(kpts) == 0') elif viztype in ['warped', 'unwarped']: fx = vh.get_ibsdat(ax, 'fx') if fx is not None and viztype == 'warped': viz.show_keypoint_gradient_orientations( ibs, aid, fx, fnum=df2.next_fnum()) else: print('...Unknown viztype: %r' % viztype) viz.draw()
def _on_keypoints_click(event): print('[viz] clicked keypoint view') if event is None or event.xdata is None or event.inaxes is None: annote_ptr[0] = (annote_ptr[0] + 1) % 3 mode = annote_ptr[0] ell = mode == 1 pts = mode == 2 print('... default kpts view mode=%r' % mode) _viz_keypoints(fnum, ell=ell, pts=pts, **kwargs) # MAYBE: remove kwargs else: ax = event.inaxes viztype = ph.get_plotdat(ax, 'viztype', None) print('[ik] viztype=%r' % viztype) if viztype == 'keypoints': kpts = ph.get_plotdat(ax, 'kpts', []) if len(kpts) == 0: print('...nokpts') else: print('...nearest') x, y = event.xdata, event.ydata fx = ut.nearest_point(x, y, kpts)[0] _select_ith_kpt(fx) elif viztype == 'warped': hs_fx = ph.get_plotdat(ax, 'fx', None) #kpts = ph.get_plotdat(ax, 'kpts', []) if hs_fx is not None: # Ugly. Interactions should be changed to classes. kp = self.kpts[hs_fx] # FIXME sift = self.vecs[hs_fx] df2.draw_keypoint_gradient_orientations( chip, kp, sift=sift, mode='vec', fnum=df2.next_fnum()) elif viztype.startswith('colorbar'): pass # Hack to get a specific scoring feature #sortx = self.fs.argsort() #idx = np.clip(int(np.round(y * len(sortx))), 0, len(sortx) - 1) #mx = sortx[idx] #(fx1, fx2) = self.fm[mx] #(fx1, fx2) = self.fm[mx] #print('... selected score at rank idx=%r' % (idx,)) #print('... selected score with fs=%r' % (self.fs[mx],)) #print('... resolved to mx=%r' % mx) #print('... fx1, fx2 = %r, %r' % (fx1, fx2,)) #self.select_ith_match(mx) else: print('...unhandled') ph.draw()
def _on_keypoints_click(event): print('[viz] clicked keypoint view') if event is None or event.xdata is None or event.inaxes is None: annote_ptr[0] = (annote_ptr[0] + 1) % 3 mode = annote_ptr[0] ell = mode == 1 pts = mode == 2 print('... default kpts view mode=%r' % mode) _viz_keypoints(fnum, ell=ell, pts=pts, **kwargs) # MAYBE: remove kwargs else: ax = event.inaxes viztype = ph.get_plotdat(ax, 'viztype', None) print('[ik] viztype=%r' % viztype) if viztype == 'keypoints': kpts = ph.get_plotdat(ax, 'kpts', []) if len(kpts) == 0: print('...nokpts') else: print('...nearest') x, y = event.xdata, event.ydata fx = ut.nearest_point(x, y, kpts)[0] _select_ith_kpt(fx) elif viztype == 'warped': hs_fx = ph.get_plotdat(ax, 'fx', None) #kpts = ph.get_plotdat(ax, 'kpts', []) if hs_fx is not None: # Ugly. Interactions should be changed to classes. kp = self.kpts[hs_fx] # FIXME sift = self.vecs[hs_fx] df2.draw_keypoint_gradient_orientations(chip, kp, sift=sift, mode='vec', fnum=df2.next_fnum()) elif viztype.startswith('colorbar'): pass # Hack to get a specific scoring feature #sortx = self.fs.argsort() #idx = np.clip(int(np.round(y * len(sortx))), 0, len(sortx) - 1) #mx = sortx[idx] #(fx1, fx2) = self.fm[mx] #(fx1, fx2) = self.fm[mx] #print('... selected score at rank idx=%r' % (idx,)) #print('... selected score with fs=%r' % (self.fs[mx],)) #print('... resolved to mx=%r' % mx) #print('... fx1, fx2 = %r, %r' % (fx1, fx2,)) #self.select_ith_match(mx) else: print('...unhandled') ph.draw()
def on_click_inside(self, event, ax): index = ph.get_plotdat(ax, 'index') print('index = %r' % (index, )) if index is not None: if self.MOUSE_BUTTONS[event.button] == 'right': if self.context_option_funcs is not None: #if event.button == 3: options = self.context_option_funcs[index]() self.show_popup_menu(options, event) elif self.MOUSE_BUTTONS[event.button] == 'left': #bbox_list = ph.get_plotdat(ax, 'bbox_list') gpath = self.gpath_list[index] if ut.is_funclike(gpath): print('gpath_isfunklike') print('gpath = %r' % (gpath, )) import plottool as pt fnum = pt.next_fnum() gpath(fnum=fnum) df2.update() else: bbox_list = self.bboxes_list[index] print('Bbox of figure: %r' % (bbox_list, )) theta_list = self.thetas_list[index] print('theta_list = %r' % (theta_list, )) #img = mpimg.imread(gpath) if isinstance(gpath, six.string_types): img = vt.imread(gpath) else: img = gpath fnum = df2.next_fnum() mc = interact_annotations.ANNOTATIONInteraction( img, index, self.update_images, bbox_list=bbox_list, theta_list=theta_list, fnum=fnum) mc.start() self.mc = mc # """wait for accept # have a flag to tell if a bbox has been changed, on the bbox # list that is brought it" on accept: viz_image2.show_image # callback # """ df2.update() print('Clicked: ax: num=%r' % index)
def sver_aids(ibs, qaid_list, daid_list=None): """ CommandLine: python dev.py -w --show -t sver --db PZ_MTEST --qaid 72 python dev.py -w --show -t sver --db PZ_MTEST --qaid 1 """ from ibeis.viz import interact if daid_list is None: daid_list = ibs.get_valid_aids() cm_list = ibs.query_chips(qaid_list, daid_list) for cm in cm_list: aid2 = cm.get_top_aids()[0] interact.ishow_sver(ibs, cm.qaid, aid2, fnum=df2.next_fnum(), annot_mode=1)
def on_click_inside(self, event, ax): index = ph.get_plotdat(ax, 'index') print('index = %r' % (index,)) if index is not None: if self.MOUSE_BUTTONS[event.button] == 'right': if self.context_option_funcs is not None: #if event.button == 3: options = self.context_option_funcs[index]() self.show_popup_menu(options, event) elif self.MOUSE_BUTTONS[event.button] == 'left': #bbox_list = ph.get_plotdat(ax, 'bbox_list') gpath = self.gpath_list[index] if ut.is_funclike(gpath): print('gpath_isfunklike') print('gpath = %r' % (gpath,)) import plottool as pt fnum = pt.next_fnum() gpath(fnum=fnum) df2.update() else: bbox_list = self.bboxes_list[index] print('Bbox of figure: %r' % (bbox_list,)) theta_list = self.thetas_list[index] print('theta_list = %r' % (theta_list,)) #img = mpimg.imread(gpath) if isinstance(gpath, six.string_types): img = vt.imread(gpath) else: img = gpath fnum = df2.next_fnum() mc = interact_annotations.ANNOTATIONInteraction( img, index, self.update_images, bbox_list=bbox_list, theta_list=theta_list, fnum=fnum) mc.start() self.mc = mc # """wait for accept # have a flag to tell if a bbox has been changed, on the bbox # list that is brought it" on accept: viz_image2.show_image # callback # """ df2.update() print('Clicked: ax: num=%r' % index)
def show_aids(ibs, qaid_list): for aid in qaid_list: interact.ishow_chip(ibs, aid, fnum=df2.next_fnum())
def on_click(self, event): aid = self.daid qaid = self.qaid ibs = self.ibs xywh2_ptr = self.xywh2_ptr print('[inter] clicked matches') if event is None: return button = event.button is_right_click = button == 3 # Out of axes click (x, y, ax) = (event.xdata, event.ydata, event.inaxes) if None in [x, y, ax]: in_axis = False if not is_right_click: print('... out of axis') self.chipmatch_view() viz.draw() return else: in_axis = True if in_axis: viztype = vh.get_ibsdat(ax, 'viztype', '') is_match_type = viztype in ['matches', 'multi_match'] print('[ir] viztype=%r ' % viztype) else: is_match_type = False viztype = '' if is_right_click: from ibeis.gui import inspect_gui options = [] if is_match_type: options += inspect_gui.get_aidpair_context_menu_options( self.ibs, self.qaid, self.daid, self.cm, qreq_=self.qreq_, #update_callback=self.show_page, #backend_callback=None, aid_list=aid_list) ) options += [ ('Toggle same_fig', self.toggle_samefig), ('Toggle vert', self.toggle_vert), ('query last feature', self.query_last_feature), ('show each chip', self.show_each_chip), ('show each distinctiveness chip', self.show_each_dstncvs_chip), ('show each foreground weight chip', self.show_each_fgweight_chip), ('show each probchip', self.show_each_probchip), ('show coverage', self.show_coverage), #('show each probchip', self.query_last_feature), ] #options.append(('name_interaction', self.name_interaction)) if self.H1 is not None: options.append(('Toggle homog', self.toggle_homog)) if ut.is_developer(): options.append(('dev_reload', self.dev_reload)) options.append(('dev_embed', self.dev_embed)) #options.append(('cancel', lambda: print('cancel'))) self.show_popup_menu(options, event) return if in_axis: key = '' if event.key is None else event.key print('key=%r ' % key) ctrl_down = key.find('control') == 0 # Click in match axes if is_match_type and ctrl_down: # Ctrl-Click print('.. control click') return self.sv_view() elif is_match_type: if len(self.fm) == 0: print('[inter] no feature matches to click') else: # Normal Click # Select nearest feature match to the click kpts1 = ibs.get_annot_kpts([qaid], config2_=self.query_config2_)[0] kpts2 = ibs.get_annot_kpts([aid], config2_=self.data_config2_)[0] kpts1_m = kpts1[self.fm.T[0]] kpts2_m = kpts2[self.fm.T[1]] x2, y2, w2, h2 = xywh2_ptr[0] _mx1, _dist1 = ut.nearest_point(x, y, kpts1_m) _mx2, _dist2 = ut.nearest_point(x - x2, y - y2, kpts2_m) mx = _mx1 if _dist1 < _dist2 else _mx2 (fx1, fx2) = self.fm[mx] print('... clicked mx=%r' % mx) print('... fx1, fx2 = %r, %r' % (fx1, fx2,)) self.select_ith_match(mx) elif viztype in ['warped', 'unwarped']: print('clicked at patch') ut.print_dict(ph.get_plotdat_dict(ax)) hs_aid = vh.get_ibsdat(ax, 'aid', None) hs_fx = vh.get_ibsdat(ax, 'fx', None) #hs_aid = ax.__dict__.get('_hs_aid', None) #hs_fx = ax.__dict__.get('_hs_fx', None) print('hs_fx = %r' % (hs_fx,)) print('hs_aid = %r' % (hs_aid,)) if hs_aid is not None and viztype == 'unwarped': ishow_chip(ibs, hs_aid, fx=hs_fx, fnum=df2.next_fnum()) elif hs_aid is not None and viztype == 'warped': viz.show_keypoint_gradient_orientations(ibs, hs_aid, hs_fx, fnum=df2.next_fnum()) elif viztype.startswith('colorbar'): # Hack to get a specific scoring feature sortx = self.fs.argsort() idx = np.clip(int(np.round(y * len(sortx))), 0, len(sortx) - 1) mx = sortx[idx] (fx1, fx2) = self.fm[mx] (fx1, fx2) = self.fm[mx] print('... selected score at rank idx=%r' % (idx,)) print('... selected score with fs=%r' % (self.fs[mx],)) print('... resolved to mx=%r' % mx) print('... fx1, fx2 = %r, %r' % (fx1, fx2,)) self.select_ith_match(mx) else: print('...Unknown viztype: %r' % viztype) viz.draw()
def show_nearest_descriptors(ibs, qaid, qfx, fnum=None, stride=5, qreq_=None, **kwargs): r""" Args: ibs (ibeis.IBEISController): image analysis api qaid (int): query annotation id qfx (int): query feature index fnum (int): figure number stride (int): consecutive_distance_compare (bool): CommandLine: # Find a good match to inspect python -m ibeis.viz.interact.interact_matches --test-testdata_match_interact --show --db PZ_MTEST --qaid 3 # Now inspect it python -m ibeis.viz.viz_nearest_descriptors --test-show_nearest_descriptors --show --db PZ_MTEST --qaid 3 --qfx 879 python -m ibeis.viz.viz_nearest_descriptors --test-show_nearest_descriptors --show python -m ibeis.viz.viz_nearest_descriptors --test-show_nearest_descriptors --db PZ_MTEST --qaid 3 --qfx 879 --diskshow --save foo.png --dpi=256 SeeAlso: plottool.viz_featrow ~/code/plottool/plottool/viz_featrow.py Example: >>> # DISABLE_DOCTEST >>> from ibeis.viz.viz_nearest_descriptors import * # NOQA >>> import ibeis >>> # build test data >>> qreq_ = ibeis.testdata_qreq_() >>> ibs = ibeis.opendb('PZ_MTEST') >>> qaid = qreq_.qaids[0] >>> qfx = ut.get_argval('--qfx', type_=None, default=879) >>> fnum = None >>> stride = 5 >>> # execute function >>> skip = False >>> result = show_nearest_descriptors(ibs, qaid, qfx, fnum, stride, >>> draw_chip=True, >>> draw_warped=True, >>> draw_unwarped=False, >>> draw_desc=False, qreq_=qreq_) >>> # verify results >>> print(result) >>> pt.show_if_requested() """ import plottool as pt # NOQA consecutive_distance_compare = True draw_chip = kwargs.get('draw_chip', False) draw_desc = kwargs.get('draw_desc', True) draw_warped = kwargs.get('draw_warped', True) draw_unwarped = kwargs.get('draw_unwarped', True) #skip = kwargs.get('skip', True) # Plots the nearest neighbors of a given feature (qaid, qfx) if fnum is None: fnum = df2.next_fnum() try: # Flann NN query (qfx, qfx2_daid, qfx2_dfx, qfx2_dist, K, Knorm) = get_annotfeat_nn_index(ibs, qaid, qfx, qreq_=qreq_) # Adds metadata to a feature match def get_extract_tuple(aid, fx, k=-1): rchip = ibs.get_annot_chips(aid) kp = ibs.get_annot_kpts(aid)[fx] sift = ibs.get_annot_vecs(aid)[fx] if not ut.get_argflag('--texknormplot'): aidstr = vh.get_aidstrs(aid) nidstr = vh.get_nidstrs(ibs.get_annot_nids(aid)) id_str = ' ' + aidstr + ' ' + nidstr + ' fx=%r' % (fx, ) else: id_str = nidstr = aidstr = '' info = '' if k == -1: if pt.is_texmode(): info = '\\vspace{1cm}' info += 'Query $\\mathbf{d}_i$' info += '\n\\_' info += '\n\\_' else: if len(id_str) > '': info = 'Query: %s' % (id_str, ) else: info = 'Query' type_ = 'Query' elif k < K: type_ = 'Match' if ut.get_argflag('--texknormplot') and pt.is_texmode(): #info = 'Match:\n$k=%r$, $\\frac{||\\mathbf{d}_i - \\mathbf{d}_j||}{Z}=%.3f$' % (k, qfx2_dist[0, k]) info = '\\vspace{1cm}' info += 'Match: $\\mathbf{d}_{j_%r}$\n$\\textrm{dist}=%.3f$' % ( k, qfx2_dist[0, k]) info += '\n$s_{\\tt{LNBNN}}=%.3f$' % ( qfx2_dist[0, K + Knorm - 1] - qfx2_dist[0, k]) else: info = 'Match:%s\nk=%r, dist=%.3f' % (id_str, k, qfx2_dist[0, k]) info += '\nLNBNN=%.3f' % (qfx2_dist[0, K + Knorm - 1] - qfx2_dist[0, k]) elif k < Knorm + K: type_ = 'Norm' if ut.get_argflag('--texknormplot') and pt.is_texmode(): #info = 'Norm: $j_%r$\ndist=%.3f' % (id_str, k, qfx2_dist[0, k]) info = '\\vspace{1cm}' info += 'Norm: $j_%r$\n$\\textrm{dist}=%.3f$' % ( k, qfx2_dist[0, k]) info += '\n\\_' else: info = 'Norm: %s\n$k=%r$, dist=$%.3f$' % (id_str, k, qfx2_dist[0, k]) else: raise Exception('[viz] problem k=%r') return (rchip, kp, sift, fx, aid, info, type_) extracted_list = [] # Remember the query sift feature extracted_list.append(get_extract_tuple(qaid, qfx, -1)) origsift = extracted_list[0][2] skipped = 0 for k in range(K + Knorm): #if qfx2_daid[0, k] == qaid and qfx2_dfx[0, k] == qfx: if qfx2_daid[0, k] == qaid: skipped += 1 continue tup = get_extract_tuple(qfx2_daid[0, k], qfx2_dfx[0, k], k) extracted_list.append(tup) # Draw the _select_ith_match plot nRows = len(extracted_list) if stride is None: stride = nRows # Draw selected feature matches prevsift = None px = 0 # plot offset px_shift = 0 # plot stride shift nExtracted = len(extracted_list) featrow_kw = dict( draw_chip=draw_chip, draw_desc=draw_desc, draw_warped=draw_warped, draw_unwarped=draw_unwarped, ) if ut.get_argflag('--texknormplot'): featrow_kw['ell_color'] = pt.ORANGE pass for listx, tup in enumerate(extracted_list): (rchip, kp, sift, fx, aid, info, type_) = tup if listx % stride == 0: # Create a temporary nRows and fnum in case we are splitting # up nearest neighbors into separate figures with stride _fnum = fnum + listx _nRows = min(nExtracted - listx, stride) px_shift = px df2.figure(fnum=_fnum, docla=True, doclf=True) px_ = px - px_shift px = draw_feat_row(rchip, fx, kp, sift, _fnum, _nRows, px=px_, prevsift=prevsift, origsift=origsift, aid=aid, info=info, type_=type_, **featrow_kw) px += px_shift if prevsift is None or consecutive_distance_compare: prevsift = sift df2.adjust_subplots_safe(hspace=.85, wspace=0, top=.95, bottom=.087, left=.05, right=.95) except Exception as ex: print('[viz] Error in show nearest descriptors') print(ex) raise
def query_last_feature(): viz.show_nearest_descriptors(ibs, qaid, last_state.last_fx, df2.next_fnum()) fig3 = df2.gcf() ih.connect_callback(fig3, 'button_press_event', _click_matches_click) df2.update()
def on_click(self, event): aid = self.daid qaid = self.qaid ibs = self.ibs xywh2_ptr = self.xywh2_ptr print('[inter] clicked matches') if event is None: return button = event.button is_right_click = button == 3 # Out of axes click (x, y, ax) = (event.xdata, event.ydata, event.inaxes) if None in [x, y, ax]: in_axis = False if not is_right_click: print('... out of axis') self.chipmatch_view() viz.draw() return else: in_axis = True if in_axis: viztype = vh.get_ibsdat(ax, 'viztype', '') is_match_type = viztype in ['matches', 'multi_match'] print('[ir] viztype=%r ' % viztype) else: is_match_type = False viztype = '' if is_right_click: from ibeis.gui import inspect_gui options = [] if is_match_type: options += inspect_gui.get_aidpair_context_menu_options( self.ibs, self.qaid, self.daid, self.cm, qreq_=self.qreq_, #update_callback=self.show_page, #backend_callback=None, aid_list=aid_list) ) options += [ ('Toggle same_fig', self.toggle_samefig), ('Toggle vert', self.toggle_vert), ('query last feature', self.query_last_feature), ('show each chip', self.show_each_chip), ('show each distinctiveness chip', self.show_each_dstncvs_chip), ('show each foreground weight chip', self.show_each_fgweight_chip), ('show each probchip', self.show_each_probchip), ('show coverage', self.show_coverage), #('show each probchip', self.query_last_feature), ] #options.append(('name_interaction', self.name_interaction)) if self.H1 is not None: options.append(('Toggle homog', self.toggle_homog)) if ut.is_developer(): options.append(('dev_reload', self.dev_reload)) options.append(('dev_embed', self.dev_embed)) #options.append(('cancel', lambda: print('cancel'))) self.show_popup_menu(options, event) return if in_axis: key = '' if event.key is None else event.key print('key=%r ' % key) ctrl_down = key.find('control') == 0 # Click in match axes if is_match_type and ctrl_down: # Ctrl-Click print('.. control click') return self.sv_view() elif is_match_type: if len(self.fm) == 0: print('[inter] no feature matches to click') else: # Normal Click # Select nearest feature match to the click kpts1 = ibs.get_annot_kpts([qaid], config2_=self.query_config2_)[0] kpts2 = ibs.get_annot_kpts([aid], config2_=self.data_config2_)[0] kpts1_m = kpts1[self.fm.T[0]] kpts2_m = kpts2[self.fm.T[1]] x2, y2, w2, h2 = xywh2_ptr[0] _mx1, _dist1 = ut.nearest_point(x, y, kpts1_m) _mx2, _dist2 = ut.nearest_point(x - x2, y - y2, kpts2_m) mx = _mx1 if _dist1 < _dist2 else _mx2 (fx1, fx2) = self.fm[mx] print('... clicked mx=%r' % mx) print('... fx1, fx2 = %r, %r' % ( fx1, fx2, )) self.select_ith_match(mx) elif viztype in ['warped', 'unwarped']: print('clicked at patch') ut.print_dict(ph.get_plotdat_dict(ax)) hs_aid = vh.get_ibsdat(ax, 'aid', None) hs_fx = vh.get_ibsdat(ax, 'fx', None) #hs_aid = ax.__dict__.get('_hs_aid', None) #hs_fx = ax.__dict__.get('_hs_fx', None) print('hs_fx = %r' % (hs_fx, )) print('hs_aid = %r' % (hs_aid, )) if hs_aid is not None and viztype == 'unwarped': ishow_chip(ibs, hs_aid, fx=hs_fx, fnum=df2.next_fnum()) elif hs_aid is not None and viztype == 'warped': viz.show_keypoint_gradient_orientations( ibs, hs_aid, hs_fx, fnum=df2.next_fnum()) elif viztype.startswith('colorbar'): # Hack to get a specific scoring feature sortx = self.fs.argsort() idx = np.clip(int(np.round(y * len(sortx))), 0, len(sortx) - 1) mx = sortx[idx] (fx1, fx2) = self.fm[mx] (fx1, fx2) = self.fm[mx] print('... selected score at rank idx=%r' % (idx, )) print('... selected score with fs=%r' % (self.fs[mx], )) print('... resolved to mx=%r' % mx) print('... fx1, fx2 = %r, %r' % ( fx1, fx2, )) self.select_ith_match(mx) else: print('...Unknown viztype: %r' % viztype) viz.draw()
def run_experiments(ibs, qaid_list): """ This function runs tests passed in with the -t flag """ print('\n') print('[dev] run_experiments') print('==========================') print('RUN EXPERIMENTS %s' % ibs.get_dbname()) print('==========================') input_test_list = params.args.tests[:] print('input_test_list = %r' % (input_test_list,)) # fnum = 1 valid_test_list = [] # build list for printing in case of failure valid_test_helpstr_list = [] # for printing def intest(*args, **kwargs): helpstr = kwargs.get('help', '') valid_test_helpstr_list.append(' -t ' + ', '.join(args) + helpstr) for testname in args: valid_test_list.append(testname) ret = testname in input_test_list ret2 = testname in params.unknown # Let unparsed args count towards tests if ret or ret2: if ret: input_test_list.remove(testname) else: ret = ret2 print('+===================') print('| running testname = %s' % (args,)) return ret return False valid_test_helpstr_list.append(' # --- Simple Tests ---') # Explicit (simple) test functions if intest('export'): export(ibs) if intest('dbinfo'): dbinfo.get_dbinfo(ibs) if intest('info'): print(ibs.get_infostr()) if intest('printcfg'): printcfg(ibs) if intest('tables'): ibs.print_tables() if intest('imgtbl'): ibs.print_image_table() valid_test_helpstr_list.append(' # --- Decor Tests ---') locals_ = locals() # Implicit (decorated) test functions for (func_aliases, func) in DEVCMD_FUNCTIONS: if intest(*func_aliases): with utool.Indenter('[dev.' + func.func_name + ']'): ret = func(ibs, qaid_list) if isinstance(ret, dict): locals_.update(ret) valid_test_helpstr_list.append(' # --- Config Tests ---') # Config driven test functions # Allow any testcfg to be in tests like: vsone_1 or vsmany_3 for test_cfg_name in experiment_configs.TEST_NAMES: if intest(test_cfg_name): test_cfg_name_list = [test_cfg_name] fnum = df2.next_fnum() experiment_harness.test_configurations(ibs, qaid_list, test_cfg_name_list, fnum) valid_test_helpstr_list.append(' # --- Help ---') if intest('help'): print('valid tests are:') print('\n'.join(valid_test_helpstr_list)) return if len(input_test_list) > 0: print('valid tests are: \n') print('\n'.join(valid_test_list)) raise Exception('Unknown tests: %r ' % input_test_list) return locals_
def ishow_image(ibs, gid, sel_aids=[], fnum=None, select_callback=None, **kwargs): if ut.VERBOSE: print(ut.get_caller_name(range(9))) print('[interact_image] gid=%r fnum=%r' % ( gid, fnum, )) if fnum is None: fnum = df2.next_fnum() # TODO: change to class based structure self = ut.DynStruct() self.fnum = fnum fig = ih.begin_interaction('image', fnum) #printDBG(utool.func_str(interact_image, [], locals())) kwargs['draw_lbls'] = kwargs.get('draw_lbls', True) def _image_view(sel_aids=sel_aids, **_kwargs): try: viz.show_image(ibs, gid, sel_aids=sel_aids, fnum=self.fnum, **_kwargs) df2.set_figtitle('Image View') except TypeError as ex: ut.printex(ex, ut.dict_str(_kwargs)) raise # Create callback wrapper def _on_image_click(event): printDBG('[inter] clicked image') if ih.clicked_outside_axis(event): # Toggle draw lbls kwargs['draw_lbls'] = not kwargs.get('draw_lbls', True) _image_view(**kwargs) else: ax = event.inaxes viztype = vh.get_ibsdat(ax, 'viztype') annotation_centers = vh.get_ibsdat(ax, 'annotation_centers', default=[]) printDBG(' annotation_centers=%r' % annotation_centers) printDBG(' viztype=%r' % viztype) if len(annotation_centers) == 0: print(' ...no chips exist to click') return x, y = event.xdata, event.ydata # Find ANNOTATION center nearest to the clicked point aid_list = vh.get_ibsdat(ax, 'aid_list', default=[]) centx, _dist = ut.nearest_point(x, y, annotation_centers) aid = aid_list[centx] print(' ...clicked aid=%r' % aid) if select_callback is not None: # HACK, should just implement this correctly here select_callback(gid, sel_aids=[aid], fnum=self.fnum) else: _image_view(sel_aids=[aid]) viz.draw() _image_view(**kwargs) viz.draw() ih.connect_callback(fig, 'button_press_event', _on_image_click)
def show_nearest_descriptors(ibs, qaid, qfx, fnum=None, stride=5, consecutive_distance_compare=False): # Plots the nearest neighbors of a given feature (qaid, qfx) if fnum is None: fnum = df2.next_fnum() try: # Flann NN query (qfx2_aid, qfx2_fx, qfx2_dist, K, Knorm) = query_helpers.get_annotfeat_nn_index(ibs, qaid, qfx) # Adds metadata to a feature match def get_extract_tuple(aid, fx, k=-1): rchip = ibs.get_annot_chips(aid) kp = ibs.get_annot_kpts(aid)[fx] sift = ibs.get_annot_desc(aid)[fx] if k == -1: info = '\nquery %s, fx=%r' % (vh.get_aidstrs(aid), fx) type_ = 'query' elif k < K: type_ = 'match' info = '\nmatch %s, fx=%r k=%r, dist=%r' % (vh.get_aidstrs(aid), fx, k, qfx2_dist[0, k]) elif k < Knorm + K: type_ = 'norm' info = '\nnorm %s, fx=%r k=%r, dist=%r' % (vh.get_aidstrs(aid), fx, k, qfx2_dist[0, k]) else: raise Exception('[viz] problem k=%r') return (rchip, kp, sift, fx, aid, info, type_) extracted_list = [] extracted_list.append(get_extract_tuple(qaid, qfx, -1)) skipped = 0 for k in xrange(K + Knorm): if qfx2_aid[0, k] == qaid and qfx2_fx[0, k] == qfx: skipped += 1 continue tup = get_extract_tuple(qfx2_aid[0, k], qfx2_fx[0, k], k) extracted_list.append(tup) # Draw the _select_ith_match plot nRows, nCols = len(extracted_list), 3 if stride is None: stride = nRows # Draw selected feature matches prevsift = None px = 0 # plot offset px_shift = 0 # plot stride shift nExtracted = len(extracted_list) for listx, tup in enumerate(extracted_list): (rchip, kp, sift, fx, aid, info, type_) = tup if listx % stride == 0: # Create a temporary nRows and fnum in case we are splitting # up nearest neighbors into separate figures with stride _fnum = fnum + listx _nRows = min(nExtracted - listx, stride) px_shift = px df2.figure(fnum=_fnum, docla=True, doclf=True) printDBG('[viz] ' + info.replace('\n', '')) px_ = px - px_shift px = draw_feat_row(rchip, fx, kp, sift, _fnum, _nRows, nCols, px_, prevsift=prevsift, aid=aid, info=info, type_=type_) + px_shift if prevsift is None or consecutive_distance_compare: prevsift = sift df2.adjust_subplots_safe(hspace=1) except Exception as ex: print('[viz] Error in show nearest descriptors') print(ex) raise
def query_aids(ibs, qaid_list): qaid2_qres = ibs.query_all(qaid_list) for qaid in qaid_list: qres = qaid2_qres[qaid] interact.ishow_qres(ibs, qres, fnum=df2.next_fnum(), annote_mode=1) return qaid2_qres
def show_aids(ibs, qaid_list): from ibeis.viz import interact for aid in qaid_list: interact.ishow_chip(ibs, aid, fnum=df2.next_fnum())
def show_nearest_descriptors(ibs, qaid, qfx, fnum=None, stride=5, qreq_=None, **kwargs): r""" Args: ibs (ibeis.IBEISController): image analysis api qaid (int): query annotation id qfx (int): query feature index fnum (int): figure number stride (int): consecutive_distance_compare (bool): CommandLine: # Find a good match to inspect python -m ibeis.viz.interact.interact_matches --test-testdata_match_interact --show --db PZ_MTEST --qaid 3 # Now inspect it python -m ibeis.viz.viz_nearest_descriptors --test-show_nearest_descriptors --show --db PZ_MTEST --qaid 3 --qfx 879 python -m ibeis.viz.viz_nearest_descriptors --test-show_nearest_descriptors --show python -m ibeis.viz.viz_nearest_descriptors --test-show_nearest_descriptors --db PZ_MTEST --qaid 3 --qfx 879 --diskshow --save foo.png --dpi=256 SeeAlso: plottool.viz_featrow ~/code/plottool/plottool/viz_featrow.py Example: >>> # DISABLE_DOCTEST >>> from ibeis.viz.viz_nearest_descriptors import * # NOQA >>> import ibeis >>> # build test data >>> qreq_ = ibeis.testdata_qreq_() >>> ibs = ibeis.opendb('PZ_MTEST') >>> qaid = qreq_.qaids[0] >>> qfx = ut.get_argval('--qfx', type_=int, default=879) >>> fnum = None >>> stride = 5 >>> # execute function >>> skip = False >>> result = show_nearest_descriptors(ibs, qaid, qfx, fnum, stride, >>> draw_chip=True, >>> draw_warped=True, >>> draw_unwarped=False, >>> draw_desc=False, qreq_=qreq_) >>> # verify results >>> print(result) >>> pt.show_if_requested() """ consecutive_distance_compare = True draw_chip = kwargs.get('draw_chip', False) draw_desc = kwargs.get('draw_desc', True) draw_warped = kwargs.get('draw_warped', True) draw_unwarped = kwargs.get('draw_unwarped', True) #skip = kwargs.get('skip', True) # Plots the nearest neighbors of a given feature (qaid, qfx) if fnum is None: fnum = df2.next_fnum() try: # Flann NN query (qfx2_daid, qfx2_dfx, qfx2_dist, K, Knorm) = get_annotfeat_nn_index(ibs, qaid, qfx, qreq_=qreq_) # Adds metadata to a feature match def get_extract_tuple(aid, fx, k=-1): rchip = ibs.get_annot_chips(aid) kp = ibs.get_annot_kpts(aid)[fx] sift = ibs.get_annot_vecs(aid)[fx] if not ut.get_argflag('--texknormplot'): aidstr = vh.get_aidstrs(aid) nidstr = vh.get_nidstrs(ibs.get_annot_nids(aid)) id_str = ' ' + aidstr + ' ' + nidstr + ' fx=%r' % (fx,) else: id_str = nidstr = aidstr = '' info = '' if k == -1: if pt.is_texmode(): info = '\\vspace{1cm}' info += 'Query $\\mathbf{d}_i$' info += '\n\\_' info += '\n\\_' else: if len(id_str) > '': info = 'Query: %s' % (id_str,) else: info = 'Query' type_ = 'Query' elif k < K: type_ = 'Match' if ut.get_argflag('--texknormplot') and pt.is_texmode(): #info = 'Match:\n$k=%r$, $\\frac{||\\mathbf{d}_i - \\mathbf{d}_j||}{Z}=%.3f$' % (k, qfx2_dist[0, k]) info = '\\vspace{1cm}' info += 'Match: $\\mathbf{d}_{j_%r}$\n$\\textrm{dist}=%.3f$' % (k, qfx2_dist[0, k]) info += '\n$s_{\\tt{LNBNN}}=%.3f$' % (qfx2_dist[0, K + Knorm - 1] - qfx2_dist[0, k]) else: info = 'Match:%s\nk=%r, dist=%.3f' % (id_str, k, qfx2_dist[0, k]) info += '\nLNBNN=%.3f' % (qfx2_dist[0, K + Knorm - 1] - qfx2_dist[0, k]) elif k < Knorm + K: type_ = 'Norm' if ut.get_argflag('--texknormplot') and pt.is_texmode(): #info = 'Norm: $j_%r$\ndist=%.3f' % (id_str, k, qfx2_dist[0, k]) info = '\\vspace{1cm}' info += 'Norm: $j_%r$\n$\\textrm{dist}=%.3f$' % (k, qfx2_dist[0, k]) info += '\n\\_' else: info = 'Norm: %s\n$k=%r$, dist=$%.3f$' % (id_str, k, qfx2_dist[0, k]) else: raise Exception('[viz] problem k=%r') return (rchip, kp, sift, fx, aid, info, type_) extracted_list = [] # Remember the query sift feature extracted_list.append(get_extract_tuple(qaid, qfx, -1)) origsift = extracted_list[0][2] skipped = 0 for k in range(K + Knorm): #if qfx2_daid[0, k] == qaid and qfx2_dfx[0, k] == qfx: if qfx2_daid[0, k] == qaid: skipped += 1 continue tup = get_extract_tuple(qfx2_daid[0, k], qfx2_dfx[0, k], k) extracted_list.append(tup) # Draw the _select_ith_match plot nRows = len(extracted_list) if stride is None: stride = nRows # Draw selected feature matches prevsift = None px = 0 # plot offset px_shift = 0 # plot stride shift nExtracted = len(extracted_list) featrow_kw = dict( draw_chip=draw_chip, draw_desc=draw_desc, draw_warped=draw_warped, draw_unwarped=draw_unwarped, ) if ut.get_argflag('--texknormplot'): featrow_kw['ell_color'] = pt.ORANGE pass for listx, tup in enumerate(extracted_list): (rchip, kp, sift, fx, aid, info, type_) = tup if listx % stride == 0: # Create a temporary nRows and fnum in case we are splitting # up nearest neighbors into separate figures with stride _fnum = fnum + listx _nRows = min(nExtracted - listx, stride) px_shift = px df2.figure(fnum=_fnum, docla=True, doclf=True) px_ = px - px_shift px = draw_feat_row(rchip, fx, kp, sift, _fnum, _nRows, px=px_, prevsift=prevsift, origsift=origsift, aid=aid, info=info, type_=type_, **featrow_kw) px += px_shift if prevsift is None or consecutive_distance_compare: prevsift = sift df2.adjust_subplots_safe(hspace=.85, wspace=0, top=.95, bottom=.087, left=.05, right=.95) except Exception as ex: print('[viz] Error in show nearest descriptors') print(ex) raise