我正在尝试 azure 图像分析 API。我有下面的代码。我认为作为面部分析的一部分,我会得到估计的年龄。我发送给它的所有内容似乎都返回 0 年龄。这是我出错的地方吗?对此有什么想法吗?短暂性脑缺血发作。
ComputerVisionClient client =
new ComputerVisionClient(new ApiKeyServiceClientCredentials(azureImageKey)) { Endpoint = endPoint };
List<VisualFeatureTypes?> features = new List<VisualFeatureTypes?>()
{
VisualFeatureTypes.Tags,
VisualFeatureTypes.ImageType,
VisualFeatureTypes.Faces,
VisualFeatureTypes.Adult,
VisualFeatureTypes.Brands,
VisualFeatureTypes.Description
};
ImageAnalysis results = await client.AnalyzeImageAsync(img, visualFeatures: features);
我尝试使用以下代码和代码中的流来从 Azure Blob 存储中的图像 URL 获取人的年龄。
代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
class Program
{
static async Task Main(string[] args)
{
string azureImageKey = "<ComputerVision_key>";
string endPoint = "<ComputerVision_endpoint>";
ComputerVisionClient client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(azureImageKey)) { Endpoint = endPoint };
string blobImageUrl = "https://<storage_name>.blob.core.windows.net/<container_name>/<image_name>.jpg"; //make sure the blob have the public read access
Stream imageStream = await DownloadImageFromBlobUrlAsync(blobImageUrl);
List<VisualFeatureTypes?> features = new List<VisualFeatureTypes?>()
{
VisualFeatureTypes.Tags,
VisualFeatureTypes.ImageType,
VisualFeatureTypes.Faces,
VisualFeatureTypes.Adult,
VisualFeatureTypes.Brands,
VisualFeatureTypes.Description
};
try
{
ImageAnalysis results = await client.AnalyzeImageInStreamAsync(imageStream, visualFeatures: features);
Console.WriteLine("Image Analysis Results:");
Console.WriteLine("\nTags:");
foreach (var tag in results.Tags)
{
Console.WriteLine($"{tag.Name} ({tag.Confidence * 100}%)");
}
Console.WriteLine("\nDetected Faces:");
foreach (var face in results.Faces)
{
Console.WriteLine($"Age: {face.Age}, Gender: {face.Gender}");
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
private static async Task<Stream> DownloadImageFromBlobUrlAsync(string blobImageUrl)
{
using (HttpClient httpClient = new HttpClient())
{
HttpResponseMessage response = await httpClient.GetAsync(blobImageUrl);
if (response.IsSuccessStatusCode)
{
Stream imageStream = await response.Content.ReadAsStreamAsync();
return imageStream;
}
else
{
throw new Exception($"Failed to download image from the storage blob URL. Status code: {response.StatusCode}");
}
}
}
}
输出:
程序运行成功,提供了 blob URL 中人员的准确年龄和性别,如下所示。