-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
142 lines (109 loc) · 2.68 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"flag"
"image"
"io/ioutil"
"log"
"path/filepath"
"strings"
"github.com/Kagami/go-face"
"gocv.io/x/gocv"
)
const dataDir = "images"
func main() {
extList := []string{
"jpg",
"png",
"jpeg",
"mp4",
}
var cnnMode = flag.Bool("cnn", false, "run with ccn mode")
flag.Parse()
rec, err := face.NewRecognizer("data")
if err != nil {
log.Fatal(err)
}
defer rec.Close()
log.Print("FaceBlur started..")
files, err := ioutil.ReadDir(dataDir)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
extSplited := strings.Split(file.Name(), ".")
ext := extSplited[len(extSplited)-1]
if !stringInSlice(ext, extList) {
return
}
img := filepath.Join(dataDir, file.Name())
if ext == "mp4" {
video, err := gocv.OpenVideoCapture(img)
if err != nil {
log.Printf("error opening video: %v\n", err)
return
}
defer video.Close()
videoHeight := int(video.Get(gocv.VideoCaptureFrameHeight))
videoWidth := int(video.Get(gocv.VideoCaptureFrameWidth))
videoWrite, err := gocv.VideoWriterFile("dist/"+extSplited[0]+"-blurred.mp4", "MP4V", video.Get(gocv.VideoCaptureFPS), videoWidth, videoHeight, true)
if err != nil {
log.Fatalf("error init write video: %v", err)
}
for video.IsOpened() {
imgMat := gocv.NewMat()
frame := video.Read(&imgMat)
if !frame {
break
}
jpgFile, err := gocv.IMEncode(gocv.JPEGFileExt, imgMat)
if err != nil {
log.Fatal(err)
}
var faces []face.Face
if *cnnMode {
faces, err = rec.RecognizeCNN(jpgFile)
} else {
faces, err = rec.Recognize(jpgFile)
}
if err != nil {
log.Fatalf("Can't recognize: %v", err)
}
log.Print(file.Name()+" Faces ", len(faces))
for _, face := range faces {
imgFace := imgMat.Region(face.Rectangle)
gocv.GaussianBlur(imgFace, &imgFace, image.Pt(75, 75), 0, 0, gocv.BorderDefault)
imgFace.Close()
}
videoWrite.Write(imgMat)
}
videoWrite.Close()
video.Close()
} else {
var faces []face.Face
if *cnnMode {
faces, err = rec.RecognizeFileCNN(img)
} else {
faces, err = rec.RecognizeFile(img)
}
if err != nil {
log.Fatalf("Can't recognize: %v", err)
}
log.Print(file.Name()+" Faces ", len(faces))
photo := gocv.IMRead(img, gocv.IMReadColor)
for _, face := range faces {
imgFace := photo.Region(face.Rectangle)
gocv.GaussianBlur(imgFace, &imgFace, image.Pt(75, 75), 0, 0, gocv.BorderDefault)
imgFace.Close()
}
gocv.IMWrite("dist/"+extSplited[0]+"-blurred."+ext, photo)
}
}
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}