您的位置:首页 > 理论基础

Azure 认知服务 (5) 计算机视觉API - 使用C#代码实现读取图片中的文字(OCR)功能

2017-08-02 15:34 1111 查看
  《Windows Azure Platform 系列文章目录

  在笔者之前的文章:Azure 认知服务 (4) 计算机视觉API - 读取图片中的文字 (OCR)

  介绍了使用用户界面,在海外的Windows Azure认知服务的读取图片功能。

  在本章,笔者会介绍如何通过国内由世纪互联运维的Azure China,使用C#代码,实现实现读取图片中的文字(OCR)功能。  

  

  我们需要准备:

  1.Azure China账户

  2.计算机视觉API的API Key

  3.分析图片的URL: https://leizhangstorage.blob.core.chinacloudapi.cn/azureblog/ocr.jpg

  我们现在开始正文

  1.我们可以访问:https://dev.cognitive.azure.cn/docs/services/56f91f2d778daf23d8ec6739/operations/56f91f2e778daf14a499e1fc

  可以看到最下面提供不同的开发语言Code Sample

  


  2.我们复制出C# Code,这是一个Windows Console

  根据注释的内容,修改变量

  (1) API Key

  (2) JPG图片URL

  代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http.Headers;
using System.Web;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MakeRequest();
Console.WriteLine("Hit ENTER to exit...");
Console.ReadLine();
}

static async void MakeRequest()
{
var client = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);

// Request headers
// 这里输入API Key
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "{subscription key}");

// Request parameters
queryString["language"] = "unk";
queryString["detectOrientation "] = "true";
var uri = "https://api.cognitive.azure.cn/vision/v1.0/ocr?" + queryString;

HttpResponseMessage response;

// 这里输入使用的jpg图片路径
string s = @"{""url"":" + @"""https://leizhangstorage.blob.core.chinacloudapi.cn/azureblog/ocr.jpg""}";

// Request body
byte[] byteData = Encoding.UTF8.GetBytes(s);

using (var content = new ByteArrayContent(byteData))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
response = await client.PostAsync(uri, content);

var contents = await response.Content.ReadAsStringAsync();
}
}
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: