C# 中识别图片中有几个人

C# 中识别图片中有几个人

可以使用Microsoft的AI平台Cognitive Services的Computer Vision API。以下是一个简单的示例代码,展示了如何使用该API来识别图片中的人物数量:

1、首先,你需要在Azure门户上创建一个Computer Vision资源,并获取你的密钥和端点。

2、安装必要的NuGet包:Microsoft.Azure.CognitiveServices.Vision.ComputerVision

python 复制代码
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
using System;
using System.IO;
using System.Threading.Tasks;
 
namespace PeopleCounter
{
    class Program
    {
        static string subscriptionKey = "你的密钥";
        static string endpoint = "你的端点";
 
        static async Task Main(string[] args)
        {
            var computerVision = new ComputerVisionClient(new ApiKeyServiceClientCredentials(subscriptionKey))
            {
                Endpoint = endpoint
            };
 
            string imageUrl = "图片的URL";
            ImageAnalysis imageAnalysis = await computerVision.AnalyzeImageAsync(imageUrl, features: new[] { VisualFeature.Faces});
 
            Console.WriteLine("Detected faces: ");
            if (imageAnalysis.Faces != null)
            {
                Console.WriteLine(imageAnalysis.Faces.Count);
            }
            else
            {
                Console.WriteLine("No faces detected.");
            }
 
            Console.ReadKey();
        }
    }
}

确保替换subscriptionKey和endpoint变量值为你的Computer Vision资源的实际值。imageUrl变量应包含你想要分析的图片的URL。

这段代码会输出图片中检测到的人脸数量。如果没有检测到人脸,它会输出"No faces detected."。请注意,这个示例使用了一个URL来指向图片,你也可以修改代码来接受本地文件路径

相关推荐
Nebula_g14 小时前
Java哈希表入门详解(Hash)
java·开发语言·学习·算法·哈希算法·初学者
努力努力再努力wz14 小时前
【C++进阶系列】:万字详解unordered_set和unordered_map,带你手搓一个哈希表!(附模拟实现unordered_set和unordered_map的源码)
java·linux·开发语言·数据结构·数据库·c++·散列表
励志不掉头发的内向程序员14 小时前
【STL库】哈希表的原理 | 哈希表模拟实现
开发语言·c++·学习·散列表
万粉变现经纪人14 小时前
如何解决 pip install -r requirements.txt 私有仓库认证失败 401 Unauthorized 问题
开发语言·python·scrapy·flask·beautifulsoup·pandas·pip
量子炒饭大师15 小时前
收集飞花令碎片——C语言字符函数与字符串函数
c语言·开发语言
懂得节能嘛.15 小时前
【设计模式】Java规则树重构复杂业务逻辑
java·开发语言·设计模式
syt_biancheng15 小时前
Qt--命名,快捷键及坐标系
开发语言·qt
极地星光15 小时前
协程:深入协程机制与实现(进阶篇)
开发语言
进击的圆儿15 小时前
高并发内存池项目开发记录 - 02
开发语言·c++·实战·项目·内存池
xingxing_F16 小时前
Swift Publisher for Mac 版面设计和编辑工具
开发语言·macos·swift