This topic describes how to use the .NET SDK's image OCR interface to recognize text in images.
Feature description
In addition to recognizing text in common images, general OCR can also recognize text on structured cards. For more information about the parameters, see Image OCR detection API.
The SDK supports only image URLs. It does not support local files or binary data.
Supported URL types: Public HTTP or HTTPS URLs with a maximum length of 2,048 characters.
Prerequisites
The dependencies for Content Moderation SDK for .NET are installed. For more information about how to install the dependencies, see Installation.
You must use the required .NET version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.
Submit a synchronous image detection task
Operation | Description | Supported region |
ImageSyncScanRequest | Submits synchronous OCR tasks with the scenes parameter set to ocr to recognize text in images. |
|
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 a Resource Access Management (RAM) user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Obtain the AccessKey secret of a RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.GetProfile(
"cn-shanghai",
"Obtain the AccessKey ID of a RAM user from an environment variable",
"Obtain the AccessKey secret of a RAM user from an environment variable");
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", "The ID of the data to detect");
//task1.Add("url", "The URL of the image to detect");
task1.Add("url", "http://example.com/xx.jpg");
// Example: Recognize the front side of an ID card.
Dictionary<string, object> cardExtras = new Dictionary<string, object>();
cardExtras.Add("card", "id-card-front");
Dictionary<string, object> httpBody = new Dictionary<string, object>();
// scenes: The detection 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);
}
}
}
}