我一直在使用Azure Cognitive服务,并一直在使用提供的示例代码来分析表单。我不确定要使用什么代码填充代码的{body}部分。谁能分享我应该在c#中使用的代码示例?我正在使用新的Form Recogniser API,但是代码与Computer Vision API相同。
我已经能够成功地使用Curl,但无法使其在C#中工作
curl -X POST "https://westus2.api.cognitive.microsoft.com/formrecognizer/v1.0-preview/custom/models/ff956100-613b-40d3-ae74-58e4fcc76384/analyze" -H "Content-Type: multipart/form-data" -F "form=@\"https://recognizermodelxero.blob.core.windows.net/testinvoices/00046266.pdf\";type=application/pdf" -H "Ocp-Apim-Subscription-Key: 1234567890"
这是我尝试过的 ...
byte[] byteData = Encoding.UTF8.GetBytes(System.IO.File.ReadAllText("test.pdf"));
...
namespace CSHttpClientSample
{
static class Program
{
static void Main()
{
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
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "{subscription key}");
// Request parameters
queryString["keys"] = "{array}";
var uri = "https://westus2.api.cognitive.microsoft.com/formrecognizer/v1.0-preview/custom/models/{id}/analyze?" + queryString;
HttpResponseMessage response;
// Request body
byte[] byteData = Encoding.UTF8.GetBytes("{body}");
using (var content = new ByteArrayContent(byteData))
{
content.Headers.ContentType = new MediaTypeHeaderValue("< your content type, i.e. application/json >");
response = await client.PostAsync(uri, content);
}
}
}
}
我正在寻找一些示例代码,以代替{body}
答案 0 :(得分:0)
下面是运行表单识别器认知服务的Python示例代码,其中我通过SAS网址传递给身体标签:
########### Python Form Recognizer Train #############
from requests import post as http_post
# Endpoint URL
base_url = r"<Endpoint>" + "/formrecognizer/v1.0-preview/custom"
source = r"<SAS URL>"
headers = {
# Request headers
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': '<Subscription Key>',
}
url = base_url + "/train"
body = {"source": source}
try:
resp = http_post(url = url, json = body, headers = headers)
print("Response status code: %d" % resp.status_code)
print("Response body: %s" % resp.json())
except Exception as e:
print(str(e))
为回答您的问题,您可以在{body}参数中通过SAS。希望对您有所帮助。
参考
C#:
请利用以下方法获取{body}参数的字节数组
static byte[] GetImageAsByteArray(string imageFilePath)
{
// Open a read-only file stream for the specified file.
using (FileStream fileStream =
new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))
{
// Read the file's contents into a byte array.
BinaryReader binaryReader = new BinaryReader(fileStream);
return binaryReader.ReadBytes((int)fileStream.Length);
}
}