-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
79 lines (72 loc) · 1.48 KB
/
utils.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
package main
import (
"bufio"
"image"
"image/jpeg"
_ "image/png"
"io"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
log "github.com/Sirupsen/logrus"
"github.com/icholy/nick_bot/faceutil"
)
func shuffle(slice []string) {
for i := range slice {
j := rand.Intn(i + 1)
slice[i], slice[j] = slice[j], slice[i]
}
}
func readLines(filename string) ([]string, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
var captions []string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
captions = append(captions, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
return captions, err
}
func testImage(imgfile string, w io.Writer) error {
f, err := os.Open(imgfile)
if err != nil {
return err
}
defer f.Close()
baseImage, _, err := image.Decode(f)
if err != nil {
return err
}
faces := faceutil.DetectFaces(baseImage)
log.Debugf("found %d face(s) in image", len(faces))
newImage := faceutil.DrawFaces(baseImage, faces)
return jpeg.Encode(w, newImage, &jpeg.Options{Quality: jpeg.DefaultQuality})
}
func testImageDir(dir string) error {
entries, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for _, e := range entries {
var (
srcFile = filepath.Join(dir, e.Name())
dstFile = filepath.Join(dir, "nick_"+e.Name())
)
f, err := os.Create(dstFile)
if err != nil {
return err
}
defer f.Close()
if err := testImage(srcFile, f); err != nil {
return err
}
}
return nil
}