title: Java and haarcascade face and mouth detection - mouth as the nose
body: <p>Today I begin to test the project which detects a smile in Java and OpenCv. To recognition face and mouth project used haarcascade_frontalface_alt and haarcascade_mcs_mouth But i don't understand why in some reasons project detect nose as a mouth. 
  I have two methods:</p>
  
  <pre><code>private ArrayList&lt;Mat&gt; detectMouth(String filename) {
      int i = 0;
      ArrayList&lt;Mat&gt; mouths = new ArrayList&lt;Mat&gt;();
      // reading image in grayscale from the given path
      image = Highgui.imread(filename, Highgui.CV_LOAD_IMAGE_GRAYSCALE);
      MatOfRect faceDetections = new MatOfRect();
      // detecting face(s) on given image and saving them to MatofRect object
      faceDetector.detectMultiScale(image, faceDetections);
      System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
      MatOfRect mouthDetections = new MatOfRect();
      // detecting mouth(s) on given image and saving them to MatOfRect object
      mouthDetector.detectMultiScale(image, mouthDetections);
      System.out.println(String.format("Detected %s mouths", mouthDetections.toArray().length));
      for (Rect face : faceDetections.toArray()) {
          Mat outFace = image.submat(face);
          // saving cropped face to picture
          Highgui.imwrite("face" + i + ".png", outFace);
          for (Rect mouth : mouthDetections.toArray()) {
              // trying to find right mouth
              // if the mouth is in the lower 2/5 of the face
              // and the lower edge of mouth is above of the face
              // and the horizontal center of the mouth is the enter of the face
              if (mouth.y &gt; face.y + face.height * 3 / 5 &amp;&amp; mouth.y + mouth.height &lt; face.y + face.height
                      &amp;&amp; Math.abs((mouth.x + mouth.width / 2)) - (face.x + face.width / 2) &lt; face.width / 10) {
                  Mat outMouth = image.submat(mouth);
                  // resizing mouth to the unified size of trainSize
                  Imgproc.resize(outMouth, outMouth, trainSize);
                  mouths.add(outMouth);
                  // saving mouth to picture 
                  Highgui.imwrite("mouth" + i + ".png", outMouth);
                  i++;
              }
          }
      }
      return mouths;
  }
  </code></pre>
  
  <p>and detect smile</p>
  
  <pre><code>private void detectSmile(ArrayList&lt;Mat&gt; mouths) {
          trainSVM();
          CvSVMParams params = new CvSVMParams();
          // set linear kernel (no mapping, regression is done in the original feature space)
          params.set_kernel_type(CvSVM.LINEAR);
      // train SVM with images in trainingImages, labels in trainingLabels, given params with empty samples
          clasificador = new CvSVM(trainingImages, trainingLabels, new Mat(), new Mat(), params);
          // save generated SVM to file, so we can see what it generated
          clasificador.save("svm.xml");
          // loading previously saved file
          clasificador.load("svm.xml");
          // returnin, if there aren't any samples
          if (mouths.isEmpty()) {
              System.out.println("No mouth detected");
              return;
          }
          for (Mat mouth : mouths) {
              Mat out = new Mat();
              // converting to 32 bit floating point in gray scale
              mouth.convertTo(out, CvType.CV_32FC1);
              if (clasificador.predict(out.reshape(1, 1)) == 1.0) {
                  System.out.println("Detected happy face");
              } else {
                  System.out.println("Detected not a happy face");
              }
          }
      }
  </code></pre>
  
  <p>Examples:</p>
  
  <p>For that picture </p>
  
  <p><a href="https://i.stack.imgur.com/9b83D.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/9b83D.jpg" alt="enter image description here"></a></p>
  
  <p>correctly detects this mounth:</p>
  
  <p><a href="https://i.stack.imgur.com/ikQpM.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ikQpM.png" alt="enter image description here"></a></p>
  
  <p>but in other picture </p>
  
  <p><a href="https://i.stack.imgur.com/t7Hg8.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/t7Hg8.jpg" alt="enter image description here"></a></p>
  
  <p>nose is detected
  <a href="https://i.stack.imgur.com/uFWLa.png" rel="noreferrer"><img src="https://i.stack.imgur.com/uFWLa.png" alt="enter image description here"></a></p>
  
  <p>What's the problem in your opinion ?</p>