-
Notifications
You must be signed in to change notification settings - Fork 81
/
Form1.cs
407 lines (340 loc) · 14.2 KB
/
Form1.cs
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using KBYAIFace;
using System.Data.SQLite;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Header;
namespace FaceRecognition
{
public partial class Form1 : Form
{
private SQLiteConnection conn;
private List<Person> personList;
float identifyThreshold = 0.7f;
byte[] templates1 = null;
byte[] templates2 = null;
float verify_threshold = 0.7f;
public Form1()
{
InitializeComponent();
textBoxMachineCode.Text = FaceSDK.GetMachineCode();
try
{
string license = File.ReadAllText("license.txt");
int ret = FaceSDK.SetActivation(license);
if (ret == (int)SDK_ERROR.SDK_SUCCESS)
{
ret = FaceSDK.InitSDK("data");
if(ret == (int)SDK_ERROR.SDK_SUCCESS)
{
MessageBox.Show("Init Successful!");
}
else
{
MessageBox.Show("Init Failed!");
}
}
else
{
throw new Exception("Activtaion Failure!");
}
}
catch (Exception ex)
{
MessageBox.Show("Activtaion Failure!");
}
InitDatabase();
RefreshListView();
}
public void InitDatabase()
{
// Get the database path
string dbPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "person.db");
// Create or open the database
conn = new SQLiteConnection($"Data Source={dbPath};Version=3;");
conn.Open();
// Create table if it doesn't exist
string createTableQuery = @"CREATE TABLE IF NOT EXISTS person (
name TEXT,
faceJpg BLOB,
templates BLOB)";
using (var command = new SQLiteCommand(createTableQuery, conn))
{
command.ExecuteNonQuery();
}
personList = LoadAllPersons();
}
public List<Person> LoadAllPersons()
{
var persons = new List<Person>();
// Query the table for all persons.
var command = new SQLiteCommand("SELECT * FROM person", conn);
var reader = command.ExecuteReader();
// Convert the data into a List<Person>.
while (reader.Read())
{
var person = new Person(reader["name"].ToString(), (byte[])reader["faceJpg"], (byte[])reader["templates"]);
persons.Add(person);
}
return persons;
}
public void InsertPerson(Person person, SQLiteConnection dbConnection)
{
string insertQuery = @"INSERT OR REPLACE INTO person (name, faceJpg, templates)
VALUES (@name, @faceJpg, @templates)";
using (var command = new SQLiteCommand(insertQuery, dbConnection))
{
command.Parameters.AddWithValue("@name", person.Name);
command.Parameters.AddWithValue("@faceJpg", person.FaceJpg);
command.Parameters.AddWithValue("@templates", person.Templates);
command.ExecuteNonQuery();
}
// Optionally update the UI or list after insertion.
personList.Add(person);
}
public void DeleteAllPerson()
{
using (var command = new SQLiteCommand("DELETE FROM person", conn))
{
command.ExecuteNonQuery();
}
personList.Clear();
}
public void RefreshListView()
{
this.listView1.Clear();
this.listView1.Columns.Add("Person", 300);
// Create an ImageList
var imageList = new ImageList();
imageList.ColorDepth = ColorDepth.Depth32Bit;
imageList.ImageSize = new Size(60, 60);
for (int i = 0; i < personList.Count; i++)
{
String itemText = $"item{i + 1}";
imageList.Images.Add(itemText, ConvertJpgByteArrayToBitmap(personList[i].FaceJpg));
}
listView1.SmallImageList = imageList;
for(int i = 0; i < personList.Count; i ++)
{
String itemText = $"item{i + 1}";
// Add items to the ListView with images and text
listView1.Items.Add(new ListViewItem
{
Text = personList[i].Name,
ImageKey = itemText
});
}
}
public static Bitmap ConvertTo24bpp(Image img)
{
var bmp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
using (var gr = Graphics.FromImage(bmp))
gr.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height));
return bmp;
}
public static Bitmap CropImage(Image img, Rectangle cropArea)
{
Bitmap bmpImage = new Bitmap(img);
return bmpImage.Clone(cropArea, bmpImage.PixelFormat);
}
public static Image LoadImageWithExif(String filePath)
{
try
{
Image image = Image.FromFile(filePath);
// Check if the image has EXIF orientation data
if (image.PropertyIdList.Contains(0x0112))
{
int orientation = image.GetPropertyItem(0x0112).Value[0];
switch (orientation)
{
case 1:
// Normal
break;
case 3:
// Rotate 180
image.RotateFlip(RotateFlipType.Rotate180FlipNone);
break;
case 6:
// Rotate 90
image.RotateFlip(RotateFlipType.Rotate90FlipNone);
break;
case 8:
// Rotate 270
image.RotateFlip(RotateFlipType.Rotate270FlipNone);
break;
default:
// Do nothing
break;
}
}
return image;
}
catch (Exception e)
{
throw new Exception("Image null!");
}
}
public byte[] BitmapToJpegByteArray(Bitmap bitmap)
{
using (MemoryStream memoryStream = new MemoryStream())
{
// Save the Bitmap as JPEG to the MemoryStream
bitmap.Save(memoryStream, ImageFormat.Jpeg);
// Return the byte array
return memoryStream.ToArray();
}
}
public static Bitmap ConvertJpgByteArrayToBitmap(byte[] jpgData)
{
using (MemoryStream ms = new MemoryStream(jpgData))
{
return new Bitmap(ms);
}
}
private void btnRegister_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
String fileName = openFileDialog1.FileName;
Image image = null;
try
{
image = LoadImageWithExif(fileName);
}
catch (Exception)
{
MessageBox.Show("Unknown Format!");
return;
}
Bitmap imgBmp = ConvertTo24bpp(image);
BitmapData bitmapData = imgBmp.LockBits(new Rectangle(0, 0, imgBmp.Width, imgBmp.Height), ImageLockMode.ReadWrite, imgBmp.PixelFormat);
int bytesPerPixel = Bitmap.GetPixelFormatSize(imgBmp.PixelFormat) / 8;
int byteCount = bitmapData.Stride * imgBmp.Height;
byte[] pixels = new byte[byteCount];
IntPtr ptrFirstPixel = bitmapData.Scan0;
Marshal.Copy(ptrFirstPixel, pixels, 0, pixels.Length);
imgBmp.UnlockBits(bitmapData);
FaceBox[] faceBoxes = new FaceBox[10];
int faceCount = FaceSDK.FaceDetection(pixels, imgBmp.Width, imgBmp.Height, faceBoxes, 10);
if (faceCount > 0)
{
Rectangle cropRect = new Rectangle(faceBoxes[0].x1, faceBoxes[0].y1, faceBoxes[0].x2 - faceBoxes[0].x1, faceBoxes[0].y2 - faceBoxes[0].y1); // Adjust crop area
Bitmap croppedImage = CropImage(image, cropRect);
PersonDialog dialog = new PersonDialog();
dialog.SetFace(croppedImage);
DialogResult res = dialog.ShowDialog();
if(res == DialogResult.OK)
{
FaceSDK.TemplateExtraction(pixels, imgBmp.Width, imgBmp.Height, ref faceBoxes[0]);
byte[] jpgData = BitmapToJpegByteArray(croppedImage);
Person person = new Person(dialog.GetName(), jpgData, faceBoxes[0].templates);
InsertPerson(person, this.conn);
RefreshListView();
}
//templates1 = faceBoxes[0].templates;
//if (templates1 != null && templates2 != null)
//{
// float similarity = FaceSDK.SimilarityCalculation(templates1, templates2);
// String recog_result = "same";
// if (similarity < verify_threshold)
// recog_result = "different";
// richTextBoxResult.Text += recog_result + " " + similarity.ToString();
//}
}
else
{
//richTextBoxResult.Text += "image1: no face detected!\n";
//templates1 = null;
MessageBox.Show("No face detected!\n");
}
}
}
private void btnIdentify_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
String fileName = openFileDialog1.FileName;
Image image = null;
try
{
image = LoadImageWithExif(fileName);
}
catch (Exception)
{
MessageBox.Show("Unknown Format!");
return;
}
Bitmap imgBmp = ConvertTo24bpp(image);
BitmapData bitmapData = imgBmp.LockBits(new Rectangle(0, 0, imgBmp.Width, imgBmp.Height), ImageLockMode.ReadWrite, imgBmp.PixelFormat);
int bytesPerPixel = Bitmap.GetPixelFormatSize(imgBmp.PixelFormat) / 8;
int byteCount = bitmapData.Stride * imgBmp.Height;
byte[] pixels = new byte[byteCount];
IntPtr ptrFirstPixel = bitmapData.Scan0;
Marshal.Copy(ptrFirstPixel, pixels, 0, pixels.Length);
imgBmp.UnlockBits(bitmapData);
FaceBox[] faceBoxes = new FaceBox[10];
int faceCount = FaceSDK.FaceDetection(pixels, imgBmp.Width, imgBmp.Height, faceBoxes, 10);
if (faceCount > 0)
{
FaceSDK.TemplateExtraction(pixels, imgBmp.Width, imgBmp.Height, ref faceBoxes[0]);
Rectangle cropRect = new Rectangle(faceBoxes[0].x1, faceBoxes[0].y1, faceBoxes[0].x2 - faceBoxes[0].x1, faceBoxes[0].y2 - faceBoxes[0].y1); // Adjust crop area
Bitmap croppedImage = CropImage(image, cropRect);
float maxSimilarity = 0f;
int maxSimilarIdx = -1;
for(int i = 0; i < personList.Count; i ++)
{
float similarity = FaceSDK.SimilarityCalculation(faceBoxes[0].templates, personList[i].Templates);
if(similarity > maxSimilarity)
{
maxSimilarity = similarity;
maxSimilarIdx = i;
}
}
if(maxSimilarity > identifyThreshold)
{
lblName.Text = $"Name: {personList[maxSimilarIdx].Name}";
lblSimilarity.Text = $"Similarity: {maxSimilarity}";
lblResult.Text = $"Result: Same Person";
pictureIdentify.Image = croppedImage;
pictureRegister.Image = ConvertJpgByteArrayToBitmap(personList[maxSimilarIdx].FaceJpg);
}
else
{
lblName.Text = "";
lblSimilarity.Text = $"Similarity: {maxSimilarity}";
lblResult.Text = $"Result: Different Person";
pictureIdentify.Image = croppedImage;
pictureRegister.Image = null;
}
}
else
{
//richTextBoxResult.Text += "image1: no face detected!\n";
//templates1 = null;
MessageBox.Show("No face detected!\n");
}
}
}
private void btnDeleteAll_Click(object sender, EventArgs e)
{
DeleteAllPerson();
RefreshListView();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}