/
opencv.go
46 lines (37 loc) · 961 Bytes
/
opencv.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
package opencvl
import (
"image"
"gocv.io/x/gocv"
)
type openCVLayer struct {
process func(mat gocv.Mat, args ...interface{}) gocv.Mat
args []interface{}
}
func (l *openCVLayer) Type() string {
return "opencv"
}
func (l *openCVLayer) execute(img *image.RGBA, time int) (*image.RGBA, error) {
mat, err := gocv.ImageToMatRGB(img)
if err != nil {
return nil, err
}
mat = l.process(mat, append([]interface{}{time}, l.args...)...)
out, err := mat.ToImage()
if err != nil {
return nil, err
}
return out.(*image.RGBA), nil
}
func (l *openCVLayer) setArgs(args []interface{}) error {
l.args = args
return nil
}
func (l *openCVLayer) build() error { return nil }
func (l *openCVLayer) cleanup() {}
// NewOpenCVLayer creates a new opencv layer, given the process function
func NewOpenCVLayer(process func(mat gocv.Mat, args ...interface{}) gocv.Mat) Layer {
return &openCVLayer{
process: process,
args: make([]interface{}, 0),
}
}