使用请求正文从 Microsoft Computer Vision 调用人脸 API 是 "application/json"

Calling Face API from Microsoft Computer Vision with request body is "application/json"

我正在尝试从 Microsoft 网站调用 Face API,我在他们的网站上找到了示例代码。在他们的代码中,他们使用 "Detect API" 作为示例,但我想测试 "Create Face List" API。我发现我需要将 "Content type" 从 "application/octet-stream" 更改为 "application/json" 并填充 "Json field"。不幸的是,我是调用 API 方面的新手。

希望大家能帮上忙。

Here是示例代码的link。

here是"Create Face List"API的link。

using (ByteArrayContent content = new ByteArrayContent(byteData))
{
    // This example uses content type "application/octet-stream".
    // The other content types you can use are "application/json"
    // and "multipart/form-data".
    content.Headers.ContentType =
        new MediaTypeHeaderValue("application/octet-stream");

    // Execute the REST API call.
    response = await client.PostAsync(uri, content);

    // Get the JSON response.
    string contentString = await response.Content.ReadAsStringAsync();

    // Display the JSON response.
    Console.WriteLine("\nResponse:\n");
    Console.WriteLine(JsonPrettyPrint(contentString));
    Console.WriteLine("\nPress Enter to exit...");
}

阅读 documentation,我看到对于 ws "Create a FaceList" 你必须调用一个 Http Put 方法,内容类型为 application/json.

这里是这个服务的描述:

FaceList - Create Create an empty face list with user-specified faceListId, name and an optional userData. Up to 64 face lists are allowed in one subscription. Face list is a list of faces, up to 1,000 faces, and used by Face - Find Similar. After creation, user should use FaceList - Add Face to import the faces. Faces are stored on server until FaceList - Delete is called. Find Similar is used for scenario like finding celebrity-like faces, similar face filtering, or as a light way face identification. But if the actual use is to identify person, please use PersonGroup / LargePersonGroup and Face - Identify. Please consider LargeFaceList when the face number is large. It can support up to 1,000,000 faces.

HTTP 方法 放

var client = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);

// Request headers
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "{subscription key}");

var uri = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}?" + queryString;

HttpResponseMessage response;

// Request body
byte[] byteData = Encoding.UTF8.GetBytes("{\"name\": \"sample_list\",\"userData\": \"User-provided data attached to the face list.\"}");

using (var content = new ByteArrayContent(byteData))
{
    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    response = await client.PutAsync(uri, content);
    // Get the JSON response.
    string contentString = await response.Content.ReadAsStringAsync();

    // Display the JSON response.
    Console.WriteLine("\nResponse:\n");
    Console.WriteLine(JsonPrettyPrint(contentString));
    Console.WriteLine("\nPress Enter to exit...");
}

目前,位置基准 url 是那些:

  • 美国西部 - westus.api.cognitive.microsoft.com
  • 美国西部 2 - westus2.api.cognitive.microsoft.com
  • 美国东部 - eastus.api.cognitive.microsoft.com
  • 美国东部 2 - eastus2.api.cognitive.microsoft.com
  • 美国中西部 - westcentralus.api.cognitive.microsoft.com
  • 美国中南部 - southcentralus.api.cognitive.microsoft.com
  • 西欧 - westeurope.api.cognitive.microsoft.com
  • 北欧 - northeurope.api.cognitive.microsoft.com
  • 东南亚 - southeastasia.api.cognitive.microsoft.com
  • 东亚 - eastasia.api.cognitive.microsoft.com
  • 澳大利亚东部 - australiaeast.api.cognitive.microsoft.com
  • 巴西南部 - brazilsouth.api.cognitive.microsoft.com
  • 加拿大中部 - canadacentral.api.cognitive.microsoft.com
  • 印度中部 - centralindia.api.cognitive.microsoft.com
  • 英国南部 - uksouth.api.cognitive.microsoft.com
  • 日本东部 - japaneast.api.cognitive.microsoft.com
  • 美国中部 - centralus.api.cognitive.microsoft.com
  • 法国中部 - francecentral.api.cognitive.microsoft.com
  • 韩国中部 - koreacentral.api.cognitive.microsoft.com
  • 日本西部 - japanwest.api.cognitive.microsoft.com
  • 美国中北部 - 北部centralus.api.cognitive.microsoft.com

请求参数

faceListId:有效字符为小写字母或数字或'-'或'_',最大长度为64。

请求headers

Content-Type(可选):发送到 API.

的 body 的媒体类型

Ocp-Apim-Subscription-Key:提供对此 API 的访问的订阅密钥。在您的认知服务帐户中找到。

请求body

JSON 请求中的字段 body:

{
    "name": "sample_list",//Name of the created face list, maximum length is 128.
    "userData": "User-provided data attached to the face list." //Optional user defined data for the face list. Length should not exceed 16KB.
}

希望对你有所帮助