-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
213 lines (168 loc) · 7.08 KB
/
MainForm.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using System.IO;
using System.Diagnostics;
using System.Speech;
using System.Speech.Synthesis;
using System.Threading.Tasks;
using System.Reflection.Emit;
namespace SmartCortana
{
public partial class mainform : Form
{
//Declararation of all variables, vectors and haarcascades
Image<Bgr, Byte> currentFrame;
Capture grabber;
HaarCascade face;
HaarCascade eye;
MCvFont font = new MCvFont(FONT.CV_FONT_HERSHEY_TRIPLEX, 0.5d, 0.5d);
Image<Gray, byte> result, TrainedFace = null;
Image<Gray, byte> gray = null;
List<Image<Gray, byte>> trainingImages = new List<Image<Gray, byte>>();
List<string> labels = new List<string>();
List<string> NamePersons = new List<string>();
int countImages, NumLabels, t;
string name, names, currentuser = null;
SpeechSynthesizer speech = new SpeechSynthesizer();
void LogAdd(string add,bool isCortana=true) {
if(isCortana)
rtbLog.AppendText("["+DateTime.Now.ToLongTimeString()+"] "+"Cortana"+": "+add+"\n");
else
rtbLog.AppendText("[" + DateTime.Now.ToLongTimeString() + "] " + currentuser+ ": " + add + "\n");
}
public mainform()
{
InitializeComponent();
//Load haarcascades for face detection
face = new HaarCascade("haarcascade_frontalface_default.xml");
try
{
//Load of previus faces and labels for each image
string Labelsinfo = File.ReadAllText(Application.StartupPath + "/Faces/FacesLabels.txt");
string[] Labels = Labelsinfo.Split('%');
NumLabels = Convert.ToInt16(Labels[0]);
countImages = NumLabels;
string LoadFaces;
for (int tf = 1; tf < NumLabels + 1; tf++)
{
LoadFaces = "face" + tf + ".bmp";
trainingImages.Add(new Image<Gray, byte>(Application.StartupPath + "/Faces/" + LoadFaces));
labels.Add(Labels[tf]);
}
}
catch (Exception e)
{
speech.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Teen);
LogAdd("there is not face in data base");
speech.SpeakAsync("there is not face in data base");
}
}
private void mainform_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void btnSend_Click(object sender, EventArgs e)
{
LogAdd(txtchat.Text, false);
}
void timertiming(object sender, EventArgs e)
{
DetectFaces();
}
private void button1_Click(object sender, EventArgs e)
{
frmAddPerson frm = new frmAddPerson();
frm.ShowDialog();
}
private void button2_Click(object sender, System.EventArgs e)
{
}
// void FrameGrabber(object sender, EventArgs e)
void DetectFaces()
{
label3.Text = "0";
NamePersons.Add("");
//Get the current frame form capture device
currentFrame = grabber.QueryFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
//Convert it to Grayscale
gray = currentFrame.Convert<Gray, Byte>();
//Face Detector
MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
face,
1.2,
10,
Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size(20, 20));
//Action for each element detected
foreach (MCvAvgComp f in facesDetected[0])
{
t = t + 1;
result = currentFrame.Copy(f.rect).Convert<Gray, byte>().Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
//draw the face detected in the 0th (gray) channel with cyan color
// currentFrame.Draw(f.rect, new Bgr(Color.Cyan), 1);
if (trainingImages.ToArray().Length != 0)
{
//TermCriteria for face recognition with numbers of trained images like maxIteration
MCvTermCriteria termCrit = new MCvTermCriteria(countImages, 0.001);
//Eigen face recognizer
EigenObjectRecognizer recognizer = new EigenObjectRecognizer(
trainingImages.ToArray(),
labels.ToArray(),
3000,
ref termCrit);
name = recognizer.Recognize(result);
// //Draw the label for each face detected and recognized
//currentFrame.Draw(name, ref font, new Point(f.rect.X , f.rect.Y ), new Bgr(Color.Cyan));
}
NamePersons[t-1] = name;
NamePersons.Add("");
//Set the number of faces detected on the scene
// label3.Text = facesDetected[0].Length.ToString();
}
t = 0;
//Names concatenation of persons recognized
//Show the faces procesed and recognized
//imageBoxFrameGrabber.Image = currentFrame;
//label4.Text = names;
for (int nnn = 0; nnn < facesDetected[0].Length; nnn++)
{
if (nnn==1)
names = names + NamePersons[nnn] + " and ";
else
names = names + NamePersons[nnn];
}
label4.Text = names;
label3.Text = facesDetected[0].Length.ToString();
if (names != "" && names != currentuser)
{
speech.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Teen);
LogAdd(names + " welcome to cortana");
speech.SpeakAsync(names + " welcome to cortana");
}
if(names!="")
currentuser = names;
names = "";
// Task.Factory.StartNew(() =>
// {
// });
NamePersons.Clear();
//Clear the list(vector) of names
}
private void FrmPrincipal_Load(object sender, EventArgs e)
{
speech.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Teen);
//Initialize the capture device
grabber = new Capture();
grabber.QueryFrame();
//Initialize the FrameGraber event
Application.Idle += new EventHandler(timertiming);
//timerCheckFace.Start();
}
}
}