-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.go
75 lines (58 loc) · 1.54 KB
/
app.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
package main
import (
"bytes"
"encoding/json"
"io"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
)
type Classification struct {
Class string
Score float32
}
type App struct {
Router *mux.Router
TFClient InceptionClient
}
func (a *App) Initialize(tfAddress string) {
a.Router = mux.NewRouter()
a.TFClient = InceptionClient{tfAddress}
a.InitializeRoutes()
}
func (a *App) Run(addr string) {
log.Fatal(http.ListenAndServe(addr, a.Router))
}
func (a *App) InitializeRoutes() {
a.Router.HandleFunc("/classify", a.classifyImage).Methods("POST")
}
func (a *App) classifyImage(w http.ResponseWriter, r *http.Request) {
var buf bytes.Buffer
file, _, err := r.FormFile("file")
if err != nil {
log.Fatalln(err)
return
}
defer file.Close()
// Copy the file data to my buffer
io.Copy(&buf, file)
resp, err := a.TFClient.Predict(buf.Bytes())
// scrape classification data
scores := resp.Outputs["scores"].GetFloatVal()
var classifications []Classification
for index, element := range resp.Outputs["classes"].GetStringVal() {
score := scores[index]
classifications = append(classifications, Classification{string(element), score})
}
log.Println(classifications)
response, _ := json.Marshal(classifications)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(response)
}
func main() {
a := App{}
a.Initialize(os.Getenv("TF_ADDRESS"))
a.Run(":8080")
}