Beispiel #1
0
class GuyaEye:

    def __init__(self, **kwargs):
        self.kwargs = kwargs

        self.images = self.kwargs['images']
        self.compare = self.kwargs['compare']
        self.threads = self.kwargs['threads']

        if self.images: self.create_database()
        elif self.compare: self.compare_image()
        else: print('[ ERROR ] Please select either "python src/main.py --images <directory>" or python src/main.py --compare <image>"!')

    def create_database(self):
        s = time.time()
        for dir, subdirs, files in os.walk(self.images):
            for i in range(len(files)): files[i] = os.path.join(self.images, files[i])
            pool = Pool(self.threads)
            
            hashes = pool.map(self.hash_image, files)
            
            self.db = Database()
            self.db.truncate()
            for hash in hashes:
                img, h = hash
                self.db.store_encoding(img, h)
        e = time.time()
        print('[ OK ] Took {:,.2f} seconds, {:,.2f} sec/image'.format(e - s, (e -s ) / len(hashes)))
    
    def compare_image(self):
        """
            Returns a list of a maximum of 10 images where from lowest distance to maximum distance, with the lowest distance being the closest match.
            A distance of 0 is an exact match.
        """
        self.db = Database()
        l, h = self.hash_image(self.compare)
        res = self.db.compare_encoding(h)
        pprint(res)

    def hash_image(self, image):
        img = Image.open(image)
        return (image, imagehash.average_hash(img, hash_size=16))

    def compare_hash(self, h1, h2):
        return abs(h1 - h2)
Beispiel #2
0
class GuyaEye:
    def __init__(self, **kwargs):
        self.kwargs = kwargs

        self.images = self.kwargs['images']
        self.compare = self.kwargs['compare']
        self.threads = self.kwargs['threads']
        self.recursive = self.kwargs['recursive']
        self.include_extensions = (self.kwargs["include_extensions"].split(",")
                                   if self.kwargs["include_extensions"] else
                                   False)
        self.exclude_extensions = (self.kwargs["exclude_extensions"].split(",")
                                   if self.kwargs["exclude_extensions"] else
                                   False)

        if self.images:
            self.create_database()
        elif self.compare:
            self.compare_image()
        else:
            print(
                '[ ERROR ] Please select either "python src/main.py --images <directory>" or python src/main.py --compare <image>"!'
            )

    def create_database(self):
        s = time.time()
        image_files = []
        for image_file in glob.iglob(self.images + "/**",
                                     recursive=self.recursive):
            if not os.path.isfile(image_file):
                continue
            elif self.include_extensions and not any(
                    image_file.endswith(ext)
                    for ext in self.include_extensions):
                continue
            elif self.exclude_extensions and any(
                    image_file.endswith(ext)
                    for ext in self.exclude_extensions):
                continue
            image_files.append(
                os.path.abspath(os.path.join(self.images, image_file)))
        pool = Pool(self.threads)

        hashes = pool.map(self.hash_image, image_files)

        self.db = Database()
        self.db.truncate()
        for hash in hashes:
            img, h = hash
            self.db.store_encoding(img, h)
        e = time.time()
        print(
            f'[ OK ] Took {e - s:,.2f} seconds, {(e -s ) / len(hashes):,.2f} sec/image'
        )

    def compare_image(self):
        """
            Returns a list of a maximum of 10 images where from lowest distance to maximum distance, with the lowest distance being the closest match.
            A distance of 0 is an exact match.
        """
        self.db = Database()
        _, h = self.hash_image(self.compare)
        res = self.db.compare_encoding(h)
        pprint(res)

    @staticmethod
    def hash_image(image):
        img = Image.open(image)
        return (image, imagehash.average_hash(img, hash_size=16))

    @staticmethod
    def compare_hash(h1, h2):
        return abs(h1 - h2)