1. 程式人生 > >Azure 認知服務 (3) 計算機視覺API - 分析圖像,使用C#代碼

Azure 認知服務 (3) 計算機視覺API - 分析圖像,使用C#代碼

iat query med ron oid sage pos png wait

  《Windows Azure Platform 系列文章目錄》

  在上一節中Azure 認知服務 (2) 計算機視覺API - 分析圖像,筆者介紹了如何使用API測試控制臺進行調試。

  本章將介紹如何使用C#代碼調用分析圖像功能。

  我們需要準備:

  1.Azure China賬戶

  2.計算機視覺API的API Key

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

  

  現在開始正文:

  1.我們可以訪問:https://dev.cognitive.azure.cn/docs/services/56f91f2d778daf23d8ec6739/operations/56f91f2e778daf14a499e1fa

  可以看到最下面提供不同的開發語言Code Sample:

  技術分享

  2.我們復制出C# Code,這是一個Windows Console

  根據註釋的內容,修改變量

  (1) API Key

  (2) JPG圖片URL

  代碼如下:

        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", "{API key}
"); // Request parameters // 這裏輸入visual Features queryString["visualFeatures"] = "Categories,Tags,Description,Faces,ImageType,Color,Adult"; queryString["details"] = ""; queryString["language"] = "en"; var uri = "https://api.cognitive.azure.cn/vision/v1.0/analyze?" + queryString; HttpResponseMessage response; // 這裏輸入使用的jpg圖片路徑 string s = @"{""url"":" + @"""https://leizhangstorage.blob.core.chinacloudapi.cn/azureblog/analyzeimagesample.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(); } }

Azure 認知服務 (3) 計算機視覺API - 分析圖像,使用C#代碼