Page 1 of 1

AI on the operation of the graphics verification code

Posted: Mon May 08, 2017 2:54 am
by MiLoo
Super simple pure PB code, neural network on the application of graphics verification code.
超级简单的纯PB代码,神经网络对图形验证码的应用.
Image
http://120.24.76.95:8090/images/neural_network_01.jpg

Graphic Verification Code Identifies the process and results of training:
图形验证码识别训练的过程和结果:
Image
http://120.24.76.95:8090/images/neural_network_14.jpg

Image
http://120.24.76.95:8090/images/neural_network_15.jpg

完整的源代码包及简单的说明下载:
http://120.24.76.95:8090/neural_network.rar

=========ADD===========
Detailed examples to explain:
详细的实例讲解:
1.http://blog.sina.com.cn/s/blog_4e4522ce0102wrz8.html
2.http://blog.sina.com.cn/s/blog_4e4522ce0102ws1a.html
3.http://blog.sina.com.cn/s/blog_4e4522ce0102ws1o.html
=======================

Simple neural network code:
简易型的神经网络代码:

Code: Select all

ForEach _ListImages()
   For Pos = 0 To 3
      *pNumber.__NumberInfo = _ListImages()\Number[Pos]
      For Idx = 0 To #MAI_Number
         Convolue.f = _DimWeight(Idx, 0) * -1
         For c = 0 To #MAI_Matrix-1
            Convolue + _DimWeight(Idx, c+1) * *pNumber\Matrix[c]
         Next 
         OutResult.f = 1/(1+Exp(-Convolue))
         If Idx = *pNumber\Number And OutResult < #MAI_Judged
            AntiConvolue.f = Modulus * (0.90 - OutResult) * Exp(-Convolue)/Sqr(1+Exp(-Convolue))
         ElseIf Idx <> *pNumber\Number And OutResult >= #MAI_Judged 
            AntiConvolue.f = Modulus * (0.10 - OutResult) * Exp(-Convolue)/Sqr(1+Exp(-Convolue))
         Else 
            Continue
         EndIf 
         _DimWeight(Idx, 0) - 01*AntiConvolue
         For c = 0 To #MAI_Matrix-1
            _DimWeight(Idx, c+1) + AntiConvolue * *pNumber\Matrix[c]
         Next
      Next 
   Next 
Next 
Add the competency layer to the neural network code:
添加竞争层的神经网络代码:

Code: Select all

ForEach _ListImages()
   For Pos = 0 To 3
      *pNumber.__NumberInfo = _ListImages()\Number[Pos]
      For Idx = 0 To #MAI_Number
         MaxResult.f = 0 : MaxR = -1
         For r = 0 To #MAI_Rotate-1
            DimConvolueW(r) = _DimWeight(Idx, r, 0) * -1
            For c = 0 To #MAI_Matrix-1
                DimConvolueW(r) + _DimWeight(Idx, r, c+1) * *pNumber\Matrix[c]
            Next 
            DimOutResult(r) = 1/(1+Exp(-DimConvolueW(r)))
            If MaxResult < DimOutResult(r) : MaxResult = DimOutResult(r) : MaxR = r : EndIf 
         Next
         
         If Idx = *pNumber\Number And MaxResult < #MAI_Judged
            RFeedback.f = Exp(-DimConvolueW(MaxR))/Sqr(1+Exp(-DimConvolueW(MaxR)))
            AntiConvolue.f = Modulus * (0.90 - DimOutResult(MaxR)) * RFeedback
         ElseIf Idx <> *pNumber\Number And MaxResult >= #MAI_Judged 
            RFeedback.f = Exp(-DimConvolueW(MaxR))/Sqr(1+Exp(-DimConvolueW(MaxR)))
            AntiConvolue.f = Modulus * (0.10 - DimOutResult(MaxR)) * RFeedback
         Else
            Continue
         EndIf   
         _DimWeight(Idx, MaxR, 0) - AntiConvolue
         For c = 0 To #MAI_Matrix-1
            _DimWeight(Idx, MaxR, c+1) + AntiConvolue * *pNumber\Matrix[c]
         Next
      Next 
   Next 
Next

Re: AI on the operation of the graphics verification code

Posted: Mon May 08, 2017 2:58 am
by MiLoo
Verification code recognition application:
验证码识别应用:

Code: Select all

; **************************************************
; *****        图形验证码识别--神经网络        *****
; *****             验证码识别应用             *****
; *****           迷路仟 QQ:714095563          *****
; *****               2017.05.07               *****
; **************************************************
; 鸣谢:Jim'QQ:390902126  提供的验证码生成器

;- Define
#MAI_LightR = 299
#MAI_LightG = 587 
#MAI_LightB = 114 
#MAI_Total  = 1000 

#MAI_Number = 9
#MAI_Matrix = 20*20
#MAI_Rotate = 2
#MAI_Judged = 0.5
#MAI_WeightSize = (#MAI_Number+1)*(#MAI_Matrix+1)*#MAI_Rotate*4


Structure __NumberInfo
   Matrix.b[#MAI_Matrix]   ;用于存放样本像素点转换值:0,1
   Number.l                ;训练目标
EndStructure

Structure __ImagesInfo
   Number.__NumberInfo[4]  ;每个验证码图片有四个字符
   ImageName$
   ImageChar$
EndStructure

Global NewList _ListImages.__ImagesInfo()
Global Dim _DimWeight.f(#MAI_Number, #MAI_Rotate-1, #MAI_Matrix)

;- Init
UsePNGImageDecoder()
CopyMemory_(@_DimWeight(), ?_WeightBin, #MAI_WeightSize)

;- Function
Procedure MAI_LoadTrainImage(*pImage.__ImagesInfo)
   ImageChar$ = StringField(GetFilePart(*pImage\ImageName$), 1, ".")
   ImageChar$ = StringField(ImageChar$, 2, "_")
   ImageChar$ = RSet(ImageChar$, 4, "0")
   ImageID = LoadImage(#PB_Any, *pImage\ImageName$)
   If ImageID = #Null : ProcedureReturn #False : EndIf 
   If ImageWidth(ImageID) < 92 : FreeImage(ImageID) : ProcedureReturn #False : EndIf 
   If StartDrawing(ImageOutput(ImageID))
      For Pos = 0 To 3
         Idx = 0 : *pImage\ImageChar$ = ImageChar$
         *pImage\Number[Pos]\Number  = Val(Mid(ImageChar$, Pos+1, 1))
         For y = 0 To 19
            For x = 0 To 19
               Color = Point(Pos * 17+x+9, y+6)
               If ((Red(Color)*#MAI_LightR+Green(Color)*#MAI_LightG+Blue(Color)*#MAI_LightB)/#MAI_Total)  <= 180
                  *pImage\Number[Pos]\Matrix[Idx] = 1
               Else 
                  *pImage\Number[Pos]\Matrix[Idx] = 0
               EndIf 
               Idx+1
            Next  
         Next 
      Next 
      StopDrawing()
   EndIf 
   FreeImage(ImageID)
   ProcedureReturn #True
EndProcedure

Procedure MAI_EnumSampleImage(SamplePath$)
   ClearList(_ListImages())
   If ExamineDirectory(0, SamplePath$, "*.png")
      While NextDirectoryEntry(0)
         FileName$ = DirectoryEntryName(0)
         If DirectoryEntryType(0) = #PB_DirectoryEntry_File
            AddElement(_ListImages())
            _ListImages()\ImageName$ = SamplePath$+FileName$
            CountFiles+1
         EndIf
      Wend
   EndIf
   ForEach _ListImages()
      If MAI_LoadTrainImage(_ListImages())
         Index+1
      Else 
         DeleteElement(_ListImages())
      EndIf 
   Next 
   Debug "枚举图片"+Str(CountFiles)+"个, 有效图片: "+Str(ListSize(_ListImages())) 
EndProcedure

Procedure MAI_IdentifyImage()
   InitTime = GetTickCount_()
   ForEach _ListImages()
      Success = 0 : ImageChar$ = #NULL$
      For Pos = 0 To 3
         *pNumber.__NumberInfo = _ListImages()\Number[Pos] : MaxResult.f = 0 : MaxIdx = -1
         For Idx = 0 To #MAI_Number
            For r = 0 To #MAI_Rotate-1
               Convolue.f = _DimWeight(Idx, r, 0) * -1
               For c = 0 To #MAI_Matrix-1
                   Convolue + _DimWeight(Idx, r, c+1) * *pNumber\Matrix[c]
               Next 
               OutResult.f = 1/(1+Exp(-Convolue))
               If MaxResult < OutResult : MaxResult = OutResult : MaxIdx = Idx : EndIf 
            Next 
         Next 
         If MaxIdx = *pNumber\Number : Success+1 : _NumSuccess+1 : Else : _NumFailure+1 : EndIf 
         ImageChar$ + ","+ Str(MaxIdx)
      Next 
      If Success = 4 
         _PicSuccess+1 
         Debug "[OK] " + Mid(ImageChar$, 2) + " << " + _ListImages()\ImageChar$
      Else 
         Debug "[NG] " + Mid(ImageChar$, 2) + " << " + _ListImages()\ImageChar$ + " <<< "
         _PicFailure+1
      EndIf 
   Next  
   PicSuccRate.f = _PicSuccess * 100/(_PicSuccess+_PicFailure)
   NumSuccRate.f = _NumSuccess * 100/(_NumSuccess+_NumFailure)
   Debug "============"
   Debug "耗时: " + Str(GetTickCount_()-InitTime)+"ms"
   Debug "图片识别率: " + StrF(PicSuccRate, 2)+"%  字符识别率: " + StrF(NumSuccRate, 2)+"%  "
EndProcedure

;- Debug
MAI_EnumSampleImage(".\训练图片\")
MAI_IdentifyImage()

;- Data 
; 对1000张图形验证码的进行132次的训练结果
DataSection
   _WeightBin:
   ......
EndDataSection

Re: AI on the operation of the graphics verification code

Posted: Mon May 08, 2017 4:15 am
by Poplar
Very good.

Re: AI on the operation of the graphics verification code

Posted: Mon May 08, 2017 7:00 am
by Fig
OK, next step is a purebasic deep learning library...
Really cool, thank you for sharing !

:D

Re: AI on the operation of the graphics verification code

Posted: Mon May 08, 2017 7:39 am
by MiLoo
Fig wrote:OK, next step is a purebasic deep learning library...
Really cool, thank you for sharing !

:D
:D :D :D