-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.go
More file actions
140 lines (114 loc) · 2.92 KB
/
main.go
File metadata and controls
140 lines (114 loc) · 2.92 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"context"
_ "embed"
"flag"
"fmt"
"log"
"github.com/orsinium-labs/wypes"
"github.com/tetratelabs/wazero"
"gocv.io/x/gocv"
)
//go:embed modules/processor.wasm
var processorFrameWasm []byte
//go:embed modules/processrs.wasm
var processrsFrameWasm []byte
//go:embed modules/processc.wasm
var processcFrameWasm []byte
var (
processor = flag.String("processor", "tinygo", "which wasmCV processor to use (tinygo|rust|c)")
frame gocv.Mat
guestDataPtr uint32
)
func main() {
flag.Parse()
var module []byte
switch *processor {
case "tinygo":
module = processorFrameWasm
case "rust":
module = processrsFrameWasm
case "c":
module = processcFrameWasm
default:
log.Panicf("unsupported processor: %s", *processor)
}
ctx := context.Background()
r := wazero.NewRuntime(ctx)
defer r.Close(ctx)
println("Defining host functions...")
modules := wypes.Modules{
"hosted": wypes.Module{
"println": wypes.H1(hostPrintln),
},
"wasm:cv/mat": wypes.Module{
"[method]mat.cols": wypes.H1(matColsFunc),
"[method]mat.rows": wypes.H1(matRowsFunc),
"[method]mat.mattype": wypes.H1(matTypeFunc),
"[method]mat.size": wypes.H3(matSizeFunc),
},
}
err := modules.DefineWazero(r, nil)
if err != nil {
fmt.Printf("error define host functions: %v", err)
return
}
fmt.Printf("Loading %s wasmCV guest module...\n", *processor)
mod, err := r.InstantiateWithConfig(ctx, module, wazero.NewModuleConfig().WithName("processor").WithStartFunctions("_initialize", "_start"))
if err != nil {
log.Panicf("failed to instantiate module: %v", err)
}
process := mod.ExportedFunction("process")
// Open the webcam.
deviceID := "0"
webcam, err := gocv.OpenVideoCapture(deviceID)
if err != nil {
fmt.Printf("Error opening video capture device: %v\n", deviceID)
return
}
defer webcam.Close()
// streaming, capture from webcam
frame = gocv.NewMat()
defer frame.Close()
fmt.Printf("Start reading device id: %v\n", deviceID)
i := 0
for {
if ok := webcam.Read(&frame); !ok {
fmt.Printf("frame error %v\n", deviceID)
continue
}
if frame.Empty() {
continue
}
i++
fmt.Printf("Read frame %d\n", i+1)
_, err := process.Call(ctx, 1)
if err != nil {
fmt.Printf("Error calling process: %v\n", err)
}
}
}
func hostPrintln(msg wypes.String) wypes.Void {
println(msg.Unwrap())
return wypes.Void{}
}
func matColsFunc(matref wypes.UInt32) wypes.UInt32 {
return wypes.UInt32(frame.Cols())
}
func matRowsFunc(matref wypes.UInt32) wypes.UInt32 {
return wypes.UInt32(frame.Rows())
}
func matTypeFunc(matref wypes.UInt32) wypes.UInt32 {
return wypes.UInt32(frame.Type())
}
func matSizeFunc(s *wypes.Store, matref wypes.UInt32, list wypes.ReturnedList[wypes.UInt32]) wypes.Void {
dims := frame.Size()
result := make([]wypes.UInt32, len(dims))
for i, dim := range dims {
result[i] = wypes.UInt32(dim)
}
list.Raw = result
list.DataPtr = guestDataPtr
list.Lower(s)
return wypes.Void{}
}