func loop(path string, paint, crosshair bool) { pictureFiles = GetFileNames(path) // images := make([][]byte, len(pictureFiles)) faceDetector := gofaces.NewFaceDetector() // for i := 0; i < len(pictureFiles); i++ { // } fmt.Println("Pictures: ", len(pictureFiles)) for i := 0; i < len(pictureFiles); i++ { fmt.Println("Processing picture: ", pictureFiles[i]) img := gofaces.GetNormalizedByteVectorFromFile(pictureFiles[i]) faces := faceDetector.Detect(img) if len(faces) > 0 { if paint { img = gofaces.PaintFace(img, faces[0]) } if faces[0].Eyes() > 1 { img = gofaces.AlignFaceInImage(img, faces[0]) } if crosshair { img = gofaces.Crosshair(img) } cropface := gofaces.CropOutFace(img, faces[0]) win := opencv.NewWindow("Face Detected") win.ShowImage(opencv.DecodeImageMem(cropface)) opencv.WaitKey(0) win.Destroy() } else { win := opencv.NewWindow("No face found") win.ShowImage(opencv.DecodeImageMem(img)) opencv.WaitKey(0) win.Destroy() } } }
func (detector *FaceDetector) Detect(img []byte) faces { image := opencv.DecodeImageMem(img) faceAreas := detector.DetectFaces(image) faces := detector.DetectFacialFeatures(image, faceAreas) return faces }
func debug(path string) { faceDetector := gofaces.NewFaceDetector() picture := gofaces.GetNormalizedByteVectorFromFile(path) image := opencv.DecodeImageMem(picture) faceAreas := faceDetector.DetectFaces(image) faces := faceDetector.DetectFacialFeatures(image, faceAreas) picture = gofaces.PaintFaces(picture, faces) win := opencv.NewWindow("Debug Face Detection") defer win.Destroy() win.ShowImage(opencv.DecodeImageMem(picture)) opencv.WaitKey(0) }
func one(path string, paint bool) { faceDetector := gofaces.NewFaceDetector() picture := gofaces.GetNormalizedByteVectorFromFile(path) faces := faceDetector.Detect(picture) if paint { picture = gofaces.PaintFace(picture, faces[0]) } picture = gofaces.AlignFaceInImage(picture, faces[0]) picture = gofaces.CropOutFace(picture, faces[0]) win := opencv.NewWindow("Face Detection") defer win.Destroy() win.ShowImage(opencv.DecodeImageMem(picture)) opencv.WaitKey(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() }
func (detector *FaceDetector) DetectFacialFeatures(image *opencv.IplImage, faceCoords []pixelCoord) faces { var faces = make(faces, 0) for _, faceCoord := range faceCoords { //Lets find us some eyes leftEyes, rightEyes := detector.DetectEyes(image, &faceCoord) if len(leftEyes) == 0 || len(rightEyes) == 0 { //No eyes found, lets flop the picture and check again fmt.Println("Flopping Picture to look for eyes") floppedImage := opencv.DecodeImageMem(FlopImage(ToByteBuffer(image))) floppedFaceCoords := faceCoord floppedFaceCoords.Flop(pixelCoord{width: image.Width(), height: image.Height()}) floppedLeftEyes, floppedRightEyes := detector.DetectEyes(floppedImage, &floppedFaceCoords) if len(leftEyes) == 0 { floppedRightEyes.FlopEmAll(pixelCoord{width: image.Width(), height: image.Height()}) leftEyes = append(leftEyes, floppedRightEyes...) } if len(rightEyes) == 0 { floppedLeftEyes.FlopEmAll(pixelCoord{width: image.Width(), height: image.Height()}) leftEyes = append(rightEyes, floppedLeftEyes...) } } leftEye, rightEye := FindBestEyes(leftEyes, rightEyes) faces = append(faces, face{ coord: faceCoord, eye_left: leftEye, eye_right: rightEye, }) } fmt.Println(faces) return faces }
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() }
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() }