Skip to content

图像加载

所有Autodistill基础模型(即Grounding DINO或CLIP)都支持提供文件名并加载相应的图像以用于标记。一些模型还允许直接传递以下格式的图像:

  • PIL Image
  • cv2图像
  • 从中获取图像的URL
  • 一个文件名,作为图像加载

这是由低级 load_image 函数处理的。这个函数允许你传递上述任何格式。如果你已经在内存中有一张图像,PIL 和 cv2 格式是理想的。基础模型使用这个函数来请求模型所需的格式。如果一个模型需要的图像格式不同于你提供的格式——例如,如果你提供了一个文件名,而模型需要一个 PIL Image 对象——那么 load_image 函数将把图像转换为正确的格式。

以下模型支持 load_image 函数。 PILcv2 表示 load_image 会将您的图像(如有必要)转换为何种格式,以便将您的图像传递给模型。

  • AltCLIP: PIL
  • CLIP: PIL
  • 基础 DINO: cv2
  • MetaCLIP: PIL
  • 远程CLIP: PIL
  • 变换器:PIL
  • SAM HQ: cv2
  • 分割任意东西: cv2
  • DETIC: PIL
  • VLPart: PIL
  • CoDet: PIL
  • OWLv2: PIL
  • 快速视觉转移: PIL
  • 快速SAM: cv2
  • SegGPT: PIL
  • OWLViT: PIL
  • BLIPv2: PIL
  • DINOv2: PIL
  • 基础SAM: cv2
  • BLIP: PIL

load_image 函数

从文件路径、URI、PIL 图像或 numpy 数组加载图像。

此函数供Autodistill模块使用。您无需直接使用它。

参数:

名称 类型 描述 默认值
image Any

要加载的图像

必填
return_format

返回图像的格式

'cv2'

返回:

类型 描述
Any

指定格式的图像

Source code in autodistill/helpers.py
21
22
23
24
25
26
27
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def load_image(
    image: Any,
    return_format="cv2",
) -> Any:
    """
    Load an image from a file path, URI, PIL image, or numpy array.

    This function is for use by Autodistill modules. You don't need to use it directly.

    Args:
        image: The image to load
        return_format: The format to return the image in

    Returns:
        The image in the specified format
    """
    if return_format not in ACCEPTED_RETURN_FORMATS:
        raise ValueError(f"return_format must be one of {ACCEPTED_RETURN_FORMATS}")

    if isinstance(image, Image.Image) and return_format == "PIL":
        return image
    elif isinstance(image, Image.Image) and return_format == "cv2":
        # channels need to be reversed for cv2
        return cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
    elif isinstance(image, Image.Image) and return_format == "numpy":
        return np.array(image)

    if isinstance(image, np.ndarray) and return_format == "PIL":
        return Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    elif isinstance(image, np.ndarray) and return_format == "cv2":
        return image
    elif isinstance(image, np.ndarray) and return_format == "numpy":
        return image

    if isinstance(image, str) and image.startswith("http"):
        if return_format == "PIL":
            response = requests.get(image)
            return Image.open(BytesIO(response.content))
        elif return_format == "cv2" or return_format == "numpy":
            response = requests.get(image)
            pil_image = Image.open(BytesIO(response.content))
            return np.array(pil_image)
    elif os.path.isfile(image):
        if return_format == "PIL":
            return Image.open(image)
        elif return_format == "cv2":
            # channels need to be reversed for cv2
            return cv2.cvtColor(np.array(Image.open(image)), cv2.COLOR_RGB2BGR)
        elif return_format == "numpy":
            pil_image = Image.open(image)
            return np.array(pil_image)
    else:
        raise ValueError(f"{image} is not a valid file path or URI")