googlesamples / mlkit

A collection of sample apps to demonstrate how to use Google's ML Kit APIs on Android and iOS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Selfie Segmentation confidence threshold

arianaa30 opened this issue · comments

I'm experimenting with the com.google.mlkit:segmentation-selfie:16.0.0-beta5 and the selfie segmentation sample code. Idk why sometimes 0.9 threshold below works and sometimes in different rooms I have to change it to 0.2. Apparently it is very prone to environment and lighting conditions.

Can you make it more stable and prone to lighting conditions in the future versions?

  private int[] maskColorsFromByteBuffer(ByteBuffer byteBuffer) {
    @ColorInt int[] colors = new int[maskWidth * maskHeight];
    for (int i = 0; i < maskWidth * maskHeight; i++) {
      float backgroundLikelihood = 1 - byteBuffer.getFloat();
      if (backgroundLikelihood > 0.9) {
        colors[i] = Color.argb(128, 255, 0, 255);
      } else if (backgroundLikelihood > 0.2) {
        // Linear interpolation to make sure when backgroundLikelihood is 0.2, the alpha is 0 and
        // when backgroundLikelihood is 0.9, the alpha is 128.
        // +0.5 to round the float value to the nearest int.
        int alpha = (int) (182.9 * backgroundLikelihood - 36.6 + 0.5);
        colors[i] = Color.argb(alpha, 255, 0, 255);
      }
    }
    return colors;
  }