forked from hybridgroup/gocv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideoio_test.go
201 lines (171 loc) · 5.12 KB
/
videoio_test.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package gocv
import (
"io/ioutil"
"math"
"path/filepath"
"strconv"
"strings"
"testing"
)
func TestVideoCaptureEmptyNumericalParameters(t *testing.T) {
_, err := VideoWriterFile(
"images/small.mp4", "MJPEG", 0, 0, 0, true)
if err == nil {
t.Error("Must fail due to an empty numerical parameters.")
}
if !strings.Contains(err.Error(), "one of the numerical parameters is equal to zero") {
t.Errorf("Must fail due to an empty numerical "+
"parameters, but have different error: %v", err)
}
}
func TestVideoCaptureCodecString(t *testing.T) {
vc, err := OpenVideoCapture("images/small.mp4")
if err != nil {
t.Errorf("TestVideoCaptureCodecString: error loading a file: %v", err)
}
if vc.CodecString() == "" {
t.Fatal("TestVideoCaptureCodecString: empty codec string")
}
}
func TestVideoCaptureCodecConversion(t *testing.T) {
vc, err := OpenVideoCapture("images/small.mp4")
if err != nil {
t.Errorf("TestVideoCaptureCodecConversion: error loading a file: %v", err)
}
if vc.CodecString() == "" {
t.Fatal("TestVideoCaptureCodecConversion: empty codec string")
}
if int64(vc.ToCodec(vc.CodecString())) != int64(vc.Get(VideoCaptureFOURCC)) {
t.Fatal("TestVideoCaptureCodecConversion: codec conversion failed")
}
}
func TestVideoCaptureCodecConversionBadInput(t *testing.T) {
vc, err := OpenVideoCapture("images/small.mp4")
if err != nil {
t.Errorf("TestVideoCaptureCodecString: error loading a file: %v", err)
}
codec := vc.ToCodec("BAD CODEC")
if int64(codec) != -1 {
t.Fatal("TestVideoCaptureCodecConversionBadInput: input validation failed")
}
}
func TestVideoCaptureInvalid(t *testing.T) {
_, err := OpenVideoCapture(1.1)
if err == nil {
t.Errorf("Should return error with invalid param")
}
}
func TestVideoCaptureWithAPI(t *testing.T) {
t.Run("video capture file with api", func(t *testing.T) {
vc, err := OpenVideoCaptureWithAPI("images/small.mp4", VideoCaptureAny)
if err != nil {
t.Errorf("error loading a file: %v", err)
}
backend := vc.Get(VideoCaptureBackend)
if backend == float64(VideoCaptureAny) {
t.Errorf("video capture backend api did not select a backend")
}
})
t.Run("video capture unknown device with api", func(t *testing.T) {
_, err := OpenVideoCaptureWithAPI(math.MaxInt32, VideoCaptureAny)
if err == nil {
t.Errorf("should return error opening device")
}
})
t.Run("video capture invalid with api", func(t *testing.T) {
_, err := OpenVideoCaptureWithAPI(1.1, VideoCaptureAny)
if err == nil {
t.Errorf("should return error with invalid param")
}
})
t.Run("video capture valid int string with api", func(t *testing.T) {
vc5, err := OpenVideoCaptureWithAPI("1", VideoCaptureAny)
defer vc5.Close()
if err == nil {
t.Errorf("should return error opening device")
}
})
}
func TestVideoCaptureFile(t *testing.T) {
vc, err := VideoCaptureFile("images/small.mp4")
defer vc.Close()
if err != nil {
t.Errorf("%s", err)
}
if !vc.IsOpened() {
t.Error("Unable to open VideoCaptureFile")
}
if fw := vc.Get(VideoCaptureFrameWidth); int(fw) != 560 {
t.Errorf("Expected frame width property of 560.0 got %f", fw)
}
if fh := vc.Get(VideoCaptureFrameHeight); int(fh) != 320 {
t.Errorf("Expected frame height property of 320.0 got %f", fh)
}
vc.Set(VideoCaptureBrightness, 100.0)
vc.Grab(10)
img := NewMat()
defer img.Close()
vc.Read(&img)
if img.Empty() {
t.Error("Unable to read VideoCaptureFile")
}
// video capture file with non-existent video
vc2, err := VideoCaptureFile("nonexistent.mp4")
defer vc2.Close()
if err == nil {
t.Errorf("Expected error when opening invalid file")
}
t.Run(" video capture file with api", func(t *testing.T) {
vc3, err := VideoCaptureFileWithAPI("images/small.mp4", VideoCaptureAny)
defer vc3.Close()
if err != nil {
t.Error(err)
}
})
t.Run("video capture non-existent video with api", func(t *testing.T) {
vc4, err := VideoCaptureFileWithAPI("nonexistent.mp4", VideoCaptureAny)
defer vc4.Close()
if err == nil {
t.Errorf("Expected error when opening invalid file")
}
})
t.Run("video capture invalid int", func(t *testing.T) {
vc5, err := OpenVideoCapture(math.MaxInt32)
defer vc5.Close()
if err == nil {
t.Errorf("should return error opening device")
}
})
t.Run("video capture invalid string", func(t *testing.T) {
vc5, err := OpenVideoCapture("test-device")
defer vc5.Close()
if err == nil {
t.Errorf("should return error opening device")
}
})
t.Run("video capture valid string", func(t *testing.T) {
vc5, err := OpenVideoCapture(strconv.Itoa(math.MaxInt32))
defer vc5.Close()
if err == nil {
t.Errorf("should return error opening device")
}
})
}
func TestVideoWriterFile(t *testing.T) {
dir, _ := ioutil.TempDir("", "gocvtests")
tmpfn := filepath.Join(dir, "test.avi")
img := IMRead("images/face-detect.jpg", IMReadColor)
if img.Empty() {
t.Error("Invalid read of Mat in VideoWriterFile test")
}
defer img.Close()
vw, _ := VideoWriterFile(tmpfn, "MJPG", 25, img.Cols(), img.Rows(), true)
defer vw.Close()
if !vw.IsOpened() {
t.Error("Unable to open VideoWriterFile")
}
err := vw.Write(img)
if err != nil {
t.Error("Invalid Write() in VideoWriter")
}
}