-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
65 lines (60 loc) · 1.43 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
package main
import (
"flag"
"fmt"
img "image"
"image/color"
"log"
cv "gocv.io/x/gocv"
)
func main() {
fmt.Println("[Info] Face Detection using gocv")
file := flag.String("image", "", "use -image and the path of the image to give the input")
// Parsing the commandline arguments
flag.Parse()
if !flag.Parsed() {
log.Fatal("Argument parsing failed")
}
if *file == "" {
log.Fatal("Invalid input please specify the image path using -image flag")
}
classifier := cv.NewCascadeClassifier()
fmt.Println("[Info] Loading classifier model..")
if !classifier.Load("models/cascade_classifier.xml") {
log.Fatal("Failed to load the classifier file")
}
defer classifier.Close()
image := cv.IMRead(*file, cv.IMReadAnyColor)
gray := cv.NewMat()
defer gray.Close()
// Changing the color mode into gray for ease of classification.
cv.CvtColor(
image,
&gray,
cv.ColorBGRToGray,
)
fmt.Println("[Info] Detecting faces...")
faces := classifier.DetectMultiScale(gray)
for _, rect := range faces {
// Dimensions
x := rect.Min.X
y := rect.Min.Y
w := rect.Dx()
h := rect.Dy()
rectangle := img.Rect(x, y, x+w, y+h)
red := color.RGBA{
255,
0,
0,
1.0,
}
cv.Rectangle(&image, rectangle, red, 1)
// Writing image into a file.
if !cv.IMWrite("output.jpg", image) {
log.Fatal("Failed to write the output image")
}
window := cv.NewWindow("Face Detection")
window.IMShow(image)
window.WaitKey(10000)
}
}