-
Notifications
You must be signed in to change notification settings - Fork 871
/
main.go
88 lines (73 loc) · 1.77 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
// What it does:
//
// This example captures video from a connected camera,
// then uses the CascadeClassifier to detect faces, blurs them
// using a Gaussian blur, then displays the blurred video in a window.
//
// How to run:
//
// faceblur [camera ID] [classifier XML file]
//
// go run ./cmd/faceblur/main.go 0 data/haarcascade_frontalface_default.xml
//
package main
import (
"fmt"
"image"
"os"
"gocv.io/x/gocv"
)
func main() {
if len(os.Args) < 3 {
fmt.Println("How to run:\n\tfaceblur [camera ID] [classifier XML file]")
return
}
// parse args
deviceID := os.Args[1]
xmlFile := os.Args[2]
// open webcam
webcam, err := gocv.OpenVideoCapture(deviceID)
if err != nil {
fmt.Printf("error opening video capture device: %v\n", deviceID)
return
}
defer webcam.Close()
// open display window
window := gocv.NewWindow("Face Blur")
defer window.Close()
// prepare image matrix
img := gocv.NewMat()
defer img.Close()
// load classifier to recognize faces
classifier := gocv.NewCascadeClassifier()
defer classifier.Close()
if !classifier.Load(xmlFile) {
fmt.Printf("Error reading cascade file: %v\n", xmlFile)
return
}
fmt.Printf("Start reading device: %v\n", deviceID)
for {
if ok := webcam.Read(&img); !ok {
fmt.Printf("Device closed: %v\n", deviceID)
return
}
if img.Empty() {
continue
}
// detect faces
rects := classifier.DetectMultiScale(img)
fmt.Printf("found %d faces\n", len(rects))
// blur each face on the original image
for _, r := range rects {
imgFace := img.Region(r)
// blur face
gocv.GaussianBlur(imgFace, &imgFace, image.Pt(75, 75), 0, 0, gocv.BorderDefault)
imgFace.Close()
}
// show the image in the window, and wait 1 millisecond
window.IMShow(img)
if window.WaitKey(1) >= 0 {
break
}
}
}