forked from foxis/EasyRobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.device.gocv.go
84 lines (68 loc) · 1.89 KB
/
reader.device.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
package reader
import (
"time"
"github.com/itohio/EasyRobot/pkg/backend"
. "github.com/itohio/EasyRobot/pkg/core/logger"
"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"
)
const DEVICE_NAME = "rdev"
func init() {
pipeline.Register(DEVICE_NAME, NewDeviceGoCV)
}
type readerDeviceGoCV struct {
readerOpts
dev *gocv.VideoCapture
}
func NewDeviceGoCV(opts ...options.Option) (pipeline.Step, error) {
o := readerOpts{}
options.ApplyOptions(&o, opts...)
newOpts := opts
newOpts = append(newOpts, WithDeviceReaderGoCVResolution(o.id, o.width, o.height))
return steps.NewReader(newOpts...)
}
func WithDeviceReaderGoCVDefault(id int) options.Option {
return steps.WithSourceReader(&readerDeviceGoCV{readerOpts: readerOpts{id: id}})
}
func WithDeviceReaderGoCVResolution(id, width, height int) options.Option {
return steps.WithSourceReader(&readerDeviceGoCV{readerOpts: readerOpts{id: id, width: width, height: height}})
}
func (s *readerDeviceGoCV) Read(o steps.SourceOptions) (img interface{}, path string, index int, timestamp int64, err error) {
mat := backend.NewGoCVMat()
s.dev.Read(mat)
if !mat.Empty() {
img = mat
}
timestamp = time.Now().UnixNano()
index = s.index
s.index++
return
}
func (s *readerDeviceGoCV) Reset() {
s.index = 0
}
func (s *readerDeviceGoCV) Open() error {
Log.Debug().Int("device", s.id).Msg("Open")
dev, err := gocv.OpenVideoCapture(s.id)
if err != nil {
Log.Error().Err(err).Int("device", s.id)
return err
}
if s.width != 0 && s.height != 0 {
dev.Set(gocv.VideoCaptureFrameWidth, float64(s.width))
dev.Set(gocv.VideoCaptureFrameHeight, float64(s.height))
}
s.dev = dev
s.index = 0
return nil
}
func (s *readerDeviceGoCV) Close() {
if s.dev != nil {
s.dev.Close()
}
}
func (s *readerDeviceGoCV) Name() string {
return DEVICE_NAME
}