文档

图片聚类

更新时间:

图片聚类基于照片的相似度将满足分组条件的照片分到同一组,您可以使用该功能筛选相册中连拍的照片,对其进行分组操作。

应用场景

  • 网盘与相册服务:可以对网盘或者相册中的相似图片进行聚类,生成个性化的图片聚类展示。

  • 图片去重:针对APP或相册中上传和生成的图片进行相似图片聚类,以便删除相似图片,从而节省存储空间和存储成本。

前提条件

  • 已根据使用场景为文件建立元数据索引。具体操作,请参见建立元数据索引

  • 数据集的模板选择Official:ImageManagement”。

创建图片聚类分组

调用CreateSimilarImageClusteringTask - 创建相似图片聚类任务接口创建图片聚类任务,对项目test-projecttest-dataset数据集中的照片进行图片聚类分组。

说明

使用图片聚类会产生 API 调用费用,详情请查看IMM计费项说明

请求示例

{
  "ProjectName": "test-project",
  "DatasetName": "test-dataset"
}

返回示例

{
  "TaskId": "SimilarImageClustering-3b4ce06c-f19e-43ba-8ae9-29a4ba617eac",
  "RequestId": "0FA88E7A-85C8-5016-8182-80FA2A711D29",
  "EventId": "3BF-1mc8MI8FsJWMMgJhDO6O98mepq1"
}

示例代码

# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import os
import sys

from typing import List

from alibabacloud_imm20200930.client import Client as imm20200930Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_imm20200930 import models as imm_20200930_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient


class Sample:
    def __init__(self):
        pass

    @staticmethod
    def create_client(
        access_key_id: str,
        access_key_secret: str,
    ) -> imm20200930Client:
        """
        使用AK&SK初始化账号Client
        @param access_key_id:
        @param access_key_secret:
        @return: Client
        @throws Exception
        """
        config = open_api_models.Config(
            # 必填,您的 AccessKey ID,
            access_key_id=access_key_id,
            # 必填,您的 AccessKey Secret,
            access_key_secret=access_key_secret
        )
        # Endpoint 请参考 https://api.aliyun.com/product/imm
        config.endpoint = f'imm.cn-beijing.aliyuncs.com'
        return imm20200930Client(config)

    @staticmethod
    def main(
        args: List[str],
    ) -> None:
        # 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        # 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378659.html
        client = Sample.create_client(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
        create_similar_image_clustering_task_request = imm_20200930_models.CreateSimilarImageClusteringTaskRequest(
            project_name='test-project',
            dataset_name='test-dataset'
        )
        runtime = util_models.RuntimeOptions()
        try:
            # 复制代码运行请自行打印 API 的返回值
            client.create_similar_image_clustering_task_with_options(create_similar_image_clustering_task_request, runtime)
        except Exception as error:
            # 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
            # 错误 message
            print(error.message)
            # 诊断地址
            print(error.data.get("Recommend"))
            UtilClient.assert_as_string(error.message)

    @staticmethod
    async def main_async(
        args: List[str],
    ) -> None:
        # 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        # 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378659.html
        client = Sample.create_client(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
        create_similar_image_clustering_task_request = imm_20200930_models.CreateSimilarImageClusteringTaskRequest(
            project_name='test-project',
            dataset_name='test-dataset'
        )
        runtime = util_models.RuntimeOptions()
        try:
            # 复制代码运行请自行打印 API 的返回值
            await client.create_similar_image_clustering_task_with_options_async(create_similar_image_clustering_task_request, runtime)
        except Exception as error:
            # 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
            # 错误 message
            print(error.message)
            # 诊断地址
            print(error.data.get("Recommend"))
            UtilClient.assert_as_string(error.message)


if __name__ == '__main__':
    Sample.main(sys.argv[1:])

查询图片聚类分组

调用QuerySimilarImageClusters - 查询相似图片聚类接口查询图片聚类任务,查询项目名称为test-project下数据集test-dataset中的图片分组信息。

请求示例

{
  "ProjectName": "test-project",
  "DatasetName": "test-dataset"
}

返回示例

{
  "SimilarImageClusters": [
    {
      "ObjectId": "SimilarImageCluster-e5cdfdad-c02a-4093-aa58-400ff2e4520b",
      "CreateTime": "2024-03-07T14:57:13.047481088+08:00",
      "UpdateTime": "2024-03-07T14:57:13.047481088+08:00",
      "Files": [
        {
          "ImageScore": 0.749,
          "URI": "oss://test-ivanivan/p637447.jpeg"
        },
        {
          "ImageScore": 0.749,
          "URI": "oss://test-ivanivan/p637448.jpeg"
        }
      ]
    },
    {
      "ObjectId": "SimilarImageCluster-3350bbcf-a044-42f2-bedc-57eede4d476f",
      "CreateTime": "2024-03-07T14:57:12.955958016+08:00",
      "UpdateTime": "2024-03-07T14:57:12.955958016+08:00",
      "Files": [
        {
          "ImageScore": 0.736,
          "URI": "oss://test-ivanivan/hanhong.png"
        },
        {
          "ImageScore": 0.736,
          "URI": "oss://test-ivanivan/hanhong2.png"
        }
      ]
    },
    {
      "ObjectId": "SimilarImageCluster-4c239671-5504-4910-90f6-03cd863f686e",
      "CreateTime": "2024-03-07T14:57:12.886128896+08:00",
      "UpdateTime": "2024-03-07T14:57:12.886128896+08:00",
      "Files": [
        {
          "ImageScore": 0.692,
          "URI": "oss://test-ivanivan/dir1/mp4_png.png"
        },
        {
          "ImageScore": 0.67,
          "URI": "oss://test-ivanivan/dir1/demo.gif"
        }
      ]
    },
    {
      "ObjectId": "SimilarImageCluster-e77ac1ad-44b4-49d5-baa7-ad871efd0503",
      "CreateTime": "2024-03-07T14:57:12.817118976+08:00",
      "UpdateTime": "2024-03-07T14:57:12.817118976+08:00",
      "Files": [
        {
          "ImageScore": 0.717,
          "URI": "oss://test-ivanivan/OIP-C.jpeg"
        },
        {
          "ImageScore": 0.717,
          "URI": "oss://test-ivanivan/OIP-C1.jpeg"
        }
      ]
    },
    {
      "ObjectId": "SimilarImageCluster-315751c6-5b69-43b4-8c37-00e7ad2ec0e6",
      "CreateTime": "2024-03-07T14:57:12.745981952+08:00",
      "UpdateTime": "2024-03-07T14:57:12.745981952+08:00",
      "Files": [
        {
          "ImageScore": 0.714,
          "URI": "oss://test-ivanivan/A6.jpg"
        },
        {
          "ImageScore": 0.709,
          "URI": "oss://test-ivanivan/A4 (1).jpg"
        }
      ]
    },
    {
      "ObjectId": "SimilarImageCluster-140d3e92-7e67-4b9d-8066-3aea778e5898",
      "CreateTime": "2024-03-07T14:57:12.65400192+08:00",
      "UpdateTime": "2024-03-07T14:57:12.65400192+08:00",
      "Files": [
        {
          "ImageScore": 0.709,
          "URI": "oss://test-ivanivan/A1 (1).jpg"
        },
        {
          "ImageScore": 0.709,
          "URI": "oss://test-ivanivan/A2 (1).jpg"
        }
      ]
    }
  ],
  "RequestId": "5830FFD2-C2E5-5431-9180-EBBACCC2FECE",
  "NextToken": ""
}

示例代码

# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import os
import sys

from typing import List

from alibabacloud_imm20200930.client import Client as imm20200930Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_imm20200930 import models as imm_20200930_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient


class Sample:
    def __init__(self):
        pass

    @staticmethod
    def create_client(
        access_key_id: str,
        access_key_secret: str,
    ) -> imm20200930Client:
        """
        使用AK&SK初始化账号Client
        @param access_key_id:
        @param access_key_secret:
        @return: Client
        @throws Exception
        """
        config = open_api_models.Config(
            # 必填,您的 AccessKey ID,
            access_key_id=access_key_id,
            # 必填,您的 AccessKey Secret,
            access_key_secret=access_key_secret
        )
        # Endpoint 请参考 https://api.aliyun.com/product/imm
        config.endpoint = f'imm.cn-beijing.aliyuncs.com'
        return imm20200930Client(config)

    @staticmethod
    def main(
        args: List[str],
    ) -> None:
        # 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        # 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378659.html
        client = Sample.create_client(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
        query_similar_image_clusters_request = imm_20200930_models.QuerySimilarImageClustersRequest(
            dataset_name='test-dataset',
            project_name='test-project'
        )
        runtime = util_models.RuntimeOptions()
        try:
            # 复制代码运行请自行打印 API 的返回值
            client.query_similar_image_clusters_with_options(query_similar_image_clusters_request, runtime)
        except Exception as error:
            # 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
            # 错误 message
            print(error.message)
            # 诊断地址
            print(error.data.get("Recommend"))
            UtilClient.assert_as_string(error.message)

    @staticmethod
    async def main_async(
        args: List[str],
    ) -> None:
        # 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        # 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378659.html
        client = Sample.create_client(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
        query_similar_image_clusters_request = imm_20200930_models.QuerySimilarImageClustersRequest(
            dataset_name='test-dataset',
            project_name='test-project'
        )
        runtime = util_models.RuntimeOptions()
        try:
            # 复制代码运行请自行打印 API 的返回值
            await client.query_similar_image_clusters_with_options_async(query_similar_image_clusters_request, runtime)
        except Exception as error:
            # 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
            # 错误 message
            print(error.message)
            # 诊断地址
            print(error.data.get("Recommend"))
            UtilClient.assert_as_string(error.message)


if __name__ == '__main__':
    Sample.main(sys.argv[1:])