Example #1
0
def testdata_imglist(shape=(32, 32, 3)):
    """
    Returns 4 colored 32x32 test images, one is structured increasing numbers,
    an images with lines of a cartoon face, and two complex images of people.

    CommandLine:
        python -m ibeis_cnn.utils --test-testdata_imglist --show

    Example:
        >>> # ENABLE_DOCTEST
        >>> from ibeis_cnn.utils import *  # NOQA
        >>> (img_list, width, height, channels) = testdata_imglist()
        >>> ut.quit_if_noshow()
        >>> import plottool as pt
        >>> pt.imshow(img_list[0], pnum=(2, 2, 1))
        >>> pt.imshow(img_list[1], pnum=(2, 2, 2))
        >>> pt.imshow(img_list[2], pnum=(2, 2, 3))
        >>> pt.imshow(img_list[3], pnum=(2, 2, 4))
        >>> ut.show_if_requested()

    """
    import vtool as vt
    x = 32
    height, width, channels = shape
    img0 = np.arange(x**2 * 3, dtype=np.uint8).reshape(x, x, 3)
    img1 = vt.imread(ut.grab_test_imgpath('jeff.png'))
    img2 = vt.imread(ut.grab_test_imgpath('carl.jpg'))
    img3 = vt.imread(ut.grab_test_imgpath('lena.png'))
    img_list = [
        vt.padded_resize(img, (width, height))
        for img in [img0, img1, img2, img3]
    ]
    return img_list, width, height, channels
Example #2
0
def cached_impaint(bgr_img,
                   cached_mask_fpath=None,
                   label_colors=None,
                   init_mask=None,
                   aug=False,
                   refine=False):
    import vtool as vt
    if cached_mask_fpath is None:
        cached_mask_fpath = 'image_' + ut.hashstr_arr(bgr_img) + '.png'
    if aug:
        cached_mask_fpath += '.' + ut.hashstr_arr(bgr_img)
        if label_colors is not None:
            cached_mask_fpath += ut.hashstr_arr(label_colors)
        cached_mask_fpath += '.png'
    #cached_mask_fpath = 'tmp_mask.png'
    if refine or not ut.checkpath(cached_mask_fpath):
        if refine and ut.checkpath(cached_mask_fpath):
            if init_mask is None:
                init_mask = vt.imread(cached_mask_fpath, grayscale=True)
        custom_mask = impaint_mask(bgr_img,
                                   label_colors=label_colors,
                                   init_mask=init_mask)
        vt.imwrite(cached_mask_fpath, custom_mask)
    else:
        custom_mask = vt.imread(cached_mask_fpath, grayscale=True)
    return custom_mask
Example #3
0
    def test_grabcut_on_aid(aid):
        chip_fpath = ibs.get_annot_chip_fpath(aid)
        probchip_fpath = ibs.get_annot_probchip_fpath(aid)

        chip_img = vt.imread(chip_fpath)
        probchip_img = vt.imread(probchip_fpath, grayscale=True)

        label_values = [cv2.GC_BGD, cv2.GC_PR_BGD, cv2.GC_PR_FGD, cv2.GC_FGD]

        def probchip_to_grabcut_labels(probchip_img, w, h):
            scaled_probchip = cv2.resize(probchip_img, dsize=(w, h))
            mask = ((len(label_values) - 1) * (scaled_probchip / 255)).astype(np.uint8)
            # Except for one center pixel
            #mask[mask.shape[0] // 2, mask.shape[1] // 2] = 3
            label_mask = mask.copy()
            for index, value in enumerate(label_values):
                label_mask[mask == index] = value
            # No certainty
            label_mask[label_mask == cv2.GC_FGD] = cv2.GC_PR_FGD
            label_mask[label_mask == cv2.GC_BGD] = cv2.GC_PR_BGD
            return label_mask

        def grabcut_labels_to_probchip(label_mask):
            image_mask = label_mask.copy()
            label_colors = np.linspace(0, 255, len(label_values)).astype(np.uint8)
            for value, color in zip(label_values, label_colors):
                image_mask[label_mask == value] = (color)
            return image_mask

        def grabcut_from_probchip(chip_img, label_mask):
            rect = (0, 0, w, h)
            bgd_model = np.zeros((1, 13 * 5), np.float64)
            fgd_model = np.zeros((1, 13 * 5), np.float64)
            num_iters = 5
            mode = cv2.GC_INIT_WITH_MASK
            # label_mask is an outvar
            label_mask_ = label_mask.copy()
            print(label_values)
            print(np.unique(label_mask_))
            with ut.Timer('grabcut'):
                cv2.grabCut(chip_img, label_mask_, rect, bgd_model, fgd_model, num_iters, mode=mode)
            #is_foreground = (label_mask == cv2.GC_FGD) + (label_mask == cv2.GC_PR_FGD)
            #is_foreground = (label_mask_ == cv2.GC_FGD)  # + (label_mask == cv2.GC_PR_FGD)
            return label_mask_

        (h, w) = chip_img.shape[0:2]
        label_mask = probchip_to_grabcut_labels(probchip_img, w, h)
        label_mask_ = grabcut_from_probchip(chip_img, label_mask)
        float_mask = grabcut_labels_to_probchip(label_mask_) / 255.0
        segmented_chip = chip_img * float_mask[:, :, None]

        next_pnum = df2.make_pnum_nextgen(2, 3)
        df2.imshow(chip_img,                               fnum=1, pnum=next_pnum())
        df2.imshow(probchip_img,                           fnum=1, pnum=next_pnum())
        df2.imshow(grabcut_labels_to_probchip(label_mask), fnum=1, pnum=next_pnum())
        df2.imshow(segmented_chip,                         fnum=1, pnum=next_pnum())
        df2.imshow(255 * (float_mask),                  fnum=1, pnum=next_pnum())
        df2.imshow(chip_img * (float_mask > .6)[:, :, None],   fnum=1, pnum=next_pnum())
        df2.present()
Example #4
0
def test_cv2_flann():
    """
    Ignore:
        [name for name in dir(cv2) if 'create' in name.lower()]
        [name for name in dir(cv2) if 'stereo' in name.lower()]

        ut.grab_zipped_url('https://priithon.googlecode.com/archive/a6117f5e81ec00abcfb037f0f9da2937bb2ea47f.tar.gz', download_dir='.')
    """
    import cv2
    from vtool.tests import dummy
    import plottool as pt
    import vtool as vt
    img1 = vt.imread(ut.grab_test_imgpath('easy1.png'))
    img2 = vt.imread(ut.grab_test_imgpath('easy2.png'))

    stereo = cv2.StereoBM_create(numDisparities=16, blockSize=15)
    disparity = stereo.compute(img1, img2)
    pt.imshow(disparity)
    pt.show()

    #cv2.estima

    flow = cv2.createOptFlow_DualTVL1()
    img1, img2 = vt.convert_image_list_colorspace([img1, img2], 'gray', src_colorspace='bgr')
    img2 = vt.resize(img2, img1.shape[0:2][::-1])
    out = img1.copy()
    flow.calc(img1, img2, out)

    orb = cv2.ORB_create()
    kp1, vecs1 = orb.detectAndCompute(img1, None)
    kp2, vecs2 = orb.detectAndCompute(img2, None)

    detector = cv2.FeatureDetector_create("SIFT")
    descriptor = cv2.DescriptorExtractor_create("SIFT")

    skp = detector.detect(img1)
    skp, sd = descriptor.compute(img1, skp)

    tkp = detector.detect(img2)
    tkp, td = descriptor.compute(img2, tkp)

    out = img1.copy()
    cv2.drawKeypoints(img1, kp1, outImage=out)
    pt.imshow(out)

    vecs1 = dummy.testdata_dummy_sift(10)
    vecs2 = dummy.testdata_dummy_sift(10)  # NOQA

    FLANN_INDEX_KDTREE = 0  # bug: flann enums are missing
    #flann_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=4)
    index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
    search_params = dict(checks=50)   # or pass empty dictionary
    flann = cv2.FlannBasedMatcher(index_params, search_params)  # NOQA

    cv2.flann.Index(vecs1, index_params)

    #cv2.FlannBasedMatcher(flann_params)

    cv2.flann.Index(vecs1, flann_params)  # NOQA
Example #5
0
def gen_feat_worker(tup):
    r"""
    Function to be parallelized by multiprocessing / joblib / whatever.
    Must take in one argument to be used by multiprocessing.map_async

    Args:
        tup (tuple):

    Returns:
        tuple: (None, kpts, vecs)

    CommandLine:
        python -m ibeis.algo.preproc.preproc_feat --exec-gen_feat_worker --show
        python -m ibeis.algo.preproc.preproc_feat --exec-gen_feat_worker --show --aid 2
        python -m ibeis.algo.preproc.preproc_feat --exec-gen_feat_worker --show --aid 1988 --db GZ_Master1
        python -m ibeis.algo.preproc.preproc_feat --exec-gen_feat_worker --show --aid 1988 --db GZ_Master1 --affine-invariance=False --scale_max=30
        python -m ibeis.algo.preproc.preproc_feat --exec-gen_feat_worker --show --aid 1988 --db GZ_Master1 --affine-invariance=False --scale_max=30
        python -m ibeis.algo.preproc.preproc_feat --exec-gen_feat_worker --show --aid 1988 --db GZ_Master1 --affine-invariance=False --bgmethod=None  --scale_max=30

    Example:
        >>> # DISABLE_DOCTEST
        >>> from ibeis.algo.preproc.preproc_feat import *  # NOQA
        >>> import utool as ut
        >>> import ibeis
        >>> import vtool as vt
        >>> ibs, aid_list = ibeis.testdata_aids('PZ_MTEST')
        >>> aid = aid_list[0]
        >>> chip_fpath = ibs.get_annot_chip_fpath(aid)
        >>> bgmethod = ut.get_argval('--bgmethod', type_=str, default='cnn')
        >>> probchip_fpath = ibs.get_annot_probchip_fpath(aid) if 'cnn' == bgmethod else None
        >>> hesaff_params = {}  # {'affine_invariance': False}
        >>> hesaff_params = ut.argparse_dict(pyhesaff.get_hesaff_default_params())
        >>> tup = (chip_fpath, probchip_fpath, hesaff_params)
        >>> (num_kpts, kpts, vecs) = gen_feat_worker(tup)
        >>> result = ('(num_kpts, kpts, vecs) = %s' % (ut.repr2((num_kpts, kpts, vecs)),))
        >>> ut.quit_if_noshow()
        >>> import plottool as pt
        >>> masked_chip, = ut.exec_func_src(gen_feat_worker, key_list=['masked_chip'], sentinal='kpts, vecs = pyhesaff')
        >>> pt.interact_keypoints.ishow_keypoints(masked_chip, kpts, vecs)
        >>> #pt.plot_score_histograms([vt.get_scales(kpts)])
        >>> ut.show_if_requested()
        >>> print(result)
    """
    import numpy as np
    import vtool as vt
    chip_fpath, probchip_fpath, hesaff_params = tup
    chip = vt.imread(chip_fpath)
    if probchip_fpath is not None:
        probchip = vt.imread(probchip_fpath, grayscale=True)
        masked_chip = (chip *
                       (probchip[:, :, None].astype(np.float32) / 255)).astype(
                           np.uint8)
    else:
        masked_chip = chip
    kpts, vecs = pyhesaff.detect_feats_in_image(masked_chip, **hesaff_params)
    num_kpts = kpts.shape[0]
    return (num_kpts, kpts, vecs)
Example #6
0
def gen_feat_worker(tup):
    r"""
    Function to be parallelized by multiprocessing / joblib / whatever.
    Must take in one argument to be used by multiprocessing.map_async

    Args:
        tup (tuple):

    Returns:
        tuple: (None, kpts, vecs)

    CommandLine:
        python -m ibeis.algo.preproc.preproc_feat --exec-gen_feat_worker --show
        python -m ibeis.algo.preproc.preproc_feat --exec-gen_feat_worker --show --aid 2
        python -m ibeis.algo.preproc.preproc_feat --exec-gen_feat_worker --show --aid 1988 --db GZ_Master1
        python -m ibeis.algo.preproc.preproc_feat --exec-gen_feat_worker --show --aid 1988 --db GZ_Master1 --affine-invariance=False --scale_max=30
        python -m ibeis.algo.preproc.preproc_feat --exec-gen_feat_worker --show --aid 1988 --db GZ_Master1 --affine-invariance=False --scale_max=30
        python -m ibeis.algo.preproc.preproc_feat --exec-gen_feat_worker --show --aid 1988 --db GZ_Master1 --affine-invariance=False --bgmethod=None  --scale_max=30

    Example:
        >>> # DISABLE_DOCTEST
        >>> from ibeis.algo.preproc.preproc_feat import *  # NOQA
        >>> import utool as ut
        >>> import ibeis
        >>> import vtool as vt
        >>> ibs, aid_list = ibeis.testdata_aids('PZ_MTEST')
        >>> aid = aid_list[0]
        >>> chip_fpath = ibs.get_annot_chip_fpath(aid)
        >>> bgmethod = ut.get_argval('--bgmethod', type_=str, default='cnn')
        >>> probchip_fpath = ibs.get_annot_probchip_fpath(aid) if 'cnn' == bgmethod else None
        >>> hesaff_params = {}  # {'affine_invariance': False}
        >>> hesaff_params = ut.argparse_dict(pyhesaff.get_hesaff_default_params())
        >>> tup = (chip_fpath, probchip_fpath, hesaff_params)
        >>> (num_kpts, kpts, vecs) = gen_feat_worker(tup)
        >>> result = ('(num_kpts, kpts, vecs) = %s' % (ut.repr2((num_kpts, kpts, vecs)),))
        >>> ut.quit_if_noshow()
        >>> import plottool as pt
        >>> masked_chip, = ut.exec_func_src(gen_feat_worker, key_list=['masked_chip'], sentinal='kpts, vecs = pyhesaff')
        >>> pt.interact_keypoints.ishow_keypoints(masked_chip, kpts, vecs)
        >>> #pt.plot_score_histograms([vt.get_scales(kpts)])
        >>> ut.show_if_requested()
        >>> print(result)
    """
    import numpy as np
    import vtool as vt

    chip_fpath, probchip_fpath, hesaff_params = tup
    chip = vt.imread(chip_fpath)
    if probchip_fpath is not None:
        probchip = vt.imread(probchip_fpath, grayscale=True)
        masked_chip = (chip * (probchip[:, :, None].astype(np.float32) / 255)).astype(np.uint8)
    else:
        masked_chip = chip
    kpts, vecs = pyhesaff.detect_feats_in_image(masked_chip, **hesaff_params)
    num_kpts = kpts.shape[0]
    return (num_kpts, kpts, vecs)
Example #7
0
def compute_or_read_annotation_chips(ibs, aid_list, ensure=True, config2_=None,
                                     verbose=False, eager=True):
    r"""
    SUPER HACY FUNCTION. NEED TO DEPRICATE

    ----------------------
    Found 1 line(s) in 'ibeis/algo/preproc/preproc_chip.py':
    preproc_chip.py :  25 |def compute_or_read_annotation_chips(ibs, aid_list,
    ensure=True):
    ----------------------
    Found 1 line(s) in 'ibeis/control/manual_chip_funcs.py':
    manual_chip_funcs.py : 313 |    chip_list =
    preproc_chip.compute_or_read_annotation_chips(ibs, aid_list, ensure=ensure)

    """
    if ensure:
        try:
            ut.assert_all_not_None(aid_list, 'aid_list')
        except AssertionError as ex:
            ut.printex(ex, key_list=['aid_list'])
            raise
    nTotal = len(aid_list)
    cfpath_list = make_annot_chip_fpath_list(ibs, aid_list, config2_=config2_)
    mk_cpath_iter = functools.partial(ut.ProgressIter, cfpath_list,
                                      nTotal=nTotal, enabled=verbose, freq=100)
    try:
        if ensure:
            cfpath_iter = mk_cpath_iter(lbl='reading ensured chips')
            chip_list = [vt.imread(cfpath) for cfpath in cfpath_iter]
            #for cfpath in cfpath_iter:
            #    yield vt.imread(cfpath)
        else:
            cfpath_iter = mk_cpath_iter(lbl='reading existing chips')
            chip_list = [None if cfpath is None else vt.imread(cfpath) for cfpath in cfpath_iter]
            #for cfpath in cfpath_iter:
            #    yield None if cfpath is None else vt.imread(cfpath)
    except IOError as ex:
        if not ut.QUIET:
            ut.printex(ex, '[preproc_chip] Handing Exception: ', iswarning=True)
        ibs.add_annot_chips(aid_list)
        try:
            cfpath_iter = mk_cpath_iter(lbl='reading fallback1 chips')
            chip_list = [vt.imread(cfpath) for cfpath in cfpath_iter]
        except IOError:
            print('[preproc_chip] cache must have been deleted from disk')
            # TODO: WE CAN SEARCH FOR NON EXISTANT PATHS HERE AND CALL
            # ibs.delete_annot_chips
            compute_and_write_chips_lazy(ibs, aid_list)
            # Try just one more time
            cfpath_iter = mk_cpath_iter(lbl='reading fallback2 chips')
            chip_list = [vt.imread(cfpath) for cfpath in cfpath_iter]

    return chip_list
Example #8
0
    def dummy_preproc_chip(depc, annot_rowid_list, config=None):
        """
        TODO: Infer properties from docstr?

        Args:
            depc (dtool.DependencyCache):
            annot_rowid_list (list): list of annot rowids
            config (dict): config dictionary

        Returns:
            tuple : ((int, int), ('extern', vt.imread))
        """
        if config is None:
            config = {}
        # Demonstates using asobject to get input to function as a dictionary
        # of properties
        #for annot in annot_list:
        #print('[preproc] Computing chips of aid=%r' % (aid,))
        print('[preproc] Computing chips')
        for aid in annot_rowid_list:
            #aid = annot['aid']
            #chip_fpath = annot['gpath']
            chip_fpath = gpath_list[aid]
            #w, h = vt.image.open_image_size(chip_fpath)
            chip = vt.imread(chip_fpath)
            size = vt.get_size(chip)
            #size = (w, h)
            #print('* chip_fpath = %r' % (chip_fpath,))
            #print('* size = %r' % (size,))
            #yield size, chip_fpath
            yield size, chip
Example #9
0
def image_src(gid=None, thumbnail=False, ibs=None, **kwargs):
    if ibs is None:
        ibs = current_app.ibs

    gid = int(gid)
    gpath = None

    if 'thumbsize' not in kwargs:
        kwargs['thumbsize'] = max(
            int(appf.TARGET_WIDTH),
            int(appf.TARGET_HEIGHT),
        )

    if 'draw_annots' not in kwargs:
        kwargs['draw_annots'] = False

    if thumbnail:
        try:
            gpath = ibs.get_image_thumbpath(gid, ensure_paths=True, **kwargs)
            orient = ibs.get_image_orientation(gid)
            image = vt.imread(gpath, orient=orient)
            h, w = image.shape[:2]
            assert h > 0, 'Invalid image thumbnail'
            assert w > 0, 'Invalid image thumbnail'
        except AssertionError:
            gpath = None

    if gpath is None:
        gpath = ibs.get_image_paths(gid)

    image_src = image_src_path(gpath, **kwargs)
    return image_src
Example #10
0
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()
Example #11
0
def kaggle7_chip_src(aid=None, ibs=None, **kwargs):
    from six.moves import cStringIO as StringIO
    from PIL import Image  # NOQA
    from flask import current_app, send_file
    from wbia.web import appfuncs as appf
    import six

    if ibs is None:
        ibs = current_app.ibs

    aid = int(aid)
    aid_list = [aid]
    chip_paths = ibs.depc_annot.get('KaggleSevenChip',
                                    aid_list,
                                    'image',
                                    read_extern=False,
                                    ensure=True)
    chip_path = chip_paths[0]

    # Load image
    assert chip_paths is not None, 'chip path should not be None'
    image = vt.imread(chip_path, orient='auto')
    image = appf.resize_via_web_parameters(image)
    image = image[:, :, ::-1]

    # Encode image
    image_pil = Image.fromarray(image)
    if six.PY2:
        img_io = StringIO()
    else:
        img_io = BytesIO()
    image_pil.save(img_io, 'JPEG', quality=100)
    img_io.seek(0)

    return send_file(img_io, mimetype='image/jpeg')
Example #12
0
def read_thumb_size(thumb_path):
    if VERBOSE_THUMB:
        print('[ThumbDelegate] Reading thumb size')
    npimg = vt.imread(thumb_path, delete_if_corrupted=True)
    (height, width) = npimg.shape[0:2]
    del npimg
    return width, height
Example #13
0
def draw_demo():
    r"""
    CommandLine:
        python -m plottool.interact_impaint --exec-draw_demo --show

    Example:
        >>> # SCRIPT
        >>> from plottool.interact_impaint import *  # NOQA
        >>> result = draw_demo()
        >>> print(result)
        >>> ut.quit_if_noshow()
        >>> import plottool as pt
        >>> ut.show_if_requested()
    """
    fpath = ut.grab_test_imgpath('zebra.png')
    img = vt.imread(fpath)
    mask = impaint_mask2(img)
    print('mask = %r' % (mask, ))
    print('mask.sum() = %r' % (mask.sum(), ))
    if False:
        plt.imshow(vt.blend_images_multiply(img, mask))
        ax = plt.gca()
        ax.grid(False)
        ax.set_xticks([])
        ax.set_yticks([])
Example #14
0
def show_hough_image(ibs, gid, species=None, fnum=None, **kwargs):
    if fnum is None:
        fnum = pt.next_fnum()
    title = 'Hough Image: ' + vh.get_image_titles(ibs, gid)
    print(title)

    if species is None:
        species = ibs.cfg.detect_cfg.species_text
    src_gpath_list = ibs.get_image_detectpaths([gid])
    dst_gpath_list = [splitext(gpath)[0] for gpath in src_gpath_list]
    hough_gpath_list = [gpath + '_' + species + '_hough.png' for gpath in dst_gpath_list]
    # Detect with hough
    config = {
        'output_gpath_list': hough_gpath_list,
    }
    print('-' * 80)
    print('')
    print('WARNING!!!')
    print('Hough image is not used often and not worth putting into depcache.')
    print('This image is computed as needed and not cached to disk.')
    print('')
    print('-' * 80)
    results_list = list(randomforest.detect_gpath_list_with_species(ibs, src_gpath_list, species, **config))  # NOQA
    # Get path
    hough_gpath = hough_gpath_list[0]
    img = vt.imread(hough_gpath)
    fig, ax = viz_image2.show_image(img, title=title, fnum=fnum, **kwargs)
    return fig, ax
Example #15
0
def _resize_worker(gfpath, new_gfpath, new_size):
    """ worker function for parallel generator """
    import vtool as vt
    img = vt.imread(gfpath)
    new_img = vt.resize(img, new_size)
    vt.imwrite(new_gfpath, new_img)
    return new_gfpath
Example #16
0
def show_probability_chip(ibs, aid, species=None, fnum=None, config2_=None,
                          blend=False, **kwargs):
    """
    TODO: allow species override in controller

    CommandLine:
        python -m ibeis.viz.viz_hough --exec-show_probability_chip --cnn --show
        python -m ibeis.viz.viz_hough --exec-show_probability_chip --cnn --show --db PZ_Master1
        python -m ibeis.viz.viz_hough --exec-show_probability_chip --cnn --show --db PZ_Master1 --aid 9970

    Example:
        >>> # SCRIPT
        >>> from ibeis.viz.viz_hough import *  # NOQA
        >>> import ibeis
        >>> from ibeis.viz import viz_chip
        >>> ibs, aid_list, kwargs, config2_ = viz_chip.testdata_showchip()
        >>> fnum = 1
        >>> species = None
        >>> aid = aid_list[0]
        >>> fig, ax = show_probability_chip(ibs, aid, species, fnum, blend=True, **kwargs)
        >>> ut.show_if_requested()
    """
    fnum = pt.ensure_fnum(fnum)
    title = 'Probability Chip: ' + ', '.join(vh.get_annot_text(ibs, [aid], True))
    hough_cpath = ibs.get_annot_probchip_fpath(aid, config2_=config2_)
    img = vt.imread(hough_cpath)
    if blend:
        chip = ibs.get_annot_chips(aid, config2_=config2_)
        img = vt.blend_images_multiply(chip, vt.resize_mask(img, chip))
    fig, ax = viz_image2.show_image(img, title=title, fnum=fnum, **kwargs)
    return fig, ax
Example #17
0
def show_hough_image(ibs, gid, species=None, fnum=None, **kwargs):
    if fnum is None:
        fnum = pt.next_fnum()
    title = 'Hough Image: ' + vh.get_image_titles(ibs, gid)
    print(title)

    if species is None:
        species = ibs.get_
        #.cfg.detect_cfg.species_text
    src_gpath_list = ibs.get_image_detectpaths([gid])
    dst_gpath_list = [splitext(gpath)[0] for gpath in src_gpath_list]
    hough_gpath_list = [
        gpath + '_' + species + '_hough.png' for gpath in dst_gpath_list
    ]
    # Detect with hough
    config = {
        'output_gpath_list': hough_gpath_list,
    }
    print('-' * 80)
    print('')
    print('WARNING!!!')
    print('Hough image is not used often and not worth putting into depcache.')
    print('This image is computed as needed and not cached to disk.')
    print('')
    print('-' * 80)
    results_list = list(
        randomforest.detect_gpath_list_with_species(  # NOQA
            ibs, src_gpath_list, species, **config))
    # Get path
    hough_gpath = hough_gpath_list[0]
    img = vt.imread(hough_gpath)
    fig, ax = viz_image2.show_image(img, title=title, fnum=fnum, **kwargs)
    return fig, ax
Example #18
0
def draw_demo():
    r"""
    CommandLine:
        python -m plottool.interact_impaint --exec-draw_demo --show

    Example:
        >>> # SCRIPT
        >>> from plottool.interact_impaint import *  # NOQA
        >>> result = draw_demo()
        >>> print(result)
        >>> ut.quit_if_noshow()
        >>> import plottool as pt
        >>> ut.show_if_requested()
    """
    fpath = ut.grab_test_imgpath('zebra.png')
    img = vt.imread(fpath)
    mask = impaint_mask2(img)
    print('mask = %r' % (mask,))
    print('mask.sum() = %r' % (mask.sum(),))
    if False:
        plt.imshow(vt.blend_images_multiply(img, mask))
        ax = plt.gca()
        ax.grid(False)
        ax.set_xticks([])
        ax.set_yticks([])
Example #19
0
    def dummy_preproc_chip(depc, annot_rowid_list, config=None):
        """
        TODO: Infer properties from docstr?

        Args:
            depc (dtool.DependencyCache):
            annot_rowid_list (list): list of annot rowids
            config (dict): config dictionary

        Returns:
            tuple : ((int, int), ('extern', vt.imread))
        """
        if config is None:
            config = {}
        # Demonstates using asobject to get input to function as a dictionary
        # of properties
        #for annot in annot_list:
        #print('[preproc] Computing chips of aid=%r' % (aid,))
        print('[preproc] Computing chips')
        for aid in annot_rowid_list:
            #aid = annot['aid']
            #chip_fpath = annot['gpath']
            chip_fpath = gpath_list[aid]
            #w, h = vt.image.open_image_size(chip_fpath)
            chip = vt.imread(chip_fpath)
            size = vt.get_size(chip)
            #size = (w, h)
            print('Dummpy preproc chip yeilds')
            print('* chip_fpath = %r' % (chip_fpath, ))
            print('* size = %r' % (size, ))
            #yield size, chip_fpath
            yield size, chip
Example #20
0
def show_probability_chip(
    ibs, aid, species=None, fnum=None, config2_=None, blend=False, **kwargs
):
    """
    TODO: allow species override in controller

    CommandLine:
        python -m wbia.viz.viz_hough --exec-show_probability_chip --cnn --show
        python -m wbia.viz.viz_hough --exec-show_probability_chip --cnn --show --db PZ_Master1
        python -m wbia.viz.viz_hough --exec-show_probability_chip --cnn --show --db PZ_Master1 --aid 9970

    Example:
        >>> # SCRIPT
        >>> from wbia.viz.viz_hough import *  # NOQA
        >>> import wbia
        >>> from wbia.viz import viz_chip
        >>> ibs, aid_list, kwargs, config2_ = viz_chip.testdata_showchip()
        >>> fnum = 1
        >>> species = None
        >>> aid = aid_list[0]
        >>> fig, ax = show_probability_chip(ibs, aid, species, fnum, blend=True, **kwargs)
        >>> ut.show_if_requested()
    """
    fnum = pt.ensure_fnum(fnum)
    title = 'Probability Chip: ' + ', '.join(vh.get_annot_text(ibs, [aid], True))
    hough_cpath = ibs.get_annot_probchip_fpath(aid, config2_=config2_)
    img = vt.imread(hough_cpath)
    if blend:
        chip = ibs.get_annot_chips(aid, config2_=config2_)
        img = vt.blend_images_multiply(chip, vt.resize_mask(img, chip))
    fig, ax = viz_image2.show_image(img, title=title, fnum=fnum, **kwargs)
    return fig, ax
Example #21
0
def testdata_blend(scale=128):
    import vtool as vt
    img_fpath = ut.grab_test_imgpath('lena.png')
    img1 = vt.imread(img_fpath)
    rng = np.random.RandomState(0)
    img2 = vt.perlin_noise(img1.shape[0:2], scale=scale, rng=rng)[None, :].T
    img1 = vt.rectify_to_float01(img1)
    img2 = vt.rectify_to_float01(img2)
    return img1, img2
Example #22
0
    def get_database_icon(ibs, max_dsize=(None, 192), aid=None):
        r"""
        Args:
            max_dsize (tuple): (default = (None, 192))

        Returns:
            None: None

        CommandLine:
            python -m wbia.control.IBEISControl --exec-get_database_icon --show
            python -m wbia.control.IBEISControl --exec-get_database_icon --show --db Oxford

        Example:
            >>> # DISABLE_DOCTEST
            >>> from wbia.control.IBEISControl import *  # NOQA
            >>> import wbia
            >>> ibs = wbia.opendb(defaultdb='testdb1')
            >>> icon = ibs.get_database_icon()
            >>> ut.quit_if_noshow()
            >>> import wbia.plottool as pt
            >>> pt.imshow(icon)
            >>> ut.show_if_requested()
        """
        # if ibs.get_dbname() == 'Oxford':
        #    pass
        # else:
        import vtool as vt

        if hasattr(ibs, 'force_icon_aid'):
            aid = ibs.force_icon_aid
        if aid is None:
            species = ibs.get_primary_database_species()
            # Use a url to get the icon
            url = {
                ibs.const.TEST_SPECIES.GIR_MASAI:
                'http://i.imgur.com/tGDVaKC.png',
                ibs.const.TEST_SPECIES.ZEB_PLAIN:
                'http://i.imgur.com/2Ge1PRg.png',
                ibs.const.TEST_SPECIES.ZEB_GREVY:
                'http://i.imgur.com/PaUT45f.png',
            }.get(species, None)
            if url is not None:
                icon = vt.imread(ut.grab_file_url(url), orient='auto')
            else:
                # HACK: (this should probably be a db setting)
                # use an specific aid to get the icon
                aid = {
                    'Oxford': 73,
                    'seaturtles': 37
                }.get(ibs.get_dbname(), None)
                if aid is None:
                    # otherwise just grab a random aid
                    aid = ibs.get_valid_aids()[0]
        if aid is not None:
            icon = ibs.get_annot_chips(aid)
        icon = vt.resize_to_maxdims(icon, max_dsize)
        return icon
Example #23
0
def bgfunc(path):
    # Test for /_test_buffered_generator_img
    #import utool as ut
    import vtool as vt
    for _ in range(1):
        img = vt.imread(path)
    img = img**1.1
    #[ut.is_prime(346373) for _ in range(2)]
    return img
Example #24
0
def testdata_blend(scale=128):
    import vtool as vt
    img_fpath = ut.grab_test_imgpath('lena.png')
    img1 = vt.imread(img_fpath)
    rng = np.random.RandomState(0)
    img2 = vt.perlin_noise(img1.shape[0:2], scale=scale, rng=rng)[None, :].T
    img1 = vt.rectify_to_float01(img1)
    img2 = vt.rectify_to_float01(img2)
    return img1, img2
Example #25
0
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
Example #26
0
def bgfunc(path):
    # Test for /_test_buffered_generator_img
    #import utool as ut
    import vtool as vt
    for _ in range(1):
        img = vt.imread(path)
    img = img ** 1.1
    #[ut.is_prime(346373) for _ in range(2)]
    return img
Example #27
0
 def parse_img_from_arg(argstr_):
     fpath = ut.get_argval(argstr_, type_=str, default='None')
     if fpath is not None and fpath != 'None':
         img = vt.imread(fpath, grayscale=True)
         print('Reading %s with stats %s' % (fpath, ut.get_stats_str(img, axis=None)))
     else:
         print('Did not read %s' % (fpath))
         img = None
     return img
def make_thread_thumb(img_path, dsize, new_verts_list, interest_list):
    r"""
    Makes thumbnail with overlay. Called in thead

    CommandLine:
        python -m wbia.guitool.api_thumb_delegate --test-make_thread_thumb --show

    Example:
        >>> # DISABLE_DOCTEST
        >>> # GUI_DOCTEST
        >>> # xdoctest: +REQUIRES(--gui)
        >>> from wbia.guitool.api_thumb_delegate import *  # NOQA
        >>> import wbia.plottool as pt
        >>> # build test data
        >>> img_path = ut.grab_test_imgpath('carl.jpg')
        >>> dsize = (32, 32)
        >>> new_verts_list = []
        >>> # execute function
        >>> thumb = make_thread_thumb(img_path, dsize, new_verts_list)
        >>> ut.quit_if_noshow()
        >>> pt.imshow(thumb)
        >>> pt.show_if_requested()
    """
    import vtool as vt
    from vtool import geometry

    orange_bgr = (0, 128, 255)
    blue_bgr = (255, 128, 0)
    # imread causes a MEMORY LEAK most likely!
    img = vt.imread(img_path)  # Read Image (.0424s) <- Takes most time!
    # if False:
    #    #http://stackoverflow.com/questions/9794019/convert-numpy-array-to-pyside-qpixmap
    #    # http://kogs-www.informatik.uni-hamburg.de/~meine/software/vigraqt/qimage2ndarray.py
    #    #import numpy as np
    #    #qimg = QtGui.QImage(img_path, str(QtGui.QImage.Format_RGB32))
    #    #temp_shape = (qimg.height(), qimg.bytesPerLine() * 8 // qimg.depth(), 4)
    #    #result_shape = (qimg.height(), qimg.width())
    #    #buf = qimg.bits().asstring(qimg.numBytes())
    #    #result  = np.frombuffer(buf, np.uint8).reshape(temp_shape)
    #    #result = result[:, :result_shape[1]]
    #    #result = result[..., :3]
    #    #img = result
    thumb = vt.image.resize(img, dsize)  # Resize to thumb dims (.0015s)
    del img
    # Draw bboxes on thumb (not image)
    color_bgr_list = [
        blue_bgr if interest else orange_bgr for interest in interest_list
    ]
    for new_verts, color_bgr in zip(new_verts_list, color_bgr_list):
        if new_verts is not None:
            geometry.draw_verts(thumb,
                                new_verts,
                                color=color_bgr,
                                thickness=2,
                                out=thumb)
        # thumb = geometry.draw_verts(thumb, new_verts, color=orange_bgr, thickness=2)
    return thumb
Example #29
0
 def root_asobject(aid):
     """ Convinience for writing preproc funcs """
     gpath = gpath_list[aid]
     root_obj = ut.LazyDict({
         'aid': aid,
         'gpath': gpath,
         'image': lambda: vt.imread(gpath)
     })
     return root_obj
Example #30
0
 def parse_img_from_arg(argstr_):
     fpath = ut.get_argval(argstr_, type_=str, default='None')
     if fpath is not None and fpath != 'None':
         img = vt.imread(fpath, grayscale=True)
         print('Reading %s with stats %s' %
               (fpath, ut.get_stats_str(img, axis=None)))
     else:
         print('Did not read %s' % (fpath))
         img = None
     return img
Example #31
0
def write_dirty_aids(ibs, dirty_probchip_fpath_list, dirty_aids, config2_,
                     species):
    if config2_ is None:
        fw_detector = ibs.cfg.featweight_cfg.fw_detector
    else:
        fw_detector = config2_.get('fw_detector')

    if fw_detector == 'rf':
        (
            extramargin_fpath_list,
            probchip_extramargin_fpath_list,
            halfoffset_cs_list,
        ) = compute_extramargin_detectchip(ibs,
                                           dirty_aids,
                                           config2_=config2_,
                                           species=species,
                                           FACTOR=4)
        #dirty_cfpath_list  = ibs.get_annot_chip_fpath(dirty_aids, ensure=True, config2_=config2_)

        config = {
            'scale_list': [1.0],
            'output_gpath_list': probchip_extramargin_fpath_list,
            'mode': 1,
        }
        probchip_generator = randomforest.detect_gpath_list_with_species(
            ibs, extramargin_fpath_list, species, **config)
        # Evalutate genrator until completion
        ut.evaluate_generator(probchip_generator)
        extramargin_mask_gen = (vt.imread(fpath, grayscale=True)
                                for fpath in probchip_extramargin_fpath_list)
        # Crop the extra margin off of the new probchips
        _iter = zip(dirty_probchip_fpath_list, extramargin_mask_gen,
                    halfoffset_cs_list)
        for (probchip_fpath, extramargin_probchip, halfmargin) in _iter:
            half_w, half_h = halfmargin
            probchip = extramargin_probchip[half_h:-half_h, half_w:-half_w]
            vt.imwrite(probchip_fpath, probchip)
    elif fw_detector == 'cnn':
        # dont use extrmargin here (for now)
        chip_fpath_list = ibs.get_annot_chip_fpath(dirty_aids,
                                                   config2_=config2_)
        mask_gen = ibs.generate_species_background_mask(
            chip_fpath_list, species)
        _iter = zip(dirty_probchip_fpath_list, mask_gen)
        for chunk in ut.ichunks(_iter, 64):
            for probchip_fpath, probchip in ut.ProgressIter(
                    chunk,
                    lbl='write probchip chunk',
                    adjust=True,
                    time_thresh=30.0):
                probchip = postprocess_mask(probchip)
                vt.imwrite(probchip_fpath, probchip)
    else:
        raise NotImplementedError('bad fw_detector=%r' % (fw_detector, ))
Example #32
0
def cached_impaint(bgr_img, cached_mask_fpath=None, label_colors=None,
                   init_mask=None, aug=False, refine=False):
    import vtool as vt
    if cached_mask_fpath is None:
        cached_mask_fpath = 'image_' + ut.hashstr_arr(bgr_img) + '.png'
    if aug:
        cached_mask_fpath += '.' + ut.hashstr_arr(bgr_img)
        if label_colors is not None:
            cached_mask_fpath += ut.hashstr_arr(label_colors)
        cached_mask_fpath += '.png'
    #cached_mask_fpath = 'tmp_mask.png'
    if refine or not ut.checkpath(cached_mask_fpath):
        if refine and ut.checkpath(cached_mask_fpath):
            if init_mask is None:
                init_mask = vt.imread(cached_mask_fpath, grayscale=True)
        custom_mask = impaint_mask(bgr_img, label_colors=label_colors, init_mask=init_mask)
        vt.imwrite(cached_mask_fpath, custom_mask)
    else:
        custom_mask = vt.imread(cached_mask_fpath, grayscale=True)
    return custom_mask
    def plot_image(self, index):
        px = index - self.start_index
        gpath = self.gpath_list[index]

        if self.vizkw is None:
            _vizkw = {}
        else:
            _vizkw = self.vizkw.copy()

        _vizkw.update({
            'fnum': self.fnum,
            'pnum': self.pnum_(px),
        })

        if ut.is_funclike(gpath):
            showfunc = gpath
            # HACK
            # override of plot image function
            showfunc(**_vizkw)
            import plottool as pt
            ax = pt.gca()
        else:
            if isinstance(gpath, six.string_types):
                img = vt.imread(gpath)
            else:
                img = gpath

            bbox_list = self.bboxes_list[index]
            #print('bbox_list %r in display for px: %r ' % (bbox_list, px))
            theta_list = self.thetas_list[index]

            label_list = [ix + 1 for ix in range(len(bbox_list))]
            #Add true values for every bbox to display
            sel_list = [True for ix in range(len(bbox_list))]
            _vizkw.update({
                #title should always be the image number
                'title': str(index),
                'bbox_list': bbox_list,
                'theta_list': theta_list,
                'sel_list': sel_list,
                'label_list': label_list,
            })
            #print(utool.dict_str(_vizkw))
            #print('vizkw = ' + utool.dict_str(_vizkw))
            _, ax = viz_image2.show_image(img, **_vizkw)
            if self.xlabel_list is not None:
                import plottool as pt
                pt.set_xlabel(self.xlabel_list[index])
            #print(index)
            ph.set_plotdat(ax, 'bbox_list', bbox_list)
            ph.set_plotdat(ax, 'gpath', gpath)
        ph.set_plotdat(ax, 'px', str(px))
        ph.set_plotdat(ax, 'index', index)
Example #34
0
    def plot_image(self, index):
        px = index - self.start_index
        gpath      = self.gpath_list[index]

        if self.vizkw is None:
            _vizkw = {}
        else:
            _vizkw = self.vizkw.copy()

        _vizkw.update({
            'fnum': self.fnum,
            'pnum': self.pnum_(px),
        })

        if ut.is_funclike(gpath):
            showfunc = gpath
            # HACK
            # override of plot image function
            showfunc(**_vizkw)
            import plottool as pt
            ax = pt.gca()
        else:
            if isinstance(gpath, six.string_types):
                img = vt.imread(gpath)
            else:
                img = gpath

            bbox_list  = self.bboxes_list[index]
            #print('bbox_list %r in display for px: %r ' % (bbox_list, px))
            theta_list = self.thetas_list[index]

            label_list = [ix + 1 for ix in range(len(bbox_list))]
            #Add true values for every bbox to display
            sel_list = [True for ix in range(len(bbox_list))]
            _vizkw.update({
                #title should always be the image number
                'title': str(index),
                'bbox_list'  : bbox_list,
                'theta_list' : theta_list,
                'sel_list'   : sel_list,
                'label_list' : label_list,
            })
            #print(utool.dict_str(_vizkw))
            #print('vizkw = ' + utool.dict_str(_vizkw))
            _, ax = viz_image2.show_image(img, **_vizkw)
            if self.xlabel_list is not None:
                import plottool as pt
                pt.set_xlabel(self.xlabel_list[index])
            #print(index)
            ph.set_plotdat(ax, 'bbox_list', bbox_list)
            ph.set_plotdat(ax, 'gpath', gpath)
        ph.set_plotdat(ax, 'px', str(px))
        ph.set_plotdat(ax, 'index', index)
Example #35
0
    def get_database_icon(ibs, max_dsize=(None, 192), aid=None):
        r"""
        Args:
            max_dsize (tuple): (default = (None, 192))

        Returns:
            None: None

        CommandLine:
            python -m ibeis.control.IBEISControl --exec-get_database_icon --show
            python -m ibeis.control.IBEISControl --exec-get_database_icon --show --db Oxford

        Example:
            >>> # DISABLE_DOCTEST
            >>> from ibeis.control.IBEISControl import *  # NOQA
            >>> import ibeis
            >>> ibs = ibeis.opendb(defaultdb='testdb1')
            >>> icon = ibs.get_database_icon()
            >>> ut.quit_if_noshow()
            >>> import plottool as pt
            >>> pt.imshow(icon)
            >>> ut.show_if_requested()
        """
        #if ibs.get_dbname() == 'Oxford':
        #    pass
        #else:
        import vtool as vt
        if hasattr(ibs, 'force_icon_aid'):
            aid = ibs.force_icon_aid
        if aid is None:
            species = ibs.get_primary_database_species()
            # Use a url to get the icon
            url = {
                ibs.const.TEST_SPECIES.GIR_MASAI: 'http://i.imgur.com/tGDVaKC.png',
                ibs.const.TEST_SPECIES.ZEB_PLAIN: 'http://i.imgur.com/2Ge1PRg.png',
                ibs.const.TEST_SPECIES.ZEB_GREVY: 'http://i.imgur.com/PaUT45f.png',
            }.get(species, None)
            if url is not None:
                icon = vt.imread(ut.grab_file_url(url), orient='auto')
            else:
                # HACK: (this should probably be a db setting)
                # use an specific aid to get the icon
                aid = {
                    'Oxford': 73,
                    'seaturtles': 37,
                }.get(ibs.get_dbname(), None)
                if aid is None:
                    # otherwise just grab a random aid
                    aid = ibs.get_valid_aids()[0]
        if aid is not None:
            icon = ibs.get_annot_chips(aid)
        icon = vt.resize_to_maxdims(icon, max_dsize)
        return icon
Example #36
0
def make_thread_thumb(img_path, dsize, new_verts_list):
    r"""
    Args:
        img_path (?):
        dsize (tuple):  width, height
        new_verts_list (list):

    Returns:
        ?: thumb

    CommandLine:
        python -m guitool.api_thumb_delegate --test-make_thread_thumb --show

    Example:
        >>> # DISABLE_DOCTEST
        >>> from guitool.api_thumb_delegate import *  # NOQA
        >>> import plottool as pt
        >>> # build test data
        >>> img_path = ut.grab_test_imgpath('carl.jpg')
        >>> dsize = (32, 32)
        >>> new_verts_list = []
        >>> # execute function
        >>> thumb = make_thread_thumb(img_path, dsize, new_verts_list)
        >>> ut.quit_if_noshow()
        >>> pt.imshow(thumb)
        >>> pt.show_if_requested()
    """
    orange_bgr = (0, 128, 255)
    # imread causes a MEMORY LEAK most likely!
    img = vt.imread(img_path)  # Read Image (.0424s) <- Takes most time!
    #if False:
    #    #http://stackoverflow.com/questions/9794019/convert-numpy-array-to-pyside-qpixmap
    #    # http://kogs-www.informatik.uni-hamburg.de/~meine/software/vigraqt/qimage2ndarray.py
    #    #import numpy as np
    #    #qimg = QtGui.QImage(img_path, str(QtGui.QImage.Format_RGB32))
    #    #temp_shape = (qimg.height(), qimg.bytesPerLine() * 8 // qimg.depth(), 4)
    #    #result_shape = (qimg.height(), qimg.width())
    #    #buf = qimg.bits().asstring(qimg.numBytes())
    #    #result  = np.frombuffer(buf, np.uint8).reshape(temp_shape)
    #    #result = result[:, :result_shape[1]]
    #    #result = result[..., :3]
    #    #img = result
    thumb = vt.image.resize(img, dsize)  # Resize to thumb dims (.0015s)
    del img
    # Draw bboxes on thumb (not image)
    for new_verts in new_verts_list:
        if new_verts is not None:
            geometry.draw_verts(thumb, new_verts, color=orange_bgr, thickness=2, out=thumb)
        #thumb = geometry.draw_verts(thumb, new_verts, color=orange_bgr, thickness=2)
    return thumb
def experiments_image_src(tag=None, **kwargs):
    tag = tag.strip().split('-')
    db = tag[0]
    gid = int(tag[1])

    ibs = experiment_init_db(db)
    config = {
        'thumbsize': 800,
    }
    gpath = ibs.get_image_thumbpath(gid, ensure_paths=True, **config)

    # Load image
    image = vt.imread(gpath, orient='auto')
    image = appf.resize_via_web_parameters(image)
    return appf.embed_image_html(image, target_width=None)
Example #38
0
 def dummy_manual_chipmask(depc, parent_rowids, config=None):
     import vtool as vt
     from plottool import interact_impaint
     mask_dpath = ut.unixjoin(depc.cache_dpath, 'ManualChipMask')
     ut.ensuredir(mask_dpath)
     if config is None:
         config = {}
     print('Requesting user defined chip mask')
     for rowid in parent_rowids:
         img = vt.imread(gpath_list[rowid])
         mask = interact_impaint.impaint_mask2(img)
         mask_fpath = ut.unixjoin(mask_dpath, 'mask%d.png' % (rowid,))
         vt.imwrite(mask_fpath, mask)
         w, h = vt.get_size(mask)
         yield (w, h), mask_fpath
Example #39
0
 def dummy_manual_chipmask(depc, parent_rowids, config=None):
     import vtool as vt
     from plottool import interact_impaint
     mask_dpath = join(depc.cache_dpath, 'ManualChipMask')
     ut.ensuredir(mask_dpath)
     if config is None:
         config = {}
     print('Requesting user defined chip mask')
     for rowid in parent_rowids:
         img = vt.imread(gpath_list[rowid])
         mask = interact_impaint.impaint_mask2(img)
         mask_fpath = join(mask_dpath, 'mask%d.png' % (rowid, ))
         vt.imwrite(mask_fpath, mask)
         w, h = vt.get_size(mask)
         yield (w, h), mask_fpath
    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 wbia.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)
Example #41
0
def compute_fgweights(ibs, aid_list, config2_=None):
    """

    Example:
        >>> # SLOW_DOCTEST
        >>> from ibeis.algo.preproc.preproc_featweight import *  # NOQA
        >>> import ibeis
        >>> ibs = ibeis.opendb('testdb1')
        >>> aid_list = ibs.get_valid_aids()[1:2]
        >>> config2_ = None
        >>> featweight_list = compute_fgweights(ibs, aid_list)
        >>> result = np.array_str(featweight_list[0][0:3], precision=3)
        >>> print(result)
        [ 0.125  0.061  0.053]

    """
    nTasks = len(aid_list)
    print('[preproc_featweight.compute_fgweights] Preparing to compute %d fgweights' % (nTasks,))
    probchip_fpath_list = preproc_probchip.compute_and_write_probchip(ibs,
                                                                      aid_list,
                                                                      config2_=config2_)
    chipsize_list = ibs.get_annot_chip_sizes(aid_list, config2_=config2_)

    #if ut.DEBUG2:
    #    from PIL import Image
    #    probchip_size_list = [Image.open(fpath).size for fpath in probchip_fpath_list]  # NOQA
    #    #with ut.embed_on_exception_context:
    #    # does not need to happen anymore
    #    assert chipsize_list == probchip_size_list, 'probably need to clear chip or probchip cache'

    kpts_list = ibs.get_annot_kpts(aid_list, config2_=config2_)
    # Force grayscale reading of chips
    probchip_list = [vt.imread(fpath, grayscale=True) if exists(fpath) else None
                     for fpath in probchip_fpath_list]

    print('[preproc_featweight.compute_fgweights] Computing %d fgweights' % (nTasks,))
    arg_iter = zip(aid_list, kpts_list, probchip_list, chipsize_list)
    featweight_gen = ut.generate(gen_featweight_worker, arg_iter,
                                    nTasks=nTasks, ordered=True, freq=10)
    featweight_param_list = list(featweight_gen)
    #arg_iter = zip(aid_list, kpts_list, probchip_list)
    #featweight_param_list1 = [gen_featweight_worker((aid, kpts, probchip)) for
    #aid, kpts, probchip in arg_iter]
    #featweight_aids = ut.get_list_column(featweight_param_list, 0)
    featweight_list = ut.get_list_column(featweight_param_list, 1)
    print('[preproc_featweight.compute_fgweights] Done computing %d fgweights' % (nTasks,))
    return featweight_list
Example #42
0
def compute_forgroundness(fpath1, kpts1, species='zebra_plains'):
    """
    hack in foregroundness
    """
    import pyrf
    import vtool as vt
    from os.path import exists
    # hack for getting a model (not entirely ibeis independent)
    trees_path = ut.get_app_resource_dir('ibeis', 'detectmodels', 'rf',
                                         species)
    tree_fpath_list = ut.glob(trees_path, '*.txt')
    detector = pyrf.Random_Forest_Detector()
    # TODO; might need to downsample
    forest = detector.forest(tree_fpath_list, verbose=False)
    gpath_list = [fpath1]
    output_gpath_list = [
        gpath + '.' + species + '.probchip.png' for gpath in gpath_list
    ]
    detectkw = {
        'scale_list': [1.15, 1.0, 0.85, 0.7, 0.55, 0.4, 0.25, 0.1],
        'output_gpath_list': output_gpath_list,
        'mode': 1,  # mode one outputs probimage
    }
    results_iter = detector.detect(forest, gpath_list, **detectkw)
    results_list = list(results_iter)  # NOQA
    probchip_list = [
        vt.imread(gpath, grayscale=True) if exists(gpath) else None
        for gpath in output_gpath_list
    ]
    #vtpatch.get_warped_patches()
    fgweights_list = []
    kpts_list = [kpts1]
    for probchip, kpts in zip(probchip_list, kpts_list):
        patch_list = [
            vt.get_warped_patch(probchip, kp)[0].astype(np.float32) / 255.0
            for kp in kpts
        ]
        weight_list = [
            vt.gaussian_average_patch(patch) for patch in patch_list
        ]
        #weight_list = [patch.sum() / (patch.size) for patch in patch_list]
        weights = np.array(weight_list, dtype=np.float32)
        fgweights_list.append(weights)
    fgweights = fgweights_list[0]
    detector.free_forest(forest)
    return fgweights
Example #43
0
def image_src_api(rowid=None, thumbnail=False, fresh=False, **kwargs):
    r"""
    Returns the image file of image <gid>

    Example:
        >>> from wbia.web.app import *  # NOQA
        >>> import wbia
        >>> with wbia.opendb_with_web('testdb1') as (ibs, client):
        ...     resp = client.get('/api/image/src/1/')
        >>> print(resp.data)
        b'\xff\xd8\xff\xe0\x00\x10JFIF...

    RESTful:
        Method: GET
        URL:    /api/image/src/<rowid>/
    """
    from PIL import Image  # NOQA

    thumbnail = thumbnail or 'thumbnail' in request.args or 'thumbnail' in request.form
    ibs = current_app.ibs
    if thumbnail:
        gpath = ibs.get_image_thumbpath(rowid, ensure_paths=True)
        fresh = fresh or 'fresh' in request.args or 'fresh' in request.form
        if fresh:
            # import os
            # os.remove(gpath)
            ut.delete(gpath)
            gpath = ibs.get_image_thumbpath(rowid, ensure_paths=True)
    else:
        gpath = ibs.get_image_paths(rowid)

    # Load image
    assert gpath is not None, 'image path should not be None'
    image = vt.imread(gpath, orient='auto')
    image = appf.resize_via_web_parameters(image)
    image = image[:, :, ::-1]

    # Encode image
    image_pil = Image.fromarray(image)
    if six.PY2:
        img_io = StringIO()
    else:
        img_io = BytesIO()
    image_pil.save(img_io, 'JPEG', quality=100)
    img_io.seek(0)
    return send_file(img_io, mimetype='image/jpeg')
Example #44
0
def image_src_api(rowid=None, thumbnail=False, fresh=False, **kwargs):
    r"""
    Returns the image file of image <gid>

    Example:
        >>> # xdoctest: +REQUIRES(--web-tests)
        >>> from wbia.web.app import *  # NOQA
        >>> import wbia
        >>> with wbia.opendb_bg_web('testdb1', start_job_queue=False, managed=True) as web_ibs:
        ...     resp = web_ibs.send_wbia_request('/api/image/src/1/', type_='get', json=False)
        >>> print(resp)

    RESTful:
        Method: GET
        URL:    /api/image/src/<rowid>/
    """
    from PIL import Image  # NOQA

    thumbnail = thumbnail or 'thumbnail' in request.args or 'thumbnail' in request.form
    ibs = current_app.ibs
    if thumbnail:
        gpath = ibs.get_image_thumbpath(rowid, ensure_paths=True)
        fresh = fresh or 'fresh' in request.args or 'fresh' in request.form
        if fresh:
            # import os
            # os.remove(gpath)
            ut.delete(gpath)
            gpath = ibs.get_image_thumbpath(rowid, ensure_paths=True)
    else:
        gpath = ibs.get_image_paths(rowid)

    # Load image
    assert gpath is not None, 'image path should not be None'
    image = vt.imread(gpath, orient='auto')
    image = appf.resize_via_web_parameters(image)
    image = image[:, :, ::-1]

    # Encode image
    image_pil = Image.fromarray(image)
    if six.PY2:
        img_io = StringIO()
    else:
        img_io = BytesIO()
    image_pil.save(img_io, 'JPEG', quality=100)
    img_io.seek(0)
    return send_file(img_io, mimetype='image/jpeg')
Example #45
0
def get_chips(ibs, cid_list, ensure=True, verbose=False, eager=True, config2_=None):
    r"""
    Returns:
        chip_list (list): a list cropped images in numpy array form by their cid

    Args:
        cid_list (list):
        ensure (bool):  eager evaluation if True

    RESTful:
        Returns the base64 encoded image of annotation (chip) <aid>  # Documented and routed in ibeis.web app.py
        Method: GET
        URL:    /api/annot/<aid>

    CommandLine:
        python -m ibeis.templates.template_generator --key chip --funcname-filter '\<get_chips\>'

    Returns:
        list: chip_list
    """
    # FIXME: HACK: this should not have to read in config2
    # (unless it needs to compute the chips?)
    from ibeis.algo.preproc import preproc_chip
    if ensure:
        try:
            ut.assert_all_not_None(cid_list, 'cid_list')
        except AssertionError as ex:
            ut.printex(ex, 'Invalid cid_list', key_list=[
                'ensure', 'cid_list'])
            raise
    aid_list = ibs.get_chip_aids(cid_list)
    if eager:
        chip_list = preproc_chip.compute_or_read_annotation_chips(
            ibs, aid_list, ensure=ensure, verbose=verbose, config2_=config2_)
    else:
        import vtool as vt
        cfpath_list = preproc_chip.make_annot_chip_fpath_list(
            ibs, aid_list, config2_=config2_)
        nInput = len(cid_list)
        chip_list = (
            vt.imread(cfpath)
            for cfpath in ut.ProgressIter(cfpath_list, Total=nInput,
                                          lbl='Lazy Reading Chips',
                                          enabled=verbose, freq=100)
        )
    return chip_list
Example #46
0
def write_dirty_aids(ibs, dirty_probchip_fpath_list, dirty_aids, config2_, species):
    if config2_ is None:
        featweight_detector = ibs.cfg.featweight_cfg.featweight_detector
    else:
        featweight_detector = config2_.get('featweight_detector')

    if featweight_detector == 'rf':
        (extramargin_fpath_list,
         probchip_extramargin_fpath_list,
         halfoffset_cs_list,
         ) = compute_extramargin_detectchip(
             ibs, dirty_aids, config2_=config2_, species=species, FACTOR=4)
        #dirty_cfpath_list  = ibs.get_annot_chip_fpath(dirty_aids, ensure=True, config2_=config2_)

        config = {
            'scale_list': [1.0],
            'output_gpath_list': probchip_extramargin_fpath_list,
            'mode': 1,
        }
        probchip_generator = randomforest.detect_gpath_list_with_species(
            ibs, extramargin_fpath_list, species, **config)
        # Evalutate genrator until completion
        ut.evaluate_generator(probchip_generator)
        extramargin_mask_gen = (
            vt.imread(fpath, grayscale=True) for fpath in probchip_extramargin_fpath_list
        )
        # Crop the extra margin off of the new probchips
        _iter = zip(dirty_probchip_fpath_list,
                    extramargin_mask_gen,
                    halfoffset_cs_list)
        for (probchip_fpath, extramargin_probchip, halfmargin) in _iter:
            half_w, half_h = halfmargin
            probchip = extramargin_probchip[half_h:-half_h, half_w:-half_w]
            vt.imwrite(probchip_fpath, probchip)
    elif featweight_detector == 'cnn':
        # dont use extrmargin here (for now)
        chip_fpath_list = ibs.get_annot_chip_fpath(dirty_aids, config2_=config2_)
        mask_gen = ibs.generate_species_background_mask(chip_fpath_list, species)
        _iter = zip(dirty_probchip_fpath_list, mask_gen)
        for chunk in ut.ichunks(_iter, 64):
            for probchip_fpath, probchip in ut.ProgressIter(chunk, lbl='write probchip chunk', adjust=True, time_thresh=30.0):
                probchip = postprocess_mask(probchip)
                vt.imwrite(probchip_fpath, probchip)
    else:
        raise NotImplementedError('bad featweight_detector=%r' % (featweight_detector,))
Example #47
0
def image_src(gid=None, thumbnail=False, fresh=False, **kwargs):
    thumbnail = thumbnail or 'thumbnail' in request.args or 'thumbnail' in request.form
    ibs = current_app.ibs
    gid = int(gid)
    if thumbnail:
        gpath = ibs.get_image_thumbpath(gid, ensure_paths=True)
        fresh = fresh or 'fresh' in request.args or 'fresh' in request.form
        if fresh:
            import os
            os.remove(gpath)
            gpath = ibs.get_image_thumbpath(gid, ensure_paths=True)
    else:
        gpath = ibs.get_image_paths(gid)

    # Load image
    image = vt.imread(gpath, orient='auto')
    image = appf.resize_via_web_parameters(image)
    return appf.embed_image_html(image, target_width=None)
Example #48
0
def image_src(gid=None, thumbnail=False, fresh=False, **kwargs):
    thumbnail = thumbnail or 'thumbnail' in request.args or 'thumbnail' in request.form
    ibs = current_app.ibs
    gid = int(gid)
    if thumbnail:
        gpath = ibs.get_image_thumbpath(gid, ensure_paths=True)
        fresh = fresh or 'fresh' in request.args or 'fresh' in request.form
        if fresh:
            import os
            os.remove(gpath)
            gpath = ibs.get_image_thumbpath(gid, ensure_paths=True)
    else:
        gpath = ibs.get_image_paths(gid)

    # Load image
    image = vt.imread(gpath, orient='auto')
    image = appf.resize_via_web_parameters(image)
    return appf.embed_image_html(image, target_width=None)
Example #49
0
File: apis.py Project: whaozl/ibeis
def image_src_api(gid=None, thumbnail=False, fresh=False, **kwargs):
    r"""
    Returns the image file of image <gid>

    Example:
        >>> # WEB_DOCTEST
        >>> from ibeis.web.app import *  # NOQA
        >>> import ibeis
        >>> web_ibs = ibeis.opendb_bg_web('testdb1', start_job_queue=False)
        >>> web_ibs.send_ibeis_request('/api/image/src/', type_='get', gid=1)
        >>> print(resp)
        >>> web_ibs.terminate2()

    RESTful:
        Method: GET
        URL:    /api/image/src/<gid>/
    """
    from PIL import Image  # NOQA
    thumbnail = thumbnail or 'thumbnail' in request.args or 'thumbnail' in request.form
    ibs = current_app.ibs
    if thumbnail:
        gpath = ibs.get_image_thumbpath(gid, ensure_paths=True)
        fresh = fresh or 'fresh' in request.args or 'fresh' in request.form
        if fresh:
            #import os
            #os.remove(gpath)
            ut.delete(gpath)
            gpath = ibs.get_image_thumbpath(gid, ensure_paths=True)
    else:
        gpath = ibs.get_image_paths(gid)

    # Load image
    assert gpath is not None, 'image path should not be None'
    image = vt.imread(gpath, orient='auto')
    image = appf.resize_via_web_parameters(image)
    image = image[:, :, ::-1]

    # Encode image
    image_pil = Image.fromarray(image)
    img_io = StringIO.StringIO()
    image_pil.save(img_io, 'JPEG', quality=100)
    img_io.seek(0)
    return send_file(img_io, mimetype='image/jpeg')
Example #50
0
def test_average_contrast():
    import vtool as vt
    ut.get_valid_test_imgkeys()
    img_fpath_list = [ut.grab_test_imgpath(key) for key in ut.get_valid_test_imgkeys()]
    img_list = [vt.imread(img, grayscale=True) for img in img_fpath_list]
    avecontrast_list = np.array([compute_average_contrast(img) for img in img_list])
    import plottool as pt
    nCols = len(img_list)
    fnum = None
    if fnum is None:
        fnum = pt.next_fnum()
    pt.figure(fnum=fnum, pnum=(2, 1, 1))
    sortx = avecontrast_list.argsort()
    y_list = avecontrast_list[sortx]
    x_list = np.arange(0, nCols) + .5
    pt.plot(x_list, y_list, 'bo-')
    sorted_imgs = ut.take(img_list, sortx)
    for px, img in ut.ProgressIter(enumerate(sorted_imgs, start=1)):
        pt.imshow(img, fnum=fnum, pnum=(2, nCols, nCols + px))
Example #51
0
 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)
Example #52
0
def test_average_contrast():
    import vtool as vt
    ut.get_valid_test_imgkeys()
    img_fpath_list = [
        ut.grab_test_imgpath(key) for key in ut.get_valid_test_imgkeys()
    ]
    img_list = [vt.imread(img, grayscale=True) for img in img_fpath_list]
    avecontrast_list = np.array(
        [compute_average_contrast(img) for img in img_list])
    import plottool as pt
    nCols = len(img_list)
    fnum = None
    if fnum is None:
        fnum = pt.next_fnum()
    pt.figure(fnum=fnum, pnum=(2, 1, 1))
    sortx = avecontrast_list.argsort()
    y_list = avecontrast_list[sortx]
    x_list = np.arange(0, nCols) + .5
    pt.plot(x_list, y_list, 'bo-')
    sorted_imgs = ut.take(img_list, sortx)
    for px, img in ut.ProgressIter(enumerate(sorted_imgs, start=1)):
        pt.imshow(img, fnum=fnum, pnum=(2, nCols, nCols + px))
Example #53
0
def show_data_image(data_uri_order, i, offset_list, all_kpts, all_vecs):
    """
    i = 12
    """
    import vtool as vt
    from os.path import join
    imgdir = ut.truepath('/raid/work/Oxford/oxbuild_images')
    gpath = join(imgdir, data_uri_order[i] + '.jpg')
    image = vt.imread(gpath)
    import plottool as pt
    pt.qt4ensure()
    # pt.imshow(image)
    l = offset_list[i]
    r = offset_list[i + 1]
    kpts = all_kpts[l:r]
    vecs = all_vecs[l:r]
    pt.interact_keypoints.ishow_keypoints(image,
                                          kpts,
                                          vecs,
                                          ori=False,
                                          ell_alpha=.4,
                                          color='distinct')
Example #54
0
def background_src_api(rowid=None, fresh=False, **kwargs):
    r"""
    Returns the image file of annot <aid>

    Example:
        >>> # xdoctest: +REQUIRES(--slow)
        >>> # xdoctest: +REQUIRES(--web-tests)
        >>> from wbia.web.app import *  # NOQA
        >>> import wbia
        >>> with wbia.opendb_with_web('testdb1') as (ibs, client):
        ...     resp = client.get('/api/background/src/1/')
        >>> print(resp.data)
        b'\xff\xd8\xff\xe0\x00\x10JFIF...

    RESTful:
        Method: GET
        URL:    /api/annot/src/<rowid>/
    """
    from PIL import Image  # NOQA

    ibs = current_app.ibs
    gpath = ibs.get_annot_probchip_fpath(rowid)

    # Load image
    assert gpath is not None, 'image path should not be None'
    image = vt.imread(gpath, orient='auto')
    image = appf.resize_via_web_parameters(image)
    image = image[:, :, ::-1]

    # Encode image
    image_pil = Image.fromarray(image)
    if six.PY2:
        img_io = StringIO()
    else:
        img_io = BytesIO()
    image_pil.save(img_io, 'JPEG', quality=100)
    img_io.seek(0)
    return send_file(img_io, mimetype='image/jpeg')
Example #55
0
def load_images(cache_data_filename='test_data.npy',
                cache_labels_filename='test_labels.npy',
                cache=True):

    cache_data_filepath = join('.', cache_data_filename)
    cache_labels_filepath = join('.', cache_labels_filename)

    if exists(cache_data_filepath) and exists(cache_labels_filepath) and cache:
        data_list = np.load(cache_data_filepath)
        label_list = np.load(cache_labels_filepath)
        return data_list, label_list

    ibs = ibeis.opendb(dbdir='/media/danger/GGR/GGR-IBEIS-TEST/')
    gid_list = ibs.get_valid_gids()
    filepath_list = ibs.get_image_paths(gid_list)

    data_list = []
    label_list = []
    for index, (gid, filepath) in enumerate(zip(gid_list, filepath_list)):
        if index % 25 == 0:
            print(index)
        data = vt.imread(filepath, orient='auto')
        data = cv2.resize(data, (192, 192), interpolation=cv2.INTER_LANCZOS4)
        aid_list = ibs.get_image_aids(gid)
        species_list = ibs.get_annot_species_texts(aid_list)
        shared_set = set(species_list) & set(['zebra_grevys', 'zebra_plains'])
        label = 'positive' if len(shared_set) > 0 else 'negative'
        data_list.append(data)
        label_list.append(label)

    data_list = np.array(data_list, dtype=np.uint8)
    label_list = np.array(label_list)

    np.save(cache_data_filepath, data_list)
    np.save(cache_labels_filepath, label_list)

    return data_list, label_list
Example #56
0
def compute_forgroundness(fpath1, kpts1, species="zebra_plains"):
    """
    hack in foregroundness
    """
    import pyrf
    import vtool as vt
    from os.path import exists

    # hack for getting a model (not entirely ibeis independent)
    trees_path = ut.get_app_resource_dir("ibeis", "detectmodels", "rf", species)
    tree_fpath_list = ut.glob(trees_path, "*.txt")
    detector = pyrf.Random_Forest_Detector()
    # TODO; might need to downsample
    forest = detector.forest(tree_fpath_list, verbose=False)
    gpath_list = [fpath1]
    output_gpath_list = [gpath + "." + species + ".probchip.png" for gpath in gpath_list]
    detectkw = {
        "scale_list": [1.15, 1.0, 0.85, 0.7, 0.55, 0.4, 0.25, 0.1],
        "output_gpath_list": output_gpath_list,
        "mode": 1,  # mode one outputs probimage
    }
    results_iter = detector.detect(forest, gpath_list, **detectkw)
    results_list = list(results_iter)  # NOQA
    probchip_list = [vt.imread(gpath, grayscale=True) if exists(gpath) else None for gpath in output_gpath_list]
    # vtpatch.get_warped_patches()
    fgweights_list = []
    kpts_list = [kpts1]
    for probchip, kpts in zip(probchip_list, kpts_list):
        patch_list = [vt.get_warped_patch(probchip, kp)[0].astype(np.float32) / 255.0 for kp in kpts]
        weight_list = [vt.gaussian_average_patch(patch) for patch in patch_list]
        # weight_list = [patch.sum() / (patch.size) for patch in patch_list]
        weights = np.array(weight_list, dtype=np.float32)
        fgweights_list.append(weights)
    fgweights = fgweights_list[0]
    detector.free_forest(forest)
    return fgweights