【发布时间】: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