mirror of
https://github.com/Kagami/go-face.git
synced 2025-09-27 03:55:51 +08:00
71 lines
2.0 KiB
Go
71 lines
2.0 KiB
Go
package face_test
|
|
|
|
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"
|
|
|
|
// This example shows the basic usage of the package: create an
|
|
// recognizer, recognize faces, classify them using few known ones.
|
|
func Example_basic() {
|
|
// Init the recognizer.
|
|
rec, err := face.NewRecognizer(dataDir)
|
|
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(dataDir, "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(dataDir, "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])
|
|
}
|