forked from foxis/EasyRobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.gocv.go
137 lines (113 loc) · 2.58 KB
/
reader.gocv.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
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
package reader
import (
"encoding/binary"
"os"
"strings"
"time"
"github.com/itohio/EasyRobot/pkg/backend"
"github.com/itohio/EasyRobot/pkg/core/options"
"github.com/itohio/EasyRobot/pkg/core/pipeline"
"github.com/itohio/EasyRobot/pkg/core/pipeline/steps"
"gocv.io/x/gocv"
)
type readerGoCV readerOpts
const IMAGE_GOCV_NAME = "rmat"
func init() {
pipeline.Register(IMAGE_GOCV_NAME, NewGoCV)
}
func NewGoCV(opts ...options.Option) (pipeline.Step, error) {
o := readerOpts{}
options.ApplyOptions(&o, opts...)
newOpts := opts
newOpts = append(newOpts, WithDefaultReader(o.paths))
return steps.NewReader(newOpts...)
}
func WithReaderGoCV(paths []string) options.Option {
return steps.WithSourceReader(&readerGoCV{paths: paths})
}
func (s *readerGoCV) Read(o steps.SourceOptions) (img interface{}, path string, index int, timestamp int64, err error) {
if s.index >= len(s.paths) {
if o.Repeat {
s.index = 0
} else {
err = pipeline.ErrEOS
return
}
}
path = s.paths[s.index]
mat := s.readMat(path)
timestamp = time.Now().UnixNano()
if !mat.Empty() {
img = backend.FromGoCVMat(mat)
}
index = s.index
s.index++
return
}
func (s *readerGoCV) readMat(path string) gocv.Mat {
if strings.HasSuffix(path, ".png") ||
strings.HasSuffix(path, ".bmp") ||
strings.HasSuffix(path, ".jpg") ||
strings.HasSuffix(path, ".jpeg") {
return gocv.IMRead(path, gocv.IMReadColor)
}
fp, err := os.Open(path)
if err != nil {
return gocv.Mat{}
}
defer fp.Close()
var (
magic uint64
lenSizes uint8
sizes []int
matType gocv.MatType
lenBytes uint64
bytes []byte
)
err = binary.Read(fp, binary.LittleEndian, &magic)
if err != nil {
return gocv.Mat{}
}
if magic != 0xabcdef0012345678 {
return gocv.Mat{}
}
err = binary.Read(fp, binary.LittleEndian, &lenSizes)
if err != nil {
return gocv.Mat{}
}
sizes = make([]int, lenSizes)
err = binary.Read(fp, binary.LittleEndian, sizes)
if err != nil {
return gocv.Mat{}
}
err = binary.Read(fp, binary.LittleEndian, &matType)
if err != nil {
return gocv.Mat{}
}
err = binary.Read(fp, binary.LittleEndian, &lenBytes)
if err != nil {
return gocv.Mat{}
}
bytes = make([]byte, lenBytes)
err = binary.Read(fp, binary.LittleEndian, bytes)
if err != nil {
return gocv.Mat{}
}
mat, err := gocv.NewMatWithSizesFromBytes(sizes, matType, bytes)
if err != nil {
return gocv.Mat{}
}
return mat
}
func (s *readerGoCV) Reset() {
s.index = 0
}
func (s *readerGoCV) Open() error {
s.index = 0
return nil
}
func (s *readerGoCV) Close() {
}
func (s *readerGoCV) Name() string {
return IMAGE_GOCV_NAME
}