Пример #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
}
Пример #2
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
}
Пример #3
0
func main() {
	filename := "../data/???.avi"
	if len(os.Args) == 2 {
		filename = os.Args[1]
	} else {
		fmt.Printf("Usage: go run player.go videoname\n")
		os.Exit(0)
	}

	cap := opencv.NewFileCapture(filename)
	if cap == nil {
		panic("can not open video")
	}
	defer cap.Release()

	win := opencv.NewWindow("GoOpenCV: VideoPlayer")
	defer win.Destroy()

	fps := int(cap.GetProperty(opencv.CV_CAP_PROP_FPS))
	frames := int(cap.GetProperty(opencv.CV_CAP_PROP_FRAME_COUNT))
	stop := false

	win.SetMouseCallback(func(event, x, y, flags int) {
		if flags&opencv.CV_EVENT_LBUTTONDOWN != 0 {
			stop = !stop
			if stop {
				fmt.Printf("status: stop")
			} else {
				fmt.Printf("status: palying")
			}
		}
	})
	win.CreateTrackbar("Seek", 1, frames, func(pos int) {
		cur_pos := int(cap.GetProperty(opencv.CV_CAP_PROP_POS_FRAMES))

		if pos != cur_pos {
			cap.SetProperty(opencv.CV_CAP_PROP_POS_FRAMES, float64(pos))
			fmt.Printf("Seek to %d(%d)\n", pos, frames)
		}
	})

	for {
		if !stop {
			img := cap.QueryFrame()
			if img == nil {
				break
			}

			frame_pos := int(cap.GetProperty(opencv.CV_CAP_PROP_POS_FRAMES))
			if frame_pos >= frames {
				break
			}
			win.SetTrackbarPos("Seek", frame_pos)

			win.ShowImage(img)
			key := opencv.WaitKey(1000 / fps)
			if key == 27 {
				os.Exit(0)
			}
		} else {
			key := opencv.WaitKey(20)
			if key == 27 {
				os.Exit(0)
			}
		}
	}

	opencv.WaitKey(0)
}