-
Notifications
You must be signed in to change notification settings - Fork 172
/
opencv_test.go
197 lines (167 loc) · 5.04 KB
/
opencv_test.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// +build gocv
// uncomment line above for gocv examples
package examples
import (
"fmt"
"image"
"image/color"
"io"
"log"
"testing"
ffmpeg "github.com/u2takey/ffmpeg-go"
"gocv.io/x/gocv"
)
// TestExampleOpenCvFaceDetect will: take a video as input => use opencv for face detection => draw box and show a window
// This example depends on gocv and opencv, please refer: https://pkg.go.dev/gocv.io/x/gocv for installation.
func TestExampleOpenCvFaceDetectWithVideo(t *testing.T) {
inputFile := "./sample_data/head-pose-face-detection-male-short.mp4"
xmlFile := "./sample_data/haarcascade_frontalface_default.xml"
w, h := getVideoSize(inputFile)
log.Println(w, h)
pr1, pw1 := io.Pipe()
readProcess(inputFile, pw1)
openCvProcess(xmlFile, pr1, w, h)
log.Println("Done")
}
func readProcess(infileName string, writer io.WriteCloser) {
log.Println("Starting ffmpeg process1")
go func() {
err := ffmpeg.Input(infileName).
Output("pipe:",
ffmpeg.KwArgs{
"format": "rawvideo", "pix_fmt": "rgb24",
}).
WithOutput(writer).
ErrorToStdOut().
Run()
log.Println("ffmpeg process1 done")
_ = writer.Close()
if err != nil {
panic(err)
}
}()
return
}
func openCvProcess(xmlFile string, reader io.ReadCloser, w, h int) {
// open display window
window := gocv.NewWindow("Face Detect")
defer window.Close()
// color for the rect when faces detected
blue := color.RGBA{B: 255}
classifier := gocv.NewCascadeClassifier()
defer classifier.Close()
if !classifier.Load(xmlFile) {
fmt.Printf("Error reading cascade file: %v\n", xmlFile)
return
}
frameSize := w * h * 3
buf := make([]byte, frameSize, frameSize)
for {
n, err := io.ReadFull(reader, buf)
if n == 0 || err == io.EOF {
return
} else if n != frameSize || err != nil {
panic(fmt.Sprintf("read error: %d, %s", n, err))
}
img, err := gocv.NewMatFromBytes(h, w, gocv.MatTypeCV8UC3, buf)
if err != nil {
fmt.Println("decode fail", err)
}
if img.Empty() {
continue
}
img2 := gocv.NewMat()
gocv.CvtColor(img, &img2, gocv.ColorBGRToRGB)
// detect faces
rects := classifier.DetectMultiScale(img2)
fmt.Printf("found %d faces\n", len(rects))
// draw a rectangle around each face on the original image, along with text identifing as "Human"
for _, r := range rects {
gocv.Rectangle(&img2, r, blue, 3)
size := gocv.GetTextSize("Human", gocv.FontHersheyPlain, 1.2, 2)
pt := image.Pt(r.Min.X+(r.Min.X/2)-(size.X/2), r.Min.Y-2)
gocv.PutText(&img2, "Human", pt, gocv.FontHersheyPlain, 1.2, blue, 2)
}
// show the image in the window, and wait 1 millisecond
window.IMShow(img2)
img.Close()
img2.Close()
if window.WaitKey(10) >= 0 {
break
}
}
return
}
// TestExampleOpenCvFaceDetectWithCamera will: task stream from webcam => use opencv for face detection => output with ffmpeg
// This example depends on gocv and opencv, please refer: https://pkg.go.dev/gocv.io/x/gocv for installation.
func TestExampleOpenCvFaceDetectWithCamera(t *testing.T) {
deviceID := "0" // camera device id
xmlFile := "./sample_data/haarcascade_frontalface_default.xml"
webcam, err := gocv.OpenVideoCapture(deviceID)
if err != nil {
fmt.Printf("error opening video capture device: %v\n", deviceID)
return
}
defer webcam.Close()
// prepare image matrix
img := gocv.NewMat()
defer img.Close()
if ok := webcam.Read(&img); !ok {
panic(fmt.Sprintf("Cannot read device %v", deviceID))
}
fmt.Printf("img: %vX%v\n", img.Cols(), img.Rows())
pr1, pw1 := io.Pipe()
writeProcess("./sample_data/face_detect.mp4", pr1, img.Cols(), img.Rows())
// color for the rect when faces detected
blue := color.RGBA{B: 255}
// 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 i := 0; i < 200; i++ {
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))
// draw a rectangle around each face on the original image, along with text identifing as "Human"
for _, r := range rects {
gocv.Rectangle(&img, r, blue, 3)
size := gocv.GetTextSize("Human", gocv.FontHersheyPlain, 1.2, 2)
pt := image.Pt(r.Min.X+(r.Min.X/2)-(size.X/2), r.Min.Y-2)
gocv.PutText(&img, "Human", pt, gocv.FontHersheyPlain, 1.2, blue, 2)
}
pw1.Write(img.ToBytes())
}
pw1.Close()
log.Println("Done")
}
func writeProcess(outputFile string, reader io.ReadCloser, w, h int) {
log.Println("Starting ffmpeg process1")
go func() {
err := ffmpeg.Input("pipe:",
ffmpeg.KwArgs{"format": "rawvideo",
"pix_fmt": "bgr24", "s": fmt.Sprintf("%dx%d", w, h),
}).
Overlay(ffmpeg.Input("./sample_data/overlay.png"), "").
Output(outputFile).
WithInput(reader).
ErrorToStdOut().
OverWriteOutput().
Run()
log.Println("ffmpeg process1 done")
if err != nil {
panic(err)
}
_ = reader.Close()
}()
}