📄 classfacerecogniser.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "ClassFaceRecogniserNeural"
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
Dim Face(1000) As classImageProcessing
Dim isFace(1000) As Boolean
Dim testFace As classImageProcessing
Dim testScene As classImageProcessing
Dim ProbabilityMatrix As classImageProcessing
Public LocatorStepSize As Integer
'store the location and probabilities of likely faces within a picture
Public NoOfProbabilities As Integer
Dim faceProbability(1000, 8) As Long
Dim NNet As ClassBackprop
Const no_of_hiddens = 5
Public Success As Single
Dim good As Double
Dim bad As Double
'Face probability threshold
Public Threshold As Single
Const CLASSIFICATION_FACE = 0
Const CLASSIFICATION_NONFACE = 1
Public Sub init(image_width As Integer, image_height As Integer)
imageWidth = image_width
imageHeight = image_height
Set testFace = New classImageProcessing
Call testFace.init(imageWidth, imageHeight)
Set NNet = New ClassBackprop
Call NNet.init(image_width * image_height, no_of_hiddens, 2)
NNet.learningRate = 0.3
Success = 0
Threshold = 0.8
LocatorStepSize = 4
End Sub
Public Sub Train()
Dim faceIndex As Integer
Dim classification As Integer
faceIndex = Int(Rnd * NoOfFaces)
Call NNet.setImage(Face(faceIndex))
If (isFace(faceIndex)) Then
Call NNet.setClassification(CLASSIFICATION_FACE)
Else
Call NNet.setClassification(CLASSIFICATION_NONFACE)
End If
Call NNet.update
classification = NNet.getClassification
If (isFace(faceIndex)) Then
If (classification = CLASSIFICATION_FACE) Then
good = good + 1
Else
bad = bad + 1
End If
Else
If (classification = CLASSIFICATION_NONFACE) Then
good = good + 1
Else
bad = bad + 1
End If
End If
Success = (good / (good + bad)) * 100
End Sub
Public Function TrainingError() As Single
TrainingError = NNet.BPerrorTotal
End Function
Public Function IsaFace(facePic As PictureBox, Optional topX As Variant, Optional topY As Variant, Optional width As Variant, Optional height As Variant) As Boolean
If (IsMissing(topX)) Then
Call testFace.update(facePic)
Else
Call testFace.update(facePic, topX, topY, width, height)
End If
Call NNet.setImage(testFace)
Call NNet.feedForward
If (NNet.getClassification = CLASSIFICATION_FACE) Then
IsaFace = True
Else
IsaFace = False
End If
End Function
Public Sub addFace(facePicture As PictureBox)
If (NoOfFaces < 1000) Then
Set Face(NoOfFaces) = New classImageProcessing
Call Face(NoOfFaces).init(imageWidth, imageHeight)
Call Face(NoOfFaces).update(facePicture)
isFace(NoOfFaces) = True
NoOfFaces = NoOfFaces + 1
End If
End Sub
Public Sub addNonFace(NonFacePicture As PictureBox)
If (NoOfFaces < 1000) Then
Set Face(NoOfFaces) = New classImageProcessing
Call Face(NoOfFaces).init(imageWidth, imageHeight)
Call Face(NoOfFaces).update(NonFacePicture)
isFace(NoOfFaces) = False
NoOfFaces = NoOfFaces + 1
End If
End Sub
Public Sub showProbabilities(canvas As PictureBox)
Dim i As Integer
Dim tx As Integer
Dim ty As Integer
Dim width As Integer
Dim height As Integer
Dim max As Single
Dim bestFit As Integer
Dim probability As Integer
Dim xx As Integer
Dim yy As Integer
Call testScene.show(canvas)
'transparent fill style
canvas.FillStyle = 1
canvas.DrawWidth = 1
max = -1
bestFit = -1
For i = 0 To NoOfProbabilities - 1
tx = faceProbability(i, 0)
ty = faceProbability(i, 1)
width = faceProbability(i, 2)
height = faceProbability(i, 3)
xx = (tx + (width / 2)) / LocatorStepSize
yy = (ty + (height / 2)) / LocatorStepSize
probability = ProbabilityMatrix.getPoint(xx, yy)
If (probability > max) Then
max = probability
bestFit = i
End If
If (probability < 220) Then
canvas.Line (tx, ty)-(tx + width, ty + height), RGB(255, 0, 0), B
Else
canvas.Line (tx, ty)-(tx + width, ty + height), RGB(0, 255, 0), B
End If
Next
End Sub
Public Sub save(filename As String)
Call NNet.save(filename)
End Sub
Public Sub load(filename As String)
Call NNet.load(filename)
End Sub
Public Function IdentifyWithinPicture(scene As PictureBox, topX As Integer, topY As Integer, width As Integer, height As Integer) As String
'scans a picture and calculates the probability of a face
'existing at any given location
Dim x As Integer
Dim y As Integer
Dim xx As Integer
Dim yy As Integer
Dim subImageWidth As Integer
Dim subimageHeight As Integer
Dim prob As Integer
NoOfProbabilities = 0
'grab the scene
Set testScene = Nothing
Set testScene = New classImageProcessing
Call testScene.init(width, height)
Call testScene.update(scene, topX, topY, width, height)
'create a probability matrix
Set ProbabilityMatrix = Nothing
Set ProbabilityMatrix = New classImageProcessing
Call ProbabilityMatrix.init((topX + width) / LocatorStepSize, (topY + height) / LocatorStepSize)
subImageWidth = width
subimageHeight = height
While (subImageWidth > imageWidth) And (subimageHeight > imageHeight)
For x = 0 To width - subImageWidth - 1 Step LocatorStepSize
For y = 0 To height - subimageHeight - 1 Step LocatorStepSize
'insert the subimage into the neural net
Call NNet.setImageScaled(testScene, x, y, subImageWidth, subimageHeight, imageWidth, imageHeight)
Call NNet.feedForward
'get the decision
If (NNet.getClassification = CLASSIFICATION_FACE) Then
If (NNet.ClassificationConfidence > Threshold) And (NoOfProbabilities < 1000) Then
faceProbability(NoOfProbabilities, 0) = topX + x
faceProbability(NoOfProbabilities, 1) = topY + y
faceProbability(NoOfProbabilities, 2) = subImageWidth
faceProbability(NoOfProbabilities, 3) = subimageHeight
faceProbability(NoOfProbabilities, 4) = NNet.ClassificationConfidence
xx = (topX + x + (subImageWidth / 2)) / LocatorStepSize
yy = (topY + y + (subimageHeight / 2)) / LocatorStepSize
prob = ProbabilityMatrix.getPoint(xx, yy)
If (prob = 0) Then
prob = (prob + (NNet.ClassificationConfidence * 255)) / 2
Else
prob = NNet.ClassificationConfidence * 255
End If
Call ProbabilityMatrix.setPoint(xx, yy, CByte(prob))
NoOfProbabilities = NoOfProbabilities + 1
End If
End If
Next
Next
subImageWidth = subImageWidth - 10
subimageHeight = subimageHeight - 10
Wend
End Function
Public Sub showProbabilityMatrix(canvas As PictureBox)
Call ProbabilityMatrix.show(canvas)
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -