You are on page 1of 16

MAIN FORM CODING:

using using using using using using using using using

System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Text; System.Windows.Forms; System.Drawing.Imaging; Microsoft.VisualBasic;

using System.Runtime.InteropServices; using System.Runtime.Serialization.Formatters.Binary; using System.IO; using WebCam;

namespace WindowsApplication3 { public partial class Form1 : Form { private int hWnd; const const const const const const const const const const const const const const const int int int int int int int int int int int int int int int WM_CAP_START = 1024; WS_CHILD = 1073741824; WS_VISIBLE = 268435456; WM_CAP_DRIVER_CONNECT = (WM_CAP_START + 10); WM_CAP_DRIVER_DISCONNECT = (WM_CAP_START + 11); WM_CAP_EDIT_COPY = (WM_CAP_START + 30); WM_CAP_SEQUENCE = (WM_CAP_START + 62); WM_CAP_FILE_SAVEAS = (WM_CAP_START + 23); WM_CAP_SET_SCALE = (WM_CAP_START + 53); WM_CAP_SET_PREVIEWRATE = (WM_CAP_START + 52); WM_CAP_SET_PREVIEW = (WM_CAP_START + 50); SWP_NOMOVE = 2; SWP_NOSIZE = 1; SWP_NOZORDER = 4; HWND_BOTTOM = 1;

//---The capGetDriverDescription function retrieves the // version description of the capture driver--[System.Runtime.InteropServices.DllImport("avicap32.dll")] static extern bool capGetDriverDescriptionA( short wDriverIndex, string lpszName, int cbName, string lpszVer, int cbVer); //---The capCreateCaptureWindow function creates a capture // window--[System.Runtime.InteropServices.DllImport("avicap32.dll")] static extern int capCreateCaptureWindowA( string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWnd, int nID); //---This function sends the specified message to a window or // windows--[System.Runtime.InteropServices.DllImport( "user32", EntryPoint = "SendMessageA")] static extern int SendMessage( int hwnd, int Msg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam); //---Sets the position of the window relative to the screen // buffer--[System.Runtime.InteropServices.DllImport(

"user32", EntryPoint = "SetWindowPos")] static extern int SetWindowPos( int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags); //--This function destroys the specified window-[System.Runtime.InteropServices.DllImport("user32")] static extern bool DestroyWindow(int hndw); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } neuralnetwork nobj = new neuralnetwork(); private void pic1_Click(object sender, EventArgs e) { PictureBox pics; try{ openFileDialog1.Filter = "All Files|*.*"; if(openFileDialog1.ShowDialog() == DialogResult.OK) { pics = (PictureBox)sender; pics.Image = new Bitmap(openFileDialog1.FileName); pics.Tag = openFileDialog1.FileName; } }catch(Exception ex){ MessageBox.Show("Error Occured While Brousing Images...", "Neural Network"); } }

public void cmdInit_Click(object sender, EventArgs e) { clsData obj; nobj.inpsize = 651; nobj.hid1size = 96; nobj.hid2size = 144; nobj.outsize=5; nobj.init(); MemoryStream MemoryStream MemoryStream MemoryStream MemoryStream ms1 ms2 ms3 ms4 ms5 = = = = = new new new new new MemoryStream(); MemoryStream(); MemoryStream(); MemoryStream(); MemoryStream();

Bitmap bmp1 = (Bitmap)pic1.Image; Bitmap bmp2 = (Bitmap)pic2.Image;

Bitmap bmp3 = (Bitmap)pic3.Image; Bitmap bmp4 = (Bitmap)pic4.Image; Bitmap bmp5 = (Bitmap)pic5.Image; bmp1.Save(ms1, nobj.bmpImage1 bmp2.Save(ms2, nobj.bmpImage2 bmp3.Save(ms3, nobj.bmpImage3 bmp4.Save(ms4, nobj.bmpImage4 bmp5.Save(ms5, nobj.bmpImage5 ImageFormat.Bmp); = ms1; ImageFormat.Bmp); = ms2; ImageFormat.Bmp); = ms3; ImageFormat.Bmp); = ms4; ImageFormat.Bmp); = ms5;

obj= ConvertToGreyScale(pic1); PicTr1.Image = obj.img; PicTr1.Tag = obj.arr; nobj.temp1 = (double[])PicTr1.Tag; for (int i = 0; i < 651; i++) nobj.temp[0, i] = nobj.temp1[i]; obj = ConvertToGreyScale(pic2); PicTr2.Image = obj.img; PicTr2.Tag = obj.arr; nobj.temp1 = (double[])PicTr2.Tag; for (int i = 0; i < 651; i++) nobj.temp[1, i] = nobj.temp1[i]; obj = ConvertToGreyScale(pic3); PicTr3.Image = obj.img; PicTr3.Tag = obj.arr; nobj.temp1 = (double[])PicTr3.Tag; for (int i = 0; i < 651; i++) nobj.temp[2, i] = nobj.temp1[i]; obj = ConvertToGreyScale(pic4); PicTr4.Image = obj.img; PicTr4.Tag = obj.arr; nobj.temp1 = (double[])PicTr4.Tag; for (int i = 0; i < 651; i++) nobj.temp[3, i] = nobj.temp1[i]; obj = ConvertToGreyScale(pic5); PicTr5.Image = obj.img; PicTr5.Tag = obj.arr; nobj.temp1 = (double[])PicTr5.Tag; for (int i = 0; i < 651; i++) nobj.temp[4, i] =nobj.temp1[i]; nobj.Generate(); pgb1.Visible = true; //forwarding starts here***************** for (int l = 0; l <= 455; l++) { pgb1.Increment(1);

for (int m = 0; m < 5; m++) { nobj.FeedFwd(m); } nobj.total = 0; } pgb1.Visible = false; pgb1.Value = 0; } private clsData ConvertToGreyScale(PictureBox Pic1) { int pixel = 1; int x, xx, y, yy, w, h, screenX, screenY, screenLeft, screenTop, screenWidth, screenHeight ; double Value; screenLeft=0; screenTop = 0; //pic1.Image = Image.FromFile("D:\\Hello.bmp"); w = Pic1.Image.Width /25; h = Pic1.Image.Height / 25; if(w<1) w=1; if(h<1) h=1; double[] arr=new double[651]; Bitmap bmp= new Bitmap( Pic1.Image); Bitmap bmp2= new Bitmap(w * 25, h * 25, PixelFormat.Format64bppArgb); Color cl; pixel = w * h; screenWidth = Pic1.Image.Width; screenHeight = Pic1.Image.Height; int cnt = 0; for (x =0 ; x< 24;x++) { for( y = 0;y<24;y++) { screenX = x * w; screenY = y * h; Value = 0; for( xx = screenX ;xx<= screenX + w - 1;xx++) { for (yy = screenY ;yy<= screenY + h - 1;yy++) { cl = bmp.GetPixel(xx, yy);//get the color of a pixel //'Convert to greyscale Value = Value + 0.299 * cl.R + 0.587 * cl.G + 0.114 * }

cl.B;

} //calculate average pixel Value = Value / pixel; cl = Color.FromArgb(Convert.ToInt32(Value), Convert.ToInt32(Value), Convert.ToInt32(Value)); //convert average value to color arr[cnt] = Value/1;//here i changed to fraction by 1000 not reqd cnt += 1; //************************************************************ ******************* //Fill resultant cordinates with average color for( xx = screenX ;xx<= screenX + w - 1;xx++) { for( yy = screenY;yy<= screenY + h - 1;yy++) bmp2.SetPixel(xx, yy, cl); //set the color }

} }

// MessageBox.Show("hello",PicTr1.Tag[12]); clsData obj = new clsData(); obj.img = (Image)bmp2; obj.arr = arr; return (obj); } private void cmdTrain_Click(object sender, EventArgs e) { pgb1.Visible = true; //forwarding starts here***************** for(int l=0;l<=455;l++) { pgb1.Increment(1); for (int m = 0; m < 5; m++) { nobj.FeedFwd(m); } nobj.total = 0; } pgb1.Visible = false; pgb1.Value = 0;

} private void cmdTest_Click(object sender, EventArgs e) { IDataObject data; Image bmap; //---copy the image to the Clipboard---

SendMessage(hWnd, WM_CAP_EDIT_COPY, 0, 0); //---retrieve the image from Clipboard and convert it // to the bitmap format--data = Clipboard.GetDataObject(); if (data.GetDataPresent(typeof(System.Drawing.Bitmap))) { bmap = ((Image)(data.GetData(typeof( System.Drawing.Bitmap)))); // bmap.Save("C:\\Data\\" + "hello" + ".bmp"); } //string fName = "c:\\Capture\\hello.bmp"; //PictureBox pics=new PictureBox(); ////pics = (PictureBox)sender; //pics.Image = new Bitmap(fName); //pics.Tag = fName;

clsData obj; obj = ConvertToGreyScale(picorg); //PicTr1.Image = obj.img; picorg.Tag = obj.arr; nobj.temp6 = (double[])picorg.Tag; for (int i = 0; i < 651; i++) nobj.temp[0, i] = nobj.temp6[i]; nobj.check(); if (nobj.max <.70) { pictest.Image = null; //Bitmap bbb = new Bitmap(@"C:\Documents and Settings\Administrator\My Documents\My Pictures\picture.bmp"); //pictest.Image = (Image)bbb; Brush b = Brushes.Red; Font ft = new Font(FontFamily.GenericSansSerif, 13, FontStyle.Bold); pictest.CreateGraphics().DrawString("Not Recognised", ft, b, 5, (pictest.Height / 2) - 13); } else { nobj.maxloc++; //MessageBox.Show("FACE RECOGNISED"); pictest.Image = null; //MessageBox.Show(nobj.maxloc + ""); switch (nobj.maxloc) { case 1: pictest.Image = new Bitmap(nobj.bmpImage1); break; case 2:

} nobj.max = 0; } }

pictest.Image break; case 3: pictest.Image break; case 4: pictest.Image break; case 5: pictest.Image break;

= new Bitmap(nobj.bmpImage2); = new Bitmap(nobj.bmpImage3); = new Bitmap(nobj.bmpImage4); = new Bitmap(nobj.bmpImage5);

private void Form1_Load_1(object sender, EventArgs e) { pgb1.Visible = false; } private void save_Click(object sender, EventArgs e) {

SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "Weight Files|*.frs"; if (dlg.ShowDialog() == DialogResult.OK) { try { Stream stream = File.Open(dlg.FileName, FileMode.Create); BinaryFormatter bformatter = new BinaryFormatter(); bformatter.Serialize(stream, nobj); stream.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } } private void PreviewVideo(PictureBox pbCtrl) { hWnd = capCreateCaptureWindowA("0", WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, pbCtrl.Handle.ToInt32(), 0); if (SendMessage(hWnd, WM_CAP_DRIVER_CONNECT, 0, 0) != 0) { SendMessage(hWnd, WM_CAP_SET_SCALE, 1, 0); SendMessage(hWnd, WM_CAP_SET_PREVIEWRATE, 30, 0); SendMessage(hWnd, WM_CAP_SET_PREVIEW, 1, 0);

SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, pbCtrl.Width, pbCtrl.Height, SWP_NOMOVE | SWP_NOZORDER); } else { DestroyWindow(hWnd); } } private void load_Click(object sender, EventArgs e) { OpenFileDialog opg = new OpenFileDialog(); opg.Filter = "Weight Files|*.frs"; if (opg.ShowDialog() == DialogResult.OK) { try { Stream stream = File.Open(opg.FileName, FileMode.Open); BinaryFormatter bformatter = new BinaryFormatter(); nobj = (neuralnetwork)bformatter.Deserialize(stream); stream.Close(); pic1.Image pic2.Image pic3.Image pic4.Image pic5.Image = = = = = new new new new new Bitmap(nobj.bmpImage1); Bitmap(nobj.bmpImage2); Bitmap(nobj.bmpImage3); Bitmap(nobj.bmpImage4); Bitmap(nobj.bmpImage5);

} catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } }

} private void button1_Click(object sender, EventArgs e) { this.Close();

} private void button2_Click(object sender, EventArgs e) {

PreviewVideo(picorg); groupBox1.Visible = false; groupBox2.Visible = false; } } }

NEURAL NETWORK CODE

using using using using using using using

System; System.Collections.Generic; System.Text; System.Windows.Forms; System.Runtime.Serialization; System.IO; System.Drawing.Imaging;

using System.Drawing; namespace WindowsApplication3 { [Serializable()] class neuralnetwork { public int inpsize, outsize, hid1size, hid2size; public int flag, m, total,maxloc; public double max=0; public double[,] temp; public double[] temp1; public double[] temp6; public double[,] weight1; public double[,] weight2; public double[,] weight3; public double[] hid1; public double[] hid2; public double[] out5; public double[] calc; public double[] error; public double[] hid2error; public double[] hid1error; public double lrate = .09; public int[,] targetout; public public public public public System.IO.MemoryStream System.IO.MemoryStream System.IO.MemoryStream System.IO.MemoryStream System.IO.MemoryStream bmpImage1 bmpImage2 bmpImage3 bmpImage4 bmpImage5 = = = = = null; null; null; null; null;

public void init() { temp = new double[outsize, inpsize]; temp1 = new double[inpsize]; temp6 = new double[inpsize]; weight1 = new double[inpsize, hid1size]; weight2 = new double[hid1size, hid2size]; weight3 = new double[hid2size, outsize]; hid1 = new double[hid1size];

hid2 = new double[hid2size]; out5 = new double[outsize]; calc = new double[245]; error = new double[outsize]; hid2error = new double[hid2size]; hid1error = new double[hid1size]; targetout = new int[outsize, outsize];// { { 1, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0 }, { 0, 0, 1, 0, 0 }, { 0, 0, 0, 1, 0 }, { 0, 0, 0, 0, 1 } }; } public void Generate() { targetout weight1 = weight2 = weight3 = } public double[,] GenRandom(int i1, int i2, double[,] arry) { Random a = new Random(); for (int k = 0; k < i1; k++) { for (int i = 0; i < i2; i++) { double s = a.Next(-1000, 1000); s = s / 1000.0f; arry[k, i] = s; = dectobin(outsize, targetout); GenRandom(inpsize, hid1size, weight1); GenRandom(hid1size, hid2size, weight2); GenRandom(hid2size, outsize, weight3);

} } return (arry); }

public int[,] dectobin(int outsize, int[,] array) { for (int i = 0; i < outsize; i++) { for (int j = 0; j < outsize; j++) { if (i == j) array[i, j] = 1; else array[i, j] = 0; } } return (array);

public double sigmoid(double n) {

double result = (1.0f / (1.0f + (Math.Pow(Math.E, -n)))); return result; } public void FeedFwd(int m) { double result = 0; int correct; for (int i = 0; i < hid1size; i++) { for (int j = 0; j < inpsize; j++) { result = result + temp[m, j] * weight1[j, i]; } calc[i] = result; hid1[i] = sigmoid(result); result = 0;

} for (int i = 0; i < hid2size; i++) { for (int j = 0; j < hid1size; j++) { result = result + hid1[j] * weight2[j, i]; } hid2[i] = sigmoid(result); result = 0;

for (int i = 0; i < outsize; i++) { for (int j = 0; j < hid2size; j++) { result = result + hid2[j] * weight3[j, i]; } out5[i] = sigmoid(result); result = 0; error[i] = out5[i] * (1 - out5[i]) * (targetout[m, i] out5[i]); } BackProp(); } public void BackProp() { double result = 0; for (int i = 0; i < hid2size; i++) { for (int j = 0; j < outsize; j++)

{ hid2[i])); } }

weight3[i, j] = weight3[i, j] + (lrate * (error[j] *

result = 0; for (int i = 0; i < hid2size; i++) { for (int j = 0; j < outsize; j++) { } result = result + (weight3[i, j] * error[j]);

hid2error[i] = result * hid2[i] * (1 - hid2[i]); result = 0;

for (int i = 0; i < hid1size; i++) { for (int j = 0; j < hid2size; j++) { weight2[i, j] = weight2[i, j] + (lrate * (hid2error[j] * hid1[i])); } } result = 0; for (int i = 0; i < hid1size; i++) { for (int j = 0; j < hid2size; j++) { result = result + (weight2[i, j] * hid2error[j]); } hid1error[i] = result * hid1[i] * (1 - hid1[i]); result = 0; } for (int i = 0; i < inpsize; i++) { for (int j = 0; j < hid1size; j++) {

temp[m, i])); } } }

weight1[i, j] = weight1[i, j] + (lrate * (hid1error[j] *

public void check() { double result = 0; int correct; for (int i = 0; i < hid1size; i++) { for (int j = 0; j < inpsize; j++) { result = result + temp[0, j] * weight1[j, i]; } calc[i] = result; hid1[i] = sigmoid(result); result = 0; } for (int i = 0; i < hid2size; i++) { for (int j = 0; j < hid1size; j++) { result = result + hid1[j] * weight2[j, i]; } hid2[i] = sigmoid(result); result = 0; } for (int i = 0; i < outsize; i++) { for (int j = 0; j < hid2size; j++) { result = result + hid2[j] * weight3[j, i]; } out5[i] = sigmoid(result); result = 0; //error[i] = out5[i] * (1 - out5[i]) * (targetout[m,i] // error[i] = 1 - out5[i]; // MessageBox.Show("erroro)))-"); // MessageBox.Show(error[0] + "");

out5[i]);

//MessageBox.Show("Face recogonised the smallest is"); //for (int i = 0; i < outsize; i++) // MessageBox.Show(out5[i] + "");

for (int i = 0; i < outsize; i++) { if (out5[i] > max) { max = out5[i]; maxloc = i; } } //MessageBox.Show("max value"); //MessageBox.Show(max + ""); } } }