Example #1
0
// 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.
func NewCameraDriver(name string, source interface{}, v ...time.Duration) *CameraDriver {
	c := &CameraDriver{
		name:     name,
		Eventer:  gobot.NewEventer(),
		Source:   source,
		interval: 10 * time.Millisecond,
		start: func(c *CameraDriver) (err error) {
			switch v := c.Source.(type) {
			case string:
				c.camera = cv.NewFileCapture(v)
			case int:
				c.camera = cv.NewCameraCapture(v)
			default:
				return errors.New("Unknown camera source")
			}
			return
		},
	}

	if len(v) > 0 {
		c.interval = v[0]
	}

	c.AddEvent("frame")

	return c
}
Example #2
0
func main() {
	win := opencv.NewWindow("Go-OpenCV Webcam")
	defer win.Destroy()

	cap := opencv.NewCameraCapture(0)
	if cap == nil {
		panic("can not open camera")
	}
	defer cap.Release()

	win.CreateTrackbar("Thresh", 1, 100, func(pos int, param ...interface{}) {
		for {
			if cap.GrabFrame() {
				img := cap.RetrieveFrame(1)
				if img != nil {
					ProcessImage(img, win, pos)
				} else {
					fmt.Println("Image ins nil")
				}
			}

			if key := opencv.WaitKey(10); key == 27 {
				os.Exit(0)
			}
		}
	})
	opencv.WaitKey(0)
}
Example #3
0
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)
				}
			}
		}
	}()
	return true
}