@@ -4,9 +4,10 @@ package main
4
4
5
5
import (
6
6
"bytes"
7
+ "errors"
7
8
"image"
8
- "log"
9
9
"math"
10
+ "runtime"
10
11
"syscall/js"
11
12
"time"
12
13
@@ -25,19 +26,20 @@ var (
25
26
model * onnx.Model
26
27
)
27
28
28
- func main ( ) {
29
+ func logInfo ( s interface {} ) {
29
30
js .Global ().Get ("document" ).
30
31
Call ("getElementById" , "info" ).
31
- Set ("innerHTML" , "Starting the WASM program" )
32
+ Set ("innerHTML" , s )
33
+ }
34
+
35
+ func loadFile () error {
32
36
graph = gorgonnx .NewGraph ()
33
37
model = onnx .NewModel (graph )
34
38
files := js .Global ().Get ("document" ).Call ("getElementById" , "knowledgeFile" ).Get ("files" )
35
39
//fmt.Println("file", files)
36
40
//fmt.Println("Length", files.Length())
37
41
if files .Length () == 1 {
38
- js .Global ().Get ("document" ).
39
- Call ("getElementById" , "info" ).
40
- Set ("innerHTML" , "Reading the model from the memory of the browser" )
42
+ logInfo ("Reading the model from the memory of the browser" )
41
43
//fmt.Println("Reading from uploaded file")
42
44
reader := js .Global ().Get ("FileReader" ).New ()
43
45
reader .Call ("readAsDataURL" , files .Index (0 ))
@@ -48,61 +50,48 @@ func main() {
48
50
content := reader .Get ("result" ).String ()
49
51
dataURL , err := dataurl .DecodeString (content )
50
52
if err != nil {
51
- js .Global ().Get ("document" ).
52
- Call ("getElementById" , "info" ).
53
- Set ("innerHTML" , err )
54
- return
53
+ logInfo (err .Error ())
54
+ return err
55
55
}
56
56
err = model .Unmarshal (dataURL .Data )
57
57
if err != nil {
58
- js .Global ().Get ("document" ).
59
- Call ("getElementById" , "info" ).
60
- Set ("innerHTML" , "Fatal... cannot decode model, please reload the page" )
58
+ logInfo ("Fatal... cannot decode model, please reload the page" )
59
+ return err
61
60
}
62
-
63
61
// modelonnx = dataURL.Data
64
62
}
65
- js .Global ().Get ("document" ).
66
- Call ("getElementById" , "info" ).
67
- Set ("innerHTML" , "Ready to process!" )
63
+ return nil
64
+ }
65
+
66
+ func main () {
67
+ logInfo ("Starting the WASM program" )
68
+ err := loadFile ()
69
+ if err != nil {
70
+ logInfo (err .Error ())
71
+ return
72
+ }
73
+ runtime .GC ()
74
+ logInfo ("Ready to process!" )
68
75
// Declare callback
69
76
cb := js .FuncOf (func (this js.Value , args []js.Value ) interface {} {
70
77
// handle event
71
78
// Get the picture
72
- js .Global ().Get ("document" ).
73
- Call ("getElementById" , "info" ).
74
- Set ("innerHTML" , "processing element" )
75
- pic := js .Global ().Get ("document" ).Call ("getElementById" , "canvasBox" ).Call ("toDataURL" )
76
- dataURL , err := dataurl .DecodeString (pic .String ())
79
+ logInfo ("processing element" )
80
+ t , err := processImgToArray ()
81
+ runtime .GC ()
77
82
if err != nil {
78
- js .Global ().Get ("document" ).
79
- Call ("getElementById" , "info" ).
80
- Set ("innerHTML" , "error" )
83
+ logInfo (err .Error ())
81
84
return nil
85
+
82
86
}
83
- var output int
84
- if dataURL .ContentType () == "image/png" {
85
- m , _ , err := image .Decode (bytes .NewReader (dataURL .Data ))
86
- if err != nil {
87
- js .Global ().Get ("document" ).
88
- Call ("getElementById" , "info" ).
89
- Set ("innerHTML" , "error" )
90
- return nil
91
- }
92
- output , err = analyze (m )
93
- if err != nil {
94
- /*js.Global().Get("document").
95
- Call("getElementById", "info").
96
- Set("innerHTML", "error")
97
- */
98
- return nil
99
- }
87
+ output , err := analyze (t )
88
+ runtime .GC ()
89
+ if err != nil {
90
+ logInfo (err .Error ())
91
+ return nil
100
92
}
101
93
102
- log .Println (output )
103
- js .Global ().Get ("document" ).
104
- Call ("getElementById" , "info" ).
105
- Set ("innerHTML" , output )
94
+ logInfo (output )
106
95
return nil
107
96
})
108
97
// Hook it up with a DOM event
@@ -113,13 +102,29 @@ func main() {
113
102
<- c
114
103
}
115
104
116
- func analyze (m image.Image ) (int , error ) {
117
- // resize the image
105
+ func processImgToArray () ([]float32 , error ) {
106
+ pic := js .Global ().Get ("document" ).Call ("getElementById" , "canvasBox" ).Call ("toDataURL" )
107
+ dataURL , err := dataurl .DecodeString (pic .String ())
108
+ if err != nil {
109
+ return nil , err
110
+ }
111
+ if dataURL .ContentType () != "image/png" {
112
+ return nil , errors .New ("not a png image" )
113
+ }
114
+ m , _ , err := image .Decode (bytes .NewReader (dataURL .Data ))
115
+ if err != nil {
116
+ return nil , err
117
+ }
118
+
118
119
img := imaging .Resize (m , size , 0 , imaging .Lanczos )
119
120
t := make ([]float32 , size * size )
120
121
for i := 0 ; i < size * size * 4 ; i += 4 {
121
122
t [i / 4 ] = float32 (img .Pix [i ])
122
123
}
124
+ return t , nil
125
+ }
126
+
127
+ func analyze (t []float32 ) (int , error ) {
123
128
T := tensor .New (tensor .WithBacking (t ), tensor .WithShape (1 , 1 , size , size ))
124
129
err := gorgonnx .Let (graph .Node (model .Input [0 ]).(node.Node ), T )
125
130
if err != nil {
@@ -141,7 +146,5 @@ func analyze(m image.Image) (int, error) {
141
146
val = v
142
147
}
143
148
}
144
- log .Println ("RES" , res )
145
-
146
149
return res , nil
147
150
}
0 commit comments