-
Notifications
You must be signed in to change notification settings - Fork 0
/
detector.go
83 lines (72 loc) · 2.03 KB
/
detector.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
package main
import (
u "./utils"
c "./core"
tf "github.com/tensorflow/tensorflow/tensorflow/go"
"log"
"fmt"
_ "image/jpeg"
_ "image/png"
"flag"
)
var model u.Model
var labels u.Labels
func main() {
mode := flag.Int("m", 0, "Run time mode: 0 => local, 1 => api")
useCase := flag.String("u", "", "Use case to run")
imgDir := flag.String("img-dir", "", "Path of a JPG image to use for input")
azureVision := flag.String("az", "", "Use Azure Vision Model")
pThresh := flag.Float64("p", 0.6, "Probability threshold")
clean := flag.Bool("c", true, "Clean after reading: 0 => yes, 1 => no")
flag.Parse()
modelToRun := *useCase
availableUseCases := u.AvailableUseCases()
if !u.IsValidUseCase(availableUseCases, modelToRun) {
log.Fatal(
fmt.Sprintf("Invalid use case.\n\tAvailable use cases: %v",
availableUseCases))
}
model.Load(modelToRun)
labels.Load(modelToRun)
if len(*azureVision) > 0 {
c.AzureTfModel.Load(*azureVision)
c.AzureTfLabels.Load(*azureVision)
// Construct an in-memory graph from the serialized form.
c.AzureGraph.Graph = tf.NewGraph()
if err := c.AzureGraph.Graph.Import(c.AzureTfModel.Model, ""); err != nil {
log.Fatal(err)
}
// Create a session for inference over graph.
session, err := tf.NewSession(c.AzureGraph.Graph, nil)
c.AzureGraph.Session = session
if err != nil {
log.Fatal(err)
}
defer session.Close()
}
// Construct an in-memory graph from the serialized form.
c.TfGraph.Graph = tf.NewGraph()
if err := c.TfGraph.Graph.Import(model.Model, ""); err != nil {
log.Fatal(err)
}
// Create a session for inference over graph.
session, err := tf.NewSession(c.TfGraph.Graph, nil)
c.TfGraph.Session = session
if err != nil {
log.Fatal(err)
}
defer session.Close()
if *mode > 1 {
log.Fatal("Mode not available.\n\tAvailable modes: 0 => local, 1 => api")
} else {
if *mode == 1 {
log.Print("Running web api")
c.RunApi(":8000")
} else {
log.Print("Running bash api")
c.ProbabilityThreshold = *pThresh
c.CleaningDir = *clean
c.RunLocal(*imgDir)
}
}
}