图片标签检测

本文介绍图片标签检测功能的应用场景和使用方法等。

功能简介

图片标签检测可以识别图片中的场景、物体和事件等内容,实现图片的自动打标。目前支持的标签种类包含三十多个分类、数千个标签,如下图所示。

图片标签检测..jpeg

应用场景

场景名称

场景说明

内容识别

根据拍摄或上传的图片,识别图片中的物品、场景等信息,可应用于拍照识物或科普类的功能或产品中。

智能相册

根据图片内容信息,并对图片进行自动分类,实现相册图库的智能分类,实现高效自动化管理。

场景分析

对图片中包含的多种事物或场景进行识别,为不同场景打上内容标签,提升场景分析效率,减少人工标注成本。

内容运营

获取图片标签信息,实现内容推荐,可广泛应用于社交或新闻资讯及电商等内容平台中。

使用限制

图片标签检测的使用限制如下表所示:

限制项

限制

图片格式

支持检测的图片格式如下:

  • PNG

  • JPG

  • JPEG

图片大小

支持检测的图片大小限制如下:

  • 图片大小不超过20 MB

  • 图片高度或宽度不超过3万像素(px)

  • 图片总像素不超过2.5亿像素(px)

前提条件

  • 已创建并获取AccessKey。具体操作,请参见创建AccessKey

  • 已开通OSS服务、创建存储空间并上传文件到存储空间。具体操作,请参见控制台上传文件

  • 已开通智能媒体管理服务。具体操作,请参见开通产品

  • 已通过智能媒体管理控制台创建项目。具体操作,请参见创建项目

  • OSS服务存储空间与智能媒体管理服务创建项目同属一个地域,例如华北2。

    说明

使用方法

调用DetectImageLabels - 检测图片中的标签信息接口检测图片中的标签信息。

检测信息

  • IMM项目名称:test-project

  • 待检测图片的存储地址:oss://test-bucket/test-object.jpg

  • 图片示例:

    test-object

请求示例

{
    "ProjectName": "test-project",
    "SourceURI": "oss://test-bucket/test-object.jpg",
    "Threshold": 0.7
}

返回示例

{
  "RequestId": "91C92EBA-5E51-50C8-B51B-8C3BDC66EB86",
  "Labels": [
    {
      "CentricScore": 0.797,
      "Language": "zh-Hans",
      "LabelConfidence": 1,
      "LabelName": "服装",
      "LabelLevel": 2,
      "ParentLabelName": "衣服"
    },
    {
      "CentricScore": 0.695,
      "Language": "zh-Hans",
      "LabelConfidence": 1,
      "LabelName": "食肉动物",
      "LabelLevel": 2,
      "ParentLabelName": "野生动物"
    },
    {
      "CentricScore": 0.723,
      "Language": "zh-Hans",
      "LabelConfidence": 0.987,
      "LabelName": "帐篷",
      "LabelLevel": 2,
      "ParentLabelName": "其他场景"
    },
    {
      "CentricScore": 0.759,
      "Language": "zh-Hans",
      "LabelConfidence": 0.963,
      "LabelName": "建筑",
      "LabelLevel": 3,
      "ParentLabelName": "地标建筑"
    },
    {
      "CentricScore": 0.695,
      "Language": "zh-Hans",
      "LabelConfidence": 0.949,
      "LabelName": "宠物",
      "LabelLevel": 1,
      "ParentLabelName": ""
    },
    {
      "CentricScore": 0.787,
      "Language": "zh-Hans",
      "LabelConfidence": 0.944,
      "LabelName": "人像",
      "LabelLevel": 2,
      "ParentLabelName": "面部"
    },
    {
      "CentricScore": 0.695,
      "Language": "zh-Hans",
      "LabelConfidence": 0.939,
      "LabelName": "狗",
      "LabelLevel": 3,
      "ParentLabelName": "宠物狗"
    },
    {
      "CentricScore": 0.803,
      "Language": "zh-Hans",
      "LabelConfidence": 0.924,
      "LabelName": "人",
      "LabelLevel": 2,
      "ParentLabelName": "面部"
    },
    {
      "CentricScore": 0.687,
      "Language": "zh-Hans",
      "LabelConfidence": 0.895,
      "LabelName": "金毛寻回犬",
      "LabelLevel": 3,
      "ParentLabelName": "宠物狗"
    },
    {
      "CentricScore": 0.689,
      "Language": "zh-Hans",
      "LabelConfidence": 0.885,
      "LabelName": "露营",
      "LabelLevel": 2,
      "ParentLabelName": "娱乐"
    },
    {
      "CentricScore": 0.76,
      "Language": "zh-Hans",
      "LabelConfidence": 0.883,
      "LabelName": "家具",
      "LabelLevel": 1,
      "ParentLabelName": ""
    },
    {
      "CentricScore": 0.802,
      "Language": "zh-Hans",
      "LabelConfidence": 0.878,
      "LabelName": "男性",
      "LabelLevel": 2,
      "ParentLabelName": "面部"
    },
    {
      "CentricScore": 0.792,
      "Language": "zh-Hans",
      "LabelConfidence": 0.85,
      "LabelName": "女性",
      "LabelLevel": 2,
      "ParentLabelName": "面部"
    },
    {
      "CentricScore": 0.722,
      "Language": "zh-Hans",
      "LabelConfidence": 0.849,
      "LabelName": "植物",
      "LabelLevel": 1,
      "ParentLabelName": ""
    },
    {
      "CentricScore": 0.729,
      "Language": "zh-Hans",
      "LabelConfidence": 0.84,
      "LabelName": "草坪",
      "LabelLevel": 3,
      "ParentLabelName": "自然景观"
    },
    {
      "CentricScore": 0.695,
      "Language": "zh-Hans",
      "LabelConfidence": 0.818,
      "LabelName": "动物",
      "LabelLevel": 2,
      "ParentLabelName": "野生动物"
    },
    {
      "CentricScore": 0.782,
      "Language": "zh-Hans",
      "LabelConfidence": 0.816,
      "LabelName": "T恤",
      "LabelLevel": 2,
      "ParentLabelName": "衣服"
    },
    {
      "CentricScore": 0.749,
      "Language": "zh-Hans",
      "LabelConfidence": 0.813,
      "LabelName": "鞋",
      "LabelLevel": 2,
      "ParentLabelName": "衣服"
    },
    {
      "CentricScore": 0.826,
      "Language": "zh-Hans",
      "LabelConfidence": 0.813,
      "LabelName": "鞋类",
      "LabelLevel": 2,
      "ParentLabelName": "衣服"
    },
    {
      "CentricScore": 0.685,
      "Language": "zh-Hans",
      "LabelConfidence": 0.775,
      "LabelName": "拉布拉多犬",
      "LabelLevel": 3,
      "ParentLabelName": "宠物狗"
    },
    {
      "CentricScore": 0.76,
      "Language": "zh-Hans",
      "LabelConfidence": 0.746,
      "LabelName": "椅子",
      "LabelLevel": 2,
      "ParentLabelName": "家具"
    },
    {
      "CentricScore": 0.757,
      "Language": "zh-Hans",
      "LabelConfidence": 0.742,
      "LabelName": "女孩",
      "LabelLevel": 2,
      "ParentLabelName": "面部"
    },
    {
      "CentricScore": 0.776,
      "Language": "zh-Hans",
      "LabelConfidence": 0.726,
      "LabelName": "微笑",
      "LabelLevel": 2,
      "ParentLabelName": "外貌特征"
    },
    {
      "CentricScore": 0.695,
      "Language": "zh-Hans",
      "LabelConfidence": 0.722,
      "LabelName": "犬类运动",
      "LabelLevel": 2,
      "ParentLabelName": "体育运动"
    },
    {
      "CentricScore": 0.685,
      "Language": "zh-Hans",
      "LabelConfidence": 0.664,
      "LabelName": "金毛",
      "LabelLevel": 3,
      "ParentLabelName": "宠物狗"
    },
    {
      "CentricScore": 0.826,
      "Language": "zh-Hans",
      "LabelConfidence": 1,
      "LabelName": "衣服",
      "LabelLevel": 1,
      "ParentLabelName": ""
    },
    {
      "CentricScore": 0.695,
      "Language": "zh-Hans",
      "LabelConfidence": 1,
      "LabelName": "野生动物",
      "LabelLevel": 1,
      "ParentLabelName": ""
    },
    {
      "CentricScore": 0.723,
      "Language": "zh-Hans",
      "LabelConfidence": 0.987,
      "LabelName": "其他场景",
      "LabelLevel": 1,
      "ParentLabelName": ""
    },
    {
      "CentricScore": 0.759,
      "Language": "zh-Hans",
      "LabelConfidence": 0.963,
      "LabelName": "地标建筑",
      "LabelLevel": 2,
      "ParentLabelName": "旅游&地理"
    },
    {
      "CentricScore": 0.803,
      "Language": "zh-Hans",
      "LabelConfidence": 0.944,
      "LabelName": "面部",
      "LabelLevel": 1,
      "ParentLabelName": ""
    },
    {
      "CentricScore": 0.695,
      "Language": "zh-Hans",
      "LabelConfidence": 0.939,
      "LabelName": "宠物狗",
      "LabelLevel": 2,
      "ParentLabelName": "宠物"
    },
    {
      "CentricScore": 0.689,
      "Language": "zh-Hans",
      "LabelConfidence": 0.885,
      "LabelName": "娱乐",
      "LabelLevel": 1,
      "ParentLabelName": ""
    },
    {
      "CentricScore": 0.729,
      "Language": "zh-Hans",
      "LabelConfidence": 0.84,
      "LabelName": "自然景观",
      "LabelLevel": 2,
      "ParentLabelName": "旅游&地理"
    },
    {
      "CentricScore": 0.776,
      "Language": "zh-Hans",
      "LabelConfidence": 0.726,
      "LabelName": "外貌特征",
      "LabelLevel": 1,
      "ParentLabelName": ""
    },
    {
      "CentricScore": 0.695,
      "Language": "zh-Hans",
      "LabelConfidence": 0.722,
      "LabelName": "体育运动",
      "LabelLevel": 1,
      "ParentLabelName": ""
    },
    {
      "CentricScore": 0.759,
      "Language": "zh-Hans",
      "LabelConfidence": 0.963,
      "LabelName": "旅游&地理",
      "LabelLevel": 1,
      "ParentLabelName": ""
    }
  ]
}
说明

检测结果显示,该检测图片包含以下标签:

  • 父级标签:衣服野生动物地标建筑面部宠物狗

  • 标签:服装食肉动物帐篷建筑宠物人脸

示例代码

以Python SDK为例,图片标签检测的完整示例代码如下。

# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import sys
import os
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:
        """
        使用AccessKey ID&AccessKey Secret初始化账号Client。
        @param access_key_id:
        @param access_key_secret:
        @return: Client
        @throws Exception
        """
        config = open_api_models.Config(
            access_key_id=access_key_id,
            access_key_secret=access_key_secret
        )
        # 填写访问的IMM域名。
        config.endpoint = f'imm.cn-beijing.aliyuncs.com'
        return imm20200930Client(config)

    @staticmethod
    def main(
        args: List[str],
    ) -> None:
        # 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
        # 强烈建议不要把ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
        # 本示例通过从环境变量中读取AccessKey,来实现API访问的身份验证。如何配置环境变量,请参见https://help.aliyun.com/document_detail/2361894.html。
        imm_access_key_id = os.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
        imm_access_key_secret = os.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
        # 初始化客户端。
        client = Sample.create_client(imm_access_key_id, imm_access_key_secret)
        detect_image_labels_request = imm_20200930_models.DetectImageLabelsRequest(
            project_name='test-project',
            source_uri='oss://test-bucket/test-object.jpg',
            threshold=0.7
        )
        runtime = util_models.RuntimeOptions()
        try:
            # 复制代码运行请自行打印API的返回值。
            client.detect_image_labels_with_options(detect_image_labels_request, runtime)
        except Exception as error:
            # 如有需要,请打印错误信息。
            UtilClient.assert_as_string(error.message)

    @staticmethod
    async def main_async(
        args: List[str],
    ) -> None:
        # 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
        # 强烈建议不要把ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
        # 本示例通过从环境变量中读取AccessKey,来实现API访问的身份验证。如何配置环境变量,请参见https://help.aliyun.com/document_detail/2361894.html。
        imm_access_key_id = os.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
        imm_access_key_secret = os.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
        # 初始化客户端。
        client = Sample.create_client(imm_access_key_id, imm_access_key_secret)
        detect_image_labels_request = imm_20200930_models.DetectImageLabelsRequest(
            project_name='test-project',
            source_uri='oss://test-bucket/test-object.jpg',
            threshold=0.7
        )
        runtime = util_models.RuntimeOptions()
        try:
            # 复制代码运行请自行打印API的返回值。
            await client.detect_image_labels_with_options_async(detect_image_labels_request, runtime)
        except Exception as error:
            # 如有需要,请打印错误信息。
            UtilClient.assert_as_string(error.message)


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

常见FAQ

  1. 图片标签检测支持识别图片中文字以及涉及日期、地点,并将其标签化吗?

    暂不支持,可使用图片语义检索,提取图片里的文字信息,然后您可以根据这些文字来确定日期和机构名称。地点的话,如果您的图片里面有exif信息,可以调用图片信息的API,获取到图片拍摄的GPS 来获取。

说明

对于含有暴力、色情或其他敏感内容的图片,自动检测可能会不够敏感或准确,有时候需要结合人工审核。