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

相关推荐
止水编程 water_proof38 分钟前
JavaScript基础
开发语言·javascript·ecmascript
小小测试开发1 小时前
JMeter JSR223预处理程序高级用法:解锁自动化测试的灵活性上限
开发语言·jmeter·groovy
星释1 小时前
Rust 练习册 22:映射函数与泛型的威力
开发语言·rust·机器人
云泽8082 小时前
C++ List 容器详解:迭代器失效、排序与高效操作
开发语言·c++·list
云帆小二2 小时前
从开发语言出发如何选择学习考试系统
开发语言·学习
许泽宇的技术分享2 小时前
当AI学会“说人话“:Azure语音合成技术的魔法世界
后端·python·flask
光泽雨2 小时前
python学习基础
开发语言·数据库·python
百***06013 小时前
python爬虫——爬取全年天气数据并做可视化分析
开发语言·爬虫·python
jghhh013 小时前
基于幅度的和差测角程序
开发语言·matlab