-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathface-detect.go
304 lines (265 loc) · 8.02 KB
/
face-detect.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
package main
// lil bullet and detectomatic selfies
// TODO: get it a cron to do it on the server
import (
"bufio"
"bytes"
"flag"
"fmt"
"image"
"log"
"os"
"path/filepath"
"image/png"
"github.com/aryann/difflib"
"github.com/oliamb/cutter"
"gocv.io/x/gocv"
)
var dirIn string
var dirOut string
var filetype string
var harrcascade string
var knownEmptyStore string
// Exists reports whether the named file or directory exists.
// apparently this can be wrong if permissions or something else thangle with it, would say true when not
func exists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
//Find the files in input directory wanted
func getFiles(dirIn string, filetype string) (files []string) {
fmt.Printf("Finding %s files in %s\n", filetype, dirIn)
files, err := filepath.Glob(dirIn + "*" + filetype)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Found %d files\n", len(files))
return files
}
// remove abs path and "face_" from either/both original and face file names
func stripFileName(name string) string {
return string(bytes.Replace([]byte(filepath.Base(name)), []byte("face_"), []byte(""), 1)) // remove 'face_' prefix
}
func getDifferentFiles(listA, listB []string) (diff []difflib.DiffRecord) {
return difflib.Diff(listA, listB)
}
func getUniqueOriginals(dirIn, dirOut, filetype string) []string {
// get file list from originals
originals := getFiles(dirIn, ".png")
faces := getFiles(dirOut, ".png")
var originalsStripped = make([]string, len(originals))
var facesStripped = make([]string, len(faces))
// strip file names, removing "face_" from faces
// and abs path from both
for i, faceFileName := range faces {
facesStripped[i] = stripFileName(faceFileName)
}
for j, originalsFileName := range originals {
originalsStripped[j] = stripFileName(originalsFileName)
}
// Constant Code Meaning
// ---------- ------ ---------------------------------------
// Common " " The element occurs in both sequences.
// LeftOnly "-" The element is unique to sequence 1.
// RightOnly "+" The element is unique to sequence 2.
diffs := getDifferentFiles(originalsStripped, facesStripped)
fmt.Printf("Diffing...\n")
var uniques []string
for _, diff := range diffs {
// fmt.Printf("%s\n", diff)
// should only have LeftOnly's, ie only have orignals that are not in faces'
if diff.Delta == difflib.LeftOnly {
uniqueAbsPath := dirIn + diff.Payload
fmt.Printf("%s\n", uniqueAbsPath)
uniques = append(uniques, uniqueAbsPath)
}
}
fmt.Printf("Found %d unique files.\n", len(uniques))
return uniques
}
func readFileLinesToStrSlice(fpath string) (out []string) {
file, err := os.Open(fpath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if scanner.Text() == "" || len(scanner.Text()) < 4 {
continue
}
out = append(out, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return out
}
var faceCropScale = 33 // percent bigger
func enlargeCrop(rect image.Rectangle, maxCols, maxRows int) (nanchor image.Point, ncols, nrows int) {
width, height := (rect.Max.X - rect.Min.X), (rect.Max.Y - rect.Min.Y)
ncols = width * (100 + faceCropScale) / 100
nrows = width * (100 + faceCropScale) / 100
// adjust anchor (top left == rect Min) given scaled rect size
x, y := rect.Min.X, rect.Min.Y
x = x - ((ncols - width) / 2)
y = y - ((nrows - height) / 2)
// ensure fit within max bounds
if x < 0 {
x = 0
}
if y < 0 {
y = 0
}
if x+ncols > maxCols {
ncols = maxCols - x
}
if y+nrows > maxRows {
nrows = maxRows - y
}
nanchor = image.Point{
X: x,
Y: y,
}
return
}
//detects faces and crops em out
func cropFaces(inputs []string, dirOut string, harrcascade string) {
err := os.MkdirAll(dirOut, 0777) // makes dir if not exists
if err != nil {
fmt.Printf("Could not create directory %s\n", dirOut)
log.Fatal(err)
}
fmt.Printf("Cropping %d images.\n", len(inputs))
//detect some faces
// load classifier to recognize faces
classifier := gocv.NewCascadeClassifier()
defer classifier.Close()
if !classifier.Load(harrcascade) {
fmt.Printf("Error reading cascade file: %v\n", harrcascade)
return
}
// track files without faces to check against
// removes a string from string slice if it exists in the slice
// this "widdles" the size of the in-memory known-no-faces list as previously checked files are referenced
// Since order is preserved, it should be relatively fast.
containsWiddle := func(sl []string, s string) ([]string, bool) {
for i, ss := range sl {
if ss == s {
sl = append(sl[:i], sl[i+1:]...)
return sl, true
}
}
return sl, false
}
nofaceslistfilePath := filepath.Clean(knownEmptyStore)
var nofaces []string
if _, e := os.Stat(nofaceslistfilePath); e == nil {
nofaces = readFileLinesToStrSlice(nofaceslistfilePath)
} else if os.IsNotExist(e) {
// simulate touch
if f, e := os.Create(nofaceslistfilePath); e != nil {
panic(e)
} else {
f.Close()
}
}
nofaceFile, err := os.OpenFile(nofaceslistfilePath, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
defer nofaceFile.Close()
l := len(inputs)
for i, element := range inputs {
outPath := dirOut + "face_" + filepath.Base(element)
fmt.Println(i+1, "/", l, ":", element)
var has bool
nofaces, has = containsWiddle(nofaces, element)
if has {
log.Println("known haz no face, skipping")
continue
}
imageMat := gocv.IMRead(element, gocv.IMReadColor)
if imageMat.Empty() {
log.Println("empty image, skipping", element)
if _, has := containsWiddle(nofaces, element); !has {
nofaceFile.WriteString(element + "\n")
}
continue
}
// check to see if completely black image.
// sometimes this happens because my computer doesn't go to sleep correctly when connected to external monitor
// ... and sometimes it's just dark
// scal := imageMat.Mean()
// if scal.Val1 < 10 && scal.Val2 < 10 && scal.Val3 < 10 {
// log.Println("dark image, skipping")
// noface.WriteString(element)
// continue
// }
rects := classifier.DetectMultiScale(imageMat)
if len(rects) == 0 {
log.Println("no faces detected")
if _, has := containsWiddle(nofaces, element); !has {
nofaceFile.WriteString(element + "\n")
}
continue
}
fs, err := os.Open(element)
if err != nil {
panic(err)
}
pngF, err := png.Decode(fs)
if err != nil {
panic(err)
}
for _, rect := range rects {
if rect.Empty() {
log.Println("no rect")
continue
}
log.Println("rect bounds", rect.Bounds().String())
face := imageMat.Region(rect)
if face.Empty() {
log.Println("empty face")
continue
}
// don't chop the chin off
a, w, h := enlargeCrop(rect, imageMat.Cols(), imageMat.Rows())
croppedImg, err := cutter.Crop(pngF, cutter.Config{
Width: w,
Height: h,
Anchor: a,
Mode: cutter.TopLeft, // optional, default value
})
if err != nil {
panic(err)
}
toimg, _ := os.Create(outPath)
if err := png.Encode(toimg, croppedImg); err != nil {
log.Println("err encoding png", err)
} else {
log.Println("->", outPath)
}
toimg.Close()
if err != nil {
log.Println("err writing face bytes", err)
os.Remove(outPath)
}
}
}
}
func main() {
cmd := os.Args[0]
fmt.Printf("Program Name: %s\n", cmd)
flag.StringVar(&dirIn, "dirIn", "/Users/ia/dev/self-portrait/data/examples/originals/", "input directory holding selfies")
flag.StringVar(&dirOut, "dirOut", "/Users/ia/dev/self-portrait/data/examples/faces/", "output directory")
flag.StringVar(&filetype, "filetype", ".png", "file type to detect faces, searches input directory")
flag.StringVar(&harrcascade, "harrcascade", "/Users/ia/gocode/src/github.com/lazywei/go-opencv/samples/haarcascade_frontalface_alt.xml", "harrcascade thing")
flag.StringVar(&knownEmptyStore, "empty", "/Users/ia/dev/self-portrait/data/knownnofaces", "file in which to store list of known no-face images")
flag.Parse()
cropFaces(getUniqueOriginals(dirIn, dirOut, filetype), dirOut, harrcascade)
}