facenet

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

README

Golang lib for detect/recognize by tensorflow facenet

Go Reference Go goreleaser GitHub go.mod Go version of a Go module GoReportCard GitHub license GitHub release

Prerequest

  1. libtensorfow 1.x Follow the instruction Install TensorFlow for C
  2. facenet tenorflow saved_model Google Drive
  3. build the executable
  4. download font(optional) Google Drive
# generated to ./bin/facenet
make facenet

Demo

demo screen capture

Install

go get -u github.com/bububa/facenet

Usage

Train faces
./bin/facenet -model=./models/facenet -db=./models/people.db -train={image folder for training} -output={fold path for output thumbs(optional)}

the train folder include folders which name is the label with images inside

Update distinct labels
./bin/facenet -model=./models/facenet -db=./models/people.db -update={labels for update seperated by comma} -output={fold path for output thumbs(optional)}
Delete distinct labels from people model
./bin/facenet -model=./models/facenet -db=./models/people.db -delete={labels for delete seperated by comma} -output={fold path for output thumbs(optional)}
Detect faces for image
./bin/facenet -model=./models/facenet -db=./models/people.db -detect={the image file path for detecting} -font={font folder for output image(optional)} -output={fold path for output thumbs(optional)}

Camera & Server

Requirements
  • libjpeg-turbo (use -tags jpeg to build without CGo)
  • On Linux/RPi native Go V4L implementation is used to capture images.
Use Opencv4
make cvcamera
On linux/Pi
# use native Go V4L implementation is used to capture images
make linux_camera
Use image/jpeg instead of libjpeg-turbo

use jpeg build tag to build with native Go image/jpeg instead of libjpeg-turbo

go build -o=./bin/cvcamera -tags=cv4,jpeg ./cmd/camera
Usage as Server
Usage of camera:
  -bind string
	Bind address (default ":56000")
  -delay int
	Delay between frames, in milliseconds (default 10)
  -width float
	Frame width (default 640)
  -height float
	Frame height (default 480)
  -index int
	Camera index
  -model string
    saved_mode path
  -db string
    classifier db

User as lib

import (
    "log"

	"github.com/llgcode/draw2d"

    "github.com/bububa/facenet"
)

func main() {
    estimator, err := facenet.New(
        facenet.WithModel("./models/facenet"),
        facenet.WithDB("./models/people.db"),
        facenet.WithFontPath("./font"),
    )
    if err != nil {
       log.Fatalln(err)
    }
	err = estimator.SetFont(&draw2d.FontData{
		Name: "NotoSansCJKsc",
		//Name:   "Roboto",
		Family: draw2d.FontFamilySans,
		Style:  draw2d.FontStyleNormal,
	}, 9)
	if err != nil {
		log.Fatalln(err)
	}

    // Delete labels
    {
        labels := []string{"xxx", "yyy"}
        for _, label := range labels {
            if deleted := estimator.DeletePerson(label); deleted {
                log.Printf("[INFO] person: %s deleted\n", label)
                continue
            }
            log.Printf("[WRN] person: %s not found\n", label)
        }
        err := estimator.SaveDB("./models/people.db")
        if err != nil {
            log.Fatalln(err)
        }
    }

    // Detect faces
    {
        img, _ := loadImage(imgPath)
        minSize := 20
		markers, err := instance.DetectFaces(img, minSize)
		if err != nil {
			log.Fatalln(err)
		}
		for _, marker := range markers.Markers() {
			if marker.Error() != nil {
				log.Printf("label: %s, %v\n", marker.Label(), marker.Error())
			} else {
				log.Printf("label: %s, distance:%f\n", marker.Label(), marker.Distance())
			}
		}
		if outputPath != "" {
            txtColor := "#FFF"
            successColor := "#4CAF50"
            failedColor := "#F44336"
            strokeWidth := 2
            successMarkerOnly := false
			markerImg := estimator.DrawMarkers(markers, txtColor, successColor, failedColor, 2, successMarkerOnly)
			if err := saveImage(markerImg, outputPath); err != nil {
				log.Fatalln(err)
			}
		}
    }

    // Training
    // check cmd/facenet
}

Documentation

Overview

Package facenet for face detection/recognization by pigo and facenet

Index

Constants

View Source
const (
	// PeopleFilename represents people data filename in zip
	PeopleFilename = "people.pb"
	// ClassifierFilename represents classifier data filename in zip
	ClassifierFilename = "classifier.model"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Estimator added in v1.1.0

type Estimator struct {
	// contains filtered or unexported fields
}

Estimator represents facenet estimator

func New

func New(opts ...Option) (*Estimator, error)

New init a new facenet Estimator

func (*Estimator) AddPerson added in v1.1.0

func (ins *Estimator) AddPerson(items ...*core.Person)

AddPerson add person to people

func (*Estimator) AddPersonSafe added in v1.1.0

func (ins *Estimator) AddPersonSafe(items ...*core.Person)

AddPersonSafe add person to people (multithread safe)

func (*Estimator) BatchTrain added in v1.1.0

func (ins *Estimator) BatchTrain(split float64, iterations int, verbosity int, batch int)

BatchTrain for trainging classifier

func (*Estimator) BatchTrainSafe added in v1.1.0

func (ins *Estimator) BatchTrainSafe(split float64, iterations int, verbosity int, batch int)

BatchTrainSafe for trainging classifier (multithread safe)

func (*Estimator) DeletePerson added in v1.1.0

func (ins *Estimator) DeletePerson(name string) bool

DeletePerson delete a person by name

func (*Estimator) DeletePersonSafe added in v1.1.0

func (ins *Estimator) DeletePersonSafe(name string) bool

DeletePersonSafe delete a person by name (multithread safe)

func (*Estimator) DetectFaces added in v1.1.0

func (ins *Estimator) DetectFaces(img image.Image, minSize int) (*core.FaceMarkers, error)

DetectFaces detect face markers from image

func (*Estimator) DetectFacesSafe added in v1.1.0

func (ins *Estimator) DetectFacesSafe(img image.Image, minSize int) (*core.FaceMarkers, error)

DetectFacesSafe detect face markers from image (multithread safe)

func (*Estimator) DrawMarkers added in v1.1.0

func (ins *Estimator) DrawMarkers(markers *core.FaceMarkers, txtColor string, successColor string, failedColor string, strokeWidth float64, succeedOnly bool) image.Image

DrawMarkers draw face markers on image

func (*Estimator) ExtractFace added in v1.1.0

func (ins *Estimator) ExtractFace(person *core.Person, img image.Image, minSize int) (*core.FaceMarker, error)

ExtractFace extract face for a person from image

func (*Estimator) ExtractFaceSafe added in v1.1.0

func (ins *Estimator) ExtractFaceSafe(person *core.Person, img image.Image, minSize int) (*core.FaceMarker, error)

ExtractFaceSafe extract face for a person from image (multithread safe)

func (*Estimator) LoadDB added in v1.1.0

func (ins *Estimator) LoadDB(fname string) error

LoadDB load db file

func (*Estimator) Match added in v1.1.0

func (ins *Estimator) Match(embedding []float32) (*core.Person, float64, error)

Match match a person with embedding

func (*Estimator) MatchSafe added in v1.1.0

func (ins *Estimator) MatchSafe(embedding []float32) (*core.Person, float64, error)

MatchSafe match a person with embedding (multithread safe)

func (*Estimator) People added in v1.1.0

func (ins *Estimator) People() *core.People

People get people

func (*Estimator) PeopleSafe added in v1.1.0

func (ins *Estimator) PeopleSafe() *core.People

PeopleSafe get people (mutlthread safe)

func (*Estimator) Predict added in v1.1.0

func (ins *Estimator) Predict(embedding []float32) ([]*core.Person, []float64, error)

Predict returns embedding predicted results

func (*Estimator) PredictSafe added in v1.1.0

func (ins *Estimator) PredictSafe(embedding []float32) ([]*core.Person, []float64, error)

PredictSafe returns embedding predicted results (multithread safe)

func (*Estimator) SaveDB added in v1.1.0

func (ins *Estimator) SaveDB(fname string) error

SaveDB save db file

func (*Estimator) SetDB added in v1.1.0

func (ins *Estimator) SetDB(db *Storage)

SetDB set db

func (*Estimator) SetFont added in v1.1.0

func (ins *Estimator) SetFont(data *draw2d.FontData, size float64) error

SetFont set font

func (*Estimator) SetFontCache added in v1.1.0

func (ins *Estimator) SetFontCache(cache draw2d.FontCache) error

SetFontCache set font cache

func (*Estimator) SetFontPath added in v1.1.0

func (ins *Estimator) SetFontPath(cachePath string) error

SetFontPath set font cache with font cache path

func (*Estimator) SetFontSize added in v1.1.0

func (ins *Estimator) SetFontSize(size float64)

SetFontSize set font size

func (*Estimator) SetModel added in v1.1.0

func (ins *Estimator) SetModel(net *core.Net)

SetModel set face net model

func (*Estimator) Train added in v1.1.0

func (ins *Estimator) Train(split float64, iterations int, verbosity int)

Train for trainging classifier

func (*Estimator) TrainSafe added in v1.1.0

func (ins *Estimator) TrainSafe(split float64, iterations int, verbosity int)

TrainSafe for trainging classifier (multithread safe)

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option face instance option interface

func WithDB added in v1.1.0

func WithDB(dbPath string) Option

WithDB set db with dbpath

func WithFontPath

func WithFontPath(fontPath string) Option

WithFontPath set font with font path

func WithModel added in v1.1.0

func WithModel(modelPath string) Option

WithModel set net model with model path

type Storage added in v1.1.0

type Storage struct {
	// contains filtered or unexported fields
}

Storage represents db storage

func NewStorage added in v1.1.0

func NewStorage(people *core.People, classifier classifier.Classifier) *Storage

NewStorage returns new Storage

func (*Storage) Add added in v1.1.0

func (s *Storage) Add(items ...*core.Person)

Add add person to people

func (*Storage) BatchTrain added in v1.1.0

func (s *Storage) BatchTrain(split float64, iterations int, verbosity int, batch int)

BatchTrain for trainging classifier

func (*Storage) Delete added in v1.1.0

func (s *Storage) Delete(name string) bool

Delete delete a person by name

func (*Storage) Load added in v1.1.0

func (s *Storage) Load(fname string) error

Load load storage from file

func (*Storage) Match added in v1.1.0

func (s *Storage) Match(input []float32) (*core.Person, float64, error)

Match returns best match result

func (*Storage) People added in v1.1.0

func (s *Storage) People() *core.People

People returns people

func (*Storage) Predict added in v1.1.0

func (s *Storage) Predict(input []float32) ([]*core.Person, []float64, error)

Predict returns predictation results

func (*Storage) Save added in v1.1.0

func (s *Storage) Save(fname string) error

Save save storage to file

func (*Storage) SetClassifier added in v1.1.0

func (s *Storage) SetClassifier(c classifier.Classifier)

SetClassifier set classifier

func (*Storage) Train added in v1.1.0

func (s *Storage) Train(split float64, iterations int, verbosity int)

Train for trainging classifier

Directories

Path Synopsis
Package classifier include different classifiers
Package classifier include different classifiers
cmd
camera/server
Package server implement server
Package server implement server
camera/server/handlers
Package handlers include server handlers
Package handlers include server handlers
Package core facenet core module
Package core facenet core module
Package imageutil image utils func
Package imageutil image utils func

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL