go-face implements face recognition for Go using dlib, a popular machine learning toolkit. Read Face recognition with Go article for some background details if you're new to FaceNet concept.
To compile go-face you need to have dlib (>= 19.10) and libjpeg development packages installed.
Latest versions of Ubuntu and Debian provide suitable dlib package so just run:
# Ubuntu
sudo apt-get install libdlib-dev libblas-dev libatlas-base-dev liblapack-dev libjpeg-turbo8-dev
# Debian
sudo apt-get install libdlib-dev libblas-dev libatlas-base-dev liblapack-dev libjpeg62-turbo-dev
Make sure you have Homebrew installed.
brew install dlib
Make sure you have MSYS2 installed.
- Run
MSYS2 MSYS
shell from Start menu - Run
pacman -Syu
and if it asks you to close the shell do that - Run
pacman -Syu
again - Run
pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-dlib
-
- If you already have Go and Git installed and available in PATH uncomment
set MSYS2_PATH_TYPE=inherit
line inmsys2_shell.cmd
located in MSYS2 installation folder - Otherwise run
pacman -S mingw-w64-x86_64-go git
- If you already have Go and Git installed and available in PATH uncomment
- Run
MSYS2 MinGW 64-bit
shell from Start menu to compile and use go-face
Try to install dlib/libjpeg with package manager of your distribution or compile from sources. Note that go-face won't work with old packages of dlib such as libdlib18. Alternatively create issue with the name of your system and someone might help you with the installation process.
Currently shape_predictor_5_face_landmarks.dat
(or shape_predictor_68_face_landmarks.dat
for more detailed facial landmarks), mmod_human_face_detector.dat
and
dlib_face_recognition_resnet_model_v1.dat
are required. You may download them
from go-face-testdata repo:
# 5-point landmarks model (default, smaller and faster)
wget https://github.com/lib-x/go-face-testdata/raw/master/models/shape_predictor_5_face_landmarks.dat
# OR 68-point landmarks model (more detailed facial features)
wget http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
bunzip2 shape_predictor_68_face_landmarks.dat.bz2
# Face recognition model
wget https://github.com/lib-x/go-face-testdata/raw/master/models/dlib_face_recognition_resnet_model_v1.dat
# CNN face detection model
wget https://github.com/lib-x/go-face-testdata/raw/master/models/mmod_human_face_detector.dat
To use go-face in your Go code:
import "github.com/lib-x/go-face"
To install go-face in your $GOPATH:
go get github.com/lib-x/go-face
For further details see GoDoc documentation.
package main
import (
"fmt"
"log"
"path/filepath"
"github.com/lib-x/go-face"
)
// Path to directory with models and test images. Here it's assumed it
// points to the <https://github.com/lib-x/go-face-testdata> clone.
const dataDir = "testdata"
var (
modelsDir = filepath.Join(dataDir, "models")
imagesDir = filepath.Join(dataDir, "images")
)
// This example shows the basic usage of the package: create an
// recognizer, recognize faces, classify them using few known ones.
func main() {
// Init the recognizer.
rec, err := face.NewRecognizer(modelsDir)
if err != nil {
log.Fatalf("Can't init face recognizer: %v", err)
}
// Free the resources when you're finished.
defer rec.Close()
// Alternative: Init recognizer with 68-point landmarks model
// rec, err := face.NewRecognizerWithShapePredictor(modelsDir, "shape_predictor_68_face_landmarks.dat")
// if err != nil {
// log.Fatalf("Can't init face recognizer: %v", err)
// }
// defer rec.Close()
// Test image with 10 faces.
testImagePristin := filepath.Join(imagesDir, "pristin.jpg")
// Recognize faces on that image.
faces, err := rec.RecognizeFile(testImagePristin)
if err != nil {
log.Fatalf("Can't recognize: %v", err)
}
if len(faces) != 10 {
log.Fatalf("Wrong number of faces")
}
// Fill known samples. In the real world you would use a lot of images
// for each person to get better classification results but in our
// example we just get them from one big image.
var samples []face.Descriptor
var cats []int32
for i, f := range faces {
samples = append(samples, f.Descriptor)
// Each face is unique on that image so goes to its own category.
cats = append(cats, int32(i))
}
// Name the categories, i.e. people on the image.
labels := []string{
"Sungyeon", "Yehana", "Roa", "Eunwoo", "Xiyeon",
"Kyulkyung", "Nayoung", "Rena", "Kyla", "Yuha",
}
// Pass samples to the recognizer.
rec.SetSamples(samples, cats)
// Now let's try to classify some not yet known image.
testImageNayoung := filepath.Join(imagesDir, "nayoung.jpg")
nayoungFace, err := rec.RecognizeSingleFile(testImageNayoung)
if err != nil {
log.Fatalf("Can't recognize: %v", err)
}
if nayoungFace == nil {
log.Fatalf("Not a single face on the image")
}
catID := rec.Classify(nayoungFace.Descriptor)
if catID < 0 {
log.Fatalf("Can't classify")
}
// Finally print the classified label. It should be "Nayoung".
fmt.Println(labels[catID])
}
Run with:
mkdir -p ~/go && cd ~/go # Or cd to your $GOPATH
mkdir -p src/go-face-example && cd src/go-face-example
git clone https://github.com/Kagami/go-face-testdata testdata
edit main.go # Paste example code
go get && go run main.go
To fetch test data and run tests:
make test
There are few suggestions:
- Try CNN recognizing
- Try different tolerance values of
ClassifyThreshold
- Try different size/padding/jittering values of
NewRecognizerWithConfig
- Use 68-point landmarks model for more detailed facial features with
NewRecognizerWithShapePredictor
- Provide more samples of each category to
SetSamples
if possible - Implement better classify heuristics (see classify.cc)
- Train network (
dlib_face_recognition_resnet_model_v1.dat
) on your own test data
The default 5-point face landmarks model is faster and suitable for most face recognition tasks. However, if you need more detailed facial features (e.g., for facial expression analysis, face morphing, or more precise face alignment), you can use the 68-point model:
// Use 68-point landmarks model
rec, err := face.NewRecognizerWithShapePredictor(modelsDir, "shape_predictor_68_face_landmarks.dat")
if err != nil {
log.Fatal(err)
}
defer rec.Close()
// You can also combine it with custom configuration
rec, err := face.NewRecognizerWithShapePredictorAndConfig(
modelsDir,
"shape_predictor_68_face_landmarks.dat",
150, // size
0.25, // padding
1, // jittering
)
Note that the 68-point model is larger and slightly slower than the 5-point model, but provides much more detailed facial landmark information.
The face recognition neural network model (dlib_face_recognition_resnet_model_v1.dat
) expects input images of exactly 150x150 pixels. When using NewRecognizerWithConfig
or NewRecognizerWithShapePredictorAndConfig
with a different size
parameter, the library will automatically resize the extracted face chips to 150x150 before feeding them to the network. This ensures compatibility while still allowing you to control the initial face extraction size for better alignment or processing needs.
go-face is licensed under CC0.