Skip to content

[Feature request] Manually set ByteBuffer and Bitmap in mlkit selfie segmentation  #422

@pawaom

Description

@pawaom

What's your feature request? Please describe.
Can we set Manually ByteBuffer and Bitmap in mlkit selfie segmentation

Mobile environment
both

Additional context
We are currently using this code

private Segmenter segmenter;
    private ByteBuffer maskBuffer = ByteBuffer.allocate(0);
    private int maskWidth = 0;
    private int maskHeight = 0;
    BitmapUtils bitmapUtils = new BitmapUtils();
    Bitmap maskBitmap;
    ProcessedListener listener;
    public SegmentHelper(ProcessedListener mlistener) {
        this.listener = mlistener;
    }

    public void init() {
        SelfieSegmenterOptions options =
                new SelfieSegmenterOptions.Builder()
                        .setDetectorMode(SelfieSegmenterOptions.SINGLE_IMAGE_MODE)
                        .build();
        segmenter = Segmentation.getClient(options);
    }

    /**
     * Processes a bitmap and informs the listener on success
     */
    public void processImage(Context mContext, Bitmap image) {
        InputImage input = InputImage.fromBitmap(image, 0);
        segmenter.process(input).addOnSuccessListener(SegmentationMask -> {
            maskBuffer = SegmentationMask.getBuffer();
            maskWidth = SegmentationMask.getWidth();
            maskHeight = SegmentationMask.getHeight();
            listener.imageProcessed();

        })
                .addOnFailureListener(e -> {
                });
    }

    /**
     * Highlights the detected background of the segmentation mask as a green overlay
     */
    public Bitmap generateMaskImage(Bitmap image) {
        maskBitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(), image.getConfig());
        for (int y = 0; y < maskHeight; y++) {
            for (int x = 0; x < maskWidth; x++) {
                int bgConfidence = (int) ((1.0 - maskBuffer.getFloat()) * 255);
                maskBitmap.setPixel(x, y, Color.argb(bgConfidence, 0, 0, 0));
            }
        }
        maskBuffer.rewind();
        return bitmapUtils.mergeBitmaps(image, maskBitmap);
    }

every thing is done by MLKIT, can we set the bytbuffer and Create a bitmap out of it Manually , since some times MLKIT misses some parts of the image we want to capture.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions