title: MLkit face detection not working with front camera for android
body: <p>I am using MLKit for face detection on android. I am following the official documentation which can be found here - <a href="https://developers.google.com/ml-kit/vision/face-detection/android" rel="nofollow noreferrer">https://developers.google.com/ml-kit/vision/face-detection/android</a>. I am successfully able to detect faces using the rear camera, but no faces are detected when I switch to the front camera. According to what I have understood from <a href="https://stackoverflow.com/questions/52437597/firebase-mlkit-text-recognition-fail-with-the-front-camera-ios">this</a> forum, the problem might be rotation compensation. I have used the same code for this purpose as shown in the documentation -</p>
  <pre><code>private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
  
  static {
      ORIENTATIONS.append(Surface.ROTATION_0, 90);
      ORIENTATIONS.append(Surface.ROTATION_90, 0);
      ORIENTATIONS.append(Surface.ROTATION_180, 270);
      ORIENTATIONS.append(Surface.ROTATION_270, 180);
  }
  
  /**
   * Get the angle by which an image must be rotated given the device's current
   * orientation.
   */
  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  private int getRotationCompensation(String cameraId, Activity activity, Context context)
          throws CameraAccessException {
      // Get the device's current rotation relative to its &quot;native&quot; orientation.
      // Then, from the ORIENTATIONS table, look up the angle the image must be
      // rotated to compensate for the device's rotation.
      int deviceRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
      int rotationCompensation = ORIENTATIONS.get(deviceRotation);
  
      // On most devices, the sensor orientation is 90 degrees, but for some
      // devices it is 270 degrees. For devices with a sensor orientation of
      // 270, rotate the image an additional 180 ((270 + 270) % 360) degrees.
      CameraManager cameraManager = (CameraManager) context.getSystemService(CAMERA_SERVICE);
      int sensorOrientation = cameraManager
              .getCameraCharacteristics(cameraId)
              .get(CameraCharacteristics.SENSOR_ORIENTATION);
      rotationCompensation = (rotationCompensation + sensorOrientation + 270) % 360;
  
      return rotationCompensation;
  }
  </code></pre>
  <p>For changing the camera, I just change a global variable cameraID and no other changes are made in the code.</p>
  <p>EDIT-
  In case of front camera, generating input image with rotation =  getRotationCompensation(...) - 180 and not subtracting 180 in case of rear camera worked for me. Not sure why that works though. I would still appreciate any explanation regarding this or how this can be avoided.</p>