在本快速入门中,你将使用适用于 C# 的计算机视觉客户端库分析本地和远程图像来提取视觉特征。 如果你愿意,可以从 GitHub 上的认知服务 Csharp 视觉存储库下载本指南中的代码作为完整的示例应用。
先决条件
- 计算机视觉订阅密钥。 你可以按照创建认知服务帐户中的说明订阅计算机视觉并获取密钥。
- 任何版本的 Visual Studio 2015 或 2017。
- Microsoft.Azure.CognitiveServices.Vision.ComputerVision 客户端库 NuGet 包。 不需下载程序包。 下面提供了安装说明。
创建和运行示例应用程序
若要运行此示例,请执行以下步骤:
在 Visual Studio 中创建一个新的 Visual C# 控制台应用。
安装计算机视觉客户端库 NuGet 包。
- 在菜单上,单击“工具”,然后依次选择“NuGet 包管理器”、“管理解决方案的 NuGet 包” 。
- 单击“浏览” 选项卡,在“搜索” 框中键入“Microsoft.Azure.CognitiveServices.Vision.ComputerVision”。
- 选择显示的 Microsoft.Azure.CognitiveServices.Vision.ComputerVision,单击项目名称旁边的复选框,然后单击“安装” 。
将 Program.cs 的内容替换为以下代码。
AnalyzeImageAsync
和AnalyzeImageInStreamAsync
方法分别为远程图像和本地图像包装分析图像 REST API。using Microsoft.Azure.CognitiveServices.Vision.ComputerVision; using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models; using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; namespace ImageAnalyze { class Program { // subscriptionKey = "0123456789abcdef0123456789ABCDEF" private const string subscriptionKey = "<SubscriptionKey>"; // localImagePath = @"C:\Documents\LocalImage.jpg" private const string localImagePath = @"<LocalImage>"; private const string remoteImageUrl = "https://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg"; // Specify the features to return private static readonly List<VisualFeatureTypes> features = new List<VisualFeatureTypes>() { VisualFeatureTypes.Categories, VisualFeatureTypes.Description, VisualFeatureTypes.Faces, VisualFeatureTypes.ImageType, VisualFeatureTypes.Tags }; static void Main(string[] args) { ComputerVisionClient computerVision = new ComputerVisionClient( new ApiKeyServiceClientCredentials(subscriptionKey), new System.Net.Http.DelegatingHandler[] { }); computerVision.Endpoint = "https://api.cognitive.azure.cn"; Console.WriteLine("Images being analyzed ..."); var t1 = AnalyzeRemoteAsync(computerVision, remoteImageUrl); var t2 = AnalyzeLocalAsync(computerVision, localImagePath); Task.WhenAll(t1, t2).Wait(5000); Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); } // Analyze a remote image private static async Task AnalyzeRemoteAsync( ComputerVisionClient computerVision, string imageUrl) { if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute)) { Console.WriteLine( "\nInvalid remoteImageUrl:\n{0} \n", imageUrl); return; } ImageAnalysis analysis = await computerVision.AnalyzeImageAsync(imageUrl, features); DisplayResults(analysis, imageUrl); } // Analyze a local image private static async Task AnalyzeLocalAsync( ComputerVisionClient computerVision, string imagePath) { if (!File.Exists(imagePath)) { Console.WriteLine( "\nUnable to open or read localImagePath:\n{0} \n", imagePath); return; } using (Stream imageStream = File.OpenRead(imagePath)) { ImageAnalysis analysis = await computerVision.AnalyzeImageInStreamAsync( imageStream, features); DisplayResults(analysis, imagePath); } } // Display the most relevant caption for the image private static void DisplayResults(ImageAnalysis analysis, string imageUri) { Console.WriteLine(imageUri); if (analysis.Description.Captions.Count != 0) { Console.WriteLine(analysis.Description.Captions[0].Text + "\n"); } else { Console.WriteLine("No description generated."); } } } }
将
<Subscription Key>
替换为有效订阅密钥。如有必要,将
computerVision.Endpoint
更改为与订阅密钥关联的 Azure 区域。将
<LocalImage>
替换为本地图像的路径和文件名。(可选)将
remoteImageUrl
设置为另一个图像 URL。运行该程序。
检查响应
成功的响应会显示每个图像的最相关的描述文字。 可以更改 DisplayResults
方法以输出不同的图像数据。 请参阅 AnalyzeLocalAsync 方法,了解详细信息。
https://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg
a large waterfall over a rocky cliff
后续步骤
探索用于分析图像、检测名人和地标、创建缩略图以及提取印刷体文本和手写文本的计算机视觉 API。