1.申请API密钥
https://azure.microsoft.com/en-us/try/cognitive-services/my-apis/?api=face-api
需要注册Azure账号,可以申请免费试用,试用期是7天。申请完了就能看到这个界面,Your APIs显示有Face API。

2.新建VS2017的控制台应用项目
微软的使用教程里说明了需要使用C#的控制台应用(.NET Framework),新建后项目列表里应当自动会有一个program.cs。
我在我的VS2017找了半天也只看到Visual C++的控制台应用程序,也在网上搜了有人说vs2017更新了应该在“Windows桌面应用程序”里,也有人说在“工具-导入和导出设置”里修改即可,尝试都无果,真正的原因很简单,就是我当初在安装的时候没有选择装.NET桌面开发,需要补装一下就好了,也是折腾了很久。。。
那就打开VS Installer再装一下。

选择修改

再把“.NET桌面开发”给勾选上下载安装

再次打开VS2017新建C#项目就可以了

3.跟着快速入门指南成功调用人脸API
快速入门:使用人脸 REST API 和 C# 检测图像中的人脸:
https://docs.microsoft.com/zh-cn/azure/cognitive-services/face/QuickStarts/CSharp
在Program.cs填入的代码:
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace FaceDetection
{
class Program
{
// Replace <Subscription Key> with your valid subscription key.
const string subscriptionKey = "<这里输入获取的API密钥>";
// NOTE: You must use the same region in your REST call as you used to
// obtain your subscription keys. For example, if you obtained your
// subscription keys from westus, replace "westcentralus" in the URL
// below with "westus".
//
// Free trial subscription keys are generated in the "westus" region.
// If you use a free trial subscription key, you shouldn't need to change
// this region.
const string uriBase =
"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect";
static void Main(string[] args)
{
// Get the path and filename to process from the user.
Console.WriteLine("Detect faces:");
Console.Write(
"Enter the path to an image with faces that you wish to analyze: ");
string imageFilePath = Console.ReadLine();
if (File.Exists

本文介绍了如何在VS2017中使用C#调用微软人脸API。首先,需要在Azure官网申请API密钥并获取免费试用资格。接着,由于初始安装可能未包含.NET桌面开发组件,需要通过VS Installer进行补充安装。最后,遵循官方快速入门指南,在控制台应用中实现人脸检测功能,并成功运行代码。
最低0.47元/天 解锁文章
7675

被折叠的 条评论
为什么被折叠?



