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

53 lines
903 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-07-07 17:31:35 -07:00
c := &CameraDriver{
Driver: *gobot.NewDriver(
name,
"CameraDriver",
),
2014-05-22 20:53:15 -07:00
Source: source,
2014-04-27 16:58:34 -07:00
}
2014-07-07 17:31:35 -07:00
c.AddEvent("frame")
return c
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 {
2014-07-07 17:31:35 -07:00
gobot.Publish(c.Event("frame"), image)
2014-04-27 16:58:34 -07:00
}
}
}
}()
return true
}
func (c *CameraDriver) Halt() bool { return true }
func (c *CameraDriver) Init() bool { return true }