All Products
Search
Document Center

Content Moderation:OCR

Last Updated:Jul 31, 2023

This topic describes how to use Content Moderation SDK for .NET to perform optical character recognition (OCR). This way, you can recognize text in images.

Prerequisites

The dependencies for Content Moderation SDK for .NET are installed. For more information about how to install the dependencies, see Installation.

Note

You must use the required .NET version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.

Submit synchronous OCR tasks

Operation

Description

Supported region

ImageSyncScanRequest

Submits synchronous OCR tasks with the scenes parameter set to ocr to recognize text in images.

  • cn-shanghai

  • cn-beijing

  • cn-shenzhen

  • ap-southeast-1

Sample code

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;

namespace csharp_sdk_sample
{
    class Program
    {
        static void Main(string[] args)
        {
            /**
             * Common ways to obtain environment variables:
             *     Obtain the AccessKey ID of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     Obtain the AccessKey secret of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "We recommend that you obtain the AccessKey ID of your RAM user from environment variables",
                    "We recommend that you obtain the AccessKey secret of your RAM user from environment variables");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            ImageSyncScanRequest request = new ImageSyncScanRequest();
            request.AcceptFormat = FormatType.JSON;
            request.ContentType = FormatType.JSON;
            request.Method = MethodType.POST;
            request.Encoding = "UTF-8";

            Dictionary<string, object> task1 = new Dictionary<string, object>();
            task1.Add("dataId", "ID of the image to be moderated");
            //task1.Add("url", "URL of the image to be moderated");
            task1.Add("url", "http://example.com/xx.jpg");

            // Example: If you moderate the image of the front side of an ID card, specify id-card-front. 
            Dictionary<string, object> cardExtras = new Dictionary<string, object>();
            cardExtras.Add("card", "id-card-front");

            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            // scenes: the moderation scenarios. 
            httpBody.Add("scenes", new List<string> { "ocr" });
            httpBody.Add("bizType", "Business scenario");
            httpBody.Add("extras", cardExtras);
            httpBody.Add("tasks", new List<Dictionary<string, object>> { task1 });

            request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON);
            try
            {
                ImageSyncScanResponse response = client.GetAcsResponse(request);
                if (response.HttpResponse.Status != 200)
                {
                    Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status);
                }
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}