1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00

51 lines
929 B
Go
Raw Normal View History

2014-04-27 16:58:34 -07:00
package opencv
import (
cv "github.com/hybridgroup/go-opencv/opencv"
"github.com/hybridgroup/gobot"
)
type CameraDriver struct {
gobot.Driver
camera *cv.Capture
2014-05-22 20:53:15 -07:00
Source interface{}
2014-04-27 16:58:34 -07:00
}
2014-05-22 20:53:15 -07:00
func NewCameraDriver(name string, source interface{}) *CameraDriver {
2014-04-27 16:58:34 -07:00
return &CameraDriver{
Driver: gobot.Driver{
Name: name,
2014-06-11 11:37:20 -07:00
Events: map[string]*gobot.Event{
"Frame": gobot.NewEvent(),
2014-04-27 16:58:34 -07:00
},
},
2014-05-22 20:53:15 -07:00
Source: source,
2014-04-27 16:58:34 -07:00
}
}
func (c *CameraDriver) Start() bool {
2014-05-22 20:53:15 -07:00
switch v := c.Source.(type) {
case string:
c.camera = cv.NewFileCapture(v)
case int:
c.camera = cv.NewCameraCapture(v)
default:
panic("unknown camera source")
2014-04-27 16:58:34 -07:00
}
2014-05-22 20:53:15 -07:00
2014-04-27 16:58:34 -07:00
go func() {
for {
if c.camera.GrabFrame() {
image := c.camera.RetrieveFrame(1)
if image != nil {
gobot.Publish(c.Events["Frame"], image)
}
}
}
}()
return true
}
func (c *CameraDriver) Halt() bool { return true }
func (c *CameraDriver) Init() bool { return true }