├── .classpath ├── .gitignore ├── README.md ├── ouput.png └── src └── com └── shekhar └── facedetection ├── FaceDetector.java ├── haarcascade_frontalface_alt.xml └── shekhar.JPG /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | bin 2 | .settings 3 | .project 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | day12-face-detection 2 | ==================== 3 | 4 | OpenCV Java example. 5 | -------------------------------------------------------------------------------- /ouput.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shekhargulati/day12-face-detection/268147a43546a615f127d1bbffe2205245ed625e/ouput.png -------------------------------------------------------------------------------- /src/com/shekhar/facedetection/FaceDetector.java: -------------------------------------------------------------------------------- 1 | package com.shekhar.facedetection; 2 | 3 | import org.opencv.core.Core; 4 | import org.opencv.core.Mat; 5 | import org.opencv.core.MatOfRect; 6 | import org.opencv.core.Point; 7 | import org.opencv.core.Rect; 8 | import org.opencv.core.Scalar; 9 | import org.opencv.highgui.Highgui; 10 | import org.opencv.objdetect.CascadeClassifier; 11 | 12 | public class FaceDetector { 13 | 14 | public static void main(String[] args) { 15 | 16 | System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 17 | System.out.println("\nRunning FaceDetector"); 18 | 19 | CascadeClassifier faceDetector = new CascadeClassifier(FaceDetector.class.getResource("haarcascade_frontalface_alt.xml").getPath()); 20 | Mat image = Highgui 21 | .imread(FaceDetector.class.getResource("shekhar.JPG").getPath()); 22 | 23 | MatOfRect faceDetections = new MatOfRect(); 24 | faceDetector.detectMultiScale(image, faceDetections); 25 | 26 | System.out.println(String.format("Detected %s faces", faceDetections.toArray().length)); 27 | 28 | for (Rect rect : faceDetections.toArray()) { 29 | Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), 30 | new Scalar(0, 255, 0)); 31 | } 32 | 33 | String filename = "ouput.png"; 34 | System.out.println(String.format("Writing %s", filename)); 35 | Highgui.imwrite(filename, image); 36 | } 37 | } -------------------------------------------------------------------------------- /src/com/shekhar/facedetection/shekhar.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shekhargulati/day12-face-detection/268147a43546a615f127d1bbffe2205245ed625e/src/com/shekhar/facedetection/shekhar.JPG --------------------------------------------------------------------------------