title: OpenCV Java Smile Detection
body: <p>I've tried to create a smile detector with source code that I've found on the Internet. It detects face and works pretty well. It uses Haar classifiers, I've found the Haar classifiers for smile recognition and tried it, however it doesn't work. I've tried to use it in the same way that was used to recognize face. Tried the same with eye classifier - and it worked. All classifiers I've found in opencv/data folder, could somebody give me a tip, what could I do more with given code?</p>
  
  <pre><code>import java.io.File;
  import org.opencv.core.Core;
  import org.opencv.core.Mat;
  import org.opencv.core.MatOfRect;
  import org.opencv.core.Point;
  import org.opencv.core.Rect;
  import org.opencv.core.Scalar;
  import org.opencv.highgui.Highgui;
  import org.opencv.objdetect.CascadeClassifier;
  
  public class SmileDetector {
  
  public void detectSmile(String filename) {
      System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
      System.out.println("\nRunning SmileDetector");
  
      CascadeClassifier faceDetector = new CascadeClassifier(new File(
              "src/main/resources/haarcascade_frontalface_alt.xml").getAbsolutePath());
      CascadeClassifier smileDetector = new CascadeClassifier(
              new File("src/main/resources/haarcascade_smile.xml").getAbsolutePath());
      Mat image = Highgui.imread(filename);
      MatOfRect faceDetections = new MatOfRect();
      MatOfRect smileDetections = new MatOfRect();
      faceDetector.detectMultiScale(image, faceDetections);
  
      System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
  
      for (Rect rect : faceDetections.toArray()) {
          Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
                  new Scalar(0, 255, 0));
      }
      Mat face = image.submat(faceDetections.toArray()[0]);
      smileDetector.detectMultiScale(face, smileDetections);
  
      for (Rect rect : smileDetections.toArray()) {
          Core.rectangle(face, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
                  new Scalar(0, 255, 0));
      }
  
      String outputFilename = "ouput.png";
      System.out.println(String.format("Writing %s", outputFilename));
      Highgui.imwrite(outputFilename, image);
      Highgui.imwrite("ee.png", face);
  }
  }
  </code></pre>