-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
176 lines (144 loc) · 5.51 KB
/
Program.cs
File metadata and controls
176 lines (144 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Dnn;
using Emgu.CV.Structure;
using FaceDetection;
using System.Drawing;
using System.Drawing.Imaging;
using System.Xml.Linq;
var useWebcam = true;
var pathToVideo = "examples/dancers.mp4";
var renderConfidence = true;
var folderPath = "C:\\Users\\felen\\Downloads\\FacesForCalibration";
var imageFiles = Directory.GetFiles(folderPath, "*.jpg"); // Adjust the file extension filter as needed
foreach (var imagePath in imageFiles)
{
Mat frame2;
var saved = false;
while (CvInvoke.WaitKey(1) == -1)
{
frame2 = CvInvoke.Imread(imagePath, ImreadModes.Color);
//sing var capture2 = new VideoCapture(0);
var detector = new FaceRectangleDetector(
new Size(frame2.Width, frame2.Height));
// using var frame2 = capture2.QueryFrame();
if (frame2 == null) break;
// Optional: draw rectangles for visual feedback
var faces = detector.GetFaceRectangles(frame2.ToBitmap());
foreach (var rect in faces)
{
CvInvoke.Rectangle(frame2, rect, new MCvScalar(0, 255, 0), 1);
var score = new FaceRecognitionScoring.FaceQualityScorer().ScoreFaceImage(imagePath, faces.ToArray());
DrawText("Score: " +score.ToString(), frame2);
}
if (!saved)
{
saved = true;
var bitmap = frame2.ToBitmap();
bitmap.Save(Path.Combine(folderPath+"\\Processed\\", "Processed_" + Path.GetFileName(imagePath)), ImageFormat.Jpeg);
}
CvInvoke.Imshow("Faces", frame2);
}
}
var windowName = "Face detection (Press any key to close)";
CvInvoke.NamedWindow(windowName);
// using var capture = useWebcam
// ? new VideoCapture(camIndex: 0)
// : new VideoCapture(fileName: pathToVideo);
////using var model = InitializeFaceDetectionModel(new Size(201, 251));
// using var model = InitializeFaceDetectionModel(new Size(capture.Width, capture.Height));
// var frame = capture.QueryFrame();
// MatToBitmap(frame);
// if (frame is null)
// {
// break;
// }
// frame = CvInvoke.Imread(imagePath, ImreadModes.Color);
// //var mat = BitmapExtension.ToMat(bitmap);
// var faces = new Mat();
// model.Detect(frame, faces);
// DrawDetectedFaces(frame, faces, renderConfidence);
// new FaceRecognitionScoring.FaceQualityScorer().ScoreFaceImage(imagePath);
// CvInvoke.Imshow(windowName, frame);
FaceDetectorYN InitializeFaceDetectionModel(Size inputSize) => new FaceDetectorYN(
model: "face_detection_yunet_2022mar.onnx",
config: string.Empty,
inputSize: inputSize,
scoreThreshold: 0.9f,
nmsThreshold: 0.3f,
topK: 5000,
backendId: Emgu.CV.Dnn.Backend.Default,
targetId: Target.Cpu);
void DrawText(string text, Mat frame2)
{
int margin = 10;
int fontScale = 1;
int thickness = 2;
var baseline = 0;
var textSize = CvInvoke.GetTextSize(text, FontFace.HersheySimplex, fontScale, thickness, ref baseline );
var textPoint = new Point(margin, frame2.Height - margin);
CvInvoke.PutText(
frame2,
text,
textPoint,
FontFace.HersheySimplex,
fontScale,
new MCvScalar(0, 255, 0),
thickness);
}
void DrawDetectedFaces(Mat frame, Mat faces, bool renderConfidence)
{
if (faces.Rows <= 0)
{
return;
}
// facesData is multidimensional array.
// The first dimension is the index of the face, the second dimension is the data for that face.
// The data for each face is 15 elements long:
// - the first 4 elements are the bounding box of the face (x, y, width, height)
// - the next 10 elements are the x and y coordinates of 5 facial landmarks:
// right eye, left eye, nose tip, right mouth corner, left mouth corner
// - the last element is the confidence score
var facesData = (float[,])faces.GetData(jagged: true);
for (var i = 0; i < facesData.GetLength(0); i++)
{
DrawFaceRectangle(frame, (int)facesData[i, 0], (int)facesData[i, 1], (int)facesData[i, 2], (int)facesData[i, 3]);
DrawFaceLandMarks(frame, i, facesData);
if (renderConfidence)
{
DrawConfidenceText(frame, (int)facesData[i, 0], (int)facesData[i, 1] - 5, facesData[i, 14]);
}
}
}
void DrawFaceRectangle(Mat frame, int x, int y, int width, int height)
{
var faceRectangle = new Rectangle(x, y, width, height);
CvInvoke.Rectangle(frame, faceRectangle, new MCvScalar(0, 255, 0), 1);
}
void DrawFaceLandMarks(Mat frame, int faceIndex, float[,] facesData)
{
var landMarkColors = new MCvScalar[]
{
new MCvScalar(255, 0, 0), // right eye
new MCvScalar(0, 0, 255), // left eye
new MCvScalar(0, 255, 0), // nose tip
new MCvScalar(255, 0, 255), // right mouth corner
new MCvScalar(0, 255, 255) // left mouth corner
};
for (var landMark = 0; landMark < 5; landMark++)
{
var x = (int)facesData[faceIndex, 4 + landMark * 2];
var y = (int)facesData[faceIndex, 4 + landMark * 2 + 1];
CvInvoke.Circle(frame, new Point(x, y), 2, landMarkColors[landMark], -1);
}
}
void DrawConfidenceText(Mat frame, int x, int y, float confidence)
{
CvInvoke.PutText(frame, $"{confidence:N4}", new Point(x, y), FontFace.HersheyComplex, 0.3, new MCvScalar(0, 0, 255), 1);
}
Bitmap MatToBitmap(Mat mat)
{
var image = mat.ToImage<Bgr, byte>();
var bitmap = image.ToBitmap();
return bitmap;
}