-
Notifications
You must be signed in to change notification settings - Fork 0
/
img.go
109 lines (98 loc) · 2.27 KB
/
img.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package elm
import (
"image"
"io/ioutil"
"net/http"
"path/filepath"
"github.com/PuerkitoBio/goquery"
"github.com/disintegration/imaging"
"github.com/kelvins/lbph"
"github.com/kelvins/lbph/lbp"
)
//GetCharacteristic converts LBH to feature vector, p: LBH
func GetCharacteristic(p [][]uint64) []float64 {
x := make([]float64, 256)
for i := 0; i < len(x); i++ {
x[i] = 0
}
for _, v := range p {
for _, vv := range v {
x[int(vv)]++
}
}
return x
}
//GetLBH gets LBH from image data, m: Black and white image
func GetLBH(m image.Image) [][]uint64 {
params := lbph.Params{
Radius: 1,
Neighbors: 8,
GridX: 8,
GridY: 8,
}
pixels, err := lbp.Calculate(m, params.Radius, params.Neighbors)
if err != nil {
panic(err)
}
return pixels
}
//GetLocalImgPathToFeaturVector obtains feature vector from image data
func GetLocalImgPathToFeaturVector(paths []string, X [][]float64, ans []float64, max int) [][]float64 {
for i, v := range paths {
file, err := imaging.Open(v)
if err != nil {
continue
}
vv := GetCharacteristic(GetLBH(file))
vv = append(vv, ans...)
X = append(X, vv)
if i == max {
break
}
}
return X
}
//GetPage will get all the url of the url destination image
func GetPage(url string) []string {
var images []string
doc, _ := goquery.NewDocument(url)
doc.Find("img").Each(func(_ int, s *goquery.Selection) {
url, _ := s.Attr("src")
images = append(images, url)
})
return images
}
//GetImgPathToFeaturVector makes all images of url a feature vector
func GetImgPathToFeaturVector(X [][]float64, urls []string, ans []float64) [][]float64 {
for _, v := range urls {
response, err := http.Get(v)
if err != nil {
panic(err)
}
defer response.Body.Close()
img, _, err := image.Decode(response.Body)
if err != nil {
panic(err)
}
vv := GetCharacteristic(GetLBH(img))
vv = append(vv, ans...)
X = append(X, vv)
}
return X
}
//Dirwalk gets the path of all the files in the directory.
func Dirwalk(dir string) []string {
files, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}
var paths []string
for _, file := range files {
if file.IsDir() {
paths = append(paths, Dirwalk(filepath.Join(dir, file.Name()))...)
continue
}
paths = append(paths, filepath.Join(dir, file.Name()))
}
return paths
}