Now we have two different uses of two different algorithms to detect faces.
Here are some observations:
- The images using PIGO are smoother—there are fewer jumps and lags.
- The PIGO algorithm jitters a little more than the standard Viola-Jones method.
- The PIGO algorithm is more robust to rotations—I could tilt my head more and still have my face detected compared to the standard Viola-Jones method.
We can of course put both of them together:
var haarCascadeFile = "Path/To/CascadeFile.xml"
var blue = color.RGBA{0, 0, 255, 0}
var green = color.RGBA{0, 255, 0, 0}
func main() {
var err error
// open webcam
if webcam, err = gocv.VideoCaptureDevice(0); err != nil {
log.Fatal(err)
}
defer webcam.Close()
width := int(webcam.Get(gocv.VideoCaptureFrameWidth))
height := int(webcam.Get(gocv.VideoCaptureFrameHeight))
// open display window
window := gocv.NewWindow("Face Detect")
defer window.Close()
// prepare image matrix
img := gocv.NewMat()
defer img.Close()
// set up pigo
goImg, grayGoImg, pigoClass, cParams, imgParams := pigoSetup(width,
height)
// create classifier and load model
classifier := gocv.NewCascadeClassifier()
if !classifier.Load(haarCascadeFile) {
log.Fatalf("Error reading cascade file: %v ", haarCascadeFile)
}
defer classifier.Close()
for {
if ok := webcam.Read(&img); !ok {
fmt.Printf("cannot read device %d ", deviceID)
return
}
if img.Empty() {
continue
}
// use PIGO
if err = naughtyToImage(&img, goImg); err != nil {
log.Fatal(err)
}
grayGoImg = naughtyGrayscale(grayGoImg, goImg)
imgParams.Pixels = grayGoImg
dets := pigoClass.RunCascade(imgParams, cParams)
dets = pigoClass.ClusterDetections(dets, 0.3)
for _, det := range dets {
if det.Q < 5 {
continue
}
x := det.Col - det.Scale/2
y := det.Row - det.Scale/2
r := image.Rect(x, y, x+det.Scale, y+det.Scale)
gocv.Rectangle(&img, r, green, 3)
}
// use GoCV
rects := classifier.DetectMultiScale(img)
for _, r := range rects {
gocv.Rectangle(&img, r, blue, 3)
}
window.IMShow(img)
if window.WaitKey(1) >= 0 {
break
}
}
}
Here we see PIGO and GoCV both managed to detect them rather accurately, and that they agree with each other quite a lot.
Additionally we can see that there is now a fairly noticeable lag between actions and when the actions are displayed on screen. This is because there is more work to be done.