• Stars
    star
    1,114
  • Rank 41,670 (Top 0.9 %)
  • Language
    Go
  • License
    Creative Commons ...
  • Created over 6 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🔍 Face recognition with Go

go-face Build Status GoDoc

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.

Requirements

To compile go-face you need to have dlib (>= 19.10) and libjpeg development packages installed.

Ubuntu 18.10+, Debian sid

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

macOS

Make sure you have Homebrew installed.

brew install dlib

Windows

Make sure you have MSYS2 installed.

  1. Run MSYS2 MSYS shell from Start menu
  2. Run pacman -Syu and if it asks you to close the shell do that
  3. Run pacman -Syu again
  4. Run pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-dlib
    1. If you already have Go and Git installed and available in PATH uncomment set MSYS2_PATH_TYPE=inherit line in msys2_shell.cmd located in MSYS2 installation folder
    2. Otherwise run pacman -S mingw-w64-x86_64-go git
  5. Run MSYS2 MinGW 64-bit shell from Start menu to compile and use go-face

Other systems

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.

Models

Currently shape_predictor_5_face_landmarks.dat, mmod_human_face_detector.dat and dlib_face_recognition_resnet_model_v1.dat are required. You may download them from go-face-testdata repo:

wget https://github.com/Kagami/go-face-testdata/raw/master/models/shape_predictor_5_face_landmarks.dat
wget https://github.com/Kagami/go-face-testdata/raw/master/models/dlib_face_recognition_resnet_model_v1.dat
wget https://github.com/Kagami/go-face-testdata/raw/master/models/mmod_human_face_detector.dat

Usage

To use go-face in your Go code:

import "github.com/Kagami/go-face"

To install go-face in your $GOPATH:

go get github.com/Kagami/go-face

For further details see GoDoc documentation.

Example

package main

import (
	"fmt"
	"log"
	"path/filepath"

	"github.com/Kagami/go-face"
)

// Path to directory with models and test images. Here it's assumed it
// points to the <https://github.com/Kagami/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()

	// 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

Test

To fetch test data and run tests:

make test

FAQ

How to improve recognition accuracy

There are few suggestions:

  • Try CNN recognizing
  • Try different tolerance values of ClassifyThreshold
  • Try different size/padding/jittering values of NewRecognizerWithConfig
  • 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

License

go-face is licensed under CC0.

More Repositories

1

ffmpeg.js

Port of FFmpeg with Emscripten
JavaScript
3,299
star
2

avif.js

:shipit: AVIF polyfill for the browser
JavaScript
681
star
3

webm.js

JavaScript WebM converter
JavaScript
456
star
4

boram

🎞️ Cross-platform graphical WebM converter
JavaScript
441
star
5

mpv.js

🎥 mpv pepper plugin
C++
407
star
6

vmsg

🎵 Library for creating voice messages
JavaScript
348
star
7

go-avif

🎨 Go AVIF library
Go
323
star
8

gulp-ng-annotate

📌 Add angularjs dependency injection annotations with ng-annotate
JavaScript
266
star
9

webm.py

🎞️ Cross-platform command-line WebM converter
Python
142
star
10

chaptcha

Break 2ch CAPTCHA using OpenCV and FANN
Python
87
star
11

mpv_slicing

Cut video fragments with mpv
Lua
82
star
12

wybm

✂️ Extract and cut youtube webms
JavaScript
58
star
13

dav1d.js

⚗️ WebAssembly AV1 decoder
C
34
star
14

tistore

📷 Tistory photo grabber
JavaScript
24
star
15

kagome

Application cage
Shell
19
star
16

mpv_frame_info

Show frame info with mpv
Lua
18
star
17

video-tools

Various video tools
Jupyter Notebook
16
star
18

go-face-testdata

🗃️ Test data for go-face
15
star
19

github-social-graph

Build simple social graphs for GitHub
Python
15
star
20

kisa

XMPP stress tool
Python
14
star
21

docker_cve-2015-2925

Docker + CVE-2015-2925 = escaping from --volume
11
star
22

wasm-polyfill.js

🍴 Fork of rfk's project
JavaScript
11
star
23

bnw-meow

Kawaii single-page web interface for BnW
CoffeeScript
10
star
24

nacl_sdk

🍴 Fork of Native Client SDK
Python
10
star
25

av1-bench

🏎️ AV1 encoders benchmarks
Python
9
star
26

dotfiles

Dotfiles the easy way
Vim Script
7
star
27

jade-pages-brunch

Adds Jade static pages support to brunch
CoffeeScript
7
star
28

kpopnet

[MOVED]
Go
7
star
29

SVT-AV1

🍴 Welcome to the GitHub repo for the SVT-AV1 encoder! To see a list of feature request and view what is planned for the SVT-AV1 encoder, visit our Trello page: bit.ly/SVT-AV1 Help us grow the community by subscribing to our SVT-AV1 mailing list! http://bit.ly/svt-av1-mailing
C
7
star
30

ng-annotate-uglify-js-brunch

Adds ng-annotate AND UglifyJS support to brunch
JavaScript
5
star
31

jade-ngtemplates-brunch

Adds Jade AngularJS templates support to brunch
CoffeeScript
4
star
32

awesome-cloud-cli

A curated list of useful CLI tools for cloud hostings
JavaScript
4
star
33

ninnin

🎞️ mpv-based video encoding tool
TypeScript
4
star
34

hangeul.js

🇰🇷 Hangeul transliteration library
JavaScript
3
star
35

cirno

(OBSOLETE) Dumb XMPP library, the successor of the XMPP lib
Haskell
3
star
36

dogfood

🔩 Basic blocks of operating system
C
3
star
37

webmify

Allow to watch WebMs in Edge
JavaScript
3
star
38

web-bench

Benchmarks for various web platforms
Perl
3
star
39

parcel-plugin-disable-loaders

📦 Allow to disable parcel loaders
JavaScript
3
star
40

shitsu

Tiny and flexible XMPP bot framework
Python
3
star
41

jqw

JQ compiled to WebAssembly
TypeScript
2
star
42

skip-loader

A loader that returns an empty string module
JavaScript
2
star
43

lame-svn

Git mirror
C
2
star
44

gulp-recipes

Tiny gulp recipes
1
star