-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (47 loc) · 1.13 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
package main
import (
"fmt"
"image/color"
"log"
"gocv.io/x/gocv"
)
var xmlFile = "/usr/local/Cellar/opencv/4.5.3_2/share/opencv4/haarcascades/haarcascade_eye_tree_eyeglasses.xml"
func main() {
webcam, err := gocv.VideoCaptureDevice(0)
if err != nil {
log.Fatalf("error opening web cam: %v", err)
}
defer webcam.Close()
// open display window
window := gocv.NewWindow("Face Detect")
defer window.Close()
// prepare image matrix
img := gocv.NewMat()
defer img.Close()
classifier := gocv.NewCascadeClassifier()
defer classifier.Close()
if !classifier.Load(xmlFile) {
fmt.Printf("Error reading cascade file: %v\n", xmlFile)
return
}
for {
if ok := webcam.Read(&img); !ok || img.Empty() {
fmt.Printf("cannot read device")
return
}
if img.Empty() {
continue
}
// detect faces
rects := classifier.DetectMultiScale(img)
fmt.Printf("found %d faces\n", len(rects))
color := color.RGBA{0, 255, 0, 0}
for _, r := range rects {
fmt.Println("detected", r)
gocv.Rectangle(&img, r, color, 3)
}
// show the image in the window, and wait 1 millisecond
window.IMShow(img)
window.WaitKey(50)
}
}