Azure 认知服务 {body} 示例

Azure Cognitive services {body} example

我一直在使用 Azure 认知服务,并一直在使用提供的示例代码来分析表单。我不确定使用什么代码来填充代码的 {body} 部分。谁能分享我应该在 c# 中使用的代码示例?我正在使用新的表单识别器 API,但是代码与计算机视觉 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} 的示例代码

下面是Python 运行 表单识别器认知服务的示例代码,其中我通过 SAS url:

########### 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。希望对你有帮助。

参考

https://review.docs.microsoft.com/en-us/azure/cognitive-services/form-recognizer/quickstarts/python-train-extract?branch=release-build-cogserv-forms-recognizer

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);

            }

        }