すべてのプロダクト
Search
ドキュメントセンター

Content Moderation:OCR

最終更新日:Jan 08, 2025

このトピックでは、.NET 用 Content Moderation SDK を使用して光学式文字認識 (OCR) を実行する方法について説明します。この方法で、画像内のテキストを認識できます。

前提条件

.NET 用 Content Moderation SDK の依存関係がインストールされていること。依存関係のインストール方法の詳細については、インストールをご参照ください。

説明

依存関係をインストールするには、インストール トピックに記載されている必要な .NET バージョンを使用する必要があります。そうしないと、後続の操作呼び出しが失敗します。

同期 OCR タスクの送信

操作

説明

サポートされているリージョン

ImageSyncScanRequest

scenes パラメーターを ocr に設定した同期 OCR タスクを送信して、画像内のテキストを認識します。

  • cn-shanghai

  • cn-beijing

  • cn-shenzhen

  • ap-southeast-1

サンプルコード

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)
        {
            // 環境変数から値を取得する一般的な方法
            // RAM ユーザーの AccessKey ID を取得します: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
            // RAM ユーザーの AccessKey シークレットを取得します: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "RAM ユーザーの AccessKey ID は環境変数から取得することをお勧めします",
                    "RAM ユーザーの AccessKey シークレットは環境変数から取得することをお勧めします");
            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");
            //task1.Add("url", "モデレート対象の画像の URL");
            task1.Add("url", "http://example.com/xx.jpg");

            // 例: ID カードの表面の画像をモデレートする場合は、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: モデレーションシナリオ
            httpBody.Add("scenes", new List<string> { "ocr" });
            httpBody.Add("bizType", "ビジネスシナリオ");
            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("リクエストが失敗しました。status:{0}", response.HttpResponse.Status);
                }
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("エラー情報で失敗しました: {0}", ex.Message);
            }
        }
    }
}