forked from xlvector/captcha
/
connected_component_predictor.go
81 lines (74 loc) · 2.05 KB
/
connected_component_predictor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package captcha
import (
"captcha/cv"
"log"
"image"
_ "strconv"
)
type BinaryImageConnectedComponentPredictor struct {
Dx, Dy int
}
func (self *BinaryImageConnectedComponentPredictor) Guess(img *cv.BinaryImage, mki *MaskIndex, chType int) []*Result {
scaler := cv.ScaleBinaryImage{Height : SCALE_HEIGHT}
ccs := cv.ExtractAllConnectedComponentInBinaryImage(img, self.Dx, self.Dy)
ret := NewResult("", 0.0)
if len(ccs) < 4 {
return []*Result{}
}
for _, cc := range ccs {
if cc.Size < 50 || cc.Width < 3 || cc.Height < 8 {
continue
}
unScaleCC := cv.CopyBinaryImage(cc)
cc = scaler.Process(cc)
localResults := mki.FindBestMatchedMasks(cc, chType, 0)
if localResults == nil || len(localResults) == 0{
continue
}
if localResults[0].Weight < 0.65 {
continue
}
log.Println(localResults[0].Label, localResults[0].Weight)
ret.Label += localResults[0].Label
ret.Weight += localResults[0].Weight
ret.AddComponent(unScaleCC)
if len(ret.Label) > 8 {
return []*Result{}
}
}
return []*Result{ret}
}
type ConnectedComponentPredictor struct {
Dx, Dy int
}
func (self *ConnectedComponentPredictor) Guess(img image.Image, mki *MaskIndex, chType int) []*Result {
scaler := cv.ScaleBinaryImage{Height : SCALE_HEIGHT}
ccs := cv.ExtractAllConnectedComponent(img, self.Dx, self.Dy)
ret := NewResult("", 0.0)
if len(ccs) < 4 {
return []*Result{}
}
imgSize := img.Bounds().Dx() * img.Bounds().Dy()
for _, cc := range ccs {
if cc.Size < 50 || cc.Size + 100 > imgSize || cc.Width + 10 > img.Bounds().Dx() || cc.Width < 3 || cc.Height < 8 {
continue
}
unScaleCC := cv.CopyBinaryImage(cc)
cc = scaler.Process(cc)
localResults := mki.FindBestMatchedMasks(cc, chType, 0)
if localResults == nil || len(localResults) == 0{
continue
}
log.Println(localResults[0].Label, localResults[0].Weight)
if localResults[0].Weight < 0.65 {
continue
}
ret.Label += localResults[0].Label
ret.Weight += localResults[0].Weight
ret.AddComponent(unScaleCC)
if len(ret.Label) > 8 {
return []*Result{}
}
}
return []*Result{ret}
}