示例#1
0
func main() {
	_, currentfile, _, _ := runtime.Caller(0)
	image := opencv.LoadImage(path.Join(path.Dir(currentfile), "../images/lena.jpg"))

	cascade := opencv.LoadHaarClassifierCascade(path.Join(path.Dir(currentfile), "haarcascade_frontalface_alt.xml"))
	faces := cascade.DetectObjects(image)

	for _, value := range faces {
		opencv.Rectangle(image,
			opencv.Point{value.X() + value.Width(), value.Y()},
			opencv.Point{value.X(), value.Y() + value.Height()},
			opencv.ScalarAll(255.0), 1, 1, 0)
	}

	win := opencv.NewWindow("Face Detection")
	win.ShowImage(image)
	opencv.WaitKey(0)
}
示例#2
0
func main() {
	_, currentfile, _, _ := runtime.Caller(0)
	filename := path.Join(path.Dir(currentfile), "../images/fruits.jpg")
	if len(os.Args) == 2 {
		filename = os.Args[1]
	}

	img0 := opencv.LoadImage(filename)
	if img0 == nil {
		panic("LoadImage fail")
	}
	defer img0.Release()

	fmt.Print("Hot keys: \n",
		"\tESC - quit the program\n",
		"\tr - restore the original image\n",
		"\ti or ENTER - run inpainting algorithm\n",
		"\t\t(before running it, paint something on the image)\n",
	)

	img := img0.Clone()
	inpainted := img0.Clone()
	inpaint_mask := opencv.CreateImage(img0.Width(), img0.Height(), 8, 1)

	opencv.Zero(inpaint_mask)
	//opencv.Zero( inpainted )

	win := opencv.NewWindow("image")
	defer win.Destroy()

	prev_pt := opencv.Point{-1, -1}
	win.SetMouseCallback(func(event, x, y, flags int, param ...interface{}) {
		if img == nil {
			os.Exit(0)
		}

		if event == opencv.CV_EVENT_LBUTTONUP ||
			(flags&opencv.CV_EVENT_FLAG_LBUTTON) == 0 {
			prev_pt = opencv.Point{-1, -1}
		} else if event == opencv.CV_EVENT_LBUTTONDOWN {
			prev_pt = opencv.Point{x, y}
		} else if event == opencv.CV_EVENT_MOUSEMOVE &&
			(flags&opencv.CV_EVENT_FLAG_LBUTTON) != 0 {
			pt := opencv.Point{x, y}
			if prev_pt.X < 0 {
				prev_pt = pt
			}

			rgb := opencv.ScalarAll(255.0)
			opencv.Line(inpaint_mask, prev_pt, pt, rgb, 5, 8, 0)
			opencv.Line(img, prev_pt, pt, rgb, 5, 8, 0)
			prev_pt = pt

			win.ShowImage(img)
		}
	})
	win.ShowImage(img)
	opencv.WaitKey(0)

	win2 := opencv.NewWindow("inpainted image")
	defer win2.Destroy()
	win2.ShowImage(inpainted)

	for {
		key := opencv.WaitKey(20)
		if key == 27 {
			os.Exit(0)
		} else if key == 'r' {
			opencv.Zero(inpaint_mask)
			opencv.Copy(img0, img, nil)
			win.ShowImage(img)
		} else if key == 'i' || key == '\n' {
			opencv.Inpaint(img, inpaint_mask, inpainted, 3,
				opencv.CV_INPAINT_TELEA,
			)
			win2.ShowImage(inpainted)
		}
	}
	os.Exit(0)
}
func main() {
	gbot := gobot.NewGobot()

	window := opencv.NewWindowDriver("window")
	camera := opencv.NewCameraDriver("camera", 0)

	//e := edison.NewEdisonAdaptor("edison")
	//led1 := gpio.NewLedDriver(e, "led", "13")
	//firmataAdaptor := firmata.NewFirmataAdaptor("arduino", "/dev/ttyACM0")
	//led1 := gpio.NewLedDriver(firmataAdaptor, "led", "13")
	//motor := gpio.NewMotorDriver(firmataAdaptor, "motor", "4")
	//led2 := gpio.NewLedDriver(firmataAdaptor, "led", "10")
	//led3 := gpio.NewLedDriver(firmataAdaptor, "led", "9")

	_, currentfile, _, _ := runtime.Caller(0)
	work := func() {
		//speed := byte(0)
		//fadeAmount := byte(15)
		gobot.On(camera.Event("frame"), func(data interface{}) {
			cascade := cv.LoadHaarClassifierCascade(path.Join(path.Dir(currentfile), "haarcascade_frontalface_alt.xml"))
			nestedcascade := cv.LoadHaarClassifierCascade(path.Join(path.Dir(currentfile), "smiled_04.xml"))

			i := data.(*cv.IplImage)
			faces := cascade.DetectObjects(i)
			teeth := 0
			//flagcenter:= false
			for _, value := range faces {
				/*cv.Rectangle(i,
				cv.Point{value.X() + value.Width(), value.Y()},
				cv.Point{value.X(), value.Y() + value.Height()},
				cv.ScalarAll(255.0), 1, 0, 0)*/

				mouths := nestedcascade.DetectObjects(i)
				for _, value1 := range mouths {
					if float64(value1.Y()) > float64(value.Y())+float64(value.Height())*3/5 && float64(value1.Y())+float64(value1.Height()) < float64(value.Y())+float64(value.Height()) && math.Abs((float64(value1.X())+float64(value1.Width())/2)-(float64(value.X())+float64(value.Width())/2)) < float64(value.Width())/10 {

						cv.Rectangle(i,
							cv.Point{value1.X() + value1.Width(), value1.Y()},
							cv.Point{value1.X(), value1.Y() + value1.Height()},
							cv.ScalarAll(255.0), 1, 0, 0)
						teeth = teeth + 1
						//if value1.X()>240 && value1.Y()>240 && value1.Height()>50 && value1.Width()>100{ flagcenter=true }else{ flagcenter=false }
						fmt.Println(value1.X(), value1.Y(), value1.Height(), value1.Width())
					}
				}
				//if len(smiles) > 0 { fmt.Println("worked")}
			}
			window.ShowImage(i)
			if teeth > 0 {
				//led1.On()
				//motor.Speed(speed)
				//speed = speed + fadeAmount
				//if speed == 0 || speed == 255 {
				//	fadeAmount = -fadeAmount
				//}
				fmt.Println("good")
				//led2.On()
				//led3.On()
				/*f := faces[0]
				w := f.Width()
				fmt.Println(w)
				switch {
				case w < 250:
					fmt.Println("*")
					led2.On()
					led3.Off()
					//red3.Off()
					//red4.Off()
					//red5.Off()
				case w > 250 && w < 350:
					fmt.Println("**")
					led2.Off()
					led3.On()
					//red3.Off()
					//red4.Off()
					//red5.Off()
				case w > 350 && w < 400:
					fmt.Println("***")
					led2.On()
					led3.On()
					//red3.On()
					//red4.Off()
					//red5.Off()
				case w > 400 && w < 450:
					fmt.Println("****")
					led2.On()
					led3.On()
					//red3.On()
					//red4.On()
					//red5.Off()
				case w > 450:
					fmt.Println("*****")
					led2.Off()
					led3.Off()
					//red3.On()
					//red4.On()
					//red5.On()
				}*/
			} else {
				//led1.Off()
				//speed = byte(0)
				//fadeAmount := byte(15)
				fmt.Println("smile please")
				//led2.Off()
				//led3.Off()
				//red3.Off()
				//red4.Off()
				//red5.Off()
			}

		})

	}

	/*work1 := func() {
	        gobot.Every(1*time.Second, func() {

	        })
	}*/

	robot := gobot.NewRobot("cameraBot",
		//[]gobot.Connection{firmataAdaptor},
		[]gobot.Device{window, camera},
		work,
	)

	/*robot1 := gobot.NewRobot("ledBot",
	        []gobot.Device{window,camera,led1},
	        work,
	)*/

	gbot.AddRobot(robot)
	//gbot.AddRobot(robot1)

	gbot.Start()

}