Sistem Absensi Menggunakan Wajah Pada Jaringan Syaraf Tiruan Dengan Algoritma Learning Vector Quantization (LVQ)

A-1

LISTING PROGRAM

Form Utama :
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;

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
using
using
using
using
using
using
using
using
using
using


System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
Emgu.CV;
Emgu.CV.Structure;
Emgu.CV.CvEnum;

Universitas Sumatera Utara

A-2

using
using
using
using


System.IO;
System.Diagnostics;
Microsoft.Office.Interop.Excel;
System.Runtime.InteropServices;

namespace Skripsi_Absensi_Wajah
{
public partial class Absen : Form
{
//Declararation of all variables, vectors and haarcascades
Image currentFrame;
Capture grabber;
HaarCascade face;
Image result, TrainedFace = null;
Image gray = null;
List trainingImages = new List();
List labels = new List();
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)
{
}
private void coverToolStripMenuItem_Click(object sender, EventArgs
e)

Universitas Sumatera Utara

A-3


{
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();
//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().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();
}
//Show the faces procesed and recognized
imageBoxFrameGrabber.Image = currentFrame;
}

Universitas Sumatera Utara

A-4

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,
xlWorkSheet.Cells[2,
xlWorkSheet.Cells[3,
xlWorkSheet.Cells[4,
xlWorkSheet.Cells[4,
xlWorkSheet.Cells[4,

xlWorkSheet.Cells[4,
xlWorkSheet.Cells[4,
xlWorkSheet.Cells[4,
xlWorkSheet.Cells[4,

1]
1]
1]
1]
2]
3]
4]
5]
7]
8]

=
=
=
=
=
=
=
=
=
=

"Jaringan Syaraf Tiruan";
"Amer Sharif, S.Si., M.Kom";
"Ilmu Komputer USU";
"Date";
"Time In";
"Student's Name";
"NIM";
"Address";
"Present";
"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
{
//Trained face counter
ContTrain = ContTrain + 1;

Universitas Sumatera Utara

A-5

//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();
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
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;

Universitas Sumatera Utara

A-6

}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " +
ex.ToString());
}
finally
{
GC.Collect();
}
}
private void button3_Click(object sender, EventArgs e)
{
int countNames = 0;
int countNamesnotPresent = 0;
String Todaysdate = DateTime.Now.ToString("dd/MM/yyyy");
String Todayshours = DateTime.Now.ToString("HH:mm:ss");
int _lastRow = xlWorkSheet.Range["A" +
xlWorkSheet.Rows.Count].End[Microsoft.Office.Interop.Excel.XlDirection.xlUp
].Row + 1;
String cellValue =
Convert.ToString((xlWorkSheet.Cells[_lastRow-1, 1] as
Microsoft.Office.Interop.Excel.Range).Value);
TimeSpan start = new TimeSpan(21, 0, 0); //10 o'clock
TimeSpan late = new TimeSpan(21, 22, 0); // 10.15 o'clock
TimeSpan end = new TimeSpan(22, 0, 0); //12 o'clock
TimeSpan now = DateTime.Now.TimeOfDay;
if (cellValue == "Date")
{
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);
}

}
else if (cellValue.Substring(0, 10) != Todaysdate)
{
xlWorkSheet.Cells[_lastRow , 1] = "Date";
xlWorkSheet.Cells[_lastRow, 2] = "Time In";
xlWorkSheet.Cells[_lastRow, 3] = "Student's Name";
xlWorkSheet.Cells[_lastRow, 4] = "NIM";
xlWorkSheet.Cells[_lastRow, 5] = "Address";

Universitas Sumatera Utara

A-7

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