Jump to content
Sign in to follow this  
IchBistTod

facial recognition

Recommended Posts

IchBistTod

Due to a lot of hard work and help of others in these forums I have successfully made an application to compare two images.

The next step is facial recoginition. However theissue is the current method only returns 25% positives on faces, this is much too low.

This is because if the face is offset by even 1 pixel to the resolution of the image(a cm to the left,right up or down) from the previously stored image, the comparison is thrown offas its comparing pixels that dont match up to the location of the previous pixels.

I am requesting help in perhaps, finding where the faces start to match up and comparing the two images starting from there. All work thus far is below.

(any improvements are appreciated as well)

ImageCompare (Main App):

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ISFUNCS.au3>

#Region ### START Koda GUI section ### Form=
;GUICreate(".", 0, 0, 1, 1)
;WinSetOnTop(".", "", 1)
;GUISetState()
$Form1 = GUICreate("IS", 652, 443, 192, 124)
$Pic1 = GUICtrlCreatePic("select.jpg", 16, 12, 300, 330, BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS))

$loadfromis1 = GUICtrlCreateButton("Load From Is File", 50,345)
$exporttois1 = GUICtrlCreateButton("Export To Is File", 185,345)

$loadfromis2 = GUICtrlCreateButton("Load From Is File", 370,345)
$exporttois2 = GUICtrlCreateButton("Export To Is File", 505,345)


$Pic2 = GUICtrlCreatePic("select.jpg", 335, 12, 300, 330, BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS));
GUICtrlCreateLabel("Results:", 305, 410)
$lbl1 = GUICtrlCreateLabel("", 290, 425, 100, 25)
$compare = GUICtrlCreateButton("Compare", 288, 380, 75, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $P1I, $P2I

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        case $Pic1
            $p1I =  FileOpenDialog("select an image", @WorkingDir, "All Files(*.*)")
            GUICtrlSetImage($Pic1, $p1I)
        case $Pic2
            $p2I =  FileOpenDialog("select an image", @WorkingDir, "All Files(*.*)")
            GUICtrlSetImage($Pic2, $p2I)
        case $compare
            FileDelete( @ScriptDir & "\ImageTest1.temp.IS")
FileDelete( @ScriptDir & "\ImageTest2.temp.IS")
            _ImagetoIS($P1I, @ScriptDir & "\ImageTest1.temp.IS")
    _ImageToIS($P2I, @ScriptDir & "\ImageTest2.temp.IS")
$matrix1 = _ReadISFile( @ScriptDir & "\ImageTest1.temp.IS")
$matrix2 = _ReadISFile( @ScriptDir & "\ImageTest2.temp.IS")
;MsgBox(16, "", UBound($matrix1,1)&@CRLF&UBound($matrix1,2))
FileDelete( @ScriptDir & "\ImageTest1.temp.IS")
FileDelete( @ScriptDir & "\ImageTest2.temp.IS")
GUICtrlSetData($lbl1, _MatrixCompare($matrix1, $matrix2, UBound($matrix1,1)-1&"x"&UBound($matrix1,2)-1, 4)&"% Match")
    EndSwitch
WEnd

ISFUNCS.au3(Include):

#include-once
#include <string.au3>
#include <GDIPlus.au3>
#Region MatrixCompare
#cs
Function Matrix Compare
Author: Edward
Description: Compares two 1-based matricies of the same size
Arguments:
    $array1 - the first matrix
    $array2 - the second matrix
    $size - the demensions of the matricies(ex: 5)
    $deimal - the decimal point to return the result to
#CE
func _MatrixCompare($array1, $array2, $size, $decimal)
    $timer = TimerInit()
    local $match = 0
    $size = StringSplit($size, "x", 2)
    for $i=1 to $size[0]
        for $i2 = 1 to $size[1]
            if $array1[$i][$i2] = $array2[$i][$i2] Then
                $match +=1
            EndIf
        Next
    Next
    $total = $size[0]*$size[1]
    $return = ($match/$total)*100
    if StringInStr($return, ".") Then
    $returnx = StringSplit($return, ".")
    $return = $returnx[1]&"."&StringLeft($returnx[2], $decimal)
EndIf
    ConsoleWrite(TimerDiff($timer)&@CRLF)
    Return $return
EndFunc
#EndRegion
#Region Read .IS File
#CS
Function Read IS(Image Scan) File
Author: Edward
Description: Reads a file in IS format and returns a one-based matrix
Arguments:
    $file - the file to be read
#CE
func _ReadISFile($file)
    $timer = TimerInit()
    $data = FileRead($file)

    $darray = StringSplit(StringReplace($data, @CR, ""), @LF, 2)
    $demensions = _StringBetween($darray[0], "[", "|")
$demensions = StringSplit($demensions[0], "x",2)
    Dim $ismatrix[$demensions[0]+1][$demensions[1]+1]
    for $i=1 to $demensions[0]
        $dm=StringSplit($darray[$i], "|")
        for $ix=1 to $demensions[0]
            $ismatrix[$i][$ix] =  $dm[$ix]
        Next
    Next
        ConsoleWrite(TimerDiff($timer)&@CRLF)
    Return $ismatrix
EndFunc
#EndRegion
#Region File To IS
#CS
Function File To IS
Author: Edward
Description: reads a normal file and converts it to IS format.
Arguments:
    $file - the file to be read
#CE
Func _FileToIS($file)
    $handle = FileOpen($file,16)
    $data = FileRead($handle)
    $data2 = BinaryToString($data,1)
    $data3 = StringSplit($data2,"",0)
    $matrix = "[1x"&$data3[0]&"]"&@CRLF
    for $i = 1 to $data3[0]
        $matrix &= $data3[$i]&"|"
    Next
    $matrix&=@CRLF
        Return $matrix
EndFunc
#EndRegion

#Region Matrix to Image
#CS
Function Matrix To Image
Author: AndyG
Description: Turns an IS Matrix of colors back into an image on the specified window.
Arguments:
    $file - the file to be read
#CE
func _MatrixToImage($array, $Window)
$hWnd = WinGetHandle($window)
GUISetState()
_GDIPlus_Startup()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWnd)

For $x = 1 To UBound($array, 1) - 1
    For $y = 0 To UBound($array, 2) - 1
        $col = "0xFF" & StringMid($array[$x][$y], 5, 2) & StringMid($array[$x][$y], 3, 2) & StringMid($array[$x][$y], 1, 2)  ;BGR to RGB
        $hPen = _GDIPlus_PenCreate($col)    ;
        _GDIPlus_GraphicsDrawEllipse($hGraphic, $y, $x, 1, 1, $hPen)
    Next
Next
EndFunc
#EndRegion

#Region Image to IS
#CS
Function Image to IS
Author: Authenticity
Description: Turns an image into an IS file format for latter processing as an IS matrix.
Arguments:
    $simage - the image to be read
    $soutfile - the file to write the data to
#CE
Func _ImageToIS($sImage, $sOutputFile)
    Local $hImage, $hBmp, $iW, $iH
    Local $tBD, $iWidth, $iHeight, $iStride, $iFmt
    Local $pData, $tPixels, $xBytes
    Local $avPixels, $hFile

    $hFile = FileOpen($sOutputFile, 2)
    If @error Then Return SetError(1, 1, 0)

    _GDIPlus_Startup()

    $hImage = _GDIPlus_ImageLoadFromFile($sImage)
    If @error Then Return SetError(1, 2, 0)

    $iW = _GDIPlus_ImageGetWidth($hImage)
    $iH = _GDIPlus_ImageGetHeight($hImage)
    $hBmp = _GDIPlus_BitmapCloneArea($hImage, 0, 0, $iW, $iH, $GDIP_PXF32ARGB)
    $tBD = _GDIPlus_BitmapLockBits($hBmp, 0, 0, $iW, $iH, BitOR($GDIP_ILMREAD, $GDIP_ILMWRITE), $GDIP_PXF32ARGB)

    $iWidth = DllStructGetData($tBD, 1)
    $iHeight = DllStructGetData($tBD, 2)
    $iStride = DllStructGetData($tBD, 3)
    $iFmt = DllStructGetData($tBD, 4)
    $pData = DllStructGetData($tBD, 5)

    $tPixels = DllStructCreate("byte[" & $iW * $iH * 4 & "]", $pData)
    $xBytes = DllStructGetData($tPixels, 1)

    $sData = StringTrimRight(StringRegExpReplace(StringTrimLeft($xBytes, 2), "(.{" & 8 * $iWidth & "})", "\1@"), 1)
    $avData = StringSplit($sData, "@")
 FileWrite($hFile, "["&$ih&"x"&$iW&"|]"&@CRLF)
    FileWrite($hFile, StringRegExpReplace($avData[1], "(.{8})(?!$)", "\1|"))
    For $i = 2 To $avData[0]
        FileWrite($hFile, "|"&@CRLF & StringRegExpReplace($avData[1], "(.{8})(?!$)", "\1|"))
    Next
    FileClose($hFile)

    _GDIPlus_BitmapUnlockBits($hBmp, $tBD)
    _WinAPI_DeleteObject($hBmp)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()
EndFunc
#Endregion
#Region File To Matrix
#CS
Function File To Matrix
Author: Edward
Description: Turns the binary data of a file into .IS format for comparison.
Arguments:
    $file - the file to be read
#CE
Func file2matrix($file)
    $handle = FileOpen($file,16)
    $data = FileRead($handle)
    ;$data2 = StringToBinary($data)
    $data3 = StringSplit($data,"",0)
    $matrix = "[1x"&$data3[0]&"|]"&@CRLF
    for $i = 1 to $data3[0]
        $matrix &= StringReplace(StringReplace($data3[$i]&"|", @CR, "@CR"), @LF, "@LF")
    Next
    $matrix&=@CRLF
        Return $matrix
    EndFunc
#EndRegion

[center][/center][center]=][u][/u][/center][center][/center]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.