Ejemplo n.º 1
0
func PaintFaces(img []byte, faces faces) []byte {

	image := opencv.DecodeImageMem(img)

	for _, face := range faces {

		opencv.Rectangle(image,
			opencv.Point{face.coord.x + face.coord.width, face.coord.y},
			opencv.Point{face.coord.x, face.coord.y + face.coord.height},
			opencv.ScalarAll(0), 1, 1, 0)

		le := face.LeftEye().Center()
		re := face.RightEye().Center()

		opencv.Circle(image,
			opencv.Point{le.X(), le.Y()},
			2,
			opencv.ScalarAll(255), 1, 1, 0)

		opencv.Circle(image,
			opencv.Point{re.x, re.y},
			2,
			opencv.ScalarAll(255), 1, 1, 0)

		faceCenter := face.Center()
		opencv.Circle(image,
			opencv.Point{faceCenter.x, faceCenter.y},
			2,
			opencv.ScalarAll(50), 1, 1, 0)

		//		opencv.Rectangle(image,
		//			opencv.Point{face.mouth.x + face.mouth.width, face.mouth.y},
		//			opencv.Point{face.mouth.x, face.mouth.y + face.mouth.height},
		//			opencv.ScalarAll(2), 1, 1, 0)
	}

	buf := new(bytes.Buffer)
	err := jpeg.Encode(buf, image.ToImage(), nil)
	if err != nil {
		panic(err)
	}
	return buf.Bytes()
}
Ejemplo n.º 2
0
func PaintFace(img []byte, face face) []byte {

	image := opencv.DecodeImageMem(img)

	opencv.Rectangle(image,
		opencv.Point{face.coord.x + face.coord.width, face.coord.y},
		opencv.Point{face.coord.x, face.coord.y + face.coord.height},
		opencv.ScalarAll(0), 1, 1, 0)

	le := face.eye_left.Center()
	re := face.eye_right.Center()

	opencv.Circle(image,
		opencv.Point{le.x, le.y},
		2,
		opencv.ScalarAll(255), 1, 1, 0)

	opencv.Circle(image,
		opencv.Point{re.x, re.y},
		2,
		opencv.ScalarAll(255), 1, 1, 0)

	faceCenter := face.Center()
	opencv.Circle(image,
		opencv.Point{faceCenter.x, faceCenter.y},
		2,
		opencv.ScalarAll(50), 1, 1, 0)

	opencv.Rectangle(image,
		opencv.Point{face.mouth.x + face.mouth.width, face.mouth.y},
		opencv.Point{face.mouth.x, face.mouth.y + face.mouth.height},
		opencv.ScalarAll(2), 1, 1, 0)

	buf := new(bytes.Buffer)
	err := jpeg.Encode(buf, image.ToImage(), nil)
	if err != nil {
		panic(err)
	}
	return buf.Bytes()
}
Ejemplo n.º 3
0
func Crosshair(img []byte) []byte {

	image := opencv.DecodeImageMem(img)

	//Horizontal line

	opencv.Line(image,
		opencv.Point{0, image.Height() / 2},
		opencv.Point{image.Width(), image.Height() / 2},
		opencv.ScalarAll(0), 1, 1, 0)
	//vertical line

	opencv.Line(image,
		opencv.Point{image.Width() / 2, 0},
		opencv.Point{image.Width() / 2, image.Height()},
		opencv.ScalarAll(0), 1, 1, 0)

	buf := new(bytes.Buffer)
	err := jpeg.Encode(buf, image.ToImage(), nil)
	if err != nil {
		panic(err)
	}
	return buf.Bytes()
}
Ejemplo n.º 4
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)
}
Ejemplo n.º 5
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)
		}
	}
}
Ejemplo n.º 6
0
func (e eyeMarker) drawEyeMarker(img *opencv.IplImage, p opencv.Point) {
	opencv.Circle(img, p, 5, opencv.ScalarAll(175.0), 1, 1, 0)
}
Ejemplo n.º 7
0
func main() {
	filename := "../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)
}