예제 #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
파일: main.go 프로젝트: death/motionwatch
func main() {
	flag.Parse()

	if *user == "" {
		log.Fatal("Need gmail user name.")
	}

	if *password == "" {
		log.Fatal("Need gmail user password.")
	}

	time.Sleep(*initialSleep)

	cap := opencv.NewCameraCapture(0)
	if cap == nil {
		log.Fatal("Can't open camera.")
	}
	defer cap.Release()

	det := detector.New(&detector.Params{
		DevThreshold:   *minDev,
		PhaseThreshold: 3,
		QueryFrame:     cap.QueryFrame,
	})
	defer det.Close()

	log.Println("Watching for motion")
	for {
		if im := det.Detect(); im != nil {
			detectedMotion(im)
		}
		time.Sleep(*interval)
	}
}
예제 #3
0
func main() {
	var edge_threshold int

	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{}) {
		edge_threshold = pos
	})

	fmt.Println("Press ESC to quit")
	for {
		if cap.GrabFrame() {
			img := cap.RetrieveFrame(1)
			if img != nil {
				ProcessImage(img, win, edge_threshold)
			} else {
				fmt.Println("Image ins nil")
			}
		}
		key := opencv.WaitKey(10)

		if key == 27 {
			os.Exit(0)
		}
	}
}
예제 #4
0
파일: facelock.go 프로젝트: ecto/facelock
func main() {
	cap := opencv.NewCameraCapture(0)
	if cap == nil {
		panic("can not open camera")
	}
	defer cap.Release()

	_, currentfile, _, _ := runtime.Caller(0)
	cascade := opencv.LoadHaarClassifierCascade(path.Join(path.Dir(currentfile), "haarcascade_frontalface_alt.xml"))
	timeout := time.Second * 10
	lastFace := time.Now()

	for {
		if cap.GrabFrame() {
			img := cap.RetrieveFrame(1)
			if img == nil {
				continue
			}
			hasFaces, _ := ProcessImage(img, cascade)
			if hasFaces {
				lastFace = time.Now()
			} else {
				delta := time.Since(lastFace)
				fmt.Println("no faces for %ts", delta)
				if delta > timeout {
					fmt.Println("lock!")
					lock()
					time.Sleep(time.Second * 60)
				}
			}
		}

		time.Sleep(time.Second)
	}
}
예제 #5
0
파일: main.go 프로젝트: acrmp/pairshaped
func main() {
	hd, err := filepath.Abs(filepath.Dir(os.Args[0]))
	if err != nil {
		panic(err)
	}

	cp := path.Join(hd, "haarcascade_frontalface_alt.xml")

	if _, err := os.Stat(cp); err != nil {
		panic(err)
	}

	cm := opencv.NewCameraCapture(0)
	cs := opencv.LoadHaarClassifierCascade(cp)

	p := &pair.Checker{Camera: cm, Cascade: cs}
	t, err := p.Present()

	if err != nil {
		panic(err)
	}

	if !t {
		os.Stderr.WriteString("Your pair must be playing ping pong\n")
		os.Exit(1)
	}
}
예제 #6
0
파일: init.go 프로젝트: ananasn/meow
func init() {

	//Initialize camera
	fmt.Println("Get capture")
	Capture <- opencv.NewCameraCapture(0)
	fmt.Println("Capture done")

	// Filters is the default set of global filters.
	revel.Filters = []revel.Filter{
		revel.PanicFilter,             // Recover from panics and display an error page instead.
		revel.RouterFilter,            // Use the routing table to select the right Action
		revel.FilterConfiguringFilter, // A hook for adding or removing per-Action filters.
		revel.ParamsFilter,            // Parse parameters into Controller.Params.
		revel.SessionFilter,           // Restore and write the session cookie.
		revel.FlashFilter,             // Restore and write the flash cookie.
		revel.ValidationFilter,        // Restore kept validation errors and save new ones from cookie.
		revel.I18nFilter,              // Resolve the requested language
		HeaderFilter,                  // Add some security based headers
		revel.InterceptorFilter,       // Run interceptors around the action.
		revel.CompressFilter,          // Compress the result.
		revel.ActionInvoker,           // Invoke the action.
	}

	// register startup functions with OnAppStart
	// ( order dependent )
	// revel.OnAppStart(InitDB)
	// revel.OnAppStart(FillCache)
}
예제 #7
0
파일: camget.go 프로젝트: ArchieT/3manchess
func NewCam(index int) (Camera, error) {
	src := opencv.NewCameraCapture(index)
	cam := Camera{src, index}
	if src == nil {
		return cam, NewCamError("It is nil!")
	}
	go func() {
		defer src.Release()
		for {
		}
	}()
	return cam, nil
}
예제 #8
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}
}
func CaptureFrames() {
	var cam = opencv.NewCameraCapture(0)
	defer cam.Release()

	frames := []*opencv.IplImage{}
	//var writter = opencv.NewVideoWriter("test.avi", int(opencv.FOURCC('D','I','V','3')),20, 50, 50, 1)

	win := opencv.NewWindow("Go-OpenCV Webcam123")
	defer win.Destroy()
	for framesCounter := 0; framesCounter <= 25; framesCounter++ {
		log.Print(framesCounter)
		if cam.GrabFrame() || framesCounter > 25 {
			//            var frame = cam.RetrieveFrame(1)
			//            var img = ProcessImage(frame, 50)
			//            frames = append(frames, img)

			var frame = cam.RetrieveFrame(1)
			win.ShowImage(frame)
			//            var img3 = frame.ToImage()

			var imageHeader = opencv.CreateImage(frame.Width(), frame.Height(), frame.Depth(), frame.Channels())

			for i := 0; i < frame.Width(); i++ {
				for j := 0; j < frame.Height(); j++ {

					scalar := frame.Get2D(i, j)
					var bgra = scalar.Val()
					var newscalar = opencv.NewScalar(bgra[0], bgra[1], bgra[2], 1)
					imageHeader.Set2D(i, j, newscalar)

				}
			}
			//            var img3Opencv = opencv.FromImage(img3)

			var copy = opencv.CreateImage(frame.Width(), frame.Height(), frame.Depth(), frame.Channels())
			opencv.Copy(frame, copy, nil)
			//            frames = append(frames, img3Opencv)

			//win.ShowImage(imageHeader)

		}
	}

	var videoWritter = NewVideoWriter(opencv.FOURCC('D', 'I', 'V', '3'), 20, opencv.Size{frames[0].Width(), frames[0].Height()})
	videoWritter.SaveAsVideo(frames, "test.avi")

}
예제 #10
0
func main() {
	win := opencv.NewWindow("Go-OpenCV Webcam Face Detection")
	defer win.Destroy()

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

	cwd, err := os.Getwd()
	if err != nil {
		panic(err)
	}
	cascade := opencv.LoadHaarClassifierCascade(path.Join(cwd, "haarcascade_frontalface_alt.xml"))

	fmt.Println("Press ESC to quit")
	for {
		if cap.GrabFrame() {
			img := cap.RetrieveFrame(1)
			if img != nil {
				faces := cascade.DetectObjects(img)
				for _, value := range faces {
					opencv.Circle(img,
						opencv.Point{
							value.X() + (value.Width() / 2),
							value.Y() + (value.Height() / 2),
						},
						value.Width()/2,
						opencv.ScalarAll(255.0), 1, 1, 0)
				}

				win.ShowImage(img)
			} else {
				fmt.Println("nil image")
			}
		}
		key := opencv.WaitKey(1)

		if key == 27 {
			os.Exit(0)
		}
	}
}
예제 #11
0
func main() {

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

	cwd, err := os.Getwd()
	if err != nil {
		panic(err)
	}
	cascade := opencv.LoadHaarClassifierCascade(path.Join(cwd, "haarcascade_frontalface_alt.xml"))

	fmt.Println("Press ESC to quit")
	for {
		if cap.GrabFrame() {
			img := cap.RetrieveFrame(1)
			if img != nil {
				faces := cascade.DetectObjects(img)
				for _, value := range faces {
					leftX := value.X()
					rightX := leftX + value.Width()
					lowY := value.Y()
					highY := lowY + value.Height()
					log.Println("Face Seen! LowLeft Corner: (", leftX, ", ", lowY, "), UpRight Corner: (", rightX, ", ", highY, ")")
				}

			} else {
				fmt.Println("nil image")
			}
		}
		key := opencv.WaitKey(1)

		if key == 27 {
			os.Exit(0)
		}
	}
}
예제 #12
0
func ProcessImageTest() {

	log.Print("Try to open webcam.")
	cap := opencv.NewCameraCapture(0)
	if cap == nil {
		panic("can not open camera")
	}

	log.Print("webcame opened.")

	var outputFileName = "testvideo.mpeg"
	var width = 640
	var height = 480
	var fps = 20.0
	var fourcc = opencv.FOURCC('m', 'p', '4', 'v')
	var noOfCapturedFrames = 100

	cap.SetProperty(opencv.CV_CAP_PROP_FRAME_WIDTH, float64(width))
	cap.SetProperty(opencv.CV_CAP_PROP_FRAME_HEIGHT, float64(height))
	cap.SetProperty(opencv.CV_CAP_PROP_FPS, fps)

	log.Print("capturing ", noOfCapturedFrames, " frames and save them to the following file: ", outputFileName)
	log.Print("width: ", width)
	log.Print("height: ", height)
	log.Print("fps: ", fps)
	log.Print("fourcc: ", fourcc)

	var frames []*opencv.IplImage

	for len(frames) < noOfCapturedFrames {
		if cap.GrabFrame() {
			img := cap.RetrieveFrame(1)
			if img != nil {

				var frame = cap.RetrieveFrame(1)
				var copy = opencv.CreateImage(int(width), int(height), frame.Depth(), frame.Channels())
				defer copy.Release()
				opencv.Copy(frame, copy, nil)

				frames = append(frames, copy)

			} else {
				fmt.Println("Image ins nil")
			}
		}
	}

	cap.Release()
	win := opencv.NewWindow("Go-OpenCV Webcam")
	defer win.Destroy()
	for i := 0; i < len(frames); i++ {
		log.Print("displaying frame no: ", i)
		win.ShowImage(frames[i])
		key := opencv.WaitKey(10)
		if key != 0 {
			log.Print(key)
		}
	}

	log.Print("start writing frames to file.")
	var videoWritter = NewVideoWriter(fourcc, float32(fps), opencv.Size{int(width), int(height)})
	videoWritter.SaveAsVideo(frames, outputFileName)

	log.Print("created file with name: ", outputFileName)
	os.Exit(0)
}