Objects
The Objects pipeline reads a list of images and returns a list of detected objects.
Example
The following shows a simple example using this pipeline.
from txtai.pipeline import Objects
# Create and run pipeline
objects = Objects()
objects("path to image file")
See the link below for a more detailed example.
Notebook | Description | |
---|---|---|
Generate image captions and detect objects | Captions and object detection for images |
Configuration-driven example
Pipelines are run with Python or configuration. Pipelines can be instantiated in configuration using the lower case name of the pipeline. Configuration-driven pipelines are run with workflows or the API.
config.yml
# Create pipeline using lower case class name
objects:
# Run pipeline with workflow
workflow:
objects:
tasks:
- action: objects
Run with Workflows
from txtai.app import Application
# Create and run pipeline with workflow
app = Application("config.yml")
list(app.workflow("objects", ["path to image file"]))
Run with API
CONFIG=config.yml uvicorn "txtai.api:app" &
curl \
-X POST "http://localhost:8000/workflow" \
-H "Content-Type: application/json" \
-d '{"name":"objects", "elements":["path to image file"]}'
Methods
Python documentation for the pipeline.
__init__(self, path=None, quantize=False, gpu=True, model=None, classification=False, threshold=0.9, **kwargs)
special
Source code in txtai/pipeline/image/objects.py
def __init__(self, path=None, quantize=False, gpu=True, model=None, classification=False, threshold=0.9, **kwargs):
if not PIL:
raise ImportError('Objects pipeline is not available - install "pipeline" extra to enable')
super().__init__("image-classification" if classification else "object-detection", path, quantize, gpu, model, **kwargs)
self.classification = classification
self.threshold = threshold
__call__(self, images, flatten=False, workers=0)
special
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.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
images | image|list | required | |
flatten | flatten output to a list of objects | False | |
workers | number of concurrent workers to use for processing data, defaults to None | 0 |
Returns:
Type | Description |
---|---|
list of (label, score) |
Source code in txtai/pipeline/image/objects.py
def __call__(self, images, flatten=False, workers=0):
"""
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
flatten: flatten output to a list of objects
workers: number of concurrent workers to use for processing data, defaults to None
Returns:
list of (label, score)
"""
# 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]
# Run pipeline
results = (
self.pipeline(values, num_workers=workers)
if self.classification
else self.pipeline(values, threshold=self.threshold, num_workers=workers)
)
# Build list of (id, score)
outputs = []
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 if flatten else (label, score))
unique.add(label)
outputs.append(elements)
# Return single element if single element passed in
return outputs[0] if not isinstance(images, list) else outputs