-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
326 lines (278 loc) · 8.35 KB
/
main.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package main
import (
"flag"
"fmt"
"image/color"
"image/jpeg"
"image/png"
"io/ioutil"
"log"
"math"
"os"
"path/filepath"
"time"
"github.com/disintegration/imaging"
pigo "github.com/esimov/pigo/core"
"github.com/fogleman/gg"
)
const banner = `
____ __ ___ ____ _ _ __ ____ __ _
( __)/ _\ / __)( __)( \/ ) / _\ / ___)( / )
) _)/ \( (__ ) _) / \/ \/ \\___ \ ) (
(__) \_/\_/ \___)(____)\_)(_/\_/\_/(____/(__\_)
Face mask generator
Version: %s
`
// Version indicates the current build version.
var Version string
var (
dc *gg.Context
fd *faceDetector
plc *pigo.PuplocCascade
flpcs map[string][]*pigo.FlpCascade
imgParams *pigo.ImageParams
)
// faceDetector struct contains Pigo face detector general settings.
type faceDetector struct {
angle float64
destination string
minSize int
maxSize int
shiftFactor float64
scaleFactor float64
iouThreshold float64
faceCascade string
eyesCascade string
flplocDir string
}
func main() {
var (
// Flags
source = flag.String("in", "", "Source image")
destination = flag.String("out", "", "Destination image")
cascadeFile = flag.String("cf", "cascades/facefinder", "Cascade binary file")
puplocCascade = flag.String("plc", "cascades/puploc", "Pupil localization cascade file")
flplocDir = flag.String("flpdir", "cascades/lps", "The facial landmark points base directory")
minSize = flag.Int("min", 20, "Minimum size of face")
maxSize = flag.Int("max", 1000, "Maximum size of face")
shiftFactor = flag.Float64("shift", 0.1, "Shift detection window by percentage")
scaleFactor = flag.Float64("scale", 1.1, "Scale detection window by percentage")
angle = flag.Float64("angle", 0.0, "0.0 is 0 radians and 1.0 is 2*pi radians")
iouThreshold = flag.Float64("iou", 0.2, "Intersection over union (IoU) threshold")
)
log.SetFlags(0)
flag.Usage = func() {
fmt.Fprintf(os.Stderr, fmt.Sprintf(banner, Version))
flag.PrintDefaults()
}
flag.Parse()
if len(*source) == 0 || len(*destination) == 0 || len(*cascadeFile) == 0 || len(*puplocCascade) == 0 || len(*flplocDir) == 0 {
log.Fatal("Usage: facemask -in input.jpg -out out.png -cf=/path/to/faceCascade -plc=/path/to/eyesCascade -flpdir=/path/to/landmarkCascades")
}
fileTypes := []string{".jpg", ".jpeg", ".png"}
ext := filepath.Ext(*destination)
if !inSlice(ext, fileTypes) {
log.Fatalf("Output file type not supported: %v", ext)
}
if *scaleFactor < 1.05 {
log.Fatal("Scale factor must be greater than 1.05")
}
// Progress indicator
s := new(spinner)
s.start("Processing...")
start := time.Now()
fd = &faceDetector{
angle: *angle,
destination: *destination,
minSize: *minSize,
maxSize: *maxSize,
shiftFactor: *shiftFactor,
scaleFactor: *scaleFactor,
iouThreshold: *iouThreshold,
faceCascade: *cascadeFile,
eyesCascade: *puplocCascade,
flplocDir: *flplocDir,
}
faces, err := fd.detectFaces(*source)
if err != nil {
log.Fatalf("Detection error: %v", err)
}
if err = fd.drawFaces(faces); err != nil {
log.Fatalf("Error creating the image output: %s", err)
}
s.stop()
fmt.Printf("\nDone in: \x1b[92m%.2fs\n", time.Since(start).Seconds())
}
// detectFaces run the detection algorithm over the provided source image.
func (fd *faceDetector) detectFaces(source string) ([]pigo.Detection, error) {
src, err := pigo.GetImage(source)
if err != nil {
return nil, err
}
pixels := pigo.RgbToGrayscale(src)
cols, rows := src.Bounds().Max.X, src.Bounds().Max.Y
dc = gg.NewContext(cols, rows)
dc.DrawImage(src, 0, 0)
imgParams = &pigo.ImageParams{
Pixels: pixels,
Rows: rows,
Cols: cols,
Dim: cols,
}
cParams := pigo.CascadeParams{
MinSize: fd.minSize,
MaxSize: fd.maxSize,
ShiftFactor: fd.shiftFactor,
ScaleFactor: fd.scaleFactor,
ImageParams: *imgParams,
}
faceCascade, err := ioutil.ReadFile(fd.faceCascade)
if err != nil {
return nil, err
}
p := pigo.NewPigo()
// Unpack the binary file. This will return the number of cascade trees,
// the tree depth, the threshold and the prediction from tree's leaf nodes.
classifier, err := p.Unpack(faceCascade)
if err != nil {
return nil, err
}
pl := pigo.NewPuplocCascade()
eyesCascade, err := ioutil.ReadFile(fd.eyesCascade)
if err != nil {
return nil, err
}
plc, err = pl.UnpackCascade(eyesCascade)
if err != nil {
return nil, err
}
flpcs, err = pl.ReadCascadeDir(fd.flplocDir)
if err != nil {
return nil, err
}
// Run the classifier over the obtained leaf nodes and return the detection results.
// The result contains quadruplets representing the row, column, scale and detection score.
faces := classifier.RunCascade(cParams, fd.angle)
// Calculate the intersection over union (IoU) of two clusters.
faces = classifier.ClusterDetections(faces, fd.iouThreshold)
return faces, nil
}
// drawFaces marks the detected faces with a circle in case isCircle is true, otherwise marks with a rectangle.
func (fd *faceDetector) drawFaces(faces []pigo.Detection) error {
var (
qThresh = float32(5.0)
perturb = 63
puploc *pigo.Puploc
imgScale float64
)
for _, face := range faces {
if face.Q > qThresh {
// left eye
puploc = &pigo.Puploc{
Row: face.Row - int(0.075*float32(face.Scale)),
Col: face.Col - int(0.175*float32(face.Scale)),
Scale: float32(face.Scale) * 0.25,
Perturbs: perturb,
}
leftEye := plc.RunDetector(*puploc, *imgParams, fd.angle, false)
// right eye
puploc = &pigo.Puploc{
Row: face.Row - int(0.075*float32(face.Scale)),
Col: face.Col + int(0.185*float32(face.Scale)),
Scale: float32(face.Scale) * 0.25,
Perturbs: perturb,
}
rightEye := plc.RunDetector(*puploc, *imgParams, fd.angle, false)
flp1 := flpcs["lp84"][0].GetLandmarkPoint(leftEye, rightEye, *imgParams, perturb, false)
flp2 := flpcs["lp84"][0].GetLandmarkPoint(leftEye, rightEye, *imgParams, perturb, true)
mask, err := os.OpenFile("assets/facemask.png", os.O_RDONLY, 0755)
defer mask.Close()
if err != nil {
return err
}
maskImg, err := png.Decode(mask)
if err != nil {
log.Fatal(err)
}
// Calculate the lean angle between the two mouth points.
angle := 1 - (math.Atan2(float64(flp2.Col-flp1.Col), float64(flp2.Row-flp1.Row)) * 180 / math.Pi / 90)
dx, dy := maskImg.Bounds().Dx(), maskImg.Bounds().Dy()
if face.Scale < dx || face.Scale < dy {
if dx > dy {
imgScale = float64(face.Scale) / float64(dx)
} else {
imgScale = float64(face.Scale) / float64(dy)
}
}
width, height := float64(dx)*imgScale*0.75, float64(dy)*imgScale*0.75
tx := face.Col - int(width/2)
ty := flp1.Row + (flp1.Row-flp2.Row)/2 - int(height*0.4)
resized := imaging.Resize(maskImg, int(width), int(height), imaging.Lanczos)
aligned := imaging.Rotate(resized, angle, color.Transparent)
dc.DrawImage(aligned, tx, ty)
}
}
img := dc.Image()
output, err := os.OpenFile(fd.destination, os.O_CREATE|os.O_RDWR, 0755)
defer output.Close()
if err != nil {
return err
}
ext := filepath.Ext(output.Name())
switch ext {
case ".jpg", ".jpeg":
if err := jpeg.Encode(output, img, &jpeg.Options{Quality: 100}); err != nil {
return err
}
case ".png":
if err := png.Encode(output, img); err != nil {
return err
}
}
return nil
}
type spinner struct {
stopChan chan struct{}
}
// Start process
func (s *spinner) start(message string) {
s.stopChan = make(chan struct{}, 1)
go func() {
for {
for _, r := range `⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏` {
select {
case <-s.stopChan:
return
default:
fmt.Printf("\r%s%s %c%s", message, "\x1b[35m", r, "\x1b[39m")
time.Sleep(time.Millisecond * 100)
}
}
}
}()
}
// End process
func (s *spinner) stop() {
s.stopChan <- struct{}{}
}
// inSlice checks if the item exists in the slice.
func inSlice(item string, slice []string) bool {
for _, it := range slice {
if it == item {
return true
}
}
return false
}
// drawDetections helper function to draw the detection marks
func drawDetections(ctx *gg.Context, x, y, r float64, c color.RGBA, markDet bool) {
ctx.DrawArc(x, y, r*0.15, 0, 2*math.Pi)
ctx.SetFillStyle(gg.NewSolidPattern(c))
ctx.Fill()
if markDet {
ctx.DrawRectangle(x-(r*1.5), y-(r*1.5), r*3, r*3)
ctx.SetLineWidth(2.0)
ctx.SetStrokeStyle(gg.NewSolidPattern(color.RGBA{R: 255, G: 255, B: 0, A: 255}))
ctx.Stroke()
}
}