Face Detection in Go using OpenCV and MachineBox
This is a text version of this video: packagemain #5: Face Detection in Go using OpenCV and MachineBox.
I found a very nice developer-friendly project MachineBox, which provides some machine learning tools inside Docker Container, including face detection, natural language understanding and few more. And it has SDK in Go, so we will build a program which will detect my face. We will also use OpenCV to capture video from Web camera, it also has Go bindings.
MachineBox can be installed very easily by running Docker container. First of all we need to register on machinebox.io and get key, then we can set environment variable MB_KEY
and run facebox on port 8080:
|
|
Let’s go now to localhost:8080 and verify that it’s ready, now we can use Go SDK to communicate with facebox from our Go program.
To be able to capture video and recognize faces from the web camera we have to install OpenCV. I am using GoCV.io which has Go bindings for OpenCV. Installation may be complicated, but it worked on my Mac with OpenCV3. Also let’s get Go package.
|
|
Then we need to set some environment variables, we can use env.sh
from gocv package repo:
|
|
Now let’s create a main.go file and capture video from web camera:
|
|
With OpenCV we can also use classifier to recognize faces on the image. Let’s do it, for this we’ll use Haar Cascades classifier. I already downloaded this XML file to use, you can find it in GoCV repository. With GoCV it’s also possible to draw rectangle for each face to highlight it.
|
|
To be able to recognize faces we need to train our model, in Facebox it’s very easy, we can just upload few images and set the name. I will do it by saving images of my face into a folder.
|
|
Now let’s go to localhost:8080. There are few options to train Facebox: we can use API, we can use web page or we can use Go SDK.
For this example the easiest way is to use form from web page.
Now after we trained it we can check if Facebox can find my face on the image and we can print my name.
|
|
GoCV has no option to get io.Reader
of an image, so we will open our saved file.
|
|
It’s not an advertisement, but I really think that MachineBox is a nice project to provide some Machine learning power to you within simple docker containers.