Merge branch 'dev' of http://8.153.108.90:3000/YD/Wpf_AiSportsMicrospace into dev
This commit is contained in:
commit
183b18eb1b
@ -215,73 +215,78 @@ namespace Wpf_AiSportsMicrospace.Common
|
|||||||
LoadGifToCache(relativePath);
|
LoadGifToCache(relativePath);
|
||||||
return _cache.GetValueOrDefault(relativePath);
|
return _cache.GetValueOrDefault(relativePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<StudentInfoDto> FacialRecognition(byte[] buffer, double[] xNorms)
|
public static List<StudentInfoDto> FacialRecognition(byte[] buffer, double[] xNorms)
|
||||||
{
|
{
|
||||||
if (xNorms == null || xNorms.Length == 0)
|
try
|
||||||
throw new ArgumentException("xNorms 不能为空");
|
|
||||||
|
|
||||||
using var image = SharpImage.Image.Load<Rgba32>(buffer);
|
|
||||||
|
|
||||||
int imgWidth = image.Width;
|
|
||||||
int imgHeight = image.Height;
|
|
||||||
int rectHeight = imgHeight / 2;
|
|
||||||
|
|
||||||
// 1. 排序中心点
|
|
||||||
var sortedX = xNorms.OrderBy(x => x).ToList();
|
|
||||||
int faceCount = sortedX.Count;
|
|
||||||
|
|
||||||
// 2. 计算分割边界
|
|
||||||
var splitXs = new List<double>();
|
|
||||||
for (int i = 0; i < sortedX.Count - 1; i++)
|
|
||||||
splitXs.Add((sortedX[i] + sortedX[i + 1]) / 2.0);
|
|
||||||
|
|
||||||
// 3. 起止边界
|
|
||||||
splitXs.Insert(0, 0.0);
|
|
||||||
splitXs.Add(1.0);
|
|
||||||
|
|
||||||
// 4. 输出目录
|
|
||||||
string imgDir = Path.Combine(AppContext.BaseDirectory, "img");
|
|
||||||
Directory.CreateDirectory(imgDir);
|
|
||||||
|
|
||||||
var studentInfos = new List<StudentInfoDto>();
|
|
||||||
|
|
||||||
// 5. 循环裁剪 + 同步识别
|
|
||||||
for (int i = 0; i < faceCount; i++)
|
|
||||||
{
|
{
|
||||||
double xStart = splitXs[i];
|
if (xNorms == null || xNorms.Length == 0)
|
||||||
double xEnd = splitXs[i + 1];
|
return null; // 不再抛异常,直接返回 null
|
||||||
|
|
||||||
int x1 = (int)(xStart * imgWidth);
|
using var image = SharpImage.Image.Load<Rgba32>(buffer);
|
||||||
int x2 = (int)(xEnd * imgWidth);
|
|
||||||
int w = Math.Max(1, x2 - x1);
|
|
||||||
|
|
||||||
var cropRect = new Rectangle(x1, 0, w, rectHeight);
|
int imgWidth = image.Width;
|
||||||
|
int imgHeight = image.Height;
|
||||||
|
int rectHeight = imgHeight / 2;
|
||||||
|
|
||||||
using var cropped = image.Clone(ctx => ctx.Crop(cropRect));
|
// 1. 排序中心点
|
||||||
|
var sortedX = xNorms.OrderBy(x => x).ToList();
|
||||||
|
int faceCount = sortedX.Count;
|
||||||
|
|
||||||
using var ms = new MemoryStream();
|
// 2. 计算分割边界
|
||||||
cropped.Save(ms, new JpegEncoder());
|
var splitXs = new List<double>();
|
||||||
ms.Position = 0;
|
for (int i = 0; i < sortedX.Count - 1; i++)
|
||||||
|
splitXs.Add((sortedX[i] + sortedX[i + 1]) / 2.0);
|
||||||
|
|
||||||
string imgPath = Path.Combine(imgDir, $"crop_{i + 1}_{DateTime.Now:HHmmssfff}.jpg");
|
// 3. 起止边界
|
||||||
File.WriteAllBytes(imgPath, ms.ToArray());
|
splitXs.Insert(0, 0.0);
|
||||||
|
splitXs.Add(1.0);
|
||||||
|
|
||||||
string base64 = Convert.ToBase64String(ms.ToArray());
|
// 4. 输出目录
|
||||||
|
string imgDir = Path.Combine(AppContext.BaseDirectory, "img");
|
||||||
|
Directory.CreateDirectory(imgDir);
|
||||||
|
|
||||||
Console.WriteLine($"[{i + 1}] 范围: {xStart:0.00}-{xEnd:0.00}, 宽: {w}");
|
var studentInfos = new List<StudentInfoDto>();
|
||||||
|
|
||||||
// === 同步调用人脸识别接口 ===
|
// 5. 循环裁剪 + 同步识别
|
||||||
var result = FaceRecognitionAsync(base64);
|
for (int i = 0; i < faceCount; i++)
|
||||||
|
{
|
||||||
|
double xStart = splitXs[i];
|
||||||
|
double xEnd = splitXs[i + 1];
|
||||||
|
|
||||||
if (result != null)
|
int x1 = (int)(xStart * imgWidth);
|
||||||
studentInfos.Add(result);
|
int x2 = (int)(xEnd * imgWidth);
|
||||||
|
int w = Math.Max(1, x2 - x1);
|
||||||
|
|
||||||
|
var cropRect = new Rectangle(x1, 0, w, rectHeight);
|
||||||
|
|
||||||
|
using var cropped = image.Clone(ctx => ctx.Crop(cropRect));
|
||||||
|
|
||||||
|
using var ms = new MemoryStream();
|
||||||
|
cropped.Save(ms, new JpegEncoder());
|
||||||
|
ms.Position = 0;
|
||||||
|
|
||||||
|
string imgPath = Path.Combine(imgDir, $"crop_{i + 1}_{DateTime.Now:HHmmssfff}.jpg");
|
||||||
|
File.WriteAllBytes(imgPath, ms.ToArray());
|
||||||
|
|
||||||
|
string base64 = Convert.ToBase64String(ms.ToArray());
|
||||||
|
|
||||||
|
Console.WriteLine($"[{i + 1}] 范围: {xStart:0.00}-{xEnd:0.00}, 宽: {w}");
|
||||||
|
|
||||||
|
var result = FaceRecognitionAsync(base64);
|
||||||
|
|
||||||
|
if (result != null)
|
||||||
|
studentInfos.Add(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
return studentInfos;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"FacialRecognition 异常: {ex.Message}");
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return studentInfos;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 调用人脸识别接口,返回学生信息
|
/// 调用人脸识别接口,返回学生信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user