Пример #1
0
def compute_encounters(hs, back, seconds_thresh=15):
    '''
    clusters encounters togethers (by time, not space)

    An encounter is a meeting, localized in time and space between a camera and
    a group of animals.

    Animals are identified within each encounter.
    '''
    if not 'seconds_thresh' in vars():
        seconds_thresh = 15
    gx_list = hs.get_valid_gxs()
    datetime_list = hs.gx2_exif(gx_list, tag='DateTime')

    unixtime_list = [io.exiftime_to_unixtime(datetime_str) for datetime_str in datetime_list]

    unixtime_list = np.array(unixtime_list)
    X = np.vstack([unixtime_list, np.zeros(len(unixtime_list))]).T
    print('[scripts] clustering')

    # Build a mapping from clusterxs to member gxs
    gx2_clusterid = fclusterdata(X, seconds_thresh, criterion='distance')
    clusterx2_gxs = [[] for _ in xrange(gx2_clusterid.max())]
    for gx, clusterx in enumerate(gx2_clusterid):
        clusterx2_gxs[clusterx - 1].append(gx)  # IDS are 1 based

    clusterx2_nGxs = np.array(map(len, clusterx2_gxs))
    print('cluster size stats: %s' % helpers.printable_mystats(clusterx2_nGxs))

    # Change IDs such that higher number = more gxs
    gx2_ex = [None] * len(gx2_clusterid)
    gx2_eid = [None] * len(gx2_clusterid)
    ex2_clusterx = clusterx2_nGxs.argsort()
    ex2_gxs = [None] * len(ex2_clusterx)
    for ex in xrange(len(ex2_clusterx)):
        clusterx = ex2_clusterx[ex]
        gxs = clusterx2_gxs[clusterx]
        ex2_gxs[ex] = gxs
        for gx in gxs:
            nGx = len(gxs)
            USE_STRING_ID = True
            if USE_STRING_ID:
                # String ID
                eid = 'ex=%r_nGxs=%d' % (ex, nGx)
            else:
                # Float ID
                eid = ex + (nGx / 10 ** np.ceil(np.log(nGx) / np.log(10)))
            gx2_eid[gx] = eid
            gx2_ex[gx] = ex

    hs.tables.gx2_ex  = np.array(gx2_ex)
    hs.tables.gx2_eid = np.array(gx2_eid)

    # Give info to GUI
    extra_cols = {'eid': lambda gx_list: [gx2_eid[gx] for gx in iter(gx_list)]}
    back.append_header('gxs', 'eid')
    back.populate_image_table(extra_cols=extra_cols)
    return locals()
Пример #2
0
def ensure_rgb(img):
    try:
        'Ensures numpy format and 3 channels'
        if not isinstance(img, np.ndarray):
            img = np.asarray(img)
        if img.dtype == np.float64 or np.dtype == np.float32:
            if img.max() <= 1:
                img *= 255
            img = np.array(np.round(img), dtype=np.uint8)
        if len(img.shape) == 2 or img.shape[2] == 1:
            # Only given 1 channel
            img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
        elif img.shape[2] == 4:
            img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)
        return img
    except Exception as ex:
        msg = ('[cc2] Caught Exception:\n   ex=%s\n' % str(ex) +
               '[cc2] img.shape=%r, img.dtype=%r\n' % (img.shape, img.dtype) +
               '[cc2] stats(img) = %s' % (helpers.printable_mystats(img)))
        print(msg)
        raise Exception(msg)
Пример #3
0
def ensure_rgb(img):
    try:
        'Ensures numpy format and 3 channels'
        if not isinstance(img, np.ndarray):
            img = np.asarray(img)
        if img.dtype == np.float64 or np.dtype == np.float32:
            if img.max() <= 1:
                img *= 255
            img = np.array(np.round(img), dtype=np.uint8)
        if len(img.shape) == 2 or img.shape[2] == 1:
            # Only given 1 channel
            img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
        elif img.shape[2] == 4:
            img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)
        return img
    except Exception as ex:
        msg = ('[cc2] Caught Exception:\n   ex=%s\n' % str(ex) +
               '[cc2] img.shape=%r, img.dtype=%r\n' % (img.shape, img.dtype) +
               '[cc2] stats(img) = %s' % (helpers.printable_mystats(img)))
        print(msg)
        raise Exception(msg)
Пример #4
0
def db_info(hs):
    # Name Info
    nx2_cxs    = np.array(hs.get_nx2_cxs())
    nx2_nChips = np.array(map(len, nx2_cxs))
    uniden_cxs = np.hstack(nx2_cxs[[0, 1]])
    num_uniden = nx2_nChips[0] + nx2_nChips[1]
    nx2_nChips[0:2] = 0  # remove uniden names
    # Seperate singleton / multitons
    multiton_nxs,  = np.where(nx2_nChips > 1)
    singleton_nxs, = np.where(nx2_nChips == 1)
    valid_nxs      = np.hstack([multiton_nxs, singleton_nxs])
    num_names_with_gt = len(multiton_nxs)
    # Chip Info
    cx2_roi = hs.tables.cx2_roi
    multiton_cx_lists = nx2_cxs[multiton_nxs]
    multiton_cxs = np.hstack(multiton_cx_lists)
    singleton_cxs = nx2_cxs[singleton_nxs]
    multiton_nx2_nchips = map(len, multiton_cx_lists)
    valid_cxs = hs.get_valid_cxs()
    num_chips = len(valid_cxs)
    # Image info
    gx2_gname  = hs.tables.gx2_gname
    cx2_gx = hs.tables.cx2_gx
    num_images = len(gx2_gname)
    img_list = helpers.list_images(hs.dirs.img_dir, fullpath=True)

    def wh_print_stats(wh_list):
        if len(wh_list) == 0:
            return '{empty}'
        stat_dict = OrderedDict(
            [( 'max', wh_list.max(0)),
             ( 'min', wh_list.min(0)),
             ('mean', wh_list.mean(0)),
             ( 'std', wh_list.std(0))])
        arr2str = lambda var: '[' + (', '.join(map(lambda x: '%.1f' % x, var))) + ']'
        ret = (',\n    '.join(['%r:%s' % (key, arr2str(val)) for key, val in stat_dict.items()]))
        return '{\n    ' + ret + '}'

    def get_img_size_list(img_list):
        ret = []
        for img_fpath in img_list:
            try:
                size = Image.open(img_fpath).size
                ret.append(size)
            except Exception as ex:
                print(repr(ex))
                pass
        return ret

    print('reading image sizes')
    if len(cx2_roi) == 0:
        chip_size_list = []
    else:
        chip_size_list = cx2_roi[:, 2:4]
    img_size_list  = np.array(get_img_size_list(img_list))
    img_size_stats  = wh_print_stats(img_size_list)
    chip_size_stats = wh_print_stats(chip_size_list)
    multiton_stats  = helpers.printable_mystats(multiton_nx2_nchips)

    num_names = len(valid_nxs)
    # print
    info_str = '\n'.join([
        (' DB Info: ' + hs.get_db_name()),
        (' * #Img   = %d' % num_images),
        (' * #Chips = %d' % num_chips),
        (' * #Names = %d' % len(valid_nxs)),
        (' * #Unidentified Chips = %d' % len(uniden_cxs)),
        (' * #Singleton Names    = %d' % len(singleton_nxs)),
        (' * #Multiton Names     = %d' % len(multiton_nxs)),
        (' * #Multiton Chips     = %d' % len(multiton_cxs)),
        (' * Chips per Multiton Names = %s' % (multiton_stats,)),
        (' * #Img in dir = %d' % len(img_list)),
        (' * Image Size Stats = %s' % (img_size_stats,)),
        (' * Chip Size Stats = %s' % (chip_size_stats,)), ])
    print(info_str)
    return locals()