服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - asp.net core 使用 tensorflowjs实现 face recognition的源代码

asp.net core 使用 tensorflowjs实现 face recognition的源代码

2022-11-23 11:44阿新 C#

tensorflowjs,在该项目中使用了ml5js这个封装过的机器学习JavaScript类库, 使用起来更简单,本文给大家分享asp.net core 使用 tensorflowjs实现 face recognition的源代码,需要的朋友参考下吧

asp.net core 使用 tensorflowjs实现 face recognition的源代码asp.net core 使用 tensorflowjs实现 face recognition的源代码

功能描述

  1. 上传照片文件名及是系统要识别标签或是照片的名称(人物标识)
  2. 提取照片脸部特征值(调用 facemesh模型)
  3. 保存特征值添加样本(调用 knnClassifier)
  4. 测试上传的图片是否识别正确

项目依赖的库

源代码(neozhu/smartadmin.core.urf: Domain Driven Design (DDD) ultra-lightweight rapid development architecture(support .net 5.0) (github.com)

tensorflowjs,在该项目中我使用了ml5js这个封装过的机器学习JavaScript类库, 使用起来更简单

Demo

http://106.52.105.140:6200/photos/index

demo/123456

代码实现

上传照片功能

asp.net core 使用 tensorflowjs实现 face recognition的源代码

asp.net core 参考CleanArchitecture 结构实现后台代码,

参考代码如下(具体请看源代码):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
namespace SmartAdmin.Application.Photos.Commands
{
  public partial class AddPhotoCommand : IRequest<Result<int>>
  {
    public Stream Stream { get; set; }
    public string FileName { get; set; }
    public decimal Size { get; set; }
    public string Path { get; set; }
 
  }
  internal class AddPhotoCommandHandler : IRequestHandler<AddPhotoCommand, Result<int>>
  {
    private readonly IUnitOfWork unitOfWork;
    private readonly IPhotoService photoService;
 
    public AddPhotoCommandHandler(IUnitOfWork unitOfWork,
      IPhotoService photoService)
    {
      this.unitOfWork = unitOfWork;
      this.photoService = photoService;
    }
    public async Task<Result<int>> Handle(AddPhotoCommand request, CancellationToken cancellationToken)
    {
      var info = new DirectoryInfo(request.Path);
      if (!info.Exists)
      {
        info.Create();
      }
      using (FileStream outputFileStream = new FileStream(Path.Combine(request.Path,request.FileName), FileMode.Create))
      {
        request.Stream.CopyTo(outputFileStream);
        outputFileStream.Close();
      }
      var photo = new Photo()
      {
        Name = Path.GetFileNameWithoutExtension(request.FileName),
        Size = request.Size,
        Path = $"/photos/{request.FileName}",
      };
      this.photoService.Insert(photo);
      await this.unitOfWork.SaveChangesAsync();
      return await Result<int>.SuccessAsync(0, "保存成功");
    }
 
  }
}

facemesh模型提取照片中脸部特特信息

扫描图片获取图片中脸部的特征信息以一个多维数组的形式保存到数据库中,这些特征值将用与下一步的KNN分类识别使用

asp.net core 使用 tensorflowjs实现 face recognition的源代码

完成每一张照片中脸部信息的数字转化

参考代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function predict() {
     const img = document.getElementById('photo-canvas');
     facemesh.predict(img).then(faces => {
       console.log(faces)
       if (faces) {
         const canvas = document.getElementById("photo-canvas");
         const photoId=canvas.getAttribute("photo-id");
         const photoName=canvas.getAttribute("photo-name");
         console.log(canvas)
         var draw = canvas.getContext("2d");
         var mesh = faces[0].scaledMesh;
         console.log(mesh);
         /* highlight facial landmark points on canvas board */
         draw.fillStyle = "#00FF00";
         for (i = 0; i < mesh.length; i++) {
           var [x, y, z] = mesh[i];
           draw.fillRect(Math.round(x), Math.round(y), 2, 2);
         }
         updateLandmarks(photoId,JSON.stringify(mesh));
         knnClassifier.addExample(mesh, photoName);
         canvas.setAttribute("photo-mesh", JSON.stringify(mesh));
         $('#testbutton').attr('disabled', false);
       }
     });
   }
 
  function updateLandmarks(id,landmarks){
    $.post('/Photos/Update',{Id:id,Landmarks:landmarks}).done(res=>{
     console.log(res);
     reload();
    }).fail(res=>{
     $.messager.alert('更新失败', res, 'error');
    })
   } 

添加分类识别样本数据

facemesh模型只负责把照片中面部特征转换成一个数组,如果需要对每一张照片的数据再进行分类就需要用到KNN模型,添加的样本数据越多,识别的就越正确。

参考代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
let knnClassifier =ml5.KNNClassifier();
    function training(){
       $.messager.progress({msg:'training....'});
       $.get('/Photos/GetAll').done(res=>{
        for(let i=0;i<50;i++){
        res.map(item=>{
        if(item.Landmarks){
        knnClassifier.addExample(JSON.parse(item.Landmarks), item.Name);
        }
        });
        }
        $.messager.progress('close')
           if(knnClassifier.getNumLabels()>0){
           knnClassifier.classify(JSON.parse(res[2].Landmarks),(err,result)=>{
             console.log(result);
         })
       $('#testbutton').attr('disabled', false);
       }
       })
    }

测试照片识别结果

上传一张照片匹配维护的照片库中照片名称是否正确

asp.net core 使用 tensorflowjs实现 face recognition的源代码

参考代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
function testPredict(){
      const img = document.getElementById('testphoto_img');
      facemesh.predict(img).then(faces => {
        console.log(faces)
        if (faces) {
          knnClassifier.classify(faces[0].scaledMesh,(err,result)=>{
          console.log(result);
          $.messager.alert('Result:',result.label);
          $('#testresult').text(result.label);
         })
        }
      });
    }

到这里就全部完成了,对tensorflow感兴趣的朋友可以留言,下面有时间会继续更新,实现利用摄像头来识别人脸。

对asp.net coreCleanArchitecture 感兴趣的朋友可以从github下载,也可以留言交流,这个项目我也会继续更新,如果喜欢,请给个星星。

以上就是asp.net core 使用 tensorflowjs实现 face recognition(源代码)的详细内容,更多关于asp.net core实现face recognition的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/neozhu/archive/2021/06/24/14925905.html

延伸 · 阅读

精彩推荐
  • C#C# 中如何使用Thread

    C# 中如何使用Thread

    这篇文章主要介绍了C# 中使用 Thread的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...

    一线码农10932022-10-31
  • C#c# 编写的简单飞行棋游戏

    c# 编写的简单飞行棋游戏

    这个简单的飞行棋游戏主要是讲的方法怎么应用,充分的去理解方法和方法的调用。整体收获还是很大的。感兴趣的朋友可以参考下...

    静态类4232022-11-22
  • C#C# 6.0 的知识梳理

    C# 6.0 的知识梳理

    目前最新的版本是C# 7.0,VS的最新版本为Visual Studio 2017 RC,两者都尚未进入正式阶段。C# 6.0虽说出了一段时间,但是似乎有许多人对这一块知识并不了解。...

    反骨仔(二五仔)10502021-12-16
  • C#C#图片添加水印的实现代码

    C#图片添加水印的实现代码

    这篇文章主要为大家详细介绍了C#给图片添加水印的实现代码,不仅可以为图片加文字水印,还可以判断是否是图片文件,感兴趣的小伙伴们可以参考一下...

    C#教程网6902021-11-12
  • C#WPF简单的数据库查询实例

    WPF简单的数据库查询实例

    下面小编就为大家分享一篇WPF简单的数据库查询实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    happy多乐4802022-02-12
  • C#C# FileStream简单介绍和使用

    C# FileStream简单介绍和使用

    这篇文章主要为大家详细介绍了C# FileStream基本功能和使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Kaivin.bao9202022-07-21
  • C#Unity shader实现高斯模糊效果

    Unity shader实现高斯模糊效果

    这篇文章主要为大家详细介绍了Unity shader实现高斯模糊效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    贪玩的孩纸时代6602022-03-11
  • C#C#通过正则表达式实现提取网页中的图片

    C#通过正则表达式实现提取网页中的图片

    本文给大家分享的是使用C#通过正则表达式来实现提取网页中的图片的代码,十分的方便,有需要的小伙伴可以参考下。...

    C#教程网8782021-11-05