LISTING PROGRAM
Form Utama :
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data;
using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms; namespace FaceRecognize {
public partial class MainForm : Form {
public MainForm() {
InitializeComponent(); }
private void absenToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1(); form1.Show();
this.Hide(); }
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
FormAbout formabout = new FormAbout(); formabout.Show();
this.Hide(); }
} }
Form Absen
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data;
using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms; using Emgu.CV;
using System.IO;
using System.Diagnostics;
using Microsoft.Office.Interop.Excel; using System.Runtime.InteropServices; namespace Skripsi_Absensi_Wajah {
public partial class Absen : Form {
//Declararation of all variables, vectors and haarcascades Image<Bgr, Byte> currentFrame;
Capture grabber; HaarCascade face;
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>(); int ContTrain;
double[] gambaruji; double[,] gambaracuan; int linecount = 0;
String nama, nim, alamatt;
Microsoft.Office.Interop.Excel.Application xlexcel; Microsoft.Office.Interop.Excel.Workbook xlWorkBook; Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; public Absen()
{
InitializeComponent();
//Load haarcascades for face detection
face = new HaarCascade("haarcascade_frontalface_default.xml"); //Load of previus trainned faces and labels for each image try
{
string alamat = "E:\\Kuliah\\SEM VIII\\Skripsi Absensi Wajah\\Skripsi Absensi Wajah\\bin\\Debug\\Trained\\ " +
"/TrainedFaces.txt";
string[] lines = File.ReadAllLines(alamat); linecount = lines.Length;
} catch {
if (linecount == 0) {
MessageBox.Show("Nothing in binary database, please add at least a face(Simply train the prototype with the Add Face Button).", "Triained faces load", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); }
} }
private void groupBox1_Enter(object sender, EventArgs e) {
}
{
MainForm mainform = new MainForm(); mainform.Show();
this.Hide(); }
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
About about = new About(); about.Show();
this.Hide(); }
void FrameGrabber(object sender, EventArgs e) {
label3.Text = "0";
//Get the current frame form capture device
currentFrame = grabber.QueryFrame().Resize(410, 300, 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]) {
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 blue color
currentFrame.Draw(f.rect, new Bgr(Color.Red), 2);
if (trainingImages.ToArray().Length != 0) {
//TermCriteria for face recognition with numbers of trained images like maxIteration
MCvTermCriteria termCrit = new MCvTermCriteria(ContTrain, 0.001);
}
//Set the number of faces detected on the scene label3.Text = facesDetected[0].Length.ToString(); }
private void button1_Click(object sender, EventArgs e) {
//Initialize the capture device grabber = new Capture(); grabber.QueryFrame();
//Initialize the FrameGraber event
System.Windows.Forms.Application.Idle += new EventHandler(FrameGrabber);
button1.Enabled = false;
FileInfo fi = new FileInfo(@"E:\\Kuliah\\SEM VIII\\Skripsi Absensi Wajah\\Skripsi Absensi Wajah\\bin\\Debug\\Trained\\Absen.xlsx"); if (!fi.Exists)
{
xlexcel = new Microsoft.Office.Interop.Excel.Application(); xlexcel.Visible = true;
xlWorkBook =
(Microsoft.Office.Interop.Excel.Workbook)(xlexcel.Workbooks.Add(System.Refl ection.Missing.Value));
xlWorkSheet =
(Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.ActiveSheet; xlWorkSheet.Cells[1, 1] = "Jaringan Syaraf Tiruan"; xlWorkSheet.Cells[2, 1] = "Amer Sharif, S.Si., M.Kom"; xlWorkSheet.Cells[3, 1] = "Ilmu Komputer USU";
xlWorkSheet.Cells[4, 1] = "Date"; xlWorkSheet.Cells[4, 2] = "Time In";
xlWorkSheet.Cells[4, 3] = "Student's Name"; xlWorkSheet.Cells[4, 4] = "NIM";
xlWorkSheet.Cells[4, 5] = "Address"; xlWorkSheet.Cells[4, 7] = "Present"; xlWorkSheet.Cells[4, 8] = "Not Present";
xlWorkSheet.get_Range("A1", "D1").Font.Bold = true; xlWorkSheet.get_Range("A1", "D1").VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
} else {
xlexcel = new Microsoft.Office.Interop.Excel.Application(); xlexcel.Visible = true;
xlWorkBook = xlexcel.Workbooks.Open("E:\\Kuliah\\SEM VIII\\Skripsi Absensi Wajah\\Skripsi Absensi
Wajah\\bin\\Debug\\Trained\\Absen.xlsx", 0, false, 5, "", "", true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", true, false, 0, true, 1, 0);
xlWorkSheet =
(Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1) ;
} }
private void button2_Click(object sender, EventArgs e) {
try {
//Get a gray frame from capture device
gray = grabber.QueryGrayFrame().Resize(410, 300, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
//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]) {
TrainedFace = currentFrame.Copy(f.rect).Convert<Gray, byte>();
break; }
//resize face detected image for force to compare the same size with the
//test image with cubic interpolation type method TrainedFace = result.Resize(100, 100,
Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
trainingImages.Add(TrainedFace); labels.Add(textBox1.Text);
//Show face added in threshold
TrainedFace = TrainedFace.ThresholdBinary(new Gray(50), new Gray(255));
imageBox1.Image = TrainedFace;
String Todayshour = DateTime.Now.ToString("HH-mm-ss"); imageBox1.Image.Save("E:\\Kuliah\\SEM VIII\\Skripsi Absensi Wajah\\Skripsi Absensi Wajah\\bin\\Debug\\Trained\\" + Todayshour + "-" + textBox1.Text + ".bmp");
File.AppendAllText("E:\\Kuliah\\SEM VIII\\Skripsi Absensi Wajah\\Skripsi Absensi Wajah\\bin\\Debug\\Trained\\ " +
"/TrainedFaces.txt", Todayshour + "-" + textBox1.Text + "-" +txtNIM.Text + "-"+ txtAddress.Text +"\n");
MessageBox.Show(textBox1.Text + "´s face detected and added :)", "Training OK", MessageBoxButtons.OK, MessageBoxIcon.Information); }
catch (Exception k) {
MessageBox.Show("Enable the face detection first", "Training Fail", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); MessageBox.Show(k.ToString());
} }
private void releaseObject(object obj) {
try {
xlWorkSheet.Cells[_lastRow, 7] = "Present"; xlWorkSheet.Cells[_lastRow , 8] = "Not Present"; xlWorkSheet.Cells[_lastRow + 1, 1] = Todaysdate; xlWorkSheet.Cells[_lastRow + 1, 2] = Todayshours; xlWorkSheet.Cells[_lastRow + 1, 3] = nama;
xlWorkSheet.Cells[_lastRow + 1, 4] = nim; xlWorkSheet.Cells[_lastRow + 1, 5] = alamatt; if ((now >= start) && (now > late) && (now < end)) {
xlWorkSheet.Cells[_lastRow + 1, 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
xlWorkSheet.Cells[_lastRow + 1, 2].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
xlWorkSheet.Cells[_lastRow + 1, 3].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
xlWorkSheet.Cells[_lastRow + 1, 4].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
xlWorkSheet.Cells[_lastRow + 1, 5].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
} } else {
xlWorkSheet.Cells[_lastRow, 1] = Todaysdate; xlWorkSheet.Cells[_lastRow, 2] = Todayshours; xlWorkSheet.Cells[_lastRow, 3] = nama;
xlWorkSheet.Cells[_lastRow, 4] = nim; xlWorkSheet.Cells[_lastRow, 5] = alamatt;
if ((now >= start) && (now > late) && (now < end)) {
xlWorkSheet.Cells[_lastRow, 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); xlWorkSheet.Cells[_lastRow, 2].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); xlWorkSheet.Cells[_lastRow, 3].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); xlWorkSheet.Cells[_lastRow, 4].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); xlWorkSheet.Cells[_lastRow, 5].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
} }
_lastRow = xlWorkSheet.Range["A" +
xlWorkSheet.Rows.Count].End[Microsoft.Office.Interop.Excel.XlDirection.xlUp ].Row + 1;
for (int p = 5; p <= _lastRow-1; p++) {
if (Convert.ToString((xlWorkSheet.Cells[p, 1] as Microsoft.Office.Interop.Excel.Range).Value).Length < 10) {
}
{
countNames++; }
}
//MessageBox.Show(countNames.ToString());
int fileCount = (Directory.GetFiles(@"E:\\Kuliah\\SEM VIII\\Skripsi Absensi Wajah\\Skripsi Absensi
Wajah\\bin\\Debug\\Trained\\").Length - 2) / 5;
countNamesnotPresent = fileCount - countNames; for (int p = 5; p <= _lastRow-1; p++)
{
if (Convert.ToString((xlWorkSheet.Cells[p, 1] as Microsoft.Office.Interop.Excel.Range).Value) == "Date")
{ } else {
xlWorkSheet.Cells[_lastRow-1, 8] = countNamesnotPresent.ToString();
xlWorkSheet.Cells[_lastRow-1, 7] = countNames.ToString();
} }
countNames = 0;
System.Media.SoundPlayer player = new
System.Media.SoundPlayer(@"E:\\Kuliah\\SEM VIII\\Welcome.wav"); player.Play();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
xlWorkBook.Save();
xlWorkBook.Close(true, misValue, misValue); xlexcel.Quit();
}
private void Absen_FormClosing_1(object sender, FormClosingEventArgs e)
{
xlWorkBook.Save();
xlWorkBook.Close(true, misValue, misValue); xlexcel.Quit();
}
private void button4_Click(object sender, EventArgs e) {
//Trained face counter ContTrain = ContTrain + 1;
//Get a gray frame from capture device
gray = grabber.QueryGrayFrame().Resize(410, 300, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
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]) {
TrainedFace = currentFrame.Copy(f.rect).Convert<Gray, byte>();
break; }
TrainedFace = result.Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
trainingImages.Add(TrainedFace); labels.Add(textBox1.Text);
TrainedFace = TrainedFace.ThresholdBinary(new Gray(50), new Gray(255));
imageBox1.Image = TrainedFace; Bitmap gu = TrainedFace.ToBitmap(); gambaruji = new double[10000]; int k = 0;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
if (gu.GetPixel(i, j).R < 50) {
gambaruji[k] = 0; }
else {
gambaruji[k] = 1; }
k++; }
}
string alamat = "E:\\Kuliah\\SEM VIII\\Skripsi Absensi
Wajah\\Skripsi Absensi Wajah\\bin\\Debug\\Trained\\" + "/TrainedFaces.txt"; string[] lines = File.ReadAllLines(alamat);
int linecount = lines.Length; Bitmap gc;
gambaracuan = new double[linecount, 10000]; for (int i = 0; i < linecount; i++)
{
string[] word = lines[i].Split('-'); k = 0;
gc = (Bitmap)Image.FromFile(@"E:\\Kuliah\\SEM VIII\\Skripsi Absensi Wajah\\Skripsi Absensi Wajah\\bin\\Debug\\Trained\\" + word[0] + "-" + word[1] + "-"-"-" + word[2] + "-"-"-" + word[3] + "-".bmp"-");
{
for (int j = 0; j < 100; j++) {
if (gc.GetPixel(ii, j).R < 50) {
gambaracuan[i, k] = 0; }
else {
gambaracuan[i, k] = 1; }
k++; } } }
LVQ newLVQ = new LVQ();
int hasil = newLVQ.lvqrun(gambaruji, gambaracuan); string[] words = lines[hasil].Split('-');
nama = words[3]; nim = words[4]; alamatt = words[5]; labelbaru.Text = nama;
} } }
Form About
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data;
using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms; namespace FaceRecognize {
public partial class FormAbout : Form {
public FormAbout() {
InitializeComponent(); }
private void coverToolStripMenuItem_Click(object sender, EventArgs e)
{
MainForm mainform = new MainForm(); mainform.Show();
this.Hide(); }
{
Form1 form1 = new Form1(); form1.Show();
this.Hide(); }
} }
Class LVQ
using System;
using System.Collections.Generic; using System.Linq;
using System.Text; namespace FaceRecognize {
class LVQ {
int hasil; double minAbs;
double numXk = 10000; double numW = 10000; double lr = 0.05; double deca = 0.1; int maxEp = 100;
public int lvqrun(double[] gambaruji, double[,] gambaracuan) {
double[] abss = new double[gambaracuan.GetLength(0)]; for(int i = 0; i<maxEp;i++)
{
for (int j= 0 ; j<abss.Length; j++) {
abss[j] = 0; }
minAbs = 0;
for (int j= 0 ; j<numXk; j++) {
for (int k= 0 ; k<abss.Length; k++) {
abss[k] = abss[k] + (Math.Pow((gambaruji[j] - gambaracuan[k,j]),2));
} }
for (int k= 0 ; k<abss.Length; k++) {
abss[k] = Math.Sqrt(abss[k]); }
minAbs = abss.Min();
for (int k = 0; k < abss.Length; k++) {
if (minAbs == abss[k]) {
gambaracuan[k,j] = gambaracuan[k,j] - lr * (gambaruji[j] - gambaracuan[k,j]);
} break; }
else {
for (int j = 0; j < numW; j++) {
gambaracuan[k,j] = gambaracuan[k,j] - lr * (gambaruji[j] - gambaracuan[k,j]);
} break; }
}
lr = lr - (lr * deca); }
for (int k = 0; k < abss.Length; k++) {
if (minAbs == abss[k]) {
hasil = k; break; }
}
return hasil; }