def double_detect(img_fpath, **kw): import pyhesaff # Checks to make sure computation is determinsitc _kpts, _vecs = pyhesaff.detect_feats(img_fpath, **kw) kpts_, vecs_ = pyhesaff.detect_feats(img_fpath, **kw) assert np.all(kpts_ == _kpts) assert np.all(vecs_ == _vecs) print('double detect ok') return kpts_, vecs_
def load_test_data(short=False, n=0, use_cpp=False, **kwargs): if 'short' not in vars(): short = False # Read Image #ellipse.rrr() nScales = 4 nSamples = 16 img_fpath = get_test_image() imgBGR = cv2.imread(img_fpath) imgLAB = cv2.cvtColor(imgBGR, cv2.COLOR_BGR2LAB) imgL = imgLAB[:, :, 0] detect_kwargs = {'scale_min': 20, 'scale_max': 100} detect_kwargs.update(kwargs) if not use_cpp: kpts, desc = pyhesaff.detect_feats(img_fpath, **detect_kwargs) else: # Try the new C++ code [kpts], [desc] = pyhesaff.detect_feats_list([img_fpath], **detect_kwargs) if short and n > 0: extra_fxs = [] if split(img_fpath)[1] == 'zebra.png': extra_fxs = [374, 520, 880][0:1] fxs = np.array(spaced_elements2(kpts, n).tolist() + extra_fxs) kpts = kpts[fxs] desc = desc[fxs] test_data = locals() return test_data
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 load_test_data(short=False, n=0, use_cpp=False, **kwargs): if 'short' not in vars(): short = False # Read Image #ellipse.rrr() nScales = 4 nSamples = 16 img_fpath = get_test_image() imgBGR = cv2.imread(img_fpath) imgLAB = cv2.cvtColor(imgBGR, cv2.COLOR_BGR2LAB) imgL = imgLAB[:, :, 0] detect_kwargs = { 'scale_min': 20, 'scale_max': 100 } detect_kwargs.update(kwargs) if not use_cpp: kpts, desc = pyhesaff.detect_feats(img_fpath, **detect_kwargs) else: # Try the new C++ code [kpts], [desc] = pyhesaff.detect_feats_list([img_fpath], **detect_kwargs) if short and n > 0: extra_fxs = [] if split(img_fpath)[1] == 'zebra.png': extra_fxs = [374, 520, 880][0:1] fxs = np.array(spaced_elements2(kpts, n).tolist() + extra_fxs) kpts = kpts[fxs] desc = desc[fxs] test_data = locals() return test_data
def test_pyheaff(img_fpath): r""" This show is interactive in this test! Args: img_fpath (str): CommandLine: python -m pyhesaff.tests.test_pyhesaff --test-test_pyheaff python -m pyhesaff.tests.test_pyhesaff --test-test_pyheaff --show Example: >>> # ENABLE_DOCTEST >>> from pyhesaff.tests.test_pyhesaff import * # NOQA >>> img_fpath = ut.grab_test_imgpath('jeff.png') >>> test_pyheaff(img_fpath) >>> ut.show_if_requested() """ import pyhesaff kpts, desc = pyhesaff.detect_feats(img_fpath) rchip = cv2.imread(img_fpath) if ut.show_was_requested(): from plottool.interact_keypoints import ishow_keypoints ishow_keypoints(rchip, kpts, desc) return locals()
def test_simple_parallel(): r""" CommandLine: python -m pyhesaff.tests.test_pyhesaff_simple_parallel --test-test_simple_parallel --show Example: >>> # ENABLE_DOCTEST >>> from pyhesaff.tests.test_pyhesaff_simple_parallel import * # NOQA >>> import matplotlib as mpl >>> from matplotlib import pyplot as plt >>> img_fpaths, kpts_array, desc_array = test_simple_parallel() >>> ut.quit_if_noshow() >>> # Do not plot by default >>> fig = plt.figure() >>> for count, (img_fpath, kpts, desc) in enumerate(zip(img_fpaths, kpts_array, >>> desc_array)): >>> if count > 3: >>> break >>> ax = fig.add_subplot(2, 2, count + 1) >>> img = mpl.image.imread(img_fpath) >>> plt.imshow(img) >>> _xs, _ys = kpts.T[0:2] >>> ax.plot(_xs, _ys, 'ro', alpha=.5) >>> ut.show_if_requested() """ import pyhesaff test_fnames = ['carl.jpg', 'lena.png', 'zebra.png', 'ada.jpg', 'star.png'] img_fpaths = list(map(ut.grab_test_imgpath, test_fnames)) * 2 # Time parallel computation with ut.Timer('Timing Parallel'): kpts_array, desc_array = pyhesaff.detect_feats_list(img_fpaths) # Time serial computation kpts_list2 = [] desc_list2 = [] with ut.Timer('Timing Iterative'): for img_fpath in img_fpaths: kpts_, desc_ = pyhesaff.detect_feats(img_fpath) kpts_list2.append(kpts_) desc_list2.append(desc_) print('Checking for errors') for (kpts_, desc_, kpts, desc) in zip(kpts_list2, desc_list2, kpts_array, desc_array): print('shape(kpts, kpts_, desc, desc_) = %9r, %9r, %11r, %11r' % (kpts.shape, kpts_.shape, desc.shape, desc_.shape)) try: assert np.all(kpts_ == kpts), 'parallel computation inconsistent' assert np.all(desc_ == desc), 'parallel computation inconsistent' assert len(kpts_) > 0, 'no kpts detected' #assert False, 'deliberate triggering to see printouts' except Exception as ex: ut.printex(ex) raise print('Keypoints seem consistent') return img_fpaths, kpts_array, desc_array
def gen_feat_worker_old(tup): r""" Function to be parallelized by multiprocessing / joblib / whatever. Must take in one argument to be used by multiprocessing.map_async """ # import vtool as vt chip_fpath, probchip_fpath, hesaff_params = tup kpts, vecs = pyhesaff.detect_feats(chip_fpath, **hesaff_params) return (kpts.shape[0], kpts, vecs)
def gen_feat_worker_old(tup): r""" Function to be parallelized by multiprocessing / joblib / whatever. Must take in one argument to be used by multiprocessing.map_async """ #import vtool as vt chip_fpath, probchip_fpath, hesaff_params = tup kpts, vecs = pyhesaff.detect_feats(chip_fpath, **hesaff_params) return (kpts.shape[0], kpts, vecs)
def testdata_kpts(): import utool as ut import vtool as vt import pyhesaff img_fpath = ut.grab_test_imgpath(ut.get_argval('--fname', default='star.png')) kwargs = ut.parse_dict_from_argv(pyhesaff.get_hesaff_default_params()) (kpts, vecs) = pyhesaff.detect_feats(img_fpath, **kwargs) imgBGR = vt.imread(img_fpath) return kpts, vecs, imgBGR
def test_detect_then_show(ax, img_fpath): import pyhesaff kpts, vecs = pyhesaff.detect_feats(img_fpath) print('[test_detect_then_show]') print('img_fpath=%r' % img_fpath) print('kpts=%s' % (ut.truncate_str(repr(kpts)),)) print('vecs=%s' % (ut.truncate_str(repr(vecs)),)) assert len(kpts) == len(vecs) assert len(kpts) > 0, 'no keypoints were detected!' img = mpl.image.imread(img_fpath) plt.imshow(img) _xs, _ys = kpts.T[0:2] ax.plot(_xs, _ys, 'ro', alpha=.5)
def test_detect_then_show(ax, img_fpath): import pyhesaff kpts, vecs = pyhesaff.detect_feats(img_fpath) print('[test_detect_then_show]') print('img_fpath=%r' % img_fpath) print('kpts=%s' % (ut.truncate_str(repr(kpts)), )) print('vecs=%s' % (ut.truncate_str(repr(vecs)), )) assert len(kpts) == len(vecs) assert len(kpts) > 0, 'no keypoints were detected!' img = mpl.image.imread(img_fpath) plt.imshow(img) _xs, _ys = kpts.T[0:2] ax.plot(_xs, _ys, 'ro', alpha=.5)
def compute_feats(depc, chip_id_list, config): """ How SIFT features are computed for a set of chips. Example: >>> # Get circular keypoints of the first three annotations >>> config = dict(AI=False) >>> kpts_list = depc.get('feat', [1, 2, 3], 'kpts', config) """ import pyhesaff chip_gen = depc.get_native('chip', chip_id_list, 'img') for chip in chip_gen: kpts, vecs = pyhesaff.detect_feats(chip, **config) yield len(kpts), kpts, vecs
def simple_iterative_test(): r""" CommandLine: python -m pyhesaff.tests.test_pyhesaff_simple_iterative --test-simple_iterative_test python -m pyhesaff.tests.test_pyhesaff_simple_iterative --test-simple_iterative_test --show Example: >>> # GUI_DOCTEST >>> from pyhesaff.tests.test_pyhesaff_simple_iterative import * # NOQA >>> result = simple_iterative_test() >>> print(result) >>> ut.show_if_requested() """ import pyhesaff fpath_list = [ ut.grab_test_imgpath('lena.png'), ut.grab_test_imgpath('carl.jpg'), ut.grab_test_imgpath('grace.jpg'), ut.grab_test_imgpath('ada.jpg'), ] kpts_list = [] for img_fpath in fpath_list: kpts, vecs = pyhesaff.detect_feats(img_fpath) print('img_fpath=%r' % img_fpath) print('kpts=%s' % (ut.truncate_str(repr(kpts)), )) print('vecs=%s' % (ut.truncate_str(repr(vecs)), )) assert len(kpts) == len(vecs) assert len(kpts) > 0, 'no keypoints were detected!' kpts_list.append(kpts) if ut.show_was_requested(): import matplotlib as mpl from matplotlib import pyplot as plt fig = plt.figure() for i, fpath, kpts in enumerate(zip(fpath_list, kpts_list), start=1): ax = fig.add_subplot(2, 2, i) img = mpl.image.imread(fpath) plt.imshow(img) _xs, _ys = kpts.T[0:2] ax.plot(_xs, _ys, 'ro', alpha=.5)