facenet

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2021 License: Apache-2.0 Imports: 5 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 Preconverted model is in models/facenet path in this repo
  3. build the executable
# generated to ./bin/facenet
make facenet

Demo

demo screen capture

Install

go get -u github.com/bububa/facenet

Usage

Train faces
./bin/facenet -net=./models/facenet -people=./models/people/people.pb -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 -net=./models/facenet -people=./models/people/people.pb -update={labels for update seperated by comma} -output={fold path for output thumbs(optional)}
Delete distinct labels from people model
./bin/facenet -net=./models/facenet -people=./models/people/people.pb -delete={labels for delete seperated by comma} -output={fold path for output thumbs(optional)}
Detect faces for image
./bin/facenet -net=./models/facenet -people=./models/people/people.pb -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

User as lib

import (
    "log"

	"github.com/llgcode/draw2d"

    "github.com/bububa/facenet"
)

func main() {
    instance, err := facenet.New(
        facenet.WithNet("./models/facenet"),
        facenet.WithPeople("./models/people/people.pb"),
        facenet.WithFontPath("./font"),
    )
    if err != nil {
       log.Fatalln(err)
    }
	err = instance.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 := instance.DeletePerson(label); deleted {
                log.Printf("[INFO] person: %s deleted\n", label)
                continue
            }
            log.Printf("[WRN] person: %s not found\n", label)
        }
        err := instance.SaveModel(request.People)
        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 := instance.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

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Instance

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

Instance facenet Instance struct

func New

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

New init a new facenet instance

func (*Instance) AddPerson

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

AddPerson add person to people

func (*Instance) DeletePerson

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

DeletePerson delete a person by name

func (*Instance) DetectFaces

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

DetectFaces detect face markers from image

func (*Instance) DrawMarkers

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

DrawMarkers draw face markers on image

func (*Instance) ExtractFace

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

ExtractFace extract face for a person from image

func (*Instance) LoadPeople

func (ins *Instance) LoadPeople(filePath string) error

LoadPeople load people with people model filepath

func (*Instance) Match

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

Match match a person with embedding

func (*Instance) People

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

People get people

func (*Instance) Reload

func (ins *Instance) Reload()

Reload reload person model

func (*Instance) SaveModel

func (ins *Instance) SaveModel(modelPath string) error

SaveModel save people model to model file

func (*Instance) SetFont

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

SetFont set font

func (*Instance) SetFontCache

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

SetFontCache set font cache

func (*Instance) SetFontPath

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

SetFontPath set font cache with font cache path

func (*Instance) SetFontSize

func (ins *Instance) SetFontSize(size float64)

SetFontSize set font size

func (*Instance) SetNet

func (ins *Instance) SetNet(net *core.Net)

SetNet set face net model

func (*Instance) SetPeople

func (ins *Instance) SetPeople(people *core.People)

SetPeople set people model

type Option

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

Option face instance option interface

func WithFontPath

func WithFontPath(fontPath string) Option

WithFontPath set font with font path

func WithNet

func WithNet(modelPath string) Option

WithNet set net model with model path

func WithPeople

func WithPeople(modelPath string) Option

WithPeople set people model with model path

Directories

Path Synopsis
Package camera include camera modules
Package camera include camera modules
image
Package image utils.
Package image utils.
linux
Package linux implement linux device
Package linux implement linux device
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