You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
3.6 KiB
99 lines
3.6 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using FRT.Models;
|
|
using FaceRecognitionDotNet;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using System.IO;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace FRT.Controllers
|
|
{
|
|
public class HomeController : Controller
|
|
{
|
|
private readonly IHostingEnvironment _env;
|
|
private FaceRecognition _FaceRecognition;
|
|
|
|
public HomeController(IHostingEnvironment env)
|
|
{
|
|
this._env = env;
|
|
}
|
|
|
|
public IActionResult Index(string s, string t)
|
|
{
|
|
var result = 0;
|
|
DateTime start;
|
|
DateTime end;
|
|
Console.WriteLine(this._env.WebRootPath == null ? "web root is null" : this._env.WebRootPath);
|
|
var basePath = Path.Combine(this._env.WebRootPath, "face");
|
|
Console.WriteLine(basePath);
|
|
this._FaceRecognition = FaceRecognition.Create(basePath);
|
|
//
|
|
var path = Path.Combine(this._env.WebRootPath, "TestImages", $"1.jpg");
|
|
byte[] data = null;
|
|
var width = 0;
|
|
var height = 0;
|
|
using (var bitmap = new System.Drawing.Bitmap(path))
|
|
{
|
|
data = this.ToManaged(bitmap);
|
|
width = bitmap.Width;
|
|
height = bitmap.Height;
|
|
}
|
|
//using (var image1 = FaceRecognition.LoadImageFile(Path.Combine(this._env.WebRootPath, "TestImages", $"{s}.jpg")))
|
|
using (var image1 = FaceRecognition.LoadImage(data, height, width, 3))
|
|
{
|
|
using (var image2 = FaceRecognition.LoadImageFile(Path.Combine(this._env.WebRootPath, "TestImages", $"{t}.jpg")))
|
|
{
|
|
var endodings1 = this._FaceRecognition.FaceEncodings(image1).ToArray();
|
|
var endodings2 = this._FaceRecognition.FaceEncodings(image2).ToArray();
|
|
start = DateTime.Now;
|
|
foreach (var encoding in endodings1)
|
|
{
|
|
foreach (var compareFace in FaceRecognition.CompareFaces(endodings2, encoding, 0.6))
|
|
{
|
|
result += compareFace ? 1 : 0;
|
|
}
|
|
}
|
|
end = DateTime.Now;
|
|
foreach (var encoding in endodings1)
|
|
{
|
|
encoding.Dispose();
|
|
}
|
|
foreach (var encoding in endodings2)
|
|
{
|
|
encoding.Dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
this._FaceRecognition.Dispose();
|
|
return Content((end - start).TotalSeconds + " : " + result.ToString());
|
|
}
|
|
|
|
public byte[] ToManaged(System.Drawing.Bitmap bitmap)
|
|
{
|
|
var format = bitmap.PixelFormat;
|
|
var width = bitmap.Width;
|
|
var height = bitmap.Height;
|
|
var array = new byte[width * height * 3]; // bitmap is treated as rgb
|
|
|
|
System.Drawing.Imaging.BitmapData bitmapData = null;
|
|
try
|
|
{
|
|
bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.WriteOnly, format);
|
|
Marshal.Copy(bitmapData.Scan0, array, 0, array.Length);
|
|
}
|
|
finally
|
|
{
|
|
if (bitmapData != null)
|
|
bitmap.UnlockBits(bitmapData);
|
|
}
|
|
|
|
return array;
|
|
}
|
|
}
|
|
} |