源代碼(neozhu/smartadmin.core.urf: Domain Driven Design (DDD) ultra-lightweight rapid development architecture(support .net 5.0) (github.com))javascript
tensorflowjs,在該項目中我使用了ml5js這個封裝過的機器學習JavaScript類庫, 使用起來更簡單java
Demogit
http://106.52.105.140:6200/photos/indexgithub
demo/123456數據庫
asp.net core 參考CleanArchitecture 結構實現後臺代碼, canvas
參考代碼以下(具體請看源代碼):api
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, "保存成功"); } } }
掃描圖片獲取圖片中臉部的特徵信息以一個多維數組的形式保存到數據庫中,這些特徵值將用與下一步的KNN分類識別使用數組
完成每一張照片中臉部信息的數字轉化asp.net
參考代碼以下:機器學習
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模型,添加的樣本數據越多,識別的就越正確。
參考代碼:
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); } }) }
上傳一張照片匹配維護的照片庫中照片名稱是否正確
參考代碼:
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 core CleanArchitecture 感興趣的朋友能夠從github下載,也能夠留言交流,這個項目我也會繼續更新,若是喜歡,請給個星星。