Advertisement
enos

testingform

Apr 22nd, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 13.80 KB | None | 0 0
  1. Imports System.Data
  2. Imports System.Data.SqlClient
  3. Imports Accord.Vision.Detection
  4. Imports Accord.Vision.Detection.Cascades
  5. Imports Emgu.CV
  6. Imports Emgu.CV.Structure
  7. Imports Emgu.CV.Util
  8. Imports Emgu.CV.CvEnum
  9. Imports AForge.Video
  10. Imports System.Drawing.Imaging
  11. Public Class TestingForm
  12.     Dim tmpgambar(2) As Bitmap
  13.     Dim jumlahCoba As Integer
  14.     Dim imgFrame As Image(Of Bgr, Byte)
  15.     Dim vidCapture As Capture
  16.     Dim imgGray As Image(Of Gray, Byte)
  17.  
  18.     Public Sub AppIdle(ByVal sender As System.Object, ByVal e As System.EventArgs)
  19.         Try
  20.             imgFrame = vidCapture.QueryFrame.Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC)
  21.             PictureBox1.Image = imgFrame.Bitmap ' this is where captured image go.
  22.  
  23.  
  24.             Dim detector As HaarObjectDetector
  25.             Dim cascade As New FaceHaarCascade
  26.             '    Dim cascade As New NoseHaarCascade
  27.  
  28.             detector = New HaarObjectDetector(cascade, 30)
  29.  
  30.             detector.SearchMode = ObjectDetectorSearchMode.Average
  31.             detector.ScalingFactor = 1.5
  32.             detector.ScalingMode = ObjectDetectorScalingMode.GreaterToSmaller
  33.             detector.UseParallelProcessing = True
  34.             detector.Suppression = 3
  35.  
  36.             '    Dim sw As Stopwatch = Stopwatch.StartNew
  37.             Dim faceObjects As Rectangle() = detector.ProcessFrame(PictureBox1.Image)
  38.             '   sw.Stop()
  39.  
  40.             Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)
  41.             ''Dim pena As New Pen(Brushes.DarkOliveGreen, 10)
  42.             For Each face In faceObjects
  43.                 Dim CropImage = New Bitmap(face.Width, face.Height)
  44.                 Using grp = Graphics.FromImage(CropImage)
  45.                     grp.DrawImage(PictureBox1.Image, New Rectangle(0, 0, face.Width, face.Height), face, GraphicsUnit.Pixel)
  46.                 End Using
  47.                 '  PictureBox2.Image = CropImage
  48.                 g.DrawRectangle(Pens.Red, face)
  49.  
  50.                 Me.ImageList1.Images.Add(CropImage)
  51.                 If Me.ImageList1.Images.Count <= 3 Then
  52.                     Dim p As New PictureBox
  53.                     p.Image = Me.ImageList1.Images(Me.ImageList1.Images.Count - 1)
  54.                     p.Image.Save(My.Application.Info.DirectoryPath & "\hasil\Capture" & (My.Settings.Jumlahcapture + 1).ToString("000") & ".bmp")
  55.                     My.Settings.Jumlahcapture += 1
  56.                     My.Settings.Save()
  57.                     waktu = Now
  58.                     cekLogin()
  59.                 Else
  60.                     Me.ImageList1.Images.Clear()
  61.                     vidCapture.Dispose()
  62.                     Me.PictureBox1.Image = Nothing
  63.                     RemoveHandler Application.Idle, AddressOf AppIdle
  64.  
  65.                 End If
  66.  
  67.             Next
  68.             g.Dispose()
  69.  
  70.             Me.PictureBox1.Invalidate()
  71.  
  72.  
  73.         Catch ex As Exception
  74.             MessageBox.Show(ex.Message)
  75.         End Try
  76.  
  77.     End Sub
  78.  
  79.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  80.         Button3.Enabled = True
  81.         jumlahCoba = 0
  82.         Dim koneksi As New SqlClient.SqlConnection(My.Settings.koneksiString)
  83.         koneksi.Open()
  84.         Dim dt As New DataTable
  85.         Dim da As New SqlClient.SqlDataAdapter("SELECT * FROM Pengguna  WHERE username='" & Me.usernameTextBox.Text & "' AND PASSWORD='" & Me.passwordTextBox.Text & "'", koneksi)
  86.         da.Fill(dt)
  87.         If dt.Rows.Count > 0 Then
  88.             Dim dr As DataRow = dt.Rows(0)
  89.             Me.GroupBox1.Visible = False
  90.             Me.GroupBox2.Visible = True
  91.             Me.LinkLabel1.Visible = False
  92.             tipePengguna = dr("tipe").ToString
  93.             For i As Integer = 0 To tmpgambar.Length - 1
  94.                 tmpgambar(i) = Konvert(CType(dr("sample" & i + 1), Byte()))
  95.             Next
  96.  
  97.         Else
  98.             MessageBox.Show("Invalid Username and Password")
  99.         End If
  100.     End Sub
  101.     Public Sub CheckStartup()
  102.         Dim Reg = CreateObject("WScript.Shell")
  103.         Try
  104.             Reg.Regread("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\" & My.Application.Info.AssemblyName) = Application.ExecutablePath
  105.         Catch ex As Exception
  106.             Reg.RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\" & My.Application.Info.AssemblyName, Application.ExecutablePath)
  107.         End Try
  108.     End Sub
  109.  
  110.     Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
  111.         If MessageBox.Show("Apakah Anda Yakin Shutdown PC/Laptop ? ", "Peringatan !!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
  112.             powerutility("s")
  113.         End If
  114.  
  115.     End Sub
  116.  
  117.     Private Sub TestingForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  118.         Me.Opacity = 0
  119.         Me.Hide()
  120.         Dim spalas As New SplashForm
  121.         spalas.ShowDialog()
  122.         spalas.Dispose()
  123.         spalas = Nothing
  124.         Me.Opacity = 100
  125.         Timer1.Start()
  126.         Dim koneksi As New SqlClient.SqlConnection(My.Settings.koneksiString)
  127.         koneksi.Open()
  128.         Dim dt As New DataTable
  129.         Dim da As New SqlClient.SqlDataAdapter("SELECT * FROM Pengguna", koneksi)
  130.         da.Fill(dt)
  131.  
  132.         If dt.Rows.Count < 1 Then
  133.             tipePengguna = "admin"
  134.             Dim input As New InputForm
  135.             input.ShowDialog()
  136.         Else
  137.             tipePengguna = "user"
  138.             Me.Show()
  139.         End If
  140.  
  141.     End Sub
  142.  
  143.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  144.         Me.Label5.Text = Me.Label5.Text.Substring(1) & Me.Label5.Text.Substring(0, 1)
  145.     End Sub
  146.  
  147.     Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
  148.         Me.GroupBox2.Visible = False
  149.         Me.GroupBox1.Visible = True
  150.  
  151.     End Sub
  152.  
  153.  
  154.  
  155.  
  156.     Private Sub cekLogin()
  157.         Dim query As String = "INSERT INTO Hasil Values (@nama,@waktu,@ket)"
  158.         Dim koneksi As New SqlClient.SqlConnection(My.Settings.koneksiString)
  159.         koneksi.Open()
  160.         Dim jarak As New List(Of Double)
  161.         Dim PCA As New PCAClass
  162.         Dim input(,) As Double = gambar.GetGrayScale(ImageList1.Images(Me.ImageList1.Images.Count - 1))
  163.         input = PCA.TestingInput(input)
  164.  
  165.         Dim nomor As Integer = 1
  166.         Dim rata As Double = 0
  167.  
  168.         For Each d(,) As Double In PCA.Rekonstruksi
  169.             Dim gbr As New Bitmap(d.GetLength(0), d.GetLength(1))
  170.             For i As Integer = 0 To d.GetLength(0) - 1
  171.                 For j As Integer = 0 To d.GetLength(1) - 1
  172.                     Dim nilai As Integer = 0
  173.                     If Math.Abs(Math.Floor(d(i, j))) > 255 Then
  174.                         nilai = 255
  175.                     Else
  176.                         nilai = Math.Abs(Math.Floor(d(i, j)))
  177.                     End If
  178.                     gbr.SetPixel(j, i, Color.FromArgb(nilai, nilai, nilai))
  179.                 Next
  180.             Next
  181.             Try
  182.                 Dim pic As New PictureBox
  183.                 pic.Image = gbr
  184.                 pic.Image.Save(My.Application.Info.DirectoryPath & "\hasil" & nomor & ".bmp")
  185.             Catch ex As Exception
  186.  
  187.             End Try
  188.  
  189.             jarak.Add(PCA.Ecludiance(input, d))
  190.             nomor += 1
  191.             rata += jarak.Item(jarak.Count - 1)
  192.         Next
  193.         rata = rata / jarak.Count
  194.         Dim total As Double = 0
  195.         For Each n As Double In jarak
  196.             total += Math.Pow(n - rata, 2)
  197.         Next
  198.         total = Math.Sqrt(total / jarak.Count)
  199.  
  200.         Dim nilaiambang = rata + total
  201.         Dim tm() As Double = jarak.ToArray
  202.         Dim rendah, posisi As Integer
  203.         For i As Integer = 0 To jarak.Count - 1
  204.             If i = 0 Then
  205.                 rendah = tm(i)
  206.                 posisi = 1
  207.             ElseIf tm(i) < rendah Then
  208.                 rendah = tm(i)
  209.                 posisi = i + 1
  210.             End If
  211.         Next
  212.  
  213.         Dim tmptest As New Bitmap(My.Application.Info.DirectoryPath & "\hasil" & posisi & ".bmp")
  214.         Dim status As Boolean = False
  215.         For Each t As Bitmap In tmpgambar
  216.             Dim n As Double = PCA.Ecludiance(gambar.GetGrayScale(t), gambar.GetPictArray(tmptest))
  217.             If n <= 400 Then
  218.                 status = True
  219.             End If
  220.         Next
  221.         Me.Label3.Visible = True
  222.         If status Then
  223.             Dim span As TimeSpan = Now.Subtract(waktu)
  224.             Me.Label3.Text = "Waktu Eksekusi : " & span.Hours.ToString("00") & ":" & span.Minutes.ToString("00") & " : " & span.Seconds.ToString("00") & ":" & span.Milliseconds.ToString("000")
  225.             Try
  226.                 Dim da As New SqlClient.SqlDataAdapter(query, koneksi)
  227.                 da.SelectCommand.Parameters.AddWithValue("@nama", usernameTextBox.Text)
  228.                 da.SelectCommand.Parameters.AddWithValue("@waktu", span.Hours.ToString("00") & ":" & span.Minutes.ToString("00") & ":" & span.Seconds.ToString("00") & ":" & span.Milliseconds.ToString("000"))
  229.                 da.SelectCommand.Parameters.AddWithValue("@ket", "Berhasil")
  230.                 da.SelectCommand.ExecuteNonQuery()
  231.             Catch ex As Exception
  232.             End Try
  233.  
  234.  
  235.             MessageBox.Show("Dikenali")
  236.             Try
  237.                 vidCapture.Dispose()
  238.                 RemoveHandler Application.Idle, AddressOf AppIdle
  239.             Catch ex As Exception
  240.             End Try
  241.  
  242.             Me.Hide()
  243.             If tipePengguna.ToLower.Equals("admin") Then
  244.                 Dim admin As New MainForm
  245.                 admin.ShowDialog()
  246.             Else
  247.                 Dim view As New viewpenggunaForm
  248.                 view.loadData(Me.usernameTextBox.Text)
  249.                 view.ShowDialog()
  250.             End If
  251.             Me.Visible = False
  252.         Else
  253.             Dim span As TimeSpan = Now.Subtract(waktu)
  254.             Me.Label3.Text = "Waktu Eksekusi : " & span.Hours.ToString("00") & ":" & span.Minutes.ToString("00") & " : " & span.Seconds.ToString("00") & " : " & span.Milliseconds.ToString("000")
  255.             Try
  256.                 Dim da As New SqlClient.SqlDataAdapter(query, koneksi)
  257.                 da.SelectCommand.Parameters.AddWithValue("@nama", usernameTextBox.Text)
  258.                 da.SelectCommand.Parameters.AddWithValue("@waktu", span.Hours.ToString("00") & ":" & span.Minutes.ToString("00") & ":" & span.Seconds.ToString("00") & ":" & span.Milliseconds.ToString("000"))
  259.                 da.SelectCommand.Parameters.AddWithValue("@ket", "Gagal")
  260.                 da.SelectCommand.ExecuteNonQuery()
  261.             Catch ex As Exception
  262.             End Try
  263.  
  264.             MessageBox.Show("Tidak Dikenali")
  265.             jumlahCoba += 1
  266.             If jumlahCoba >= 3 Then
  267.                 Me.GroupBox2.Visible = False
  268.                 Me.Label2.Visible = True
  269.                 Me.Button4.Visible = True
  270.                 Me.Button7.Visible = True
  271.                 Me.PictureBox1.Image = Nothing
  272.             End If
  273.         End If
  274.         Me.Label3.Visible = False
  275.     End Sub
  276.     Dim waktu As DateTime
  277.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  278.         If Me.Button3.Text.ToLower.Equals("start") Then
  279.             Try
  280.                 jumlahCoba = 0
  281.                 vidCapture = New Capture() ' Put the proper arguments in here
  282.                 vidCapture.QueryFrame()
  283.                 AddHandler Application.Idle, AddressOf AppIdle
  284.                 Me.Button3.Enabled = False
  285.  
  286.  
  287.             Catch ex As Exception
  288.                 MessageBox.Show(ex.Message)
  289.             End Try
  290.  
  291.         ElseIf Me.Button3.Text.ToLower.Equals("load") Then
  292.             Dim op As New OpenFileDialog
  293.             op.InitialDirectory = My.Application.Info.DirectoryPath & "\gambar"
  294.             If op.ShowDialog() = Windows.Forms.DialogResult.OK Then
  295.                 Me.PictureBox1.Image = Image.FromFile(op.FileName)
  296.                 Me.ImageList1.Images.Add(Image.FromFile(op.FileName))
  297.                 Me.Button3.Text = "Cek"
  298.             End If
  299.         Else
  300.             waktu = Now
  301.             cekLogin()
  302.         End If
  303.  
  304.     End Sub
  305.  
  306.     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
  307.         Me.Label2.Visible = False
  308.         Me.Button4.Visible = False
  309.         Me.Button7.Visible = False
  310.         Me.GroupBox1.Visible = True
  311.  
  312.     End Sub
  313.  
  314.     Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
  315.         If MessageBox.Show("Apakah Anda Yakin Shutdown PC/Laptop ? ", "Peringatan !!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
  316.             powerutility("s")
  317.         End If
  318.     End Sub
  319.  
  320.     Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
  321.  
  322.     End Sub
  323.  
  324.     Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
  325.         Button3.Text = "Start"
  326.     End Sub
  327.  
  328.     Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
  329.         Button3.Text = "Load"
  330.     End Sub
  331.  
  332.     Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
  333.         'Me.Hide()
  334.         Dim about As New aboutForm
  335.         about.ShowDialog()
  336.         about.Dispose()
  337.         about = Nothing
  338.         Me.Show()
  339.     End Sub
  340.  
  341.    
  342.  
  343.  
  344.     Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
  345.  
  346.     End Sub
  347.  
  348.    
  349.     Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
  350.  
  351.     End Sub
  352. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement