在执行Azure视觉API时遇到问题。

huangapple go评论57阅读模式
英文:

facing issues in executing Azure vision api

问题

你好,我会为您提供翻译的代码部分:

// 我的主要函数 fi.fullfile 是我的上传文档的路径
AzureAnalyzeRequest(System.IO.File.ReadAllBytes(fi.FullName));

// 分析函数
static async void AzureAnalyzeRequest(byte[] byteData)
{
    var client = new HttpClient();
    var queryString = HttpUtility.ParseQueryString(string.Empty);

    // 请求头部
    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "MyKey");

    // 请求参数
    queryString["language"] = "en";
    queryString["pages"] = "1,2";
    var uri = "https://url-ocr.cognitiveservices.azure.com/vision/v3.2/read/analyze?" + queryString;
    HttpResponseMessage response;
    using (var content = new ByteArrayContent(byteData))
    {
        content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        response = await client.PostAsync(uri, content);
    }
}

请注意,这只是代码的翻译部分,不包括其他内容。如果您有其他问题或需要进一步的帮助,请随时提问。

英文:

Hello All I am using Azure's vision analyze api to extract text from my documents,

here is the example code for your reference

//My main function fi.fullfile is the path of my uploaded document

AzureAnalyzeRequest(System.IO.File.ReadAllBytes(fi.FullName));

analyze function

static async void AzureAnalyzeRequest(byte[] byteData)
{
    var client = new HttpClient();
    var queryString = HttpUtility.ParseQueryString(string.Empty);

    // Request headers
    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "MyKey");

    // Request parameters
    queryString["language"] = "en";
    queryString["pages"] = "1,2";
    var uri = "https://url-ocr.cognitiveservices.azure.com/vision/v3.2/read/analyze?" + queryString;
    HttpResponseMessage response;
    using (var content = new ByteArrayContent(byteData))
    {
        content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        response = await client.PostAsync(uri, content);
    }
}

when the above function executed I am getting the error of 400 bad request

but when I tested my api on the below URL

Azure Vision api

it worked fine.

what I am doing wrong here?

答案1

得分: 1

  • 根据这个MSDOCS ,该API需要一个如下形式的JSON对象:
{
	"url": ""
}
  • 我认为你正在传递一个字节数组,你需要一个包含要处理的图像URL的JSON对象。

  • 所以在这里我创建了一个名为Poco的类,该类将托管URL变量。

public class Poco
{
    public string url { get; set; }
}
  • 然后我初始化了这个类并传递了URL,然后将该对象转换为JSON对象。
Poco p = new Poco();
p.url = "<你的图像URL>";
string json = JsonConvert.SerializeObject(p);

// 在这里,我们将JSON字符串转换为StringContent,然后可以传递给HttpClient

StringContent data = new StringContent(json, Encoding.UTF8, "application/json");

现在你只需要调用API:

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "<你的密钥>");

var response = client.PostAsync(url, data);

Console.WriteLine(response.Result.StatusCode);
Console.WriteLine(response.Result);

在执行Azure视觉API时遇到问题。

  • 如果你想使用图像的字节数组,那么根据这个MSDOC,我认为content-type头应该是application/octet-stream
英文:
  • According to this MSDOCS the api needs a Json object in the following form:
{
	&quot;url&quot;:&quot;&quot;
}
  • I think you are passing a byte array, you need a Json object which will contain a URL of the image you want to process.

  • So here I have created a class called Poco which will host the URL variable.

 public class Poco
    {
        public string url { get; set; }
    }
  • Then I initialized the class and passed the URL then convert that object into a Json object.
 Poco p = new Poco();
 p.url = &quot;&lt;URL OF YOUR IMAGE&gt;&quot;;
 string json = JsonConvert.SerializeObject(p);
   
  // Here we are converting the json string to stringcontent which we can pass to httpclient  
       
 StringContent data = new StringContent(json, Encoding.UTF8, &quot;application/json&quot;);

Now all you have to do is call the Api:

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add(&quot;Ocp-Apim-Subscription-Key&quot;, &quot;&lt;YOURKEY&gt;&quot;);
           
var response = client.PostAsync(url, data);

Console.WriteLine(response.Result.StatusCode);
Console.WriteLine(response.Result);

在执行Azure视觉API时遇到问题。

  • If you want to use the byte array of image, then I think the content-type header should be application/octet-stream according to this MSDOC

huangapple
  • 本文由 发表于 2023年1月9日 17:51:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定