VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "ClassFaceRecogniser"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Public imageWidth           As Integer
Public imageHeight          As Integer

Public noOfFaces            As Integer
Private face(1000)          As ImageProcessing
Private eigenFace(1000)     As ImageProcessing
Private testFace            As ImageProcessing
Private testEigenFace       As ImageProcessing
Public identity             As ImageProcessing

Private nameOfFace(1000)    As String
Private faceTemplate()      As Single

Public Sub init(imgWIDTH As Integer, imgHEIGHT As Integer)
    
    imageWidth = imgWIDTH
    imageHeight = imgHEIGHT
    ReDim faceTemplate(imageWidth, imageHeight)
    
End Sub

Public Sub addFace(facePicture As PictureBox, faceName As String)
  
    Set face(noOfFaces) = New ImageProcessing
    Set eigenFace(noOfFaces) = New ImageProcessing
    Set testFace = New ImageProcessing
    Set testEigenFace = New ImageProcessing
  
    face(noOfFaces).init imageWidth, imageHeight
    eigenFace(noOfFaces).init imageWidth, imageHeight
    testFace.init imageWidth, imageHeight
    testEigenFace.init imageWidth, imageHeight
    face(noOfFaces).update facePicture
  
    nameOfFace(noOfFaces) = faceName
    noOfFaces = noOfFaces + 1
  
    updateFaceTemplate
    updateEigenFaces
  
End Sub

Private Sub updateFaceTemplate()

    'calculates an average face template
    Dim i   As Integer
    Dim x   As Integer
    Dim y   As Integer
  
    For i = 0 To noOfFaces - 1
        For x = 0 To imageWidth - 1
            For y = 0 To imageHeight - 1
                If (i > 0) Then
                    faceTemplate(x, y) = faceTemplate(x, y) + face(i).getPoint(x, y)
                Else
                    faceTemplate(x, y) = face(i).getPoint(x, y)
                End If
            Next
        Next
    Next
  
    For x = 0 To imageWidth - 1
        For y = 0 To imageHeight - 1
            faceTemplate(x, y) = Int(faceTemplate(x, y) / noOfFaces)
        Next
    Next
    
End Sub

Private Sub updateEigenFaces()
  
    'updates all the eigenfaces
    Dim i     As Integer
    Dim x     As Integer
    Dim y     As Integer
    Dim df    As Integer
  
    For i = 0 To noOfFaces - 1
        For x = 0 To imageWidth - 1
            For y = 0 To imageHeight - 1
                df = face(i).getPoint(x, y) - faceTemplate(x, y)
                If (df < 0) Then
                    df = 0
                End If
                eigenFace(i).setPoint x, y, CByte(df)
            Next
        Next
    Next
    
End Sub

Public Function Identify(facePicture As PictureBox) As String
  
    'identifies the given image
    Dim i               As Integer
    Dim x               As Integer
    Dim y               As Integer
    Dim df              As Integer
    Dim Distance        As Long
    Dim minDistance     As Long
    Dim retval          As String
    Dim a               As Integer
    Dim b               As Integer
  
    retval = ""
    testFace.update facePicture
  
    'calculate the eigenface
    For x = 0 To imageWidth - 1
        For y = 0 To imageHeight - 1
            df = testFace.getPoint(x, y) - faceTemplate(x, y)
            If (df < 0) Then
                df = 0
            End If
            testEigenFace.setPoint x, y, CByte(df)
        Next
    Next
  
    'compare it to other eigenfaces
    minDistance = 99999999#
    For i = 0 To noOfFaces - 1
        Distance = 0
        For x = 0 To imageWidth - 1
            For y = 0 To imageHeight - 1
                a = eigenFace(i).getPoint(x, y)
                b = testEigenFace.getPoint(x, y)
                df = Abs(a - b)
                Distance = Distance + df
            Next
        Next
        If (Distance < minDistance) Then
            minDistance = Distance
            retval = nameOfFace(i)
            Set identity = face(i)
        End If
    Next
  
  Identify = retval
  
End Function


