標籤:dev tags 控制 service json 圖片路徑 hive 一個 title
《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#代碼