2014-04-28 07:58:34 +08:00
|
|
|
package opencv
|
|
|
|
|
|
|
|
import (
|
2014-11-20 07:59:17 +08:00
|
|
|
"errors"
|
|
|
|
|
2014-11-29 10:05:19 +08:00
|
|
|
"time"
|
|
|
|
|
2014-04-28 07:58:34 +08:00
|
|
|
"github.com/hybridgroup/gobot"
|
2016-08-27 17:56:01 +08:00
|
|
|
cv "github.com/lazywei/go-opencv/opencv"
|
2014-04-28 07:58:34 +08:00
|
|
|
)
|
|
|
|
|
2014-12-23 06:24:06 +08:00
|
|
|
type capture interface {
|
|
|
|
RetrieveFrame(int) *cv.IplImage
|
|
|
|
GrabFrame() bool
|
|
|
|
}
|
|
|
|
|
2014-04-28 07:58:34 +08:00
|
|
|
type CameraDriver struct {
|
2014-11-29 10:05:19 +08:00
|
|
|
name string
|
|
|
|
camera capture
|
|
|
|
interval time.Duration
|
|
|
|
Source interface{}
|
|
|
|
start func(*CameraDriver) (err error)
|
|
|
|
gobot.Eventer
|
2014-04-28 07:58:34 +08:00
|
|
|
}
|
|
|
|
|
2014-10-21 00:23:33 +08:00
|
|
|
// NewCameraDriver creates a new driver with specified name and source.
|
|
|
|
// It also creates a start function to either set camera as a File or Camera capture.
|
2014-11-29 10:37:03 +08:00
|
|
|
func NewCameraDriver(name string, source interface{}, v ...time.Duration) *CameraDriver {
|
2014-07-08 08:31:35 +08:00
|
|
|
c := &CameraDriver{
|
2014-11-29 10:05:19 +08:00
|
|
|
name: name,
|
|
|
|
Eventer: gobot.NewEventer(),
|
|
|
|
Source: source,
|
|
|
|
interval: 10 * time.Millisecond,
|
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:
|
|
|
|
c.camera = cv.NewFileCapture(v)
|
|
|
|
case int:
|
|
|
|
c.camera = cv.NewCameraCapture(v)
|
|
|
|
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
|
|
|
|
2014-11-29 10:37:03 +08:00
|
|
|
if len(v) > 0 {
|
|
|
|
c.interval = v[0]
|
|
|
|
}
|
|
|
|
|
2014-07-08 08:31:35 +08:00
|
|
|
c.AddEvent("frame")
|
|
|
|
|
|
|
|
return c
|
2014-04-28 07:58:34 +08:00
|
|
|
}
|
|
|
|
|
2014-11-29 10:05:19 +08:00
|
|
|
func (c *CameraDriver) Name() string { return c.name }
|
|
|
|
func (c *CameraDriver) Connection() gobot.Connection { return nil }
|
|
|
|
|
2014-10-21 00:23:33 +08:00
|
|
|
// Start initializes camera by grabbing a frame
|
|
|
|
// every `interval` and publishing an frame event
|
2014-11-20 15:21:19 +08:00
|
|
|
func (c *CameraDriver) Start() (errs []error) {
|
|
|
|
if err := c.start(c); err != nil {
|
|
|
|
return []error{err}
|
2014-11-20 07:59:17 +08:00
|
|
|
}
|
2014-11-17 02:32:51 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
if c.camera.GrabFrame() {
|
|
|
|
image := c.camera.RetrieveFrame(1)
|
|
|
|
if image != nil {
|
2016-08-30 23:10:50 +08:00
|
|
|
c.Publish(c.Event("frame"), image)
|
2014-11-17 02:32:51 +08:00
|
|
|
}
|
2014-04-28 07:58:34 +08:00
|
|
|
}
|
2014-11-29 10:05:19 +08:00
|
|
|
<-time.After(c.interval)
|
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
|
2014-11-20 15:21:19 +08:00
|
|
|
func (c *CameraDriver) Halt() (errs []error) { return }
|