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来指向图片,你也可以修改代码来接受本地文件路径

相关推荐
小糯米601几秒前
JavaScript表达式与运算符
开发语言·javascript·ecmascript
北极星日淘3 分钟前
煤炉自动代拍功能开发 | Python 异步任务实现批量下单
开发语言·python·自动化
影寂ldy17 分钟前
C# 事件完整学习笔记(发布订阅 + 自定义事件 + 内置 EventHandler)
笔记·学习·c#
体验家23 分钟前
体验家 XMPlus 网页端问卷 SDK 技术解析:用几行 JavaScript 实现精准场景触发与防打扰机制
开发语言·前端·javascript
二十七剑33 分钟前
LangGraph 源码深度解析:Node 节点 Protocol 与 StateNodeSpec 核心机制
开发语言·python
AC赳赳老秦35 分钟前
OpenClaw + 云数据库运维:自动备份、扩容、迁移 RDS/MySQL 云数据库
运维·开发语言·数据库·人工智能·python·mysql·openclaw
醉城夜风~35 分钟前
类和对象III
开发语言·c++
冷小鱼37 分钟前
高级研发编码习惯:从规范到艺术,再到AI+时代的人机协同
java·开发语言·python·编码习惯
fox_lht1 小时前
15.4.循环和迭代器的性能比较
开发语言·后端·学习·rust
小鹿软件办公1 小时前
倒计时开启:Chromium 宣布几周内将全面切断 MV2 扩展支持
开发语言·javascript·ublock origin