Added a sample for face detection on video from builtin camera
This commit is contained in:
parent
60747d253e
commit
033205d5e7
1 changed files with 54 additions and 0 deletions
54
samples/webcam_facedetect.go
Normal file
54
samples/webcam_facedetect.go
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
|
||||||
|
"github.com/lazywei/go-opencv/opencv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
win := opencv.NewWindow("Go-OpenCV Webcam Face Detection")
|
||||||
|
defer win.Destroy()
|
||||||
|
|
||||||
|
cap := opencv.NewCameraCapture(0)
|
||||||
|
if cap == nil {
|
||||||
|
panic("cannot open camera")
|
||||||
|
}
|
||||||
|
defer cap.Release()
|
||||||
|
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
cascade := opencv.LoadHaarClassifierCascade(path.Join(cwd, "haarcascade_frontalface_alt.xml"))
|
||||||
|
|
||||||
|
fmt.Println("Press ESC to quit")
|
||||||
|
for {
|
||||||
|
if cap.GrabFrame() {
|
||||||
|
img := cap.RetrieveFrame(1)
|
||||||
|
if img != nil {
|
||||||
|
faces := cascade.DetectObjects(img)
|
||||||
|
for _, value := range faces {
|
||||||
|
opencv.Circle(img,
|
||||||
|
opencv.Point{
|
||||||
|
value.X() + (value.Width() / 2),
|
||||||
|
value.Y() + (value.Height() / 2),
|
||||||
|
},
|
||||||
|
value.Width()/2,
|
||||||
|
opencv.ScalarAll(255.0), 1, 1, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
win.ShowImage(img)
|
||||||
|
} else {
|
||||||
|
fmt.Println("nil image")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
key := opencv.WaitKey(1)
|
||||||
|
|
||||||
|
if key == 27 {
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue