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

相关推荐
wuyk5559 分钟前
【Socket 进阶之路】第 9 章 Linux 网络服务量产稳定性优化|心跳保活、TIME_WAIT、SO_LINGER、内存池、断线重连、完整异常防护框架
linux·服务器·开发语言·网络·物联网
邪修king18 分钟前
Re:Linux 系统篇(三十四):进程间通信开篇Chapter1 —— 匿名管道 pipe 深度拆解与代码实战
android·linux·运维·开发语言
孙启超41 分钟前
【AI开发之Rust】第 13 课:async/await 与 tokio 异步运行时
开发语言·后端·rust
Am-Chestnuts1 小时前
DeepSeek内容转Word有哪几种方法?用DS随心转对比6条主流路径
开发语言·人工智能
大黄说说1 小时前
App 启动速度优化:冷启动从 3s 降到 800ms 完整实操
开发语言
Lazionr1 小时前
多态:从多种形态到运行时绑定
开发语言·c++
大黄说说1 小时前
iOS 列表滑动卡顿:UITableView / UICollectionView 深度优化实战
开发语言
Polevne2 小时前
C# SFTP客户端实现文件传输
c#·ssh