Building a face recognition system based on Golang and OpenCV

Building a face recognition system based on Golang and OpenCV
OpenCV is a library developed for computer vision projects. She is already about 20 years old. I used it in college and still use it for my C++ and Python projects because it has good support for those languages.

But when I started learning and using Go, I wondered if OpenCV could be used to work with this language. At that time there were already examples and tutorials on integration, but it seemed to me that they were too complicated. A little later, I came across a wrapper created by The Hybrid Group. In this article, I will show you how to get started with GoCV by developing a simple face recognition system with Haar Cascades.

Skillbox recommends: Practical course "Python developer from scratch".

We remind you: for all readers of "Habr" - a discount of 10 rubles when enrolling in any Skillbox course using the "Habr" promotional code.

What will be required:

  • go;
  • OpenCV (links to installer below);
  • web or regular camcorder.

Installation

Example 1

In the first example, we will try to create an application that opens a window with a camera video stream.

First you need to import the libraries you need to work.

import(
"log"
“gocv.io/x/gocv”
)

After that, you need to create a VideoCapture object using the VideoCaptureDevice function. The latter makes it possible to capture a video stream using a camera. The function takes an integer as a parameter (it represents the device ID).

webcam, err := gocv.VideoCaptureDevice(0)
if err != nil {    log.Fatalf(“error opening web cam: %v”, err)
}
defer webcam.Close()

Now we need to create an n-dimensional matrix. It will store the images read from the camera.

img := gocv.NewMat()
defer img.Close()

To display the video stream, you need to create a window - this can be done using the NewWindow function.

window := gocv.NewWindow(“webcamwindow”)
defer window.Close()

Now let's move on to the most interesting part.

Since the video is a continuous stream of image frames, we will need to create an infinite loop to endlessly read the camera's video stream. This requires the Read method of the VideoCapture type. It will expect a Mat type (the matrix we created above), returning a boolean indicating whether a frame from the VideoCapture was read successfully or not.

for {     
        if ok := webcam.Read(&img); !ok || img.Empty( {
        log.Println(“Unable to read from the webcam”)    continue
     }
.
.
.
}

Now we need to display the frame in the created window. Pause to move to the next frame - 50 ms.

window.IMShow(img)
window.WaitKey(50)

After launching the application, a window with a video stream from the camera will open.

Building a face recognition system based on Golang and OpenCV

package main
 
import (
"log"
 
"gocv.io/x/gocv"
)
 
func main() {
webcam, err := gocv.VideoCaptureDevice(0)
if err != nil {
    log.Fatalf("error opening device: %v", err)
}
defer webcam.Close()
 
img := gocv.NewMat()
defer img.Close()
 
window := gocv.NewWindow("webcamwindow")
defer window.Close()
 
for {
if ok := webcam.Read(&img); !ok || img.Empty() {
log.Println("Unable to read from the webcam")
continue
}
 
window.IMShow(img)
window.WaitKey(50)
}
}

Example 2

In this example, let's use the previous example and build a face recognition system based on Haar Cascades.

Haar cascades are cascaded classifiers that are trained based on the Haar wavelet technique. They analyze the pixels in an image to look for specific features. To learn more about Haar Cascades, please follow the links below.

Viola-Jones object detection framework
Cascading classifiers
Haar-like feature

Download already trained cascades here. In the current example, cascades will be used to identify a person's face in front.

In order to do this, you need to create a classifier and feed it an already trained file (the link is given above). I have already uploaded the pencv_haarcascade_frontalface_default.xml file to the directory where our program is located.

harrcascade := “opencv_haarcascade_frontalface_default.xml”classifier := gocv.NewCascadeClassifier()classifier.Load(harrcascade)
defer classifier.Close()

To detect faces in an image, you need to use the method DetectMultiScale. This function takes a frame (of type Mat) that has just been read from the camera's video stream and returns an array of type Rectangle. The size of the array represents the number of faces that the classifier was able to detect in the frame. Then, to make sure we see what it found, let's iterate through the list of rectangles and print the Rectangle object to the console, creating a border around the found rectangle. This can be done using the Rectangle function. It will take the Mat read by the camera, the Rectangle object returned by the DetectMultiScale method, and the color and thickness for the border.

for _, r := range rects {
fmt.Println(“detected”, r)
gocv.Rectangle(&img, r, color, 2)
}

Building a face recognition system based on Golang and OpenCV

Building a face recognition system based on Golang and OpenCV

package main
 
import (
"fmt"
"image/color"
"log"
 
"gocv.io/x/gocv"
)
 
func main() {
webcam, err := gocv.VideoCaptureDevice(0)
if err != nil {
log.Fatalf("error opening web cam: %v", err)
}
defer webcam.Close()
 
img := gocv.NewMat()
defer img.Close()
 
window := gocv.NewWindow("webcamwindow")
defer window.Close()
 
harrcascade := "opencv_haarcascade_frontalface_default.xml"
classifier := gocv.NewCascadeClassifier()
classifier.Load(harrcascade)
defer classifier.Close()
 
color := color.RGBA{0, 255, 0, 0}
for {
if ok := webcam.Read(&img); !ok || img.Empty() {
log.Println("Unable to read from the device")
continue
}
 
rects := classifier.DetectMultiScale(img)
for _, r := range rects {
fmt.Println("detected", r)
gocv.Rectangle(&img, r, color, 3)
}
 
window.IMShow(img)
window.WaitKey(50)
}
}

And ... yes, everything worked out! We now have a simple face recognition system written in Go. In the near future, I plan to continue these experiments and create new cool things by combining Go and OpenCV.

If you are interested, please rate gRPC web server, which I wrote in Python and OpenCV. It streams data as soon as a face is detected. This is the basis for creating different clients in different programming languages. They will be able to connect to the server and read data from it.

Thanks for reading the article!

Skillbox recommends:

Source: habr.com

Add a comment