如何使用微软人脸API(Azure Recognition)-VS2017 C#

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

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值