B4A Library [class] ML Kit Subject Segmentation

1714294035666.png



Input: image
Output: image with main subject

See attached project.
Don't miss the #AdditionalJars in the main module and the manifest editor snippets.

Usage:
B4X:
Private Sub Button1_Click
    Dim cc As ContentChooser
    cc.Initialize("cc")
    cc.Show("image/*", "choose image")
    Wait For cc_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
        'I'm resizing the image here. It might be better to leave the image with the original resolution.
        Dim bmp As B4XBitmap = xui.LoadBitmapResize(Dir, FileName, B4XImageView1.mBase.Width, B4XImageView1.mBase.Height, True)
        B4XImageView1.Bitmap = bmp
        B4XImageView2.Clear
        Wait For (segmenter.Process(bmp)) Complete (Result As SegmentationResult)
        If Result.Success Then
            B4XImageView2.Bitmap = Result.ForegroundBitmap
        End If
    End If
End Sub

B4A Sdk Manager: search for mlkit and install all items. Search for odml and install as well. Don't install anything else.
 

Attachments

  • SubjectSegmentation.zip
    15.7 KB · Views: 0
Last edited:

Input: image
Output: image with main subject

See attached project.
Don't miss the #AdditionalJars in the main module and the manifest editor snippets.

Usage:
B4X:
Private Sub Button1_Click
    Dim cc As ContentChooser
    cc.Initialize("cc")
    cc.Show("image/*", "choose image")
    Wait For cc_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
        'I'm resizing the image here. It might be better to leave the image with the original resolution.
        Dim bmp As B4XBitmap = xui.LoadBitmapResize(Dir, FileName, B4XImageView1.mBase.Width, B4XImageView1.mBase.Height, True)
        B4XImageView1.Bitmap = bmp
        B4XImageView2.Clear
        Wait For (segmenter.Process(bmp)) Complete (Result As SegmentationResult)
        If Result.Success Then
            B4XImageView2.Bitmap = Result.ForegroundBitmap
        End If
    End If
End Sub
While compiling I am Getting
Organizing libraries. Error
Maven artifact not found: com.google.firebase/firebase-encoders
Source: com.google.android.gms:play-services-mlkit-subject-segmentation
what should I download from b4a sdk manager to resolve the above error? b4a version is 12.80 and B4A Sdk Manager version: 4.10
 
Update to error message :
After installing:
com.google.firebase:firebase-encoders version 17.0.0
I get this error:
Dex merge Error
Error in c:\android\tools\..\extras\b4a_remote\com\google\firebase\firebase-encoders-json\16.1.0\unpacked-firebase-encoders-json-16.1.0\jars\classes.zip:classes.dex:
Type com.google.firebase.encoders.DataEncoder is defined multiple times: c:\android\tools\..\extras\b4a_remote\com\google\firebase\firebase-encoders-json\16.1.0\unpacked-firebase-encoders-json-16.1.0\jars\classes.zip:classes.dex, c:\android\tools\..\extras\b4a_remote\com\google\firebase\firebase-encoders\17.0.0\firebase-encoders-17.0.0.zip:classes.dex
Compilation failed
Do I have to remove anything already installed?
 
Top