How to use the imagehash.phash function in ImageHash

To help you get started, we’ve selected a few ImageHash examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mindsdb / mindsdb / mindsdb / libs / phases / stats_generator / stats_generator.py View on Github external
all_possible_values = histogram.keys()

                col_stats = {
                    'data_type': data_type,
                    'data_subtype': curr_data_subtype,
                    "histogram": {
                        "x": list(histogram.keys()),
                        "y": list(histogram.values())
                    }
                    #"percentage_buckets": list(histogram.keys())
                }

            elif curr_data_subtype == DATA_SUBTYPES.IMAGE:
                image_hashes = []
                for img_path in col_data:
                    img_hash = imagehash.phash(Image.open(img_path))
                    seq_hash = []
                    for hash_row in img_hash.hash:
                        seq_hash.extend(hash_row)

                    image_hashes.append(np.array(seq_hash))

                kmeans = MiniBatchKMeans(n_clusters=20, batch_size=round(len(image_hashes)/4))

                kmeans.fit(image_hashes)

                if hmd is not None:
                    hmd['bucketing_algorithms'][col_name] = kmeans

                x = []
                y = [0] * len(kmeans.cluster_centers_)
github hj3yoo / mtg_card_detector / opencv_dnn.py View on Github external
fetch_data.get_valid_filename(card_info['name']))
            card_img = cv2.imread(img_name)

            # If the image doesn't exist, download it from the URL
            if card_img is None:
                fetch_data.fetch_card_image(card_info,
                                            out_dir='%s/card_img/png/%s' % (Config.data_dir, card_info['set']))
                card_img = cv2.imread(img_name)
            if card_img is None:
                print('WARNING: card %s is not found!' % img_name)

            # Compute value of the card's perceptual hash, then store it to the database
            #img_art = Image.fromarray(card_img[121:580, 63:685])  # For 745*1040 size card image
            img_card = Image.fromarray(card_img)
            for hs in hash_size:
                card_hash = ih.phash(img_card, hash_size=hs)
                card_info['card_hash_%d' % hs] = card_hash
                #art_hash = ih.phash(img_art, hash_size=hs)
                #card_info['art_hash_%d' % hs] = art_hash
            new_pool.loc[0 if new_pool.empty else new_pool.index.max() + 1] = card_info

    if save_to is not None:
        new_pool.to_pickle(save_to)
    return new_pool
github Ashafix / NeuroMario / GameServer.py View on Github external
def predict_finishline(self, image):
        """
        Predicts if the finish line was passed based on the appearance of the ghost
        :param image: PIL Image
        :return: bool, True if finish line was passed, False if not
        """

        img_cropped = image.crop((0, 0, 250, 100))
        img_array = np.array(img_cropped).reshape(1, -1)
        finish_line = bool(self.classifier['lap'].predict(img_array) == [1])
        if finish_line:
            hash_value = str(imagehash.phash(img_cropped, hash_size=5))
            self.printer.log(hash_value)

            if hash_value in self.true_positives and hash_value in self.false_positives:
                raise ValueError("hash {} was found in both false and true positives".format(hash_value))

            direc = os.path.join(os.getcwd(), "classifiers", "round_passed_real_cases")
            if hash_value in self.false_positives:
                self.printer.log('false positive')
                return False
            elif hash_value in self.true_positives:
                self.printer.log('true positive')
                with open("{}/{}.finished".format(direc, hash_value), 'w') as f:
                    f.write('true positive')
                return True
            else:
                image.save("{}/{}.png".format(direc, hash_value))
github mj000001 / Object-Detection-And-Tracking / identify / identify_obj.py View on Github external
def identify_obj(examplar_path, candidate_path, targets):
    # get the closest obj
    examplar = Image.open(examplar_path)
    candidate = Image.open(candidate_path)
    scores = []
    p_examplar = phash(examplar)

    for i, target in enumerate(targets):
        target = tuple(int(x) for x in target)
        img_cropped = candidate.crop(target)
        img_cropped.resize(examplar.size)
        p_img_cropped = phash(img_cropped)
        scores.append(p_img_cropped - p_examplar)

    scores.sort()
    return targets[np.argsort(scores)[0]]
github Toufool / Auto-Split / src / compare.py View on Github external
    @param mask: An image matching the dimensions of the source, but 1 channel grayscale
    @return: The similarity between the hashes of the image as a number 0 to 1.
    """

    # Since imagehash doesn't have any masking itself, bitwise_and will allow us
    # to apply the mask to the source and capture before calculating the pHash for
    # each of the images. As a result of this, this function is not going to be very
    # helpful for large masks as the images when shrinked down to 8x8 will mostly be
    # the same
    source = cv2.bitwise_and(source, source, mask=mask)
    capture = cv2.bitwise_and(capture, capture, mask=mask)
    
    source = Image.fromarray(source)
    capture = Image.fromarray(capture)

    source_hash = imagehash.phash(source)
    capture_hash = imagehash.phash(capture)

    return 1 - ((source_hash - capture_hash)/64.0)
github mindsdb / mindsdb / mindsdb / libs / helpers / general_helpers.py View on Github external
"""
    :return: The bucket in the `histogram` in which our `value` falls
    """
    if buckets is None:
        return None

    if col_stats['data_subtype'] in (DATA_SUBTYPES.SINGLE, DATA_SUBTYPES.MULTIPLE):
        if value in buckets:
            bucket = buckets.index(value)
        else:
            bucket = len(buckets) # for null values

    elif col_stats['data_subtype'] in (DATA_SUBTYPES.BINARY, DATA_SUBTYPES.INT, DATA_SUBTYPES.FLOAT):
        bucket = closest(buckets, value)
    elif col_stats['data_subtype'] in (DATA_SUBTYPES.IMAGE):
        bucket = self.hmd['bucketing_algorithms'][col_name].predict(np.array(imagehash.phash(Image.open(value)).reshape(1, -1)))[0]
    else:
        bucket = len(buckets) # for null values

    return bucket
github philipbl / duplicate-images / duplicate_finder.py View on Github external
def hash_file(file):
    try:
        hashes = []
        img = Image.open(file)

        file_size = get_file_size(file)
        image_size = get_image_size(img)
        capture_time = get_capture_time(img)

        # hash the image 4 times and rotate it by 90 degrees each time
        for angle in [ 0, 90, 180, 270 ]:
            if angle > 0:
                turned_img = img.rotate(angle, expand=True)
            else:
                turned_img = img
            hashes.append(str(imagehash.phash(turned_img)))

        hashes = ''.join(sorted(hashes))

        cprint("\tHashed {}".format(file), "blue")
        return file, hashes, file_size, image_size, capture_time
    except OSError:
        cprint("\tUnable to open {}".format(file), "red")
        return None
github CIRCL / douglas-quaid / carlhauser_server / FeatureExtractor / picture_hasher.py View on Github external
self.logger.info("Hashing picture ... ")

        # Convert bytes in PIL image
        pil_picture = Image.open(io.BytesIO(curr_picture))
        self.logger.debug(f"Picture converted to PIL Image {type(pil_picture)}")

        # DEBUG # pil_picture.save('/home/user/Desktop/debug_pil.bmp')

        try:
            # Note : @image must be a PIL instance.
            if self.fe_conf.A_HASH.get("is_enabled", False):
                self.logger.debug("A-HASH ... ")
                answer["A_HASH"] = self.check_null_hash(imagehash.average_hash(pil_picture))
            if self.fe_conf.P_HASH.get("is_enabled", False):
                self.logger.debug("P_HASH ... ")
                answer["P_HASH"] = self.check_null_hash(imagehash.phash(pil_picture))
            if self.fe_conf.P_HASH_SIMPLE.get("is_enabled", False):
                self.logger.debug("P_HASH_SIMPLE ... ")
                answer["P_HASH_SIMPLE"] = self.check_null_hash(imagehash.phash_simple(pil_picture))
            if self.fe_conf.D_HASH.get("is_enabled", False):
                self.logger.debug("D_HASH ... ")
                answer["D_HASH"] = self.check_null_hash(imagehash.dhash(pil_picture))
            if self.fe_conf.D_HASH_VERTICAL.get("is_enabled", False):
                self.logger.debug("D_HASH_VERTICAL ... ")
                answer["D_HASH_VERTICAL"] = self.check_null_hash(imagehash.dhash_vertical(pil_picture))
            if self.fe_conf.W_HASH.get("is_enabled", False):
                self.logger.debug("W_HASH ... ")
                answer["W_HASH"] = self.check_null_hash(imagehash.whash(pil_picture))
            if self.fe_conf.TLSH.get("is_enabled", False):
                self.logger.debug("TLSH ... ")
                answer["TLSH"] = self.check_null_hash(tlsh.hash(curr_picture))
github universitas / universitas.no / django / apps / photo / file_operations.py View on Github external
def get_imagehashes(fp: Fileish,
                    size=FINGERPRINT_SIZE) -> Dict[str, imagehash.ImageHash]:
    """Calculate perceptual hashes for comparison of identical images"""
    try:
        img = pil_image(fp)
        thumb = img.resize((size, size), PIL.Image.BILINEAR).convert('L')
        return dict(
            ahash=imagehash.average_hash(thumb),
            phash=imagehash.phash(thumb),
            whash=imagehash.whash(thumb),
            dhash=imagehash.dhash(thumb),
        )
    except OSError:  # corrupt image file probably
        return {}

ImageHash

Image Hashing library

BSD-2-Clause
Latest version published 2 years ago

Package Health Score

58 / 100
Full package analysis

Similar packages