Building a face recognition system based on Golang and OpenCV







OpenCV is a library designed for computer vision projects. She is already about 20 years old. I used it in college and still use it for my projects in C ++ and Python, because it has good support for these 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 got into the hands of a wrapper created by the team of The Hybrid Group. In this article, I'll show you how to get started with GoCV by developing a simple face recognition system with Haar Cascades.



Skillbox recommends: A Python Developer from scratch hands-on course.



We remind you: for all readers of “Habr” - a discount of 10,000 rubles when registering for any Skillbox course using the “Habr” promo code.



What is required:



Installation





Example 1



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



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 the video stream using the camera. The function uses 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 you need to create an n-dimensional matrix. It will store 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 endless loop to read the camera’s video stream endlessly. To do this, you need the Read method of type VideoCapture. It will wait for the type Mat (the matrix we created above), returning a boolean value indicating whether the frame from 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 you need to display the frame in the created window. Pause to go to the next frame - 50 ms.



window.IMShow (img)

window.WaitKey (50)



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







 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 take advantage of the previous example and build a face recognition system based on Haar Cascades (Haar cascades).



Haar cascades are cascading classifiers that are trained based on the Haar wavelet technique. They analyze the pixels in the image to detect certain signs. To learn more about Haar Cascades, follow the links below.



Viola-jones object detection framework

Cascading classifiers

Haar-like feature



You can download already trained cascades here . In the current example, cascades will be used to identify a person’s face in face.



In order to do this, you need to create a classifier and feed him an already trained file (the link above is given). 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 the image, you need to use the DetectMultiScale method. This function takes a frame (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 that we see what he found, let's go through the list of rectangles and display the Rectangle object on the console, creating a border around the detected rectangle. This can be done using the Rectangle function. It will take the Mat read by the camera, the Rectangle object that was returned by the DetectMultiScale method, color and thickness for the border.



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













 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, it worked out! Now we have a simple face recognition system written in Go. In the near future I plan to continue these experiments and create new cool stuff, combining Go and OpenCV.



If you're interested, then check out the gRPC web server that I wrote in Python and OpenCV. It streams data at the time of face detection. 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:






All Articles