Documentation
¶
Index ¶
- Variables
- type Data
- type Face
- type ModelFiles
- type Recognizer
- func (_this *Recognizer) AddImageToDataset(Path string, Id string) error
- func (_this *Recognizer) Close()
- func (_this *Recognizer) DownloadModels(dir string) error
- func (_this *Recognizer) DrawFaces(Path string, F []Face) (image.Image, error)
- func (_this *Recognizer) DrawFaces2(Path string, F []goFace.Face) (image.Image, error)
- func (_this *Recognizer) DrawLandmarks(Path string, F []goFace.Face) (image.Image, error)
- func (_this *Recognizer) GrayScale(imgSrc image.Image) image.Image
- func (_this *Recognizer) Identify(Path string) ([]Face, error)
- func (_this *Recognizer) IdentifyMultiples(Path string) ([]Face, error)
- func (_this *Recognizer) Init(Path string) error
- func (_this *Recognizer) LoadDataset(Path string) error
- func (_this *Recognizer) LoadImage(Path string) (image.Image, error)
- func (_this *Recognizer) RecognizeMultiples(Path string) ([]goFace.Face, error)
- func (_this *Recognizer) RecognizeSingle(Path string) (goFace.Face, error)
- func (_this *Recognizer) SaveDataset(Path string) error
- func (_this *Recognizer) SaveImage(Path string, Img image.Image) error
- func (_this *Recognizer) SetSamples()
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoFace is returned when an image has no detected face, where an // operation requires at least one. ErrNoFace = errors.New("not a face on the image") // ErrNotSingleFace is returned when an image doesn't have exactly // one detected face, where an operation requires exactly one. ErrNotSingleFace = errors.New("not a single face on the image") // ErrNoMatch is returned by Identify when the descriptor doesn't // match any Dataset entry within Tolerance. ErrNoMatch = errors.New("can't identify") // ErrDatasetFileNotFound is returned by LoadDataset when Path // doesn't exist. ErrDatasetFileNotFound = errors.New("file not found") )
Sentinel errors for the "expected" failure conditions -- check for these with errors.Is instead of matching on the error message text, which isn't part of the API contract and may change.
Functions ¶
This section is empty.
Types ¶
type Data ¶
type Data struct {
Id string
Descriptor goFace.Descriptor
}
Data descriptor of the human face.
type Face ¶
type Face struct {
Data
Rectangle image.Rectangle
// Shapes holds the facial landmark points found by the shape predictor
// model (5 points with the default shape_predictor_5_face_landmarks.dat).
Shapes []image.Point
// Distance is the squared Euclidean distance between this face's
// descriptor and the matched Dataset entry's descriptor. Only set by
// Identify/IdentifyMultiples; zero otherwise.
Distance float64
// Confidence is a convenience score in [0,1], normalized as
// 1-Distance/Tolerance -- not a calibrated probability. Only set by
// Identify/IdentifyMultiples; zero otherwise.
Confidence float64
}
Face holds coordinates and descriptor of the human face.
type ModelFiles ¶ added in v1.0.6
type ModelFiles struct {
// Landmark is the shape predictor file name. Empty (the default)
// uses go-face's shape_predictor_5_face_landmarks.dat, which returns
// 5 landmark points (eye corners and nose base) in Face.Shapes. Set
// to "shape_predictor_68_face_landmarks.dat" for full facial contour
// landmarks (jawline, eyebrows, nose bridge, eyes, lips) instead --
// it's a much larger download and slightly slower per face, so only
// opt in if you need the extra landmark detail.
Landmark string
// Descriptor is the face-descriptor (ResNet) model file name. Empty
// uses go-face's default dlib_face_recognition_resnet_model_v1.dat.
Descriptor string
// CNN is the CNN face detector model file name, used when UseCNN is
// true. Empty uses go-face's default mmod_human_face_detector.dat.
CNN string
}
ModelFiles selects non-default model file names, all resolved relative to the modelDir passed to Init. Set fields before calling Init; they have no effect afterward, since model loading happens inside Init.
type Recognizer ¶
type Recognizer struct {
Tolerance float32
UseCNN bool
UseGray bool
// Model selects non-default model file names. Set before Init.
Model ModelFiles
// Dataset holds the known face samples. Mutate it only through
// AddImageToDataset (keeps the classifier in sync automatically) or
// LoadDataset (call SetSamples afterward -- see SetSamples).
Dataset []Data
// contains filtered or unexported fields
}
A Recognizer creates face descriptors for provided images and classifies them into categories.
func (*Recognizer) AddImageToDataset ¶
func (_this *Recognizer) AddImageToDataset(Path string, Id string) error
AddImageToDataset add a sample image to the dataset.
Returns ErrNoFace if the image has no detected face, or ErrNotSingleFace if it has more than one -- check with errors.Is.
The new entry is appended to the underlying classifier immediately (via goFace.AppendSample), so it's classifiable right away -- no need to call SetSamples afterward. SetSamples is still required after LoadDataset or after mutating Dataset directly, since those don't go through this incremental path.
func (*Recognizer) Close ¶
func (_this *Recognizer) Close()
Close frees resources taken by the Recognizer. Safe to call multiple times. Don't use Recognizer after close call.
func (*Recognizer) DownloadModels ¶ added in v1.1.0
func (_this *Recognizer) DownloadModels(dir string) error
DownloadModels downloads the model files Init requires by default (shape_predictor_5_face_landmarks.dat, dlib_face_recognition_resnet_model_v1.dat, mmod_human_face_detector.dat) from the dlib-models repository into dir, creating dir if it doesn't exist yet.
Any file that already exists in dir is left untouched and not re-downloaded, so it's safe to call this on every startup before Init.
func (*Recognizer) DrawFaces2 ¶
DrawFaces2 draws the faces in the original image
func (*Recognizer) DrawLandmarks ¶ added in v1.0.5
DrawLandmarks draws the facial landmark points found in the original image.
func (*Recognizer) GrayScale ¶
func (_this *Recognizer) GrayScale(imgSrc image.Image) image.Image
GrayScale Convert an image to grayscale
func (*Recognizer) Identify ¶ added in v1.1.0
func (_this *Recognizer) Identify(Path string) ([]Face, error)
Identify returns the single face identified in the image.
Returns ErrNotSingleFace if the image doesn't have exactly one face, or ErrNoMatch if the face doesn't match any Dataset entry within Tolerance -- check with errors.Is.
Matches against the sample set from the most recent SetSamples call, not necessarily the current Dataset -- see SetSamples.
func (*Recognizer) IdentifyMultiples ¶ added in v1.1.0
func (_this *Recognizer) IdentifyMultiples(Path string) ([]Face, error)
IdentifyMultiples returns every face identified in the image. Faces with no Dataset entry within Tolerance are skipped -- an empty slice (not an error) is returned if none matched.
Matches against the sample set from the most recent SetSamples call, not necessarily the current Dataset -- see SetSamples.
func (*Recognizer) Init ¶
func (_this *Recognizer) Init(Path string) error
Init initialise a recognizer interface. Set Model before calling Init to choose non-default model files (e.g. Model.Landmark for full facial contour landmarks instead of the default 5 points).
func (*Recognizer) LoadDataset ¶
func (_this *Recognizer) LoadDataset(Path string) error
LoadDataset loads the data from the json file into the Dataset.
Returns ErrDatasetFileNotFound if Path doesn't exist -- check with errors.Is.
Call SetSamples afterward: as with AddImageToDataset, Identify and IdentifyMultiples won't see the loaded entries until SetSamples runs again.
func (*Recognizer) LoadImage ¶
func (_this *Recognizer) LoadImage(Path string) (image.Image, error)
LoadImage Load an image from file
func (*Recognizer) RecognizeMultiples ¶
func (_this *Recognizer) RecognizeMultiples(Path string) ([]goFace.Face, error)
RecognizeMultiples returns all faces found on the provided image, sorted from left to right. Empty list is returned if there are no faces, error is returned if there was some error while decoding/processing image.
func (*Recognizer) RecognizeSingle ¶
func (_this *Recognizer) RecognizeSingle(Path string) (goFace.Face, error)
RecognizeSingle returns the face on the image, or ErrNotSingleFace if it doesn't have exactly one -- check with errors.Is.
func (*Recognizer) SaveDataset ¶
func (_this *Recognizer) SaveDataset(Path string) error
SaveDataset saves Dataset data to a json file
func (*Recognizer) SaveImage ¶
func (_this *Recognizer) SaveImage(Path string, Img image.Image) error
SaveImage Save an image to jpeg file
func (*Recognizer) SetSamples ¶
func (_this *Recognizer) SetSamples()
SetSamples rebuilds the classifier's sample set from the entire current Dataset.
AddImageToDataset already keeps the classifier in sync incrementally, so this is only needed after LoadDataset (bulk load) or after mutating Dataset directly -- those don't go through AddImageToDataset's incremental path, so the classifier would otherwise keep matching against a stale or empty sample set.


