Exemple #1
0
def make_wordfigures(ibs, metrics, invindex, figdir, wx_sample, wx2_dpath):
    """
    Builds mosaics of patches assigned to words in sample
    ouptuts them to disk
    """
    from plottool import draw_func2 as df2
    import vtool as vt
    import parse

    vocabdir = join(figdir, 'vocab_patches2')
    ut.ensuredir(vocabdir)
    dump_word_patches(ibs, vocabdir, invindex, wx_sample, metrics)

    # COLLECTING PART --- collects patches in word folders
    #vocabdir

    seldpath = vocabdir + '_selected'
    ut.ensurepath(seldpath)
    # stack for show
    for wx, dpath in ut.progiter(six.iteritems(wx2_dpath),
                                 lbl='Dumping Word Images:',
                                 num=len(wx2_dpath),
                                 freq=1,
                                 backspace=False):
        #df2.rrr()
        fpath_list = ut.ls(dpath)
        fname_list = [basename(fpath_) for fpath_ in fpath_list]
        patch_list = [vt.imread(fpath_) for fpath_ in fpath_list]
        # color each patch by nid
        nid_list = [
            int(parse.parse('{}_nid={nid}_{}', fname)['nid'])
            for fname in fname_list
        ]
        nid_set = set(nid_list)
        nid_list = np.array(nid_list)
        if len(nid_list) == len(nid_set):
            # no duplicate names
            newpatch_list = patch_list
        else:
            # duplicate names. do coloring
            sortx = nid_list.argsort()
            patch_list = np.array(patch_list, dtype=object)[sortx]
            fname_list = np.array(fname_list, dtype=object)[sortx]
            nid_list = nid_list[sortx]
            colors = (255 *
                      np.array(df2.distinct_colors(len(nid_set)))).astype(
                          np.int32)
            color_dict = dict(zip(nid_set, colors))
            wpad, hpad = 3, 3
            newshape_list = [
                tuple(
                    (np.array(patch.shape) + (wpad * 2, hpad * 2, 0)).tolist())
                for patch in patch_list
            ]
            color_list = [color_dict[nid_] for nid_ in nid_list]
            newpatch_list = [
                np.zeros(shape) + color[None, None]
                for shape, color in zip(newshape_list, color_list)
            ]
            for patch, newpatch in zip(patch_list, newpatch_list):
                newpatch[wpad:-wpad, hpad:-hpad, :] = patch
            #img_list = patch_list
            #bigpatch = vt.stack_image_recurse(patch_list)
        #bigpatch = vt.stack_image_list(patch_list, vert=False)
        bigpatch = vt.stack_square_images(newpatch_list)
        bigpatch_fpath = join(seldpath, basename(dpath) + '_patches.png')

        #
        def _dictstr(dict_):
            str_ = ut.dict_str(dict_, newlines=False)
            str_ = str_.replace('\'', '').replace(': ', '=').strip('{},')
            return str_

        figtitle = '\n'.join([
            'wx=%r' % wx,
            'stat(pdist): %s' % _dictstr(metrics.wx2_pdist_stats[wx]),
            'stat(wdist): %s' % _dictstr(metrics.wx2_wdist_stats[wx]),
        ])
        metrics.wx2_nMembers[wx]

        df2.figure(fnum=1, doclf=True, docla=True)
        fig, ax = df2.imshow(bigpatch, figtitle=figtitle)
        #fig.show()
        df2.set_figtitle(figtitle)
        df2.adjust_subplots(top=.878, bottom=0)
        df2.save_figure(1, bigpatch_fpath)
Exemple #2
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 = cv2.imread(chip_fpath)
        probchip_img = cv2.imread(probchip_fpath, flags=cv2.IMREAD_GRAYSCALE)

        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()
Exemple #3
0
def make_wordfigures(ibs, metrics, invindex, figdir, wx_sample, wx2_dpath):
    """
    Builds mosaics of patches assigned to words in sample
    ouptuts them to disk
    """
    from plottool import draw_func2 as df2
    import vtool as vt
    import parse

    vocabdir = join(figdir, 'vocab_patches2')
    ut.ensuredir(vocabdir)
    dump_word_patches(ibs, vocabdir, invindex, wx_sample, metrics)

    # COLLECTING PART --- collects patches in word folders
    #vocabdir

    seldpath = vocabdir + '_selected'
    ut.ensurepath(seldpath)
    # stack for show
    for wx, dpath in ut.progiter(six.iteritems(wx2_dpath), lbl='Dumping Word Images:', num=len(wx2_dpath), freq=1, backspace=False):
        #df2.rrr()
        fpath_list = ut.ls(dpath)
        fname_list = [basename(fpath_) for fpath_ in fpath_list]
        patch_list = [gtool.imread(fpath_) for fpath_ in fpath_list]
        # color each patch by nid
        nid_list = [int(parse.parse('{}_nid={nid}_{}', fname)['nid']) for fname in fname_list]
        nid_set = set(nid_list)
        nid_list = np.array(nid_list)
        if len(nid_list) == len(nid_set):
            # no duplicate names
            newpatch_list = patch_list
        else:
            # duplicate names. do coloring
            sortx = nid_list.argsort()
            patch_list = np.array(patch_list, dtype=object)[sortx]
            fname_list = np.array(fname_list, dtype=object)[sortx]
            nid_list = nid_list[sortx]
            colors = (255 * np.array(df2.distinct_colors(len(nid_set)))).astype(np.int32)
            color_dict = dict(zip(nid_set, colors))
            wpad, hpad = 3, 3
            newshape_list = [tuple((np.array(patch.shape) + (wpad * 2, hpad * 2, 0)).tolist()) for patch in patch_list]
            color_list = [color_dict[nid_] for nid_ in nid_list]
            newpatch_list = [np.zeros(shape) + color[None, None] for shape, color in zip(newshape_list, color_list)]
            for patch, newpatch in zip(patch_list, newpatch_list):
                newpatch[wpad:-wpad, hpad:-hpad, :] = patch
            #img_list = patch_list
            #bigpatch = vt.stack_image_recurse(patch_list)
        #bigpatch = vt.stack_image_list(patch_list, vert=False)
        bigpatch = vt.stack_square_images(newpatch_list)
        bigpatch_fpath = join(seldpath, basename(dpath) + '_patches.png')

        #
        def _dictstr(dict_):
            str_ = ut.dict_str(dict_, newlines=False)
            str_ = str_.replace('\'', '').replace(': ', '=').strip('{},')
            return str_

        figtitle = '\n'.join([
            'wx=%r' % wx,
            'stat(pdist): %s' % _dictstr(metrics.wx2_pdist_stats[wx]),
            'stat(wdist): %s' % _dictstr(metrics.wx2_wdist_stats[wx]),
        ])
        metrics.wx2_nMembers[wx]

        df2.figure(fnum=1, doclf=True, docla=True)
        fig, ax = df2.imshow(bigpatch, figtitle=figtitle)
        #fig.show()
        df2.set_figtitle(figtitle)
        df2.adjust_subplots(top=.878, bottom=0)
        df2.save_figure(1, bigpatch_fpath)
Exemple #4
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()