Exemplo n.º 1
0
def convert_named_chips(db_dir, img_dpath=None):
    print('\n --- Convert Named Chips ---')
    # --- Initialize ---
    gt_format = '{}_{:d}.jpg'
    print('gt_format (name, num) = %r' % gt_format)
    if img_dpath is None:
        img_dpath = db_dir + '/images'
    print('Converting db_dir=%r and img_dpath=%r' % (db_dir, img_dpath))
    # --- Build Image Table ---
    helpers.print_('Building name table: ')
    gx2_gname = helpers.list_images(img_dpath)
    gx2_gid = range(1, len(gx2_gname) + 1)
    print('There are %d images' % len(gx2_gname))
    # ---- Build Name Table ---
    helpers.print_('Building name table: ')
    name_set = set([])
    for gx, gname in enumerate(gx2_gname):
        name, num = parse.parse(gt_format, gname)
        name_set.add(name)
    nx2_name = ['____', '____'] + list(name_set)
    nx2_nid = [1, 1] + range(2, len(name_set) + 2)
    print('There are %d names' % (len(nx2_name) - 2))
    # ---- Build Chip Table ---
    print('[converdb] Building chip table: ')
    cx2_cid = []
    cx2_theta = []
    cx2_roi = []
    cx2_nx = []
    cx2_gx = []
    cid = 1

    def add_to_hs_tables(gname, name, roi, theta=0):
        cid = len(cx2_cid) + 1
        nx = nx2_name.index(name)
        gx = gx2_gname.index(gname)
        cx2_cid.append(cid)
        cx2_roi.append(roi)
        cx2_nx.append(nx)
        cx2_gx.append(gx)
        cx2_theta.append(theta)
        return cid

    for gx, gname in enumerate(gx2_gname):
        name, num = parse.parse(gt_format, gname)
        img_fpath = join(img_dpath, gname)
        (w, h) = Image.open(img_fpath).size
        roi = [1, 1, w, h]
        cid = add_to_hs_tables(gname, name, roi)
    cx2_nid = np.array(nx2_nid)[cx2_nx]
    cx2_gid = np.array(gx2_gid)[cx2_gx]
    print('There are %d chips' % (cid - 1))

    # Write tables
    internal_dir = join(db_dir, ld2.RDIR_INTERNAL2)
    helpers.ensurepath(internal_dir)
    write_chip_table(internal_dir, cx2_cid, cx2_gid, cx2_nid, cx2_roi,
                     cx2_theta)
    write_name_table(internal_dir, nx2_nid, nx2_name)
    write_image_table(internal_dir, gx2_gid, gx2_gname)
Exemplo n.º 2
0
def convert_named_chips(db_dir, img_dpath=None):
    print('\n --- Convert Named Chips ---')
    # --- Initialize ---
    gt_format = '{}_{:d}.jpg'
    print('gt_format (name, num) = %r' % gt_format)
    if img_dpath is None:
        img_dpath = db_dir + '/images'
    print('Converting db_dir=%r and img_dpath=%r' % (db_dir, img_dpath))
    # --- Build Image Table ---
    helpers.print_('Building name table: ')
    gx2_gname = helpers.list_images(img_dpath)
    gx2_gid   = range(1, len(gx2_gname) + 1)
    print('There are %d images' % len(gx2_gname))
    # ---- Build Name Table ---
    helpers.print_('Building name table: ')
    name_set = set([])
    for gx, gname in enumerate(gx2_gname):
        name, num = parse.parse(gt_format, gname)
        name_set.add(name)
    nx2_name  = ['____', '____'] + list(name_set)
    nx2_nid   = [1, 1] + range(2, len(name_set) + 2)
    print('There are %d names' % (len(nx2_name) - 2))
    # ---- Build Chip Table ---
    print('[converdb] Building chip table: ')
    cx2_cid     = []
    cx2_theta   = []
    cx2_roi     = []
    cx2_nx      = []
    cx2_gx      = []
    cid = 1

    def add_to_hs_tables(gname, name, roi, theta=0):
        cid = len(cx2_cid) + 1
        nx = nx2_name.index(name)
        gx = gx2_gname.index(gname)
        cx2_cid.append(cid)
        cx2_roi.append(roi)
        cx2_nx.append(nx)
        cx2_gx.append(gx)
        cx2_theta.append(theta)
        return cid

    for gx, gname in enumerate(gx2_gname):
        name, num = parse.parse(gt_format, gname)
        img_fpath = join(img_dpath, gname)
        (w, h) = Image.open(img_fpath).size
        roi = [1, 1, w, h]
        cid = add_to_hs_tables(gname, name, roi)
    cx2_nid = np.array(nx2_nid)[cx2_nx]
    cx2_gid = np.array(gx2_gid)[cx2_gx]
    print('There are %d chips' % (cid - 1))

    # Write tables
    internal_dir = join(db_dir, ld2.RDIR_INTERNAL2)
    helpers.ensurepath(internal_dir)
    write_chip_table(internal_dir, cx2_cid, cx2_gid, cx2_nid, cx2_roi, cx2_theta)
    write_name_table(internal_dir, nx2_nid, nx2_name)
    write_image_table(internal_dir, gx2_gid, gx2_gname)
Exemplo n.º 3
0
 def import_images_from_dir(back):
     # File -> Import Images From Directory
     msg = 'Select directory with images in it'
     img_dpath = guitools.select_directory(msg)
     print('[*back] selected %r' % img_dpath)
     fpath_list = helpers.list_images(img_dpath, fullpath=True)
     back.hs.add_images(fpath_list)
     back.populate_image_table()
     print('')
Exemplo n.º 4
0
 def import_images_from_dir(back):
     # File -> Import Images From Directory
     msg = 'Select directory with images in it'
     img_dpath = guitools.select_directory(msg)
     print('[*back] selected %r' % img_dpath)
     fpath_list = helpers.list_images(img_dpath, fullpath=True)
     back.hs.add_images(fpath_list)
     back.populate_image_table()
     print('')
Exemplo n.º 5
0
defaultdb = None
preload = False

args = parse_arguments(defaultdb, defaultdb == 'cache')
# --- Build HotSpotter API ---
hs = open_database(db_dir)

#%%
# =============================================================================
#     Initialization -  images
# =============================================================================
#-------function: [hsgui]-[guiback]-def import_images_from_dir(back):
if Flag_add_img & 1:
    print('[*back] selected %r' % img_dpath)
    fpath_list = helpers.list_images(img_dpath, fullpath=True)
    hs.add_images(fpath_list)
    #        hs.add_templates(img_dpath)
    print('')
'''     
#------function def import_images_from_file(back):
# File -> Import Images From File
#hs.add_images(fpath_list)
'''

#%%
# =============================================================================
#     Initialization -  chips
# =============================================================================

if Flag_add_chip & 1:
Exemplo n.º 6
0
def imagetables_from_img_dpath(img_dpath=None):
    gx2_gname = helpers.list_images(img_dpath)
    gx2_gid = range(1, len(gx2_gname) + 1)
    print('There are %d images' % len(gx2_gname))
    return gx2_gid, gx2_gname
Exemplo n.º 7
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()
Exemplo n.º 8
0
def imagetables_from_img_dpath(img_dpath=None):
    gx2_gname = helpers.list_images(img_dpath)
    gx2_gid   = range(1, len(gx2_gname) + 1)
    print('There are %d images' % len(gx2_gname))
    return gx2_gid, gx2_gname