Advertisement
ArCiGo

FaceDetection

Nov 24th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. import org.opencv.core.Core;
  2. import org.opencv.core.Mat;
  3. import org.opencv.core.MatOfRect;
  4. import org.opencv.core.Point;
  5. import org.opencv.core.Rect;
  6. import org.opencv.core.Scalar;
  7. import org.opencv.highgui.Highgui;
  8. import org.opencv.objdetect.CascadeClassifier;
  9.  
  10. //
  11. // Detects faces in an image, draws boxes around them, and writes the results
  12. // to "faceDetection.png".
  13. //
  14. class DetectFaceDemo {
  15.  
  16.     public void run() {
  17.         System.out.println("\nRunning DetectFaceDemo");
  18.  
  19.     // Create a face detector from the cascade file in the resources
  20.         // directory.
  21.         CascadeClassifier faceDetector = new CascadeClassifier("C:\\opencv\\sources\\data\\lbpcascades\\lbpcascade_frontalface.xml");
  22.         //CascadeClassifier faceDetector = new CascadeClassifier("C:\\opencv\\sources\\data\\lbpcascades\\haarcascade_frontalface_alt.xml");
  23.         //Mat image = Highgui.imread("C:\\opencv\\shekhar.JPG");
  24.         Mat image = Highgui.imread("FotoTest1.jpg");
  25.  
  26.     // Detect faces in the image.
  27.         // MatOfRect is a special container class for Rect.
  28.         MatOfRect faceDetections = new MatOfRect();
  29.         faceDetector.detectMultiScale(image, faceDetections);
  30.  
  31.         System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
  32.  
  33.         // Draw a bounding box around each face.
  34.         for (Rect rect : faceDetections.toArray()) {
  35.             Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
  36.         }
  37.  
  38.         // Save the visualized detection.
  39.         String filename = "faceDetection.png";
  40.         System.out.println(String.format("Writing %s", filename));
  41.         Highgui.imwrite(filename, image);
  42.     }
  43. }
  44.  
  45. public class OCV_FaceDetection {
  46.  
  47.     public static void main(String[] args) {
  48.         System.out.println("Hello, OpenCV");
  49.  
  50.         // Load the native library.
  51.         System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  52.         new DetectFaceDemo().run();
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement