Example #1
0
def clusters(args):
    db = decode(args.db)

    imgs = list(db.images)
    batches = imgs if args.num_threads == 1 else _split(imgs, args.num_threads)

    pool = ThreadPool(args.num_threads)
    batch_clusters = pool.map(
        functools.partial(_get_neighbours, db.tree, args.num_neighbours,
                          args.min_distance), batches)
    pool.close()
    pool.join()

    clusters_list = [item for sublist in batch_clusters for item in sublist]

    clusters = {}
    for i in range(0, len(imgs)):
        if len(clusters_list[i]) != 0:
            clusters[imgs[i].path] = clusters_list[i]

    for img_path, nearest in clusters.items():
        print('image: {}'.format(img_path))
        print('\tpath\tdistance')
        for n in nearest:
            print('\t{}\t{}'.format(n[0], n[1]))
        print()
Example #2
0
def search_by_distance(args):
    db = decode(args.db)
    query = image.Image(args.query, db.hash_type, db.hash_size)
    out = db.tree.get_within_distance(query, args.max_distance)

    if len(out) == 0:
        return
    for img in out:
        if os.path.abspath(args.query) == img.path:
            continue
        print('path {} distance: {}'.format(img.path, query.distance(img)))
Example #3
0
def remove(args):
    db = decode(args.db)
    path = os.path.abspath(args.image)
    img = image.Image(path, db.hash_type, db.hash_size)

    if path in db.image_hashes:
        if db.tree.remove(img):
            del db.image_hashes[path]
            db.encode(args.db)
        else:
            raise ValueError('path is in dict but not in tree')
Example #4
0
def search_nearest(args):
    db = decode(args.db)
    query = image.Image(args.query, db.hash_type, db.hash_size)
    out = db.tree.get_nearest_neighbours(query, args.num_neighbours,
                                         args.max_results)

    if len(out) == 0:
        return
    for img in out:
        if os.path.abspath(args.query) == img.path:
            continue
        print('path {} distance: {}'.format(img.path, query.distance(img)))
Example #5
0
def rebuild(args):
    db = decode(args.db)
    hash_size = args.hash_size if args.hash_size != 0 else db.hash_size
    hash_type = args.hash_type if len(args.hash_type) != 0 else db.hash_type

    imgs = []
    files = []

    for path in db.image_hashes:
        img = image.Image(path, hash_type, hash_size)
        imgs.append(img)
        files.append(img)

    tree = VPTree(distance_fn=image.distance_fn)
    tree.add_list(imgs)
    db = DB(tree, files, hash_type, hash_size)
    db.encode(args.db)
Example #6
0
def update(args):
    db = decode(args.db)
    walkdir(
        args.dir, args.recursive, lambda path: update_func(
            path, db.tree, db.image_hashes, db.hash_type, db.hash_size))
    db.encode(args.db)
Example #7
0
def add(args):
    db = decode(args.db)
    img = image.Image(args.image, db.hash_type, db.hash_size)
    db.tree.add(img)
    db.image_hashes.add(args.image)
    db.encode(args.db)