💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Add object detection pipeline, closes #148
davidmezzettia437f0bbdbdc
diff --git a/src/python/txtai/pipeline/__init__.py b/src/python/txtai/pipeline/__init__.pyindex 755f04ee..991decdb 100644--- a/src/python/txtai/pipeline/__init__.py+++ b/src/python/txtai/pipeline/__init__.py@@ -7,6 +7,7 @@ from .base import Pipelinefrom .factory import PipelineFactoryfrom .hfmodel import HFModelfrom .hfpipeline import HFPipeline+from .image import *from .segment import *from .text import *from .tensors import Tensorsdiff --git a/src/python/txtai/pipeline/image/objects.py b/src/python/txtai/pipeline/image/objects.pynew file mode 100644index 00000000..6da62d8b--- /dev/null+++ b/src/python/txtai/pipeline/image/objects.py@@ -0,0 +1,60 @@+"""+Objects module+"""++from ..hfpipeline import HFPipeline+++class Objects(HFPipeline):+ """+ Applies object detection models to images. Supports both object detection models and image classification models.+ """++ def __init__(self, path=None, quantize=False, gpu=True, model=None, classification=False, threshold=0.9):+ super().__init__("image-classification" if classification else "object-detection", path, quantize, gpu, model)++ self.classification = classification+ self.threshold = threshold++ def __call__(self, images):+ """+ Applies object detection/image classification models to images. Returns a list of (label, score).++ This method supports a single image or a list of images. If the input is an image, the return+ type is a 1D list of (label, score). If text is a list, a 2D list of (label, score) is+ returned with a row per image.++ Args:+ images: image|list++ Returns:+ list of (label, score)+ """++ # Convert single element to list+ values = [images] if not isinstance(images, list) else images++ # Run pipeline+ results = self.pipeline(values) if self.classification else self.pipeline(values, threshold=self.threshold)++ # Build list of (id, score)+ scores = []+ for result in results:+ # Convert to (label, score) tuples+ result = [(x["label"], x["score"]) for x in result if x["score"] > self.threshold]++ # Sort by score descending+ result = sorted(result, key=lambda x: x[1], reverse=True)++ # Deduplicate labels+ unique = set()+ elements = []+ for label, score in result:+ if label not in unique:+ elements.append((label, score))+ unique.add(label)++ scores.append(elements)++ # Return single element if single element passed in+ return scores[0] if not isinstance(images, list) else scoresdiff --git a/test/python/testobjects.py b/test/python/testobjects.pynew file mode 100644index 00000000..f5a3ba35--- /dev/null+++ b/test/python/testobjects.py@@ -0,0 +1,32 @@+"""+Objects module tests+"""++import unittest++from txtai.pipeline import Objects++# pylint: disable = C0411+from utils import Utils+++class TestObjects(unittest.TestCase):+ """+ Object detection tests+ """++ def testClassification(self):+ """+ Test object detection using an image classification model+ """++ objects = Objects(classification=True, threshold=0.3)+ self.assertEqual(objects(Utils.PATH + "/books.jpg")[0][0], "library")++ def testDetection(self):+ """+ Test object detection using an object detection model+ """++ objects = Objects()+ self.assertEqual(objects(Utils.PATH + "/books.jpg")[0][0], "book")