跳到内容

对象

pipeline pipeline

对象流水线读取图像列表并返回检测到的对象列表。

示例

下面显示了使用此流水线的简单示例。

from txtai.pipeline import Objects

# Create and run pipeline
objects = Objects()
objects("path to image file")

请参阅下面的链接以获取更详细的示例。

Notebook 描述
生成图像标题和检测对象 图像的标题和对象检测 Open In Colab

配置驱动的示例

流水线可以通过 Python 或配置运行。流水线可以在配置中使用流水线的全小写名称实例化。配置驱动的流水线可以使用工作流API运行。

config.yml

# Create pipeline using lower case class name
objects:

# Run pipeline with workflow
workflow:
  objects:
    tasks:
      - action: objects

使用工作流运行

from txtai import Application

# Create and run pipeline with workflow
app = Application("config.yml")
list(app.workflow("objects", ["path to image file"]))

使用 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"]}'

方法

流水线的 Python 文档。

__init__(path=None, quantize=False, gpu=True, model=None, classification=False, threshold=0.9, **kwargs)

源代码位于 txtai/pipeline/image/objects.py
21
22
23
24
25
26
27
28
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__(images, flatten=False, workers=0)

将对象检测/图像分类模型应用于图像。返回一个 (标签, 分数) 列表。

此方法支持单个图像或图像列表。如果输入是单个图像,则返回类型是 (标签, 分数) 的一维列表。如果输入是列表,则返回一个二维的 (标签, 分数) 列表,每行对应一个图像。

参数

名称 类型 描述 默认值
images

image|list

必需
flatten

将输出展平为对象列表

False
workers

用于处理数据的并发工作进程数量,默认为 None

0

返回值

类型 描述

(标签, 分数) 列表

源代码位于 txtai/pipeline/image/objects.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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