-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacial_features.go
85 lines (64 loc) · 1.79 KB
/
facial_features.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
package main
import (
"image"
"image/color"
"image/draw"
"math"
)
// Feature reprecent a set of facial features
// (the points of the eyes and the mount)
type Feature struct {
LeftEye image.Point
RightEye image.Point
Mouth image.Point
}
func handleFacialFeatures(features []Feature,
original image.Image, timestamp string) image.Image {
w := original.Bounds().Max.X
h := original.Bounds().Max.Y
puts("Image width:", w, "height:", h)
m := image.NewRGBA(original.Bounds())
draw.Draw(m, original.Bounds(), original, image.ZP, draw.Src)
if len(features) > 0 {
puts("Found features:", len(features), features)
for _, f := range features {
hatRect := calculateHatRect(f)
if Debug {
red := color.RGBA{255, 0, 0, 255}
green := color.RGBA{0, 255, 0, 255}
blue := color.RGBA{0, 0, 255, 255}
box(m, hatRect, color.White)
square(m, f.LeftEye, 4, red)
square(m, f.Mouth, 4, green)
square(m, f.RightEye, 4, blue)
} else {
// Load the santa hat
santa := resizeImage(SantaHat, hatRect.Size().X, hatRect.Size().Y)
draw.Draw(m, hatRect, santa, image.ZP, draw.Over)
}
}
} else {
puts("No features found…")
}
return m
}
func calculateHatRect(f Feature) image.Rectangle {
d := distanceBetweenEyes(f)
d2 := distanceBetween(f.LeftEye, f.Mouth)
puts("distance between eyes:", d)
puts("distance between left eye and mouth:", d2)
x1 := f.LeftEye.X - d
x2 := f.RightEye.X + d
y1 := f.LeftEye.Y - d2 - ((x2 - x1) / 2)
y2 := f.RightEye.Y + (d2 / 2)
return image.Rect(x1, y1, x2, y2)
}
func distanceBetweenEyes(f Feature) int {
return distanceBetween(f.LeftEye, f.RightEye)
}
func distanceBetween(p1, p2 image.Point) int {
r := image.Rectangle{p1, p2}
return int(math.Sqrt(
math.Pow(float64(r.Size().X), 2) +
math.Pow(float64(r.Size().Y), 2)))
}