/
main.go
77 lines (63 loc) · 2.05 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/google/uuid"
"github.com/irdaislakhuafa/go-identiface"
)
type user struct {
id string `json:"id,omitempty"`
name string `json:"name,omitempty"`
imagePath string `json:"image_name,omitempty"`
}
var (
assetsDir = "./assets"
modelsDir = filepath.Join(assetsDir, "models")
imagesDir = filepath.Join(assetsDir, "images")
)
// Run this code at root dir of project with `go run ./example/main.go`
func main() {
// Imagine this is data users from database
users := []user{
{id: uuid.NewString(), name: "Tony Stark", imagePath: filepath.Join(imagesDir, "tony-stark.jpeg")},
{id: uuid.NewString(), name: "Tzuyu", imagePath: filepath.Join(imagesDir, "tzuyu.jpg")},
}
getUserByID := map[string]user{}
for _, user := range users {
getUserByID[user.id] = user
}
// Initialize `identiface` [string] is data type of key for each image
iFace, err := identiface.Init[string](modelsDir)
if err != nil {
panic(err)
}
defer iFace.Close()
// Adding image datasets to `go-identiface`
for _, user := range users {
fileBytes, err := os.ReadFile(user.imagePath)
if err != nil {
panic(err)
}
if err := iFace.AddSingleDatasetFromBytes(user.id, fileBytes); err != nil {
panic(err)
}
}
// After adding datasets, don't forget to load them so they can be used by `go-identiface`. After this you can identify user by human face of image
iFace.LoadDatasets()
// Imagine this is an image file sent from client (ex. Mobile/Frontend/Etc)
clientImg := "tony-stark3.jpeg"
clienImgBytes, err := os.ReadFile(filepath.Join(imagesDir, clientImg))
if err != nil {
panic(err)
}
// After get image file from client then you can identify client by face of the image
data, err := iFace.ClassifySingleFromBytes(clienImgBytes)
if err != nil { // Will return error if face is not recognized or image contain multiple face
panic(err)
}
// Imagine this is method to get detail data user from db
user := getUserByID[data.ID]
// Now you can identify user by face
fmt.Printf("user: %#v\n", user)
}