Skip to content

标题

pipeline pipeline

标题管道读取图像列表并返回这些图像的标题列表。

示例

以下展示了一个使用此管道的简单示例。

from txtai.pipeline import Caption

# 创建并运行管道
caption = Caption()
caption("图像文件路径")

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

笔记本 描述
生成图像标题并检测对象 图像的标题和对象检测 在 Colab 中打开

配置驱动的示例

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

config.yml

# 使用小写类名创建管道
caption:

# 使用工作流运行管道
workflow:
  caption:
    tasks:
      - action: caption

使用工作流运行

from txtai import Application

# 使用工作流创建并运行管道
app = Application("config.yml")
list(app.workflow("caption", ["图像文件路径"]))

使用 API 运行

CONFIG=config.yml uvicorn "txtai.api:app" &

curl \
  -X POST "http://localhost:8000/workflow" \
  -H "Content-Type: application/json" \
  -d '{"name":"caption", "elements":["图像文件路径"]}'

方法

管道的 Python 文档。

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

Source code in txtai/pipeline/image/caption.py
21
22
23
24
25
26
def __init__(self, path=None, quantize=False, gpu=True, model=None, **kwargs):
    if not PIL:
        raise ImportError('Captions pipeline is not available - install "pipeline" extra to enable')

    # Call parent constructor
    super().__init__("image-to-text", path, quantize, gpu, model, **kwargs)

__call__(images)

Builds captions for images.

This method supports a single image or a list of images. If the input is an image, the return type is a string. If text is a list, a list of strings is returned

Parameters:

Name Type Description Default
images

image|list

required

Returns:

Type Description

list of captions

Source code in txtai/pipeline/image/caption.py
28
29
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
def __call__(self, images):
    """
    Builds captions for images.

    This method supports a single image or a list of images. If the input is an image, the return
    type is a string. If text is a list, a list of strings is returned

    Args:
        images: image|list

    Returns:
        list of captions
    """

    # 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]

    # Get and clean captions
    captions = []
    for result in self.pipeline(values):
        text = " ".join([r["generated_text"] for r in result]).strip()
        captions.append(text)

    # Return single element if single element passed in
    return captions[0] if not isinstance(images, list) else captions