-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
94 lines (77 loc) · 1.68 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"fmt"
"io"
"os"
"strings"
"sync"
)
type Classifier interface {
Classify(file io.Reader) (map[string]float64, error)
}
func main() {
config, err := InitConfig()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
images, err := loadImages()
if err != nil {
fmt.Errorf("Error reading images: %s", err)
os.Exit(1)
}
rekog, err := NewRekognitionClient(config.AwsRegion, config.AwsAccessKeyID, config.AwsSecretAccessKey)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
vision, err := NewVisionClient()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var wg sync.WaitGroup
for _, img := range images {
wg.Add(1)
go func(img string, wg *sync.WaitGroup) {
report(img, vision, rekog)
wg.Done()
}(img, &wg)
}
wg.Wait()
}
func report(image string, vision, rekog Classifier) error {
fileName := strings.Replace(image, "images/", "", 1)
messages := make(chan string, 2)
go func(filePath, fileName string) {
file, err := os.Open(filePath)
if err != nil {
return
}
defer file.Close()
cat, _ := rekog.Classify(file)
messages <- fmt.Sprintf("%s -- REKOG: %v\n", fileName, formatResult(cat))
}(image, fileName)
go func(filePath, fileName string) {
file, err := os.Open(filePath)
if err != nil {
return
}
defer file.Close()
cat, _ := vision.Classify(file)
messages <- fmt.Sprintf("%s -- Vision: %v\n", fileName, formatResult(cat))
}(image, fileName)
report := ""
report += <-messages
report += <-messages
report += "------------------------"
fmt.Println(report)
return nil
}
func formatResult(r map[string]float64) string {
var res string
for k, v := range r {
res += fmt.Sprintf("%s: %.2f ", k, v)
}
return res
}