/
filesource.go
55 lines (48 loc) · 1.1 KB
/
filesource.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
package videosource
import (
log "github.com/sirupsen/logrus"
"gocv.io/x/gocv"
)
// FileSource is a file source
type FileSource struct {
BaseVideo
filename string
gocvVideoCapture *gocv.VideoCapture
}
// NewFileSource creates a new FileSource
func NewFileSource(name string, filename string) VideoSource {
v := &FileSource{
BaseVideo: *NewBaseVideo(name),
filename: filename,
gocvVideoCapture: nil,
}
return v
}
// Initialize implements interface
func (f *FileSource) Initialize() (ok bool) {
gocvVideoCapture, err := gocv.VideoCaptureFile(f.filename)
if err != nil {
log.Warnf("Could not open video capture file: %s\n", f.filename)
return
}
f.gocvVideoCapture = gocvVideoCapture
ok = true
return
}
// Cleanup implements interface
func (f *FileSource) Cleanup() {
if f.gocvVideoCapture != nil {
f.gocvVideoCapture.Close()
}
}
// ReadImage implements interface
func (f *FileSource) ReadImage() (done bool, image Image) {
if f.gocvVideoCapture == nil {
done = true
return
}
mat := gocv.NewMat()
done = !f.gocvVideoCapture.Read(&mat)
image = *NewImage(mat)
return
}