1

我一直在使用 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} 的示例代码

4

1 回答 1

0

下面是运行表单识别器认知服务的 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);

            }

        }
于 2019-05-13T06:20:36.890 回答