英文:
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
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);
- 如果你想使用图像的字节数组,那么根据这个MSDOC,我认为
content-type
头应该是application/octet-stream
。
英文:
- According to this MSDOCS the api needs a Json object in the following form:
{
"url":""
}
-
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 = "<URL OF YOUR IMAGE>";
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, "application/json");
Now all you have to do is call the Api:
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "<YOURKEY>");
var response = client.PostAsync(url, data);
Console.WriteLine(response.Result.StatusCode);
Console.WriteLine(response.Result);
- If you want to use the byte array of image, then I think the
content-type
header should beapplication/octet-stream
according to this MSDOC
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论