簡體   English   中英

Microsoft認知服務Recognize Text API

[英]Microsoft cognitive service Recognize Text api

我通過傳遞我從手機拍攝的圖像來調用Microsoft識別文本api,沒有發生錯誤,但是每次api將返回空字符串,因為結果與發布的圖像無關緊要。 我用Microsoft OCR API嘗試了這些圖像,它返回了我的結果,有人可以幫忙嗎?

我通過傳遞我從手機上拍攝的圖像來呼叫Microsoft識別文本api,沒有錯誤發生,但是每次api將返回空字符串,因為結果與我發布的圖像無關。

Recognize Text API的文檔中 ,我們可以找到:

該服務已接受該請求,稍后將開始處理。

它將立即返回“已接受”,並包含“ Operation-Location”標頭。 客戶端應使用此標頭中指定的URL進一步查詢操作狀態。

我懷疑您在請求識別文本后直接從響應中獲取/提取了內容,因此內容將為string.Empty。

為了獲得文本操作結果的識別 ,您需要使用響應標頭“ Operation-Location ”中指定的URL發出進一步的請求。

在下面的測試代碼中,我們可以發現內容確實為空。

測試代碼:

var client = new HttpClient();

var queryString = HttpUtility.ParseQueryString(string.Empty);

client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "{Subscription_Key_here}");

queryString["mode"] = "Printed";

var uri = "https://{region}.api.cognitive.microsoft.com/vision/v2.0/recognizeText?" + queryString;

HttpResponseMessage response;

var imagePath = @"D:\xxx\xxx\xxx\testcontent.PNG";

Stream imageStream = File.OpenRead(imagePath);

BinaryReader binaryReader = new BinaryReader(imageStream);

byte[] byteData = binaryReader.ReadBytes((int)imageStream.Length);

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

    if (response.IsSuccessStatusCode)
    {
        var contentString = await response.Content.ReadAsStringAsync();

        var operation_location = response.Headers.GetValues("Operation-Location").FirstOrDefault();

        Console.WriteLine($"Response content is empty({contentString == string.Empty}).\n\rYou should further query the operation status using the URL ({operation_location}) specified in response header.");
    }
}

測試結果:

在此處輸入圖片說明

注意:識別文本API當前處於預覽狀態,僅適用於英文文本。 如您所述,Computer Vision中的OCR技術還可以幫助檢測圖像中的文本內容,並且OCR當前支持25種語言,有時我們可以使用OCR作為替代解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM