Windows:百度人脸识别

目录

1.技术支持:

2.条件:

2.1百度ai

2.2设置配置

2.3下载Nuget库

3.windows页面设计

4.代码设计

4.1初始化和设置

4.2获取图像路径

4.3Bitmap 转 Byte Array

4.4Bitmap2Byte 方法

 5.效果展示

6.总结

1.技术支持:

        本程序编程语言为C#最终实现windows桌面开发。开发所使用的IDE为VS。

2.条件:

2.1百度ai

人脸识别小程序

网址:https://cloud.baidu.com/

1.登录:

2.点击使用:

3.创建应用:

4.得到API key

2.2设置配置

2.3下载Nuget库

3.windows页面设计

4.代码设计

4.1初始化和设置

public Form1()
{
    InitializeComponent();
    axWindowsMediaPlayer1.uiMode = "Invisible";
    client = new Face(API_KEY, SECRET_KEY);
}

4.2获取图像路径

private string GetImagePath()
{
    string personImgPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)
                 + Path.DirectorySeparatorChar.ToString() + "PersonImg";
    if (!Directory.Exists(personImgPath))
    {
        Directory.CreateDirectory(personImgPath);
    }

    return personImgPath;
}

4.3Bitmap 转 Byte Array

public byte[] Bitmap2Byte(Bitmap bitmap)
{
    try
    {
        using (MemoryStream stream = new MemoryStream())
        {
            bitmap.Save(stream, ImageFormat.Jpeg);
            byte[] data = new byte[stream.Length];
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(data, 0, Convert.ToInt32(stream.Length));
            return data;
        }
    }
    catch (Exception ex) { }
    return null;
}
  1. 创建 MemoryStream

    使用 using 语句创建一个 MemoryStream 对象。using 语句确保在使用完 MemoryStream 后会自动释放资源,避免内存泄漏。
  2. 保存 Bitmap 到 MemoryStream

    使用 bitmap.Save(stream, ImageFormat.Jpeg) 方法将 Bitmap 对象保存到 MemoryStream 中,指定图像格式为 JPEG。
  3. 创建字节数组

    创建一个字节数组 data,其长度与 MemoryStream 的长度相同。
  4. 将 MemoryStream 的位置设置为开始

    使用 stream.Seek(0, SeekOrigin.Begin) 将 MemoryStream 的当前位置设置为起始位置。
  5. 读取 MemoryStream 到字节数组

    使用 stream.Read(data, 0, Convert.ToInt32(stream.Length)) 将 MemoryStream 中的数据读取到字节数组 data 中。
  6. 返回字节数组

    返回包含图像数据的字节数组。

4.4Bitmap2Byte 方法

public byte[] BitmapSource2Byte(BitmapSource source)
{
    try
    {
        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
        encoder.QualityLevel = 100;
        using (MemoryStream stream = new MemoryStream())
        {
            encoder.Frames.Add(BitmapFrame.Create(source));
            encoder.Save(stream);
            byte[] bit = stream.ToArray();
            stream.Close();
            return bit;
        }
    }
    catch (Exception ex)
    {
        ClassLoger.Error("BitmapSource2Byte", ex);
    }
    return null;
}
  1. 创建 MemoryStream

    使用 using 语句创建一个 MemoryStream 对象。MemoryStream 是一个内存中的流,用于临时存储图像数据。

    using 语句确保在使用完 MemoryStream 后会自动释放资源,避免内存泄漏。
  2. 将 Bitmap 保存到 MemoryStream

    使用 bitmap.Save 方法将 Bitmap 对象保存到 MemoryStream 中。这里指定了图像格式为 ImageFormat.Jpeg

    这一步将 Bitmap 对象的图像数据以 JPEG 格式写入到 MemoryStream 中。
  3. 创建字节数组

    创建一个字节数组 data,其长度与 MemoryStream 的长度相同。stream.Length 返回 MemoryStream 中的字节数。

  4. 将 MemoryStream 的位置设置为开始

    使用 stream.Seek(0, SeekOrigin.Begin) 将 MemoryStream 的当前位置设置为起始位置。这是为了确保接下来的读取操作从流的开始位置开始。

  5. 读取 MemoryStream 到字节数组

    使用 stream.Read(data, 0, Convert.ToInt32(stream.Length)) 将 MemoryStream 中的数据读取到字节数组 data 中。

    stream.Read 方法从流中读取字节,并将其存储到 data 数组中,从偏移量 0 开始,读取的字节数为 stream.Length
  6. 返回字节数组

    返回包含图像数据的字节数组 data

  7. 异常处理
    如果在转换过程中发生异常,捕获异常并处理。这里可以记录日志或显示错误信息。最终返回 null 表示转换失败。

 5.效果展示

6.总结

        通过上述代码和步骤,我们能够轻松实现从摄像头捕捉视频帧并进行人脸识别的功能。具体而言,利用 Bitmap 转换为字节数组的方法,我们可以将捕捉到的视频帧转换为适合传输和处理的格式。这种转换为后续的人脸识别算法提供了基础数据输入。结合视频捕捉技术与百度AI的人脸识别能力,我们可以开发出各种智能化的应用程序。这些应用程序不仅能够提高系统的智能水平,还可以为用户提供更加安全、便捷的体验。例如,智能门禁系统、实时监控系统和个性化服务推荐系统等,都可以通过这种技术实现。

  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值