go-face icon indicating copy to clipboard operation
go-face copied to clipboard

Error recognizing unclassified faces.

Open andre-ols opened this issue 3 months ago • 0 comments

I'm encountering an error when running the library's example code. In it, we import an image with 10 faces, then we import an image with another face and the system classifies it. The problem I'm encountering is precisely when I import an image of a face that isn't in the original photo, with 10 people. The classification system should return an index less than 0 according to the documentation, but this is not the behavior. It always returns a real index.

When using an image of Obama, the system output is Yuha

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.
	unknownImage := filepath.Join(imagesDir, "obama.jpg")
	unknownFace, err := rec.RecognizeSingleFile(unknownImage)
	if err != nil {
		log.Fatalf("Can't recognize: %v", err)
	}
	if unknownFace == nil {
		log.Fatalf("Not a single face on the image")
	}
	catID := rec.Classify(unknownFace.Descriptor)
	if catID < 0 {
		log.Fatalf("Can't classify")
	}
	
	fmt.Println(labels[catID])
}

andre-ols avatar Mar 09 '24 00:03 andre-ols