def run_benchmark():
    import ubelt as ub

    data_dim = 128
    num_dpts = 1000000
    num_qpts = 25000
    num_neighbs = 5
    random_seed = 42
    rng = np.random.RandomState(0)

    dataset = rand_vecs(num_dpts, data_dim, rng)
    testset = rand_vecs(num_qpts, data_dim, rng)
    # Build determenistic flann object
    flann = pyflann.FLANN()
    print('building datset for %d vecs' % (len(dataset)))

    with ub.Timer(label='building kdtrees', verbose=True) as t:
        params = flann.build_index(
            dataset,
            algorithm='kdtree',
            trees=6,
            random_seed=random_seed,
            cores=6,
        )

    print(params)

    qvec_chunks = list(ub.chunks(testset, 1000))
    times = []
    for qvecs in ub.ProgIter(qvec_chunks, label='find nn'):
        with ub.Timer(verbose=0) as t:
            _ = flann.nn_index(testset, num_neighbs)  # NOQA
        times.append(t.ellapsed)
    print(np.mean(times))
Esempio n. 2
0
def check_performance_versus_tqdm():
    import progiter
    import tqdm
    import ubelt as ub

    self = iterable = LongIterable(duration=3)

    progkw = dict(
        leave=True,
        mininterval=2.0,
    )

    with ub.Timer('tqdm') as timer1:
        Progress = tqdm.tqdm
        prog = Progress(iterable, **progkw)
        for idx in prog:
            pass

    with ub.Timer('progiter.progiter') as timer2:
        Progress = progiter.ProgIter
        prog = Progress(iterable, **progkw)
        for idx in prog:
            pass

    with ub.Timer('ub progiter') as timer3:
        Progress = ub.ProgIter
        prog = Progress(iterable, **progkw)
        for idx in prog:
            pass

    print('timer1.elapsed = {!r}'.format(timer1.elapsed))
    print('timer2.elapsed = {!r}'.format(timer2.elapsed))
    print('timer3.elapsed = {!r}'.format(timer3.elapsed))
Esempio n. 3
0
def _devcheck_sample_full_image():
    """
    """
    import kwimage
    import numpy as np

    sampler = grab_camvid_sampler()

    cid_to_cidx = sampler.catgraph.id_to_idx
    classes = sampler.catgraph

    # Try loading an entire image
    img, annots = sampler.load_image_with_annots(1)

    file = img['imdata']
    imdata = file[:]

    aids = [ann['id'] for ann in annots]
    _annots = sampler.dset.annots(aids)

    sseg_list = []
    for s in _annots.lookup('segmentation'):
        m = kwimage.MultiPolygon.coerce(s)
        sseg_list.append(m)

    aids = _annots.aids
    cids = _annots.cids
    boxes = _annots.boxes
    segmentations = kwimage.PolygonList(sseg_list)
    class_idxs = np.array([cid_to_cidx[cid] for cid in cids])

    dets = kwimage.Detections(
        aids=aids,
        boxes=boxes,
        class_idxs=class_idxs,
        segmentations=segmentations,
        classes=classes,
        datakeys=['aids'],
    )

    if 1:
        print('dets = {!r}'.format(dets))
        print('dets.data = {!r}'.format(dets.data))
        print('dets.meta = {!r}'.format(dets.meta))

    if ub.argflag('--show'):
        import kwplot

        with ub.Timer('dets.draw_on'):
            canvas = imdata.copy()
            canvas = dets.draw_on(canvas)
            kwplot.imshow(canvas, pnum=(1, 2, 1), title='dets.draw_on')

        with ub.Timer('dets.draw'):
            kwplot.imshow(imdata,
                          pnum=(1, 2, 2),
                          docla=True,
                          title='dets.draw')
            dets.draw()
Esempio n. 4
0
def test_timer_default_verbosity():
    with CaptureStdout() as cap:
        ub.Timer('').tic().toc()
    assert cap.text == '', 'should be quiet by default when label is not given'

    with CaptureStdout() as cap:
        ub.Timer('a label').tic().toc()
    assert cap.text != '', 'should be verbose by default when label is given'
Esempio n. 5
0
 def _ensure_coco(coco):
     # Map a file path or an in-memory dataset to a CocoDataset
     import kwcoco
     import six
     from os.path import exists
     if coco is None:
         return None
     elif isinstance(coco, six.string_types):
         fpath = _rectify_fpath(coco)
         if exists(fpath):
             with ub.Timer(
                     'read kwcoco dataset: fpath = {!r}'.format(fpath)):
                 coco = kwcoco.CocoDataset(fpath, autobuild=False)
             print('building kwcoco index')
             coco._build_index()
         else:
             if not coco.lower().startswith('special:'):
                 import warnings
                 warnings.warn('warning start dataset codes with special:')
                 code = coco
             else:
                 code = coco.lower()[len('special:'):]
             coco = kwcoco.CocoDataset.demo(code)
     else:
         # print('live dataset')
         assert isinstance(coco, kwcoco.CocoDataset)
     return coco
Esempio n. 6
0
def test_timer_nonewline():
    from xdoctest.utils import CaptureStdout
    with CaptureStdout() as cap:
        timer = ub.Timer(newline=False)
        timer.tic()
        timer.toc()
    assert cap.text.replace('u', '').startswith("\ntic('')...toc('')")
Esempio n. 7
0
def test_timer_error():
    try:
        with ub.Timer() as timer:
            raise Exception()
    except Exception as ex:
        pass
    assert timer.elapsed > 0
Esempio n. 8
0
def check_fsspec():
    import ubelt as ub
    from os.path import join
    dpath = ub.ensure_app_cache_dir('ubelt/simple_server')
    info = ub.cmd(['python', '-m', 'http.server', '--directory', dpath], detatch=True)

    fnames = ['file_{}.txt'.format(i) for i in range(100)]
    for fname in fnames:
        ub.writeto(join(dpath, fname), ub.hash_data(fname))
    # info = ub.cmd('python -m http.server --directory "{}"'.format(dpath), verbose=3)

    # proc = info['proc']
    # TODO: ub.cmd return with some object that can tee the output on demand?
    # _proc_iteroutput = ub.util_cmd._proc_iteroutput_thread(proc)
    # line = next(_proc_iteroutput)

    urls = ['http://localhost:8000/{}'.format(fname) for fname in fnames]

    import fsspec
    file = fsspec.open(urls[0]).open().read()

    with ub.Timer(label='fsspec.cat', verbose=1):
        fs = fsspec.filesystem("http")
        out = fs.cat(urls)  # fetches data concurrently

    with ub.Timer(label='ub.DownloadManager', verbose=1):
        dpath = ub.ensure_app_cache_dir('ubelt/simple_download_root')
        dman = ub.DownloadManager(dpath)
        for url in urls:
            dman.submit(url)

        results = []
        for future in dman.as_completed(prog=True):
            fpath = future.result()
            results.append(fpath)
            # print('fpath = {!r}'.format(fpath))

    proc.terminate()
Esempio n. 9
0
def main_selenium():
    from selenium import webdriver
    ensure_selenium_chromedriver()
    # url = 'https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/'
    # url = 'https://www.reddit.com/r/TheSilphArena/comments/jr2ee9/season_5_little_cup_meta_moves_infographic/'
    # url = 'https://en.wikipedia.org/wiki/Prime_number'
    # url = 'https://networkx.org/documentation/networkx-1.10/reference/generated/networkx.algorithms.simple_paths.all_simple_paths.html'
    url = 'https://pypi.org/project/networkx/'
    with ub.Timer() as timer:
        driver = webdriver.Chrome()
        driver.get(url)
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        driver.implicitly_wait(30)
    print('timer.elapsed = {!r}'.format(timer.elapsed))
Esempio n. 10
0
def main_requests():
    urls = [
        'https://google.com',
        'https://whitehouse.gov',
        'https://cmake.org',
        'https://stackoverflow.com/',
        'https://reddit.com',
        'https://github.com/ipfs/go-ipfs',
    ]
    times = {}
    for url in urls:
        with ub.Timer() as timer:
            resp = requests.get(url)
            print(resp.text)
        times[url] = timer.elapsed
    print('times = {}'.format(ub.repr2(times, nl=1)))
    total = sum(times.values())
    print('total = {!r}'.format(total))
Esempio n. 11
0
 def after_initialize(harn, **kw):
     harn.draw_timer = ub.Timer().tic()
Esempio n. 12
0
def grab_coco_camvid():
    """
    Example:
        >>> # xdoctest: +REQUIRES(--download)
        >>> dset = grab_coco_camvid()
        >>> print('dset = {!r}'.format(dset))
        >>> # xdoctest: +REQUIRES(--show)
        >>> import kwplot
        >>> plt = kwplot.autoplt()
        >>> plt.clf()
        >>> dset.show_image(gid=1)

    Ignore:
        import xdev
        gid_list = list(dset.imgs)
        for gid in xdev.InteractiveIter(gid_list):
            dset.show_image(gid)
            xdev.InteractiveIter.draw()
    """
    import kwcoco
    cache_dpath = ub.ensure_app_cache_dir('kwcoco', 'camvid')
    coco_fpath = join(cache_dpath, 'camvid.mscoco.json')

    # Need to manually bump this if you make a change to loading
    SCRIPT_VERSION = 'v4'

    # Ubelt's stamp-based caches are super cheap and let you take control of
    # the data format.
    stamp = ub.CacheStamp('camvid_coco',
                          cfgstr=SCRIPT_VERSION,
                          dpath=cache_dpath,
                          product=coco_fpath,
                          hasher='sha1',
                          verbose=3)
    if stamp.expired():
        camvid_raw_info = grab_raw_camvid()
        dset = convert_camvid_raw_to_coco(camvid_raw_info)
        with ub.Timer('dumping MS-COCO dset to: {}'.format(coco_fpath)):
            dset.dump(coco_fpath)
        # Mark this process as completed by saving a small file containing the
        # hash of the "product" you are stamping.
        stamp.renew()

    # We can also cache the index build step independently. This uses
    # ubelt.Cacher, which is pickle based, and writes the actual object to
    # disk. Each type of caching has its own uses and tradeoffs.
    cacher = ub.Cacher('prebuilt-coco',
                       cfgstr=SCRIPT_VERSION,
                       dpath=cache_dpath,
                       verbose=3)
    dset = cacher.tryload()
    if dset is None:
        print('Reading coco_fpath = {!r}'.format(coco_fpath))
        dset = kwcoco.CocoDataset(coco_fpath, tag='camvid')
        # Directly save the file to disk.
        dset._build_index()
        dset._build_hashid()
        cacher.save(dset)

    camvid_dset = dset
    print('Loaded camvid_dset = {!r}'.format(camvid_dset))
    return camvid_dset
Esempio n. 13
0
def test_timer_nonewline():
    with CaptureStdout() as cap:
        timer = ub.Timer(newline=False, verbose=1)
        timer.tic()
        timer.toc()
    assert cap.text.replace('u', '').startswith("\ntic('')...toc('')")
Esempio n. 14
0
import ubelt as ub
import sys
import time
from datetime import timedelta

timer = ub.Timer().tic()

print('Welcome to stopwatch')

try:
    while True:
        total_sec = timer.toc()
        total_min, sec_rem = divmod(total_sec, 60)
        total_hour, min_part = divmod(total_min, 60)
        hour_part = total_hour
        sec_part = int(sec_rem)
        subsec = str(sec_rem - sec_part).split('.')[-1][0:4]
        x = f'{int(total_hour):02d}:{int(min_part):02d}:{sec_part:02d}.{subsec}'
        # delta = timedelta(seconds=sec)
        sys.stdout.write(str(x))
        time.sleep(0.001)
        sys.stdout.write('\r')
except KeyboardInterrupt:
    print('')
    print(timer.toc())
"""
python ~/misc/learn/stopwatch.py
"""
Esempio n. 15
0
    def after_initialize(harn, **kw):
        harn.draw_timer = ub.Timer().tic()

        # hack:
        for k, v in harn.datasets.items():
            v.raw_model = harn.xpu.raw(harn.model)
Esempio n. 16
0
    def random_negatives(self,
                         num,
                         anchors=None,
                         window_size=None,
                         gids=None,
                         thresh=0.0,
                         exact=True,
                         rng=None,
                         patience=None):
        """
        Finds random boxes that don't have a large overlap with positive
        instances.

        Args:
            num (int): number of negative boxes to generate (actual number of
                boxes returned may be less unless `exact=True`)

            anchors (ndarray): prior normalized aspect ratios for negative
                boxes. Mutually exclusive with `window_size`.

            window_size (ndarray): absolute (W, H) sizes to use for negative
                boxes.  Mutually exclusive with `anchors`.

            gids (List[int]): image-ids to generate negatives for,
                if not specified generates for all images.

            thresh (float): overlap area threshold as a percentage of
                the negative box size. When thresh=0.0, that means
                negatives cannot overlap any positive, when threh=1.0, there
                are no constrains on negative placement.

            exact (bool): if True, ensure that we generate exactly `num` boxes

            rng (RandomState): random number generator

        Example:
            >>> from ndsampler.isect_indexer import *
            >>> import ndsampler
            >>> import kwcoco
            >>> dset = kwcoco.CocoDataset.demo('shapes8')
            >>> self = FrameIntersectionIndex.from_coco(dset)
            >>> anchors = np.array([[.35, .15], [.2, .2], [.1, .1]])
            >>> #num = 25
            >>> num = 5
            >>> rng = kwarray.ensure_rng(None)
            >>> neg_gids, neg_boxes = self.random_negatives(
            >>>     num, anchors, gids=[1], rng=rng, thresh=0.01, exact=1)
            >>> # xdoc: +REQUIRES(--show)
            >>> gid = sorted(set(neg_gids))[0]
            >>> boxes = neg_boxes.compress(neg_gids == gid)
            >>> import kwplot
            >>> kwplot.autompl()
            >>> img = kwimage.imread(dset.imgs[gid]['file_name'])
            >>> kwplot.imshow(img, doclf=True, fnum=1, colorspace='bgr')
            >>> support = self._support(gid)
            >>> kwplot.draw_boxes(support, color='blue')
            >>> kwplot.draw_boxes(boxes, color='orange')

        Example:
            >>> from ndsampler.isect_indexer import *
            >>> import kwcoco
            >>> dset = kwcoco.CocoDataset.demo('shapes8')
            >>> self = FrameIntersectionIndex.from_coco(dset)
            >>> #num = 25
            >>> num = 5
            >>> rng = kwarray.ensure_rng(None)
            >>> window_size = (50, 50)
            >>> neg_gids, neg_boxes = self.random_negatives(
            >>>     num, window_size=window_size, gids=[1], rng=rng,
            >>>     thresh=0.01, exact=1)
            >>> # xdoc: +REQUIRES(--show)
            >>> import kwplot
            >>> kwplot.autompl()
            >>> gid = sorted(set(neg_gids))[0]
            >>> boxes = neg_boxes.compress(neg_gids == gid)
            >>> img = kwimage.imread(dset.imgs[gid]['file_name'])
            >>> kwplot.imshow(img, doclf=True, fnum=1, colorspace='bgr')
            >>> support = self._support(gid)
            >>> support.draw(color='blue')
            >>> boxes.draw(color='orange')
        """

        if not ((window_size is None) ^ (anchors is None)):
            raise ValueError('window_size and anchors are mutually exclusive')

        rng = kwarray.ensure_rng(rng)
        all_gids = self.all_gids if gids is None else gids

        def _generate_rel(n):
            # Generate n candidate boxes in the normalized 0-1 domain
            cand_boxes = kwimage.Boxes.random(num=n,
                                              scale=1.0,
                                              format='tlbr',
                                              anchors=anchors,
                                              anchor_std=0,
                                              rng=rng)

            chosen_gids = np.array(sorted(rng.choice(all_gids, size=n)))
            gid_to_boxes = kwarray.group_items(cand_boxes, chosen_gids, axis=0)

            neg_gids = []
            neg_boxes = []
            for gid, img_boxes in gid_to_boxes.items():
                qtree = self.qtrees[gid]
                # scale from normalized coordinates to image coordinates
                img_boxes = img_boxes.scale((qtree.width, qtree.height))
                for box in img_boxes:
                    # isect_aids, overlaps = self.ious(gid, box)
                    isect_aids, overlaps = self.iooas(gid, box)
                    if len(overlaps) == 0 or overlaps.max() < thresh:
                        neg_gids.append(gid)
                        neg_boxes.append(box.data)
            return neg_gids, neg_boxes

        def _generate_abs(n):
            # Randomly choose images to generate boxes for
            chosen_gids = np.array(sorted(rng.choice(all_gids, size=n)))
            gid_to_nboxes = ub.dict_hist(chosen_gids)

            neg_gids = []
            neg_boxes = []
            for gid, nboxes in gid_to_nboxes.items():
                qtree = self.qtrees[gid]
                scale = (qtree.width, qtree.height)
                anchors_ = np.array([window_size]) / np.array(scale)
                if np.any(anchors_ > 1.0):
                    continue
                img_boxes = kwimage.Boxes.random(num=nboxes,
                                                 scale=1.0,
                                                 format='tlbr',
                                                 anchors=anchors_,
                                                 anchor_std=0,
                                                 rng=rng)
                img_boxes = img_boxes.scale(scale)
                for box in img_boxes:
                    # isect_aids, overlaps = self.ious(gid, box)
                    isect_aids, overlaps = self.iooas(gid, box)
                    if len(overlaps) == 0 or overlaps.max() < thresh:
                        neg_gids.append(gid)
                        neg_boxes.append(box.data)
            return neg_gids, neg_boxes

        if window_size is not None:
            _generate = _generate_abs
        elif anchors is not None:
            _generate = _generate_rel
        else:
            raise ValueError(
                'must specify at least one window_size or anchors')

        if exact:
            # TODO: Dont attempt to sample negatives from images where the
            # positives cover more than a threshold percent. (Handle the case
            # of chip detections)

            factor = 2  # oversample factor
            if patience is None:
                patience = int(np.sqrt(num * 10) + 1)
            remaining_patience = patience
            timer = ub.Timer().tic()
            # Generate boxes until we have enough
            neg_gids, neg_boxes = _generate(n=int(num * factor))
            n_tries = 1
            for n_tries in it.count(n_tries):
                want = num - len(neg_boxes)
                if want <= 0:
                    break
                extra_gids, extra_boxes = _generate(n=int(want * factor))
                neg_gids.extend(extra_gids)
                neg_boxes.extend(extra_boxes)
                if len(neg_boxes) < num:
                    # If we haven't found a significant number of boxes our
                    # patience decreases (if the wall time is getting large)
                    if len(extra_boxes) <= (num // 10) and timer.toc() > 1.0:
                        remaining_patience -= 1
                        if remaining_patience == 0:
                            break

            if len(neg_boxes) < num:
                # but throw an error if we don't make any progress
                message = ('Cannot make a negative sample with thresh={} '
                           'in under {} tries. Found {} but need {}'.format(
                               thresh, n_tries, len(neg_boxes), num))
                if exact == 'warn':
                    warnings.warn(message)
                else:
                    raise Exception(message)
            print('n_tries = {!r}'.format(n_tries))

            neg_gids = neg_gids[:num]
            neg_boxes = neg_boxes[:num]
        else:
            neg_gids, neg_boxes = _generate(n=num)

        neg_gids = np.array(neg_gids)
        neg_boxes = kwimage.Boxes(np.array(neg_boxes), 'tlbr')
        return neg_gids, neg_boxes
Esempio n. 17
0
    def expected_samples(self, target, method='approx'):
        """
        Args:
            target (float): fraction of events between 0 and 1
            method (str): can be 'approx' or 'montecarlo'.

        Example:
            >>> probs = np.ones(4)
            >>> probs = np.random.rand(int(10000))
            >>> probs = probs / probs.sum()
            >>> self = CouponCollector(probs)
            >>> ev_exact = self.expected_samples(target=1.0, method='exact')
            >>> print('ev_exact = {!r}'.format(ev_exact))
            >>> ev_approx = self.expected_samples(target=1.0, method='approx')
            >>> print('ev_approx = {!r}'.format(ev_approx))
            >>> ev_monte = self.expected_samples(target=1.0, method='montecarlo')
            >>> print('ev_monte = {!r}'.format(ev_monte))
        """
        probs = self.probs

        if method == 'exact':
            if target != 1.0:
                raise NotImplementedError(
                    'exact method only implemented for target=1')
            ev = coupon_collector_expected_samples(self.probs)

        if method == 'approx':
            # Theoretical approximation (this is not exactly right)
            def _integrand(t):
                return self.prob_sampled_in(target, t, method=method)

            ev, abserr = integrate.quad(
                func=_integrand,
                a=len(self.probs),  # These bounds are dubious
                b=len(self.probs)**2)
            # def func(t):
            #     return (self.prob_sampled_in(target, t) - 0.5) ** 2
            # x0 = len(probs) * np.log(len(probs))
            # import scipy
            # scipy.optimize.newton(func, x0)

        elif method == 'montecarlo':
            ntrials = 100
            timeout = 30
            trails = []
            with ub.Timer() as timer:
                for _ in range(ntrials):
                    remain = set(range(len(probs)))
                    all_items = np.arange(len(probs))
                    n_seen = 0
                    while len(remain):
                        got = np.random.choice(all_items, p=probs)
                        if got in remain:
                            remain.remove(got)
                        n_seen += 1
                        if timer.toc() > timeout:
                            # Limit attempts
                            break
                    trails.append(n_seen)
                    if timer.toc() > 2:
                        # Limit attempts
                        break
            expected_number_samples = np.mean(trails)
            ev = expected_number_samples
        return ev
Esempio n. 18
0
def simple_pipeline():
    """
    Processing_with_species_id.m is their main file

    OpenCV2:
        cd ~/code/VIAME/plugins/camtrawl/python
        workon_py2
        source ~/code/VIAME/build/install/setup_viame.sh
        # Ensure python and sprokit knows about our module
        export PYTHONPATH=$(pwd):$PYTHONPATH
        export KWIVER_DEFAULT_LOG_LEVEL=info
        export SPROKIT_PYTHON_MODULES=kwiver.processes:viame.processes:camtrawl_processes

        python ~/code/VIAME/plugins/camtrawl/python/run_camtrawl.py --dataset=demo

    OpenCV3:
        cd ~/code/VIAME/plugins/camtrawl/python
        workon_py2
        source ~/code/VIAME/build-cv3-py2/install/setup_viame.sh
        # Ensure python and sprokit knows about our module
        export PYTHONPATH=$(pwd):$PYTHONPATH
        export KWIVER_DEFAULT_LOG_LEVEL=info
        export SPROKIT_PYTHON_MODULES=kwiver.processes:viame.processes:camtrawl_processes

        python ~/code/VIAME/plugins/camtrawl/python/run_camtrawl.py --dataset=demo

        /home/joncrall/code/VIAME/build-cv3-py2/install/bin/pipeline_runner -p /home/joncrall/.cache/sprokit/temp_pipelines/temp_pipeline_file.pipe -S pythread_per_process
    """

    # Setup the input files
    import ubelt as ub
    dataset = ub.argval('--dataset', default='demo')

    if dataset == 'demo':
        import zipfile
        from os.path import commonprefix
        dpath = ub.ensure_app_cache_dir('camtrawl')
        try:
            demodata_zip = ub.grabdata(
                'http://acidalia:8000/data/camtrawl_demodata.zip', dpath=dpath)
        except Exception:
            raise ValueError(
                'Demo data is currently only available on Kitware VPN')
        with zipfile.ZipFile(demodata_zip) as zfile:
            dname = commonprefix(zfile.namelist())
            data_fpath = join(dpath, dname)
            if not exists(data_fpath):
                zfile.extractall(dpath)

        print('data_fpath = {!r}'.format(data_fpath))

        cal_fpath = join(data_fpath, 'cal.npz')
        datakw = {
            'data_fpath': data_fpath,
            'img_path1': join(data_fpath, 'left'),
            'img_path2': join(data_fpath, 'right'),
        }
        print('datakw = {!r}'.format(datakw))
    elif dataset == 'test':
        data_fpath = expanduser('~/data/autoprocess_test_set')
        cal_fpath = join(data_fpath, 'cal_201608.mat')
        datakw = {
            'data_fpath': data_fpath,
            'img_path1': join(data_fpath, 'image_data/left'),
            'img_path2': join(data_fpath, 'image_data/right'),
        }
    elif dataset == 'haul83-small':
        data_fpath = expanduser('~/data/camtrawl_stereo_sample_data_small')
        cal_fpath = join(data_fpath,
                         '201608_calibration_data/selected/Camtrawl_2016.npz')
        datakw = {
            'data_fpath': data_fpath,
            'img_path1': join(data_fpath, 'Haul_83/left'),
            'img_path2': join(data_fpath, 'Haul_83/right'),
        }
    elif dataset == 'haul83':
        data_fpath = expanduser('~/data/camtrawl_stereo_sample_data/')
        cal_fpath = join(data_fpath,
                         '201608_calibration_data/selected/Camtrawl_2016.npz')
        datakw = {
            'data_fpath':
            data_fpath,
            'img_path1':
            join(
                data_fpath,
                'Haul_83/D20160709-T021759/images/AB-800GE_00-0C-DF-06-40-BF'
            ),  # left
            'img_path2':
            join(
                data_fpath,
                'Haul_83/D20160709-T021759/images/AM-800GE_00-0C-DF-06-20-47'
            ),  # right
            'start_frame':
            2000,
            'end_frame':
            5000,
        }
    else:
        import argparse
        import os
        parser = argparse.ArgumentParser(description='Camtrawl pipline demo')
        parser.add_argument(
            '--cal',
            help='path to matlab or numpy stereo calibration file',
            default='cal.npz')
        parser.add_argument('--left',
                            help='path to directory containing left images',
                            default='left')
        parser.add_argument('--right',
                            help='path to directory containing right images',
                            default='right')
        args = parser.parse_args()
        config = args.__dict__.copy()
        img_path1, img_path2, cal_fpath = ub.take(config,
                                                  ['left', 'right', 'cal'])
        data_fpath = os.path.dirname(img_path1)
        datakw = {
            'data_fpath': data_fpath,
            'img_path1': img_path1,
            'img_path2': img_path2,
        }
        if not exists(img_path1):
            raise IOError(
                'left image path {!r} does not exist'.format(img_path1))
        if not exists(img_path2):
            raise IOError(
                'right image path {!r} does not exist'.format(img_path2))
        if not exists(cal_fpath):
            raise IOError(
                'calibration file path {!r} does not exist'.format(cal_fpath))

        # raise KeyError('Unknown dataset {}'.format(dataset))

    make_image_input_files(**datakw)

    def add_stereo_camera_branch(pipe, prefix):
        """
        Helper that defines a single branch, so it can easilly be duplicated.
        """
        image_list_file = join(data_fpath, prefix + 'images.txt')
        cam = {}

        # --- Node ---
        cam['imread'] = imread = pipe.add_process(name=prefix + 'imread',
                                                  type='frame_list_input',
                                                  config={
                                                      'image_list_file':
                                                      image_list_file,
                                                      'frame_time': 0.03333333,
                                                      'image_reader:type':
                                                      'ocv',
                                                  })
        # ------------

        # --- Node ---
        cam['detect'] = detect = pipe.add_process(name=prefix + 'detect',
                                                  type='camtrawl_detect_fish',
                                                  config={})
        detect.iports.connect({
            'image': imread.oports['image'],
            # 'image_file_name': imread.oports['image_file_name'],
        })
        # ------------
        return cam

    pipe = define_pipeline.Pipeline()
    cam1 = add_stereo_camera_branch(pipe, 'cam1_')
    cam2 = add_stereo_camera_branch(pipe, 'cam2_')

    # stereo_cameras = pipe.add_process(
    #     name='stereo_cameras', type='stereo_calibration_camera_reader',
    #     config={
    #         # 'cal_fpath': cal_fpath,
    #     })

    # ------
    pipe.add_process(name='measure',
                     type='camtrawl_measure',
                     config={
                         'cal_fpath': cal_fpath,
                         'output_fpath': './camtrawl_out.csv',
                     })
    pipe['measure'].iports.connect({
        # 'camera1': stereo_cameras.oports['camera1'],
        # 'camera2': stereo_cameras.oports['camera2'],
        'detected_object_set1':
        cam1['detect'].oports['detected_object_set'],
        'detected_object_set2':
        cam2['detect'].oports['detected_object_set'],
        'image_file_name1':
        cam1['imread'].oports['image_file_name'],
        'image_file_name2':
        cam2['imread'].oports['image_file_name'],
    })
    # ------

    pipe.config['_pipeline:_edge']['capacity'] = 1
    pipe.config['_scheduler']['type'] = 'pythread_per_process'

    # pipe.draw_graph('pipeline.png')
    # import ubelt as ub
    # ub.startfile('pipeline.png')

    print('  --- RUN PIPELINE ---')
    import ubelt as ub
    with ub.Timer('Running Pipeline'):
        pipe.run()

    return pipe
Esempio n. 19
0
def _devcheck_load_sub_image():
    import kwimage
    import numpy as np

    sampler = grab_camvid_sampler()

    cid_to_cidx = sampler.catgraph.id_to_idx
    classes = sampler.catgraph

    # Try loading a subregion of an image
    sample = sampler.load_positive(2)
    imdata = sample['im']
    annots = sample['annots']
    aids = annots['aids']
    cids = annots['cids']
    boxes = annots['rel_boxes']
    class_idxs = np.array([cid_to_cidx[cid] for cid in cids])
    segmentations = annots['rel_ssegs']

    raw_dets = kwimage.Detections(
        aids=aids,
        boxes=boxes,
        class_idxs=class_idxs,
        segmentations=segmentations,
        classes=classes,
        datakeys=['aids'],
    )

    # Clip boxes to the image boundary
    input_dims = imdata.shape[0:2]
    raw_dets.data['boxes'] = raw_dets.boxes.clip(0, 0, input_dims[1],
                                                 input_dims[0])

    keep = []
    for i, s in enumerate(raw_dets.data['segmentations']):
        # TODO: clip polygons
        m = s.to_mask(input_dims)
        if m.area > 0:
            keep.append(i)
    dets = raw_dets.take(keep)

    heatmap = dets.rasterize(bg_size=(1, 1), input_dims=input_dims)

    if 1:
        print('dets = {!r}'.format(dets))
        print('dets.data = {!r}'.format(dets.data))
        print('dets.meta = {!r}'.format(dets.meta))

    if ub.argflag('--show'):
        import kwplot

        kwplot.autompl()
        heatmap.draw()

        draw_boxes = 1

        kwplot.figure(doclf=True)
        with ub.Timer('dets.draw_on'):
            canvas = imdata.copy()
            # TODO: add logic to color by class
            canvas = dets.draw_on(canvas, boxes=draw_boxes, color='random')
            kwplot.imshow(canvas, pnum=(1, 2, 1), title='dets.draw_on')

        with ub.Timer('dets.draw'):
            kwplot.imshow(imdata,
                          pnum=(1, 2, 2),
                          docla=True,
                          title='dets.draw')
            dets.draw(boxes=draw_boxes, color='random')