Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Add Image Hash pipeline, closes #240

davidmezzettic39bfad8e34e
diff --git a/setup.py b/setup.py
index 98a59c18..ca48f8dd 100644
--- a/setup.py
+++ b/setup.py
@@ -6,7 +6,7 @@ with open("README.md", "r", encoding="utf-8") as f:
DESCRIPTION = "".join([line for line in f if "gh-dark-mode-only" not in line])
# Required dependencies
-install = ["faiss-cpu>=1.7.1.post2", "numpy>=1.18.4", "PyYAML>=5.3", "torch>=1.6.0", "transformers>=4.12.3", "tokenizers>=0.10.3"]
+install = ["faiss-cpu>=1.7.1.post2", "numpy>=1.18.4", "pyyaml>=5.3", "torch>=1.6.0", "transformers>=4.12.3", "tokenizers>=0.10.3"]
# Optional dependencies
extras = {}
@@ -28,6 +28,7 @@ extras["model"] = ["onnxruntime>=1.8.1"]
extras["pipeline"] = [
"beautifulsoup4>=4.9.3",
"fasttext>=0.9.2",
+ "imagehash>=4.2.1",
"nltk>=3.5",
"onnx>=1.10.1",
"onnxmltools>=1.9.1",
diff --git a/src/python/txtai/pipeline/image/__init__.py b/src/python/txtai/pipeline/image/__init__.py
index f1ac9151..1cdc0ecf 100644
--- a/src/python/txtai/pipeline/image/__init__.py
+++ b/src/python/txtai/pipeline/image/__init__.py
@@ -3,4 +3,5 @@ Image imports
"""
from .caption import Caption
+from .imagehash import ImageHash
from .objects import Objects
diff --git a/src/python/txtai/pipeline/image/imagehash.py b/src/python/txtai/pipeline/image/imagehash.py
new file mode 100644
index 00000000..5f7a271d
--- /dev/null
+++ b/src/python/txtai/pipeline/image/imagehash.py
@@ -0,0 +1,90 @@
+"""
+ImageHash module
+"""
+
+import numpy as np
+
+# Conditional import
+try:
+ from PIL import Image
+ import imagehash
+
+ PIL = True
+except ImportError:
+ PIL = False
+
+from ..base import Pipeline
+
+
+class ImageHash(Pipeline):
+ """
+ Generates perceptual image hashes. These hashes can be used to detect near-duplicate images. This method is not
+ backed by machine learning models and not intended to find conceptually similar images.
+ """
+
+ def __init__(self, algorithm="average", size=8, strings=True):
+ """
+ Creates an ImageHash pipeline.
+
+ Args:
+ algorithm: image hashing algorithm (average, perceptual, difference, wavelet, color)
+ size: hash size
+ strings: outputs hex strings if True (default), otherwise the pipeline returns numpy arrays
+ """
+
+ if not PIL:
+ raise ImportError('ImageHash pipeline is not available - install "pipeline" extra to enable')
+
+ self.algorithm = algorithm
+ self.size = size
+ self.strings = strings
+
+ def __call__(self, images):
+ """
+ Generates perceptual image hashes.
+
+ Args:
+ images: image|list
+
+ Returns:
+ list of hashes
+ """
+
+ # Convert single element to list
+ values = [images] if not isinstance(images, list) else images
+
+ # Open images if file strings
+ values = [Image.open(image) if isinstance(image, str) else image for image in values]
+
+ # Convert images to hashes
+ hashes = [self.ihash(image) for image in values]
+
+ # Return single element if single element passed in
+ return hashes[0] if not isinstance(images, list) else hashes
+
+ def ihash(self, image):
+ """
+ Gets an image hash for image.
+
+ Args:
+ image: PIL image
+
+ Returns:
+ hash as hex string
+ """
+
+ # Apply hash algorithm
+ if self.algorithm == "perceptual":
+ data = imagehash.phash(image, self.size)
+ elif self.algorithm == "difference":
+ data = imagehash.dhash(image, self.size)
+ elif self.algorithm == "wavelet":
+ data = imagehash.whash(image, self.size)
+ elif self.algorithm == "color":
+ data = imagehash.colorhash(image, self.size)
+ else:
+ # Default to average hash
+ data = imagehash.average_hash(image, self.size)
+
+ # Convert to output data type
+ return str(data) if self.strings else data.hash.astype(np.float32).reshape(-1)
diff --git a/test/python/testoptional.py b/test/python/testoptional.py
index 98607497..4c77fbbe 100644
--- a/test/python/testoptional.py
+++ b/test/python/testoptional.py
@@ -27,6 +27,7 @@ class TestOptional(unittest.TestCase):
"fastapi",
"fasttext",
"hnswlib",
+ "imagehash",
"nltk",
"libcloud.storage.providers",
"onnxmltools",
@@ -116,7 +117,7 @@ class TestOptional(unittest.TestCase):
Test missing pipeline dependencies
"""
- from txtai.pipeline import Caption, HFOnnx, MLOnnx, Objects, Segmentation, Tabular, Textractor, Transcription, Translation
+ from txtai.pipeline import Caption, HFOnnx, ImageHash, MLOnnx, Objects, Segmentation, Tabular, Textractor, Transcription, Translation
with self.assertRaises(ImportError):
Caption()
@@ -124,6 +125,9 @@ class TestOptional(unittest.TestCase):
with self.assertRaises(ImportError):
HFOnnx()("google/bert_uncased_L-2_H-128_A-2", quantize=True)
+ with self.assertRaises(ImportError):
+ ImageHash()
+
with self.assertRaises(ImportError):
MLOnnx()
diff --git a/test/python/testpipeline/testimagehash.py b/test/python/testpipeline/testimagehash.py
new file mode 100644
index 00000000..5e69a40e
--- /dev/null
+++ b/test/python/testpipeline/testimagehash.py
@@ -0,0 +1,74 @@
+"""
+ImageHash module tests
+"""
+
+import unittest
+
+from PIL import Image
+
+from txtai.pipeline import ImageHash
+
+# pylint: disable = C0411
+from utils import Utils
+
+
+class TestCaption(unittest.TestCase):
+ """
+ Caption tests.
+ """
+
+ @classmethod
+ def setUpClass(cls):
+ """
+ Caches an image to hash
+ """
+
+ cls.image = Image.open(Utils.PATH + "/books.jpg")
+
+ def testArray(self):
+ """
+ Test numpy return type
+ """
+
+ ihash = ImageHash(strings=False)
+ self.assertEqual(ihash(self.image).shape, (64,))
+
+ def testAverage(self):
+ """
+ Test average hash
+ """
+
+ ihash = ImageHash("average")
+ self.assertEqual(ihash(self.image), "0859dd04bfbfbf00")
+
+ def testColor(self):
+ """
+ Test color hash
+ """
+
+ ihash = ImageHash("color")
+ self.assertEqual(ihash(self.image), "1ffffe02000e000c0e0000070000")
+
+ def testDifference(self):
+ """
+ Test difference hash
+ """
+
+ ihash = ImageHash("difference")
+ self.assertEqual(ihash(self.image), "d291996d6969686a")
+
+ def testPerceptual(self):
+ """
+ Test perceptual hash
+ """
+
+ ihash = ImageHash("perceptual")
+ self.assertEqual(ihash(self.image), "8be8418577b331b9")
+
+ def testWavelet(self):
+ """
+ Test wavelet hash
+ """
+
+ ihash = ImageHash("wavelet")
+ self.assertEqual(ihash(Utils.PATH + "/books.jpg"), "68015d85bfbf3f00")