Use Function Compute to perform incremental image detection in OSS

更新时间:
复制 MD 格式

To check images uploaded to an Object Storage Service (OSS) bucket for prohibited content in real time, you can use Function Compute to call the enhanced image moderation service of Content Moderation. Based on the detection results, you can decide how to handle the images. For example, you can make them public or transfer them for manual review. This topic describes how to use Function Compute to perform incremental detection on images in an OSS bucket.

Solution architecture

函数计算.png

Advantages

Compared with version 1.0 of the OSS violation detection feature in Content Moderation, the Function Compute solution provides the following advantages:

  • You can configure multiple functions to use different detection settings for different OSS buckets.

  • Detection logs are written to the corresponding project in Simple Log Service (SLS). This makes it easier to query and analyze detection results.

  • Function Compute provides real-time dynamic scaling based on events and requests. This enables incremental image detection in milliseconds.

  • It provides virtual machine-level isolation, which lets you process files in OSS buckets with more flexibility and security.

Note

To periodically or batch-detect images in an OSS bucket, see Detect existing images, audio, videos, and documents in OSS.

Billing

When you use Function Compute to perform incremental detection on images in an OSS bucket, the following fees are incurred:

Category

Description

Content Moderation service fees

Fees for calling the enhanced image moderation API. For more information, see Introduction to and billing of the enhanced image moderation service V2.0.

OSS access and data transfer fees

Request fees are incurred when you access files in an OSS bucket. For more information, see Request fees.

Calling the moderation API using an OSS internal endpoint in the same region results in internal data transfer. Calling the moderation API using a public endpoint or across regions incurs Internet data transfer costs. For more information, see Data transfer fees.

Function Compute invocation and resource fees

Using Function Compute to trigger OSS events and invoke functions incurs resource and invocation fees. For more information, see Billing overview.

Regions supported by the enhanced image moderation service

Region

Public endpoint

Internal endpoint

China (Shanghai)

green-cip.cn-shanghai.aliyuncs.com

green-cip-vpc.cn-shanghai.aliyuncs.com

China (Beijing)

green-cip.cn-beijing.aliyuncs.com

green-cip-vpc.cn-beijing.aliyuncs.com

China (Hangzhou)

green-cip.cn-hangzhou.aliyuncs.com

green-cip-vpc.cn-hangzhou.aliyuncs.com

China (Shenzhen)

green-cip.cn-shenzhen.aliyuncs.com

Not available

China (Chengdu)

green-cip.cn-chengdu.aliyuncs.com

Not available

Singapore

green-cip.ap-southeast-1.aliyuncs.com

green-cip-vpc.ap-southeast-1.aliyuncs.com

Prerequisites

Procedure

  1. Log on to the Function Compute console, select a region, and then create a service and a function. For more information, see Manage services and Manage functions.

    When you create the function, select Create with Built-in Runtime. The core field configurations are as follows:

    • In the Basic Settings section, set Handler Type to Event Handler.

    • In the Function Code section, select a Runtime, set Code Upload Method to Sample Code, and select oss-trigger.

    • In the Trigger Configurations section, set Trigger Type to Object Storage Service (OSS), Bucket Name to osstest-20, File Prefix to img, and File Suffix to png. For Triggering Events, select oss:ObjectCreated:PutObject and oss:ObjectCreated:PostObject, and set Role Name to AliyunOSSEventNotificationRole.

      When you create a function for the first time, you must grant the event source permission to access Function Compute as prompted. For more information, see Grant an event source permissions to access Function Compute.

      For more information about OSS trigger events, see OSS trigger overview.

  2. Use the function to call the enhanced image moderation service of Content Moderation.

    After the function is created, you are redirected to the function code page. On the editor page, you can edit the sample code to call Content Moderation. For more information about the parameters, see Synchronous detection API for the enhanced image moderation service V2.0.

    For more information about how to edit function code, see Code development overview.

    The following code provides a Python example for the enhanced image moderation service. Before you run the sample code, add your AccessKey ID and AccessKey secret to the config.ini configuration file.

    # -*- coding: utf-8 -*-
    import json, uuid
    import configparser
    from aliyunsdkcore.client import AcsClient
    from aliyunsdkcore.request import CommonRequest
    def handler(event, context):
        config = configparser.ConfigParser()
        config.read("config.ini", encoding="GB18030")
        # The configuration file where the AccessKey ID and AccessKey secret are stored.
        evt = json.loads(event)
        evt = evt['events'][0]
        bucket_name = evt['oss']['bucket']['name']
        object_name = evt['oss']['object']['key']
        
        # A public endpoint. The object must have public-read permissions. Data transfer costs over the Internet are incurred.
        objecturl = "https://" + bucket_name + ".oss-cn-hangzhou.aliyuncs.com/" + object_name
        client = AcsClient(config.get("config", "accessKeyId"), config.get("config", "accessSecret"), 'cn-shanghai')
        request = CommonRequest()
        request.set_read_timeout(6000)  # The read timeout period.
        request.set_connect_timeout(3000)  # The connection timeout period.
        request.set_accept_format('json')
        request.set_method('POST')
        request.set_protocol_type('https')  # https | http
        request.set_domain('green-cip.cn-shanghai.aliyuncs.com')
        request.set_version('2022-03-02')
        request.set_action_name('ImageModeration')
        
        request.add_query_param("Service", "baselineCheck")
        request.add_query_param("ServiceParameters",
                                {
                                    'imageUrl': objecturl,
                                    'dataId': str(uuid.uuid4())
                                })
        
        response = client.do_action_with_exception(request)
        print(str(response, encoding='utf-8'))
        
        response_json = json.loads(response)
        if response_json.get("Code") == 200:
            for item in response_json.get("Data", {}).get("Result", []):
                if item["Label"] == "nonLabel":
                    print("nonLabel No label is hit")
                elif "pornographic" in item["Label"] and int(item["Confidence"]) > 80:
                    print("The image may contain pornographic content")
                elif "political" in item["Label"] and int(item["Confidence"]) > 80:
                    print("The image may contain politically sensitive content")
                elif "violent" in item["Label"] and int(item["Confidence"]) > 80:
                    print("The image may contain violent or terrorist content")
                elif "fraud" in item["Label"] and int(item["Confidence"]) > 80:
                    print("The image may contain risky content")
        else:
            print("Request failed. Error code:", response_json.get("Code"))

    For OSS images in the China (Shanghai) or China (Beijing) region, you can use an OSS internal endpoint to call the moderation service in the same region. Before you use this method, you must use your Alibaba Cloud account to grant permissions on the Cloud Resource Access Authorization page. For more information about the parameters, see Synchronous detection API for the enhanced image moderation service V2.0.

    # -*- coding: utf-8 -*-
    import json, uuid
    import configparser
    from aliyunsdkcore.client import AcsClient
    from aliyunsdkcore.request import CommonRequest
    def handler(event, context):
        config = configparser.ConfigParser()
        config.read("config.ini", encoding="GB18030")
        # The configuration file where the AccessKey ID and AccessKey secret are stored.
        evt = json.loads(event)
        evt = evt['events'][0]
        bucket_name = evt['oss']['bucket']['name']
        object_name = evt['oss']['object']['key']
    
        client = AcsClient(config.get("config", "accessKeyId"), config.get("config", "accessSecret"), 'cn-shanghai')
        request = CommonRequest()
        request.set_read_timeout(6000)  # The read timeout period.
        request.set_connect_timeout(3000)  # The connection timeout period.
        request.set_accept_format('json')
        request.set_method('POST')
        request.set_protocol_type('https')  # https | http
        request.set_domain('green-cip.cn-shanghai.aliyuncs.com')
        request.set_version('2022-03-02')
        request.set_action_name('ImageModeration')
        
        request.add_query_param("Service", "baselineCheck")
        request.add_query_param("ServiceParameters",
                                {
                                    'ossRegionId': 'cn-shanghai',
                                    'ossBucketName': bucket_name,
                                    'ossObjectName': object_name,
                                    'dataId': str(uuid.uuid4())
                                })
        
        response = client.do_action_with_exception(request)
        print(str(response, encoding='utf-8'))
        
        response_json = json.loads(response)
        if response_json.get("Code") == 200:
            for item in response_json.get("Data", {}).get("Result", []):
                if item["Label"] == "nonLabel":
                    print("nonLabel No label is hit")
                elif "pornographic" in item["Label"] and int(item["Confidence"]) > 80:
                    print("The image may contain pornographic content")
                elif "political" in item["Label"] and int(item["Confidence"]) > 80:
                    print("The image may contain politically sensitive content")
                elif "violent" in item["Label"] and int(item["Confidence"]) > 80:
                    print("The image may contain violent or terrorist content")
                elif "fraud" in item["Label"] and int(item["Confidence"]) > 80:
                    print("The image may contain risky content")
        else:
            print("Request failed. Error code:", response_json.get("Code"))
  3. Perform incremental image detection.

    When you use the OSS PostObject or PutObject operation to upload an image file, you trigger the function. After the detection is complete, you can view the detection results in Invocation Logs. For more information, see View invocation logs.

    To further process OSS files, such as freezing or deleting them, see Manage files.