asp.net core 使用 tensorflowjs實(shí)現(xiàn) face recognition的源代碼


功能描述
- 上傳照片文件名及是系統(tǒng)要識(shí)別標(biāo)簽或是照片的名稱(人物標(biāo)識(shí))
- 提取照片臉部特征值(調(diào)用 facemesh模型)
- 保存特征值添加樣本(調(diào)用 knnClassifier)
- 測(cè)試上傳的圖片是否識(shí)別正確
項(xiàng)目依賴的庫(kù)
tensorflowjs,在該項(xiàng)目中我使用了ml5js這個(gè)封裝過的機(jī)器學(xué)習(xí)JavaScript類庫(kù), 使用起來(lái)更簡(jiǎn)單
Demo
http://106.52.105.140:6200/photos/index
demo/123456
代碼實(shí)現(xiàn)
上傳照片功能

asp.net core 參考CleanArchitecture 結(jié)構(gòu)實(shí)現(xiàn)后臺(tái)代碼,
參考代碼如下(具體請(qǐng)看源代碼):
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模型提取照片中臉部特特信息
掃描圖片獲取圖片中臉部的特征信息以一個(gè)多維數(shù)組的形式保存到數(shù)據(jù)庫(kù)中,這些特征值將用與下一步的KNN分類識(shí)別使用

完成每一張照片中臉部信息的數(shù)字轉(zhuǎn)化
參考代碼如下:
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');
})
}
添加分類識(shí)別樣本數(shù)據(jù)
facemesh模型只負(fù)責(zé)把照片中面部特征轉(zhuǎn)換成一個(gè)數(shù)組,如果需要對(duì)每一張照片的數(shù)據(jù)再進(jìn)行分類就需要用到KNN模型,添加的樣本數(shù)據(jù)越多,識(shí)別的就越正確。
參考代碼:
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);
}
})
}
測(cè)試照片識(shí)別結(jié)果
上傳一張照片匹配維護(hù)的照片庫(kù)中照片名稱是否正確

參考代碼:
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);
})
}
});
}
到這里就全部完成了,對(duì)tensorflow感興趣的朋友可以留言,下面有時(shí)間會(huì)繼續(xù)更新,實(shí)現(xiàn)利用攝像頭來(lái)識(shí)別人臉。
對(duì)asp.net coreCleanArchitecture 感興趣的朋友可以從github下載,也可以留言交流,這個(gè)項(xiàng)目我也會(huì)繼續(xù)更新,如果喜歡,請(qǐng)給個(gè)星星。
以上就是asp.net core 使用 tensorflowjs實(shí)現(xiàn) face recognition(源代碼)的詳細(xì)內(nèi)容,更多關(guān)于asp.net core實(shí)現(xiàn)face recognition的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C# string格式的日期時(shí)間字符串轉(zhuǎn)為DateTime類型的方法
這篇文章主要介紹了C# string格式的日期時(shí)間字符串轉(zhuǎn)為DateTime類型的方法,需要的朋友可以參考下2017-02-02
C#使用foreach語(yǔ)句搜索數(shù)組元素的方法
這篇文章主要介紹了C#使用foreach語(yǔ)句搜索數(shù)組元素的方法,涉及C#使用foreach語(yǔ)句遍歷數(shù)組實(shí)現(xiàn)搜索功能的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
C#實(shí)現(xiàn)AddRange為數(shù)組添加多個(gè)元素的方法
這篇文章主要介紹了C#實(shí)現(xiàn)AddRange為數(shù)組添加多個(gè)元素的方法,實(shí)例分析了AddRange方法的使用技巧,需要的朋友可以參考下2015-06-06
C#實(shí)現(xiàn)Word轉(zhuǎn)換TXT的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)Word轉(zhuǎn)換TXT的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12

