Commit 0429304c by 何阳

半张脸尝试

parent c683a1ef

99.4 KB | W: | H:

99.4 KB | W: | H:

FaceServer/Face/heyang.jpg
FaceServer/Face/何阳.jpg
FaceServer/Face/heyang.jpg
FaceServer/Face/何阳.jpg
  • 2-up
  • Swipe
  • Onion skin
......@@ -195,6 +195,9 @@
<None Include="Model\shape_predictor_68_face_landmarks.dat">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Model\shape_predictor_81_face_landmarks.dat">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
......@@ -213,7 +216,7 @@
<Content Include="App.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Face\heyang.jpg">
<Content Include="Face\何阳.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
......
The file could not be displayed because it is too large.
......@@ -11,16 +11,30 @@ using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using System.Text;
using Serilog;
using FaceRecognitionDotNet.Extensions;
using DlibDotNet;
using Rectangle = System.Drawing.Rectangle;
public class FaceRecognitionServer
{
private static FaceRecognition FaceRecognition = null;
private static Dictionary<string, FaceEncoding> KnownFaceEncodings = null;
public Rectangle Roi { set; get; } = default;
public System.Drawing.Rectangle Roi { set; get; } = default;
public int RoiSize { set; get; } = default;
public FaceRecognitionServer(string faceDataPath, string modelPath)
{
FaceRecognition = FaceRecognition ?? FaceRecognition.Create(modelPath);
FaceRecognition.InternalEncoding = Encoding.Default;
var modelParameter = new ModelParameter
{
PosePredictor68FaceLandmarksModel = File.ReadAllBytes(Path.Combine(modelPath, "shape_predictor_68_face_landmarks.dat")),
PosePredictor5FaceLandmarksModel = File.ReadAllBytes(Path.Combine(modelPath, "shape_predictor_5_face_landmarks.dat")),
FaceRecognitionModel = File.ReadAllBytes(Path.Combine(modelPath, "dlib_face_recognition_resnet_model_v1.dat")),
CnnFaceDetectorModel = File.ReadAllBytes(Path.Combine(modelPath, "mmod_human_face_detector.dat"))
};
//FaceRecognition = FaceRecognition ?? FaceRecognition.Create(modelPath);
FaceRecognition = FaceRecognition ?? FaceRecognition.Create(modelParameter);
KnownFaceEncodings = KnownFaceEncodings ?? LoadKnownFaces(FaceRecognition, faceDataPath);
}
......@@ -64,14 +78,11 @@ public class FaceRecognitionServer
bitmap.Dispose();
// Detect faces
var faceLocations = FaceRecognition.FaceLocations(image);
if (!faceLocations.Any())
{
image.Dispose();
return new List<FaceData>();
}
var faceLandmarks = FaceRecognition.FaceLandmark(image, faceLocations).ToList();
List<FaceData> faces = new List<FaceData>();
for (int i = 0; i < faceLocations.Count(); i++)
......@@ -82,7 +93,13 @@ public class FaceRecognitionServer
upperFacePoints.AddRange(landmarks[FacePart.RightEyebrow]);
upperFacePoints.AddRange(landmarks[FacePart.LeftEye]);
upperFacePoints.AddRange(landmarks[FacePart.RightEye]);
upperFacePoints.AddRange(landmarks[FacePart.NoseBridge]);
//假设额外的额头点是从编号60到81
for (int pointIndex = 60; pointIndex <= 81; pointIndex++)
{
var foreheadPoint = GetLandmarkPoint(landmarks, pointIndex); // 获取编号为pointIndex的特征点
upperFacePoints.Add(foreheadPoint);
}
// 计算上半脸特征点的边界
var minX = upperFacePoints.Min(point => point.Point.X) + Roi.X;
......@@ -132,6 +149,21 @@ public class FaceRecognitionServer
}
}
private FacePoint GetLandmarkPoint(IDictionary<FacePart, IEnumerable<FacePoint>> landmarks, int pointIndex)
{
foreach (var item in landmarks.Values)
{
foreach (var point in item)
{
if (point.Index == pointIndex)
{
return point;
}
}
}
throw new KeyNotFoundException();
}
// Helper method to load known faces
Dictionary<string, FaceEncoding> LoadKnownFaces(FaceRecognition faceRecognition, string faceDataPath)
{
......@@ -160,6 +192,14 @@ public class FaceRecognitionServer
upperFaceLandmarks.AddRange(faceLandmarks[FacePart.RightEyebrow]);
upperFaceLandmarks.AddRange(faceLandmarks[FacePart.LeftEye]);
upperFaceLandmarks.AddRange(faceLandmarks[FacePart.RightEye]);
//假设额外的额头点是从编号60到81
for (int pointIndex = 60; pointIndex <= 81; pointIndex++)
{
var foreheadPoint = GetLandmarkPoint(faceLandmarks, pointIndex); // 获取编号为pointIndex的特征点
upperFaceLandmarks.Add(foreheadPoint);
}
// Calculate the bounding box for the upper face landmarks
var minX = upperFaceLandmarks.Min(point => point.Point.X);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment