package main
import (
"gocv.io/x/gocv"
)
func main() {
// Open a video capture device. Use 0 for the default camera.
webcam, _ := gocv.VideoCaptureDevice(0)
defer webcam.Close()
// Create a window to display the video feed.
window := gocv.NewWindow("Face Detection")
defer window.Close()
// Load the pre-trained face detection classifier.
classifier := gocv.NewCascadeClassifier()
defer classifier.Close()
classifier.Load("haarcascade_frontalface_default.xml")
// Create an image matrix to hold each frame from the video feed.
img := gocv.NewMat()
defer img.Close()
// Continuously capture and process video frames.
for {
// Read a frame from the video feed.
webcam.Read(&img)
// If the frame is empty, break out of the loop.
if img.Empty() {
break
}
// Detect faces in the current frame.
rectangles := classifier.DetectMultiScale(img)
// Draw rectangles around detected faces.
for _, r := range rectangles {
gocv.Rectangle(&img, r, gocv.Scalar{0, 255, 0, 0}, 3)
}
// Show the frame with rectangles.
window.IMShow(img)
// Wait for a key event to break out of the loop.
if window.WaitKey(1) >= 0 {
break
}
}
}