【问题标题】:Azure Face Detect API - An Error occurred while sending the requestAzure 人脸检测 API - 发送请求时出错
【发布时间】:2020-11-17 10:44:39
【问题描述】:

我正在使用 Azure 认知服务在我的 ASP.NET MVC Web 应用程序中检测图像中的人脸。但是它的“DetectWithStreamAsync”总是抛出“发送请求时发生错误”。我从这个项目https://github.com/Azure-Samples/Cognitive-Face-CSharp-sample 中获取了一个参考,这是 WPF 应用程序。但是,当我使用相同的代码、订阅密钥和端点 URL 时,此 WPF 应用程序不会引发相同的异常。仅当 MVC 应用程序发出请求时才会引发异常。

编辑:我也尝试过使用 Microsoft.ProjectOxford.Face 的“DetectAsync”方法,但遇到了同样的异常。

我的代码如下:

using Microsoft.Azure.CognitiveServices.Vision.Face;
using Microsoft.Azure.CognitiveServices.Vision.Face.Models;

namespace MyApplication.FaceRecognition
{
    public class FaceRecognitionService
    {
        private static string key =ConfigurationManager.AppSettings["FaceAPIKey"];
        private static string endPoint =ConfigurationManager.AppSettings["FaceAPIEndPoint"];  

        private readonly IFaceClient faceClient = new FaceClient(
          new ApiKeyServiceClientCredentials(key),
          new System.Net.Http.DelegatingHandler[] { });


        public FaceRecognitionService()
        {  
            faceClient.Endpoint = endPoint;
        }

        public async Task<Guid> DetectFace(string imgPath)
        {
            Guid faceID = Guid.Empty;

            try
            {
                using (Stream faceimagestream = File.OpenRead(imgPath))
                {
                    var faces = await faceClient.Face.DetectWithStreamAsync(faceimagestream,true,false);
                    if (faces.Count > 0)
                        faceID = faces[0].FaceId?? default;
                }

                return faceID;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        
    }
    
}


【问题讨论】:

    标签: c# asp.net-mvc face-detection azure-cognitive-services face-api


    【解决方案1】:

    @Sanjay 感谢您的解决方案。 4.6版本也足够了。
    我像下面这样编辑了 web.config,它可以工作了!

    <system.web>
      <compilation targetFramework="4.6" />
      <httpRuntime targetFramework="4.6" />
    </system.web>
    

    【讨论】:

      【解决方案2】:

      检查内部异常后,我得到“底层连接已关闭,发送时发生意外错误”。这是由于安全协议的问题。原来我的 httpRuntime targetFramework 是 4.5,将其更改为 4.7 或启用 TLS 1.2 解决了上述错误。

      【讨论】:

        猜你喜欢
        • 2018-05-01
        • 2018-12-31
        • 1970-01-01
        • 1970-01-01
        • 2018-02-18
        • 1970-01-01
        • 2020-12-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多