示例#1
0
class Engine:
    def __init__(self):
        self.hashnet = Hashnet()
        self.hashnet.load()
        self.db = DBManager(**db_params)

    def match(self, target, candidates, threshold=10):
        hash_matrix = get_hash_matrix(candidates)
        hashcode, features = target

        hamming_dists = hamming_dist(hashcode, hash_matrix)
        indices = np.argpartition(hamming_dists, threshold)[:threshold]
        shortlist = [candidates[i] for i in indices]

        feature_matrix = get_feature_matrix(shortlist)
        dists = cosine_dist(features, feature_matrix)
        winner = shortlist[dists.argmin()]
        distance = dists.min()
        return winner, distance

    def search(self, target_image, page_size):
        tick = datetime.now()
        target_features = self.hashnet.extract_features(target_image)
        session = self.db.Session()
        current_page = 0
        queryset = session.query(Image).filter(Image.hash != None)
        optimal_dist = None
        winner = None
        while True:
            query = queryset.limit(page_size).offset(current_page * page_size)
            candidates = query.all()
            if not len(candidates):
                break
            current_page += 1
            matched, distance = self.match(target_features, candidates)
            if optimal_dist is None or distance < optimal_dist:
                optimal_dist = distance
                winner = matched
        session.close()
        tock = datetime.now()
        print("\nSearching completed in {}".format(tock - tick), flush=True)
        if winner is None:
            print(
                "There is nothing found during the search, most likely because there's not images in the database",
                flush=True)
            return None, None
        print("Most similar image is {}\nwith cosine distance={}".format(
            winner, optimal_dist),
              flush=True)
        return winner, 1 - optimal_dist
示例#2
0
            ssh_pkey=ssh_private_key,
            remote_bind_address=(
                db_params['host'],
                3306
            )
        )
        server.start()
        db_params['host'] = server.local_bind_host
        db_params['port'] = server.local_bind_port
    db = DBManager(**db_params)
    storage = Storage(**oss_params)
    hashnet = Hashnet()
    hashnet.load()

    print("Scanning database for images not being processed...")
    session = db.Session()
    results = session.query(Image).filter(
        or_(
            Image.features == None,
            Image.hash == None
        )
    ).all()
    n = len(results)
    print("Scanning complete! {} in total.\n".format(n))

    print("Processing images...")
    batch_size = 32

    widgets = [progressbar.Percentage(), " ", progressbar.Bar(), " ", progressbar.ETA()]
    pbar = progressbar.ProgressBar(maxval=n, widgets=widgets, term_width=50).start()
    for i in range(0, n, batch_size):