2014-04-28 07:58:34 +08:00
|
|
|
package opencv
|
|
|
|
|
|
|
|
import (
|
2014-11-20 07:59:17 +08:00
|
|
|
"errors"
|
|
|
|
|
2017-03-28 02:10:37 +08:00
|
|
|
"gobot.io/x/gobot"
|
2017-10-19 00:13:40 +08:00
|
|
|
"gocv.io/x/gocv"
|
2014-04-28 07:58:34 +08:00
|
|
|
)
|
|
|
|
|
2014-12-23 06:24:06 +08:00
|
|
|
type capture interface {
|
2018-04-24 18:56:26 +08:00
|
|
|
Read(img *gocv.Mat) bool
|
2014-12-23 06:24:06 +08:00
|
|
|
}
|
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
const (
|
|
|
|
// Frame event
|
|
|
|
Frame = "frame"
|
|
|
|
)
|
|
|
|
|
2016-12-01 21:58:58 +08:00
|
|
|
// CameraDriver is the Gobot Driver for the OpenCV camera
|
2014-04-28 07:58:34 +08:00
|
|
|
type CameraDriver struct {
|
2017-10-05 23:20:42 +08:00
|
|
|
name string
|
|
|
|
camera capture
|
|
|
|
Source interface{}
|
|
|
|
start func(*CameraDriver) (err error)
|
2014-11-29 10:05:19 +08:00
|
|
|
gobot.Eventer
|
2014-04-28 07:58:34 +08:00
|
|
|
}
|
|
|
|
|
2016-10-01 23:53:39 +08:00
|
|
|
// NewCameraDriver creates a new driver with specified source.
|
2014-10-21 00:23:33 +08:00
|
|
|
// It also creates a start function to either set camera as a File or Camera capture.
|
2017-10-05 23:20:42 +08:00
|
|
|
func NewCameraDriver(source interface{}) *CameraDriver {
|
2014-07-08 08:31:35 +08:00
|
|
|
c := &CameraDriver{
|
2017-10-05 23:20:42 +08:00
|
|
|
name: "Camera",
|
|
|
|
Eventer: gobot.NewEventer(),
|
|
|
|
Source: source,
|
2014-11-20 07:59:17 +08:00
|
|
|
start: func(c *CameraDriver) (err error) {
|
2014-07-24 07:38:46 +08:00
|
|
|
switch v := c.Source.(type) {
|
|
|
|
case string:
|
2017-10-05 23:05:10 +08:00
|
|
|
c.camera, _ = gocv.VideoCaptureFile(v)
|
2014-07-24 07:38:46 +08:00
|
|
|
case int:
|
2017-10-05 23:05:10 +08:00
|
|
|
c.camera, _ = gocv.VideoCaptureDevice(v)
|
2014-07-24 07:38:46 +08:00
|
|
|
default:
|
2014-11-20 07:59:17 +08:00
|
|
|
return errors.New("Unknown camera source")
|
2014-07-24 07:38:46 +08:00
|
|
|
}
|
2014-11-20 07:59:17 +08:00
|
|
|
return
|
2014-07-24 07:38:46 +08:00
|
|
|
},
|
2014-04-28 07:58:34 +08:00
|
|
|
}
|
2014-07-08 08:31:35 +08:00
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
c.AddEvent(Frame)
|
2014-07-08 08:31:35 +08:00
|
|
|
|
|
|
|
return c
|
2014-04-28 07:58:34 +08:00
|
|
|
}
|
|
|
|
|
2016-12-01 21:58:58 +08:00
|
|
|
// Name returns the Driver name
|
|
|
|
func (c *CameraDriver) Name() string { return c.name }
|
|
|
|
|
|
|
|
// SetName sets the Driver name
|
|
|
|
func (c *CameraDriver) SetName(n string) { c.name = n }
|
|
|
|
|
|
|
|
// Connection returns the Driver's connection
|
2014-11-29 10:05:19 +08:00
|
|
|
func (c *CameraDriver) Connection() gobot.Connection { return nil }
|
|
|
|
|
2017-10-05 23:20:42 +08:00
|
|
|
// Start initializes camera by grabbing frames
|
2016-11-07 21:55:21 +08:00
|
|
|
func (c *CameraDriver) Start() (err error) {
|
2014-11-20 15:21:19 +08:00
|
|
|
if err := c.start(c); err != nil {
|
2016-11-07 21:55:21 +08:00
|
|
|
return err
|
2014-11-20 07:59:17 +08:00
|
|
|
}
|
2017-10-05 23:05:10 +08:00
|
|
|
img := gocv.NewMat()
|
2014-11-17 02:32:51 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
2018-04-24 18:56:26 +08:00
|
|
|
if ok := c.camera.Read(&img); ok {
|
2017-10-05 23:05:10 +08:00
|
|
|
c.Publish(Frame, img)
|
2014-04-28 07:58:34 +08:00
|
|
|
}
|
|
|
|
}
|
2014-11-17 02:32:51 +08:00
|
|
|
}()
|
2014-11-21 09:54:48 +08:00
|
|
|
return
|
2014-04-28 07:58:34 +08:00
|
|
|
}
|
|
|
|
|
2014-10-21 00:23:33 +08:00
|
|
|
// Halt stops camera driver
|
2016-11-07 21:55:21 +08:00
|
|
|
func (c *CameraDriver) Halt() (err error) { return }
|