forked from haquenafeem/go-recognizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
180 lines (129 loc) · 3.3 KB
/
image.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package recognizer
import (
"crypto/rand"
"encoding/hex"
"image"
"image/color"
"image/jpeg"
"io"
"os"
"path/filepath"
goFace "github.com/Kagami/go-face"
"github.com/disintegration/imaging"
"github.com/fogleman/gg"
"github.com/golang/freetype/truetype"
goLibJPEG "github.com/pixiv/go-libjpeg/jpeg"
"golang.org/x/image/font/gofont/goregular"
)
/*
LoadImage Load an image from file
*/
func (_this *Recognizer) LoadImage(Path string) (image.Image, error) {
existingImageFile, err := os.Open(Path)
if err != nil {
return nil, err
}
defer existingImageFile.Close()
imageData, _, err := image.Decode(existingImageFile)
if err != nil {
return nil, err
}
return imageData, nil
}
/*
SaveImage Save an image to jpeg file
*/
func (_this *Recognizer) SaveImage(Path string, Img image.Image) error {
outputFile, err := os.Create(Path)
if err != nil {
return err
}
err = _this.encodeJPEG(outputFile, Img)
if err != nil {
return err
}
return outputFile.Close()
}
/*
GrayScale Convert an image to grayscale
*/
func (_this *Recognizer) GrayScale(imgSrc image.Image) image.Image {
return imaging.Grayscale(imgSrc)
}
/*
createTempGrayFile create a temporary image in grayscale
*/
func (_this *Recognizer) createTempGrayFile(Path, Id string) (string, error) {
name := _this.tempFileName(Id, ".jpeg")
img, err := _this.LoadImage(Path)
if err != nil {
return "", err
}
img = _this.GrayScale(img)
err = _this.SaveImage(name, img)
if err != nil {
return "", err
}
return name, nil
}
// tempFileName generates a temporary filename
func (_this *Recognizer) tempFileName(prefix, suffix string) string {
randBytes := make([]byte, 16)
rand.Read(randBytes)
return filepath.Join(os.TempDir(), prefix+hex.EncodeToString(randBytes)+suffix)
}
/*
DrawFaces draws the faces identified in the original image
*/
func (_this *Recognizer) DrawFaces(Path string, F []Face) (image.Image, error) {
img, err := _this.LoadImage(Path)
if err != nil {
return nil, err
}
font, err := truetype.Parse(goregular.TTF)
if err != nil {
return nil, err
}
face := truetype.NewFace(font, &truetype.Options{Size: 24})
dc := gg.NewContextForImage(img)
dc.SetFontFace(face)
for _, f := range F {
dc.SetRGB255(0, 0, 255)
x := float64(f.Rectangle.Min.X)
y := float64(f.Rectangle.Min.Y)
w := float64(f.Rectangle.Dx())
h := float64(f.Rectangle.Dy())
dc.DrawString(f.Id, x, y+h+20)
dc.DrawRectangle(x, y, w, h)
dc.SetLineWidth(4.0)
dc.SetStrokeStyle(gg.NewSolidPattern(color.RGBA{R: 0, G: 0, B: 255, A: 255}))
dc.Stroke()
}
img = dc.Image()
return img, nil
}
/*
DrawFaces2 draws the faces in the original image
*/
func (_this *Recognizer) DrawFaces2(Path string, F []goFace.Face) (image.Image, error) {
aux := make([]Face, 0)
for _, f := range F {
auxFace := Face{}
auxFace.Rectangle = f.Rectangle
auxFace.Descriptor = f.Descriptor
aux = append(aux, auxFace)
}
return _this.DrawFaces(Path, aux)
}
func (_this *Recognizer) encodeJPEG(w io.Writer, m image.Image) error {
if _this.UseFastJPEGEncoding {
goLibJPEG.Encode(w, m, &goLibJPEG.EncoderOptions{Quality: 100})
}
return jpeg.Encode(w, m, nil)
}
func (_this *Recognizer) decodeJPEG(r io.Reader) (image.Image, error) {
if _this.UseFastJPEGEncoding {
return goLibJPEG.Decode(r, &goLibJPEG.DecoderOptions{})
}
return jpeg.Decode(r)
}