Add more opencv test coverage

This commit is contained in:
Adrian Zankich 2014-07-23 16:38:46 -07:00
parent eee92acd30
commit c081f46609
8 changed files with 26256 additions and 47 deletions

View File

@ -7,8 +7,9 @@ import (
type CameraDriver struct {
gobot.Driver
camera *cv.Capture
camera capture
Source interface{}
start func(*CameraDriver)
}
func NewCameraDriver(name string, source interface{}) *CameraDriver {
@ -18,6 +19,16 @@ func NewCameraDriver(name string, source interface{}) *CameraDriver {
"CameraDriver",
),
Source: source,
start: func(c *CameraDriver) {
switch v := c.Source.(type) {
case string:
c.camera = cv.NewFileCapture(v)
case int:
c.camera = cv.NewCameraCapture(v)
default:
panic("unknown camera source")
}
},
}
c.AddEvent("frame")
@ -26,27 +37,16 @@ func NewCameraDriver(name string, source interface{}) *CameraDriver {
}
func (c *CameraDriver) Start() bool {
switch v := c.Source.(type) {
case string:
c.camera = cv.NewFileCapture(v)
case int:
c.camera = cv.NewCameraCapture(v)
default:
panic("unknown camera source")
}
go func() {
for {
if c.camera.GrabFrame() {
image := c.camera.RetrieveFrame(1)
if image != nil {
gobot.Publish(c.Event("frame"), image)
}
c.start(c)
gobot.Every(c.Interval(), func() {
if c.camera.GrabFrame() {
image := c.camera.RetrieveFrame(1)
if image != nil {
gobot.Publish(c.Event("frame"), image)
}
}
}()
})
return true
}
func (c *CameraDriver) Halt() bool { return true }
func (c *CameraDriver) Init() bool { return true }

View File

@ -1,38 +1,52 @@
package opencv
import (
"github.com/hybridgroup/gobot"
"testing"
"time"
"github.com/hybridgroup/gobot"
)
func initTestCameraDriver() *CameraDriver {
return NewCameraDriver("bot", "")
d := NewCameraDriver("bot", "")
d.start = func(c *CameraDriver) {
d.camera = &testCapture{}
}
return d
}
func TestCameraDriverStart(t *testing.T) {
t.SkipNow()
sem := make(chan bool)
d := initTestCameraDriver()
gobot.Assert(t, d.Start(), true)
gobot.On(d.Event("frame"), func(data interface{}) {
sem <- true
})
select {
case <-sem:
case <-time.After(100 * time.Millisecond):
t.Errorf("Event \"frame\" was not published")
}
}
func TestCameraDriver(t *testing.T) {
d := NewCameraDriver("bot", "")
d.Start()
gobot.Refute(t, d.camera, nil)
func TestCameraDriverStartPanic(t *testing.T) {
recovered := false
defer func() {
if r := recover(); r != nil {
recovered = true
}
}()
NewCameraDriver("bot", false).Start()
gobot.Expect(t, recovered, true)
defer func() {
r := recover()
if r != nil {
gobot.Assert(t, "unknown camera source", r)
} else {
t.Errorf("Did not return Unknown camera error")
}
}()
d = NewCameraDriver("bot", true)
d.Start()
}
func TestCameraDriverHalt(t *testing.T) {
d := initTestCameraDriver()
gobot.Assert(t, d.Halt(), true)
}
func TestCameraDriverInit(t *testing.T) {
d := initTestCameraDriver()
gobot.Assert(t, d.Init(), true)
}

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.1 KiB

View File

@ -0,0 +1,18 @@
package opencv
import cv "github.com/hybridgroup/go-opencv/opencv"
type capture interface {
RetrieveFrame(int) *cv.IplImage
GrabFrame() bool
}
type testCapture struct{}
func (c *testCapture) RetrieveFrame(i int) *cv.IplImage {
return &cv.IplImage{}
}
func (c *testCapture) GrabFrame() bool {
return true
}

View File

@ -0,0 +1,18 @@
package opencv
import (
"path"
"runtime"
"testing"
cv "github.com/hybridgroup/go-opencv/opencv"
"github.com/hybridgroup/gobot"
)
func TestUtils(t *testing.T) {
_, currentfile, _, _ := runtime.Caller(0)
image := cv.LoadImage(path.Join(path.Dir(currentfile), "lena-256x256.jpg"))
rect := DetectFaces("haarcascade_frontalface_alt.xml", image)
gobot.Refute(t, len(rect), 0)
gobot.Refute(t, DrawRectangles(image, rect, 0, 0, 0, 0), nil)
}

View File

@ -1,11 +1,12 @@
package opencv
import (
"path"
"runtime"
"testing"
cv "github.com/hybridgroup/go-opencv/opencv"
"github.com/hybridgroup/gobot"
"testing"
"path"
"runtime"
)
func initTestWindowDriver() *WindowDriver {
@ -13,7 +14,6 @@ func initTestWindowDriver() *WindowDriver {
}
func TestWindowDriverStart(t *testing.T) {
t.SkipNow()
d := initTestWindowDriver()
gobot.Assert(t, d.Start(), true)
}
@ -29,11 +29,9 @@ func TestWindowDriverInit(t *testing.T) {
}
func TestWindowDriverShowImage(t *testing.T) {
t.SkipNow()
d := initTestWindowDriver()
_, currentfile, _, _ := runtime.Caller(0)
image := cv.LoadImage(path.Join(path.Dir(currentfile), "test.png"))
d.Start()
d.ShowImage(image)
_, currentfile, _, _ := runtime.Caller(0)
image := cv.LoadImage(path.Join(path.Dir(currentfile), "lena-256x256.jpg"))
d.Start()
d.ShowImage(image)
}