2021-05-24 15:54:22 +02:00
|
|
|
package face
|
|
|
|
|
|
|
|
import (
|
2021-07-16 14:34:05 +02:00
|
|
|
"encoding/json"
|
2021-05-24 15:54:22 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-08-11 13:21:05 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
|
2021-05-24 15:54:22 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fastwalk"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-07-16 14:34:05 +02:00
|
|
|
var modelPath, _ = filepath.Abs("../../assets/facenet")
|
|
|
|
|
2021-05-24 15:54:22 +02:00
|
|
|
func TestDetect(t *testing.T) {
|
|
|
|
expected := map[string]int{
|
|
|
|
"1.jpg": 1,
|
|
|
|
"2.jpg": 1,
|
|
|
|
"3.jpg": 1,
|
|
|
|
"4.jpg": 1,
|
2021-05-25 18:01:21 +02:00
|
|
|
"5.jpg": 1,
|
2021-05-24 15:54:22 +02:00
|
|
|
"6.jpg": 1,
|
|
|
|
"7.jpg": 0,
|
|
|
|
"8.jpg": 0,
|
|
|
|
"9.jpg": 0,
|
|
|
|
"10.jpg": 0,
|
|
|
|
"11.jpg": 0,
|
|
|
|
"12.jpg": 1,
|
|
|
|
"13.jpg": 0,
|
|
|
|
"14.jpg": 0,
|
|
|
|
"15.jpg": 0,
|
|
|
|
"16.jpg": 1,
|
|
|
|
"17.jpg": 1,
|
|
|
|
"18.jpg": 2,
|
2021-05-25 18:01:21 +02:00
|
|
|
"19.jpg": 0,
|
2021-05-24 15:54:22 +02:00
|
|
|
}
|
|
|
|
|
2021-07-16 14:34:05 +02:00
|
|
|
faceindices := map[string][]int{
|
|
|
|
"18.jpg": {0, 1},
|
|
|
|
"1.jpg": {2},
|
|
|
|
"4.jpg": {3},
|
|
|
|
"5.jpg": {4},
|
|
|
|
"6.jpg": {5},
|
|
|
|
"2.jpg": {6},
|
|
|
|
"12.jpg": {7},
|
|
|
|
"16.jpg": {8},
|
|
|
|
"17.jpg": {9},
|
|
|
|
"3.jpg": {10},
|
|
|
|
}
|
|
|
|
|
|
|
|
faceindexToPersonid := [11]int{
|
|
|
|
0, 1, 1, 1, 2, 0, 1, 0, 0, 1, 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
var embeddings [11][]float32
|
|
|
|
|
2021-08-11 13:21:05 +02:00
|
|
|
tfInstance := NewNet(modelPath, "testdata/cache", false)
|
2021-07-16 14:34:05 +02:00
|
|
|
|
|
|
|
if err := tfInstance.loadModel(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-05-24 15:54:22 +02:00
|
|
|
if err := fastwalk.Walk("testdata", func(fileName string, info os.FileMode) error {
|
2021-08-11 13:21:05 +02:00
|
|
|
if info.IsDir() || strings.HasPrefix(filepath.Base(fileName), ".") || strings.Contains(fileName, "cache") {
|
2021-05-24 15:54:22 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run(fileName, func(t *testing.T) {
|
2021-08-11 13:21:05 +02:00
|
|
|
fileHash := fs.Hash(fileName)
|
2021-05-24 15:54:22 +02:00
|
|
|
baseName := filepath.Base(fileName)
|
|
|
|
|
2021-05-25 18:01:21 +02:00
|
|
|
faces, err := Detect(fileName)
|
2021-05-24 15:54:22 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:34:05 +02:00
|
|
|
t.Logf("found %d faces in '%s'", len(faces), baseName)
|
2021-05-24 15:54:22 +02:00
|
|
|
|
2021-05-25 14:30:24 +02:00
|
|
|
if len(faces) > 0 {
|
|
|
|
t.Logf("results: %#v", faces)
|
|
|
|
|
|
|
|
for i, f := range faces {
|
2021-07-16 14:34:05 +02:00
|
|
|
t.Logf("marker[%d]: %#v %#v", i, f.Marker(), f.Face)
|
2021-05-25 14:30:24 +02:00
|
|
|
t.Logf("landmarks[%d]: %s", i, f.RelativeLandmarksJSON())
|
2021-07-16 14:34:05 +02:00
|
|
|
|
2021-08-11 13:21:05 +02:00
|
|
|
img, err := tfInstance.getFaceCrop(fileName, fileHash, f.Face)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
embedding := tfInstance.getEmbeddings(img)
|
2021-07-16 14:34:05 +02:00
|
|
|
|
|
|
|
if b, err := json.Marshal(embedding[0]); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
|
|
|
t.Logf("embedding: %#v", string(b))
|
|
|
|
}
|
|
|
|
|
2021-08-11 13:21:05 +02:00
|
|
|
t.Logf("faces: %d %v", i, faceindices[baseName])
|
2021-07-16 14:34:05 +02:00
|
|
|
embeddings[faceindices[baseName][i]] = embedding[0]
|
2021-05-24 17:57:44 +02:00
|
|
|
}
|
2021-05-24 15:54:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if i, ok := expected[baseName]; ok {
|
2021-05-25 18:01:21 +02:00
|
|
|
assert.Equal(t, i, len(faces))
|
2021-05-31 15:40:52 +02:00
|
|
|
assert.Equal(t, i, faces.Count())
|
|
|
|
if faces.Count() == 0 {
|
|
|
|
assert.Equal(t, 100, faces.Uncertainty())
|
|
|
|
} else {
|
|
|
|
assert.Truef(t, faces.Uncertainty() >= 0 && faces.Uncertainty() <= 50, "uncertainty should be between 0 and 50")
|
|
|
|
}
|
|
|
|
t.Logf("uncertainty: %d", faces.Uncertainty())
|
2021-05-24 15:54:22 +02:00
|
|
|
} else {
|
2021-05-31 15:40:52 +02:00
|
|
|
t.Logf("unknown test result for %s", baseName)
|
2021-05-24 15:54:22 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-07-16 14:34:05 +02:00
|
|
|
|
|
|
|
// Distance Matrix
|
|
|
|
correct := 0
|
|
|
|
|
|
|
|
for i := 0; i < len(embeddings); i++ {
|
|
|
|
for j := 0; j < len(embeddings); j++ {
|
|
|
|
if i >= j {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dist := EuclidianDistance(embeddings[i], embeddings[j])
|
|
|
|
t.Logf("Dist for %d %d (faces are %d %d) is %f", i, j, faceindexToPersonid[i], faceindexToPersonid[j], dist)
|
|
|
|
if faceindexToPersonid[i] == faceindexToPersonid[j] {
|
|
|
|
if dist < 1.21 {
|
|
|
|
correct += 1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if dist >= 1.21 {
|
|
|
|
correct += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Correct for %d", correct)
|
|
|
|
|
|
|
|
// there are a few incorrect results
|
|
|
|
// 4 out of 55 with the 1.21 threshold
|
|
|
|
assert.True(t, correct == 51)
|
2021-05-24 15:54:22 +02:00
|
|
|
}
|