-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmain.go
91 lines (71 loc) · 1.62 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
package main
import (
"flag"
"image"
"image/draw"
"image/jpeg"
_ "image/png"
"os"
"path/filepath"
"github.com/zikes/chrisify/facefinder"
"github.com/disintegration/imaging"
)
var haarCascade = flag.String("haar", "haarcascade_frontalface_alt.xml", "The location of the Haar Cascade XML configuration to be provided to OpenCV.")
var facesDir = flag.String("faces", "faces", "The directory to search for faces.")
func main() {
flag.Parse()
var chrisFaces FaceList
var facesPath string
var err error
if *facesDir != "" {
facesPath, err = filepath.Abs(*facesDir)
if err != nil {
panic(err)
}
}
err = chrisFaces.Load(facesPath)
if err != nil {
panic(err)
}
if len(chrisFaces) == 0 {
panic("no faces found")
}
file := flag.Arg(0)
finder := facefinder.NewFinder(*haarCascade)
baseImage := loadImage(file)
faces := finder.Detect(baseImage)
bounds := baseImage.Bounds()
canvas := canvasFromImage(baseImage)
for _, face := range faces {
rect := rectMargin(30.0, face)
newFace := chrisFaces.Random()
if newFace == nil {
panic("nil face")
}
chrisFace := imaging.Fit(newFace, rect.Dx(), rect.Dy(), imaging.Lanczos)
draw.Draw(
canvas,
rect,
chrisFace,
bounds.Min,
draw.Over,
)
}
if len(faces) == 0 {
face := imaging.Resize(
chrisFaces[0],
bounds.Dx()/3,
0,
imaging.Lanczos,
)
face_bounds := face.Bounds()
draw.Draw(
canvas,
bounds,
face,
bounds.Min.Add(image.Pt(-bounds.Max.X/2+face_bounds.Max.X/2, -bounds.Max.Y+int(float64(face_bounds.Max.Y)/1.9))),
draw.Over,
)
}
jpeg.Encode(os.Stdout, canvas, &jpeg.Options{jpeg.DefaultQuality})
}