예제 #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 TrackEyes(srcFile, destFile string) {
	var video = opencv.NewFileCapture(srcFile)

	var fourcc = opencv.FOURCC('m', 'p', '4', 'v')
	var fps = float32(video.GetProperty(opencv.CV_CAP_PROP_FPS))
	var size = opencv.Size{int(video.GetProperty(opencv.CV_CAP_PROP_FRAME_WIDTH)),
		int(video.GetProperty(opencv.CV_CAP_PROP_FRAME_HEIGHT))}

	if video == nil {
		panic("no file found with name: " + srcFile)
	}
	defer video.Release()

	win := opencv.NewWindow("Go-OpenCV Webcam")
	defer win.Destroy()

	_, currentfile, _, _ := runtime.Caller(0)
	var cascade = path.Join(path.Dir(currentfile), "/../cascades/haarcascade_eye_tree_eyeglasses.xml")
	var eyeMarker = NewEyeMarker(cascade)

	var imgs = []*opencv.IplImage{}
	var imgs2 = []*opencv.IplImage{}

	for {
		if video.GrabFrame() {
			img := video.RetrieveFrame(1)
			if img != nil {

				var copy = opencv.CreateImage(img.Width(), img.Height(), img.Depth(), img.Channels())
				opencv.Copy(img.Clone(), copy, nil)

				var markedImg = eyeMarker.MarkEyes(copy)

				imgs = append(imgs, markedImg)
				//win.ShowImage(imgs[len(imgs)-1])
			} else {
				fmt.Println("Image ins nil")
			}
		} else {
			break
		}
		key := opencv.WaitKey(10)

		if key == 27 {
			os.Exit(0)
		}
	}

	for i := 0; i < len(imgs2); i++ {
		win.ShowImage(imgs2[i])
	}
	log.Print("before write")
	NewVideoWriter(uint32(fourcc), fps, size).SaveAsVideo(imgs, "test.avi")
	os.Exit(0)
}
예제 #3
0
func NewVideoCapture(videoFilePath string) VideoCapture {
	var camMode = false
	if videoFilePath == "" {
		camMode = true
	}
	var cap *opencv.Capture
	if videoFilePath == "" {
		cap = opencv.NewCameraCapture(0)
	} else {
		cap = opencv.NewFileCapture(videoFilePath)
	}

	var size = opencv.Size{int(cap.GetProperty(opencv.CV_CAP_PROP_FRAME_WIDTH)), int(cap.GetProperty(opencv.CV_CAP_PROP_FRAME_HEIGHT))}
	return VideoCapture{videoFilePath, camMode, cap, uint32(cap.GetProperty(opencv.CV_CAP_PROP_FOURCC)), float32(cap.GetProperty(opencv.CV_CAP_PROP_FPS)), size}
}
예제 #4
0
파일: player.go 프로젝트: nfillot/go-opencv
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)
}