-
Notifications
You must be signed in to change notification settings - Fork 160
Add an option to create a Sticker of the Android #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c7f1f49
Add initial Sticker option for the export.
riggaroo 90c8d5e
Merge branch 'main' into feature/sticker
riggaroo ff4bfcf
Add Sticker feature to remove background of image
riggaroo a15102f
Add Sticker feature to remove background of image
riggaroo 7875242
Fix background clipping
riggaroo c90dbad
Add downloading of the Subject Segmentation module on demand.
riggaroo 3d59b15
Switch to suspendCoroutine
riggaroo 8f022df
Code Cleanup
riggaroo 27c1a81
Code Cleanup
riggaroo c5b7587
Add Sticker test
riggaroo aafbba3
Switch to cancellableCoroutines, move the InstallHelper listener outs…
riggaroo 3b8f82c
PR Feedback
riggaroo ccf6a04
Merge branch 'main' into feature/sticker
riggaroo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
core/network/src/main/java/com/android/developers/androidify/di/OnDeviceModule.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2025 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.android.developers.androidify.di | ||
|
||
import android.content.Context | ||
import com.google.android.gms.common.moduleinstall.ModuleInstall | ||
import com.google.android.gms.common.moduleinstall.ModuleInstallClient | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object OnDeviceModule { | ||
@Provides | ||
fun provideModuleInstallClient(@ApplicationContext context: Context): ModuleInstallClient { | ||
return ModuleInstall.getClient(context) | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
...k/src/main/java/com/android/developers/androidify/ondevice/LocalSegmentationDataSource.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Copyright 2025 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.android.developers.androidify.ondevice | ||
|
||
import android.graphics.Bitmap | ||
import android.util.Log | ||
import com.google.android.gms.common.moduleinstall.InstallStatusListener | ||
import com.google.android.gms.common.moduleinstall.ModuleInstallClient | ||
import com.google.android.gms.common.moduleinstall.ModuleInstallRequest | ||
import com.google.android.gms.common.moduleinstall.ModuleInstallStatusUpdate | ||
import com.google.android.gms.common.moduleinstall.ModuleInstallStatusUpdate.InstallState.STATE_CANCELED | ||
import com.google.android.gms.common.moduleinstall.ModuleInstallStatusUpdate.InstallState.STATE_FAILED | ||
import com.google.mlkit.vision.common.InputImage | ||
import com.google.mlkit.vision.segmentation.subject.SubjectSegmentation | ||
import com.google.mlkit.vision.segmentation.subject.SubjectSegmenterOptions | ||
import kotlinx.coroutines.CancellableContinuation | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import javax.inject.Inject | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.resumeWithException | ||
|
||
interface LocalSegmentationDataSource { | ||
suspend fun removeBackground(bitmap: Bitmap): Bitmap | ||
} | ||
|
||
class LocalSegmentationDataSourceImpl @Inject constructor( | ||
private val moduleInstallClient: ModuleInstallClient | ||
) : LocalSegmentationDataSource { | ||
private val segmenter by lazy { | ||
val options = SubjectSegmenterOptions.Builder() | ||
.enableForegroundBitmap() | ||
.build() | ||
SubjectSegmentation.getClient(options) | ||
} | ||
|
||
private suspend fun isSubjectSegmentationModuleInstalled(): Boolean { | ||
val areModulesAvailable = | ||
suspendCancellableCoroutine { continuation -> | ||
moduleInstallClient.areModulesAvailable(segmenter) | ||
.addOnSuccessListener { | ||
continuation.resume(it.areModulesAvailable()) | ||
} | ||
.addOnFailureListener { | ||
continuation.resumeWithException(it) | ||
} | ||
} | ||
return areModulesAvailable | ||
} | ||
private class CustomInstallStatusListener( | ||
val continuation: CancellableContinuation<Boolean> | ||
) : InstallStatusListener { | ||
|
||
override fun onInstallStatusUpdated(update: ModuleInstallStatusUpdate) { | ||
Log.d("LocalSegmentationDataSource", "Download progress: ${update.installState}.. ${continuation.hashCode()} ${continuation.isActive}") | ||
if (!continuation.isActive) return | ||
if (update.installState == ModuleInstallStatusUpdate.InstallState.STATE_COMPLETED) { | ||
continuation.resume(true) | ||
} else if (update.installState == STATE_FAILED || update.installState == STATE_CANCELED) { | ||
continuation.resumeWithException( | ||
ImageSegmentationException("Module download failed or was canceled. State: ${update.installState}") | ||
) | ||
} else { | ||
Log.d("LocalSegmentationDataSource", "State update: ${update.installState}") | ||
} | ||
} | ||
} | ||
private suspend fun installSubjectSegmentationModule(): Boolean { | ||
val result = suspendCancellableCoroutine { continuation -> | ||
val listener = CustomInstallStatusListener(continuation) | ||
val moduleInstallRequest = ModuleInstallRequest.newBuilder() | ||
.addApi(segmenter) | ||
.setListener(listener) | ||
.build() | ||
|
||
moduleInstallClient | ||
.installModules(moduleInstallRequest) | ||
.addOnFailureListener { | ||
Log.e("LocalSegmentationDataSource", "Failed to download module", it) | ||
continuation.resumeWithException(it) | ||
} | ||
.addOnCompleteListener { | ||
Log.d("LocalSegmentationDataSource", "Successfully triggered download - await download progress updates") | ||
} | ||
} | ||
return result | ||
} | ||
|
||
override suspend fun removeBackground(bitmap: Bitmap): Bitmap { | ||
val areModulesAvailable = isSubjectSegmentationModuleInstalled() | ||
|
||
if (!areModulesAvailable) { | ||
Log.d("LocalSegmentationDataSource", "Modules not available - downloading") | ||
val result = installSubjectSegmentationModule() | ||
if (!result) { | ||
throw Exception("Failed to download module") | ||
riggaroo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} else { | ||
Log.d("LocalSegmentationDataSource", "Modules available") | ||
} | ||
val image = InputImage.fromBitmap(bitmap, 0) | ||
return suspendCancellableCoroutine { continuation -> | ||
segmenter.process(image) | ||
.addOnSuccessListener { result -> | ||
if (result.foregroundBitmap != null) { | ||
continuation.resume(result.foregroundBitmap!!) | ||
} else { | ||
continuation.resumeWithException(ImageSegmentationException("Subject not found")) | ||
} | ||
} | ||
.addOnFailureListener { e -> | ||
Log.e("LocalSegmentationDataSource", "Exception while executing background removal", e) | ||
continuation.resumeWithException(e) | ||
} | ||
} | ||
} | ||
} | ||
|
||
class ImageSegmentationException(message: String? = null): Exception(message) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class is used as a singleton right?
segmenter
has a close function if not that should probably be called if you are creating and destroying this classUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its not closed off in the official sample for it - so i dont think we need to call it , also haven't seen any leaks from LeakCanary for it. https://github.com/googlesamples/mlkit/blob/80d6bc64a0b88e18666f26feee0219995f1a86b2/android/vision-quickstart/app/src/main/java/com/google/mlkit/vision/demo/kotlin/subjectsegmenter/SubjectSegmenterProcessor.kt