-
Notifications
You must be signed in to change notification settings - Fork 147
Migrate Magic Selfie to Nano Banana2 #157
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
Open
lethargicpanda
wants to merge
2
commits into
main
Choose a base branch
from
te/selfie-nanobanana
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,87 +16,34 @@ | |
| package com.android.ai.samples.magicselfie.data | ||
|
|
||
| import android.graphics.Bitmap | ||
| import android.graphics.Canvas | ||
| import android.graphics.Paint | ||
| import com.google.firebase.Firebase | ||
| import com.google.firebase.ai.ai | ||
| import com.google.firebase.ai.type.GenerativeBackend | ||
| import com.google.firebase.ai.type.ImagenAspectRatio | ||
| import com.google.firebase.ai.type.ImagenGenerationConfig | ||
| import com.google.firebase.ai.type.ImagenImageFormat | ||
| import com.google.firebase.ai.type.PublicPreviewAPI | ||
| import com.google.mlkit.vision.common.InputImage | ||
| import com.google.mlkit.vision.segmentation.subject.SubjectSegmentation | ||
| import com.google.mlkit.vision.segmentation.subject.SubjectSegmenterOptions | ||
| import com.google.firebase.ai.type.ResponseModality | ||
| import com.google.firebase.ai.type.asImageOrNull | ||
| import com.google.firebase.ai.type.content | ||
| import com.google.firebase.ai.type.generationConfig | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
| import kotlin.coroutines.suspendCoroutine | ||
| import kotlin.math.roundToInt | ||
|
|
||
| @Singleton | ||
| class MagicSelfieRepository @Inject constructor() { | ||
| @OptIn(PublicPreviewAPI::class) | ||
| private val imagenModel = Firebase.ai(backend = GenerativeBackend.vertexAI()).imagenModel( | ||
| modelName = "imagen-4.0-generate-001", | ||
| generationConfig = ImagenGenerationConfig( | ||
| numberOfImages = 1, | ||
| aspectRatio = ImagenAspectRatio.PORTRAIT_3x4, | ||
| imageFormat = ImagenImageFormat.jpeg(compressionQuality = 75), | ||
| ), | ||
| ) | ||
|
|
||
| private val subjectSegmenter = SubjectSegmentation.getClient( | ||
| SubjectSegmenterOptions.Builder() | ||
| .enableForegroundBitmap() | ||
| .build(), | ||
| ) | ||
|
|
||
| suspend fun generateForegroundBitmap(bitmap: Bitmap): Bitmap { | ||
| val image = InputImage.fromBitmap(bitmap, 0) | ||
| return suspendCoroutine { continuation -> | ||
| subjectSegmenter.process(image) | ||
| .addOnSuccessListener { | ||
| it.foregroundBitmap?.let { foregroundBitmap -> | ||
| continuation.resumeWith(Result.success(foregroundBitmap)) | ||
| } | ||
| } | ||
| .addOnFailureListener { | ||
| continuation.resumeWith(Result.failure(it)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @OptIn(PublicPreviewAPI::class) | ||
| suspend fun generateBackground(prompt: String): Bitmap { | ||
| val imageResponse = imagenModel.generateImages( | ||
| prompt = prompt, | ||
| private val generativeModel by lazy { | ||
| Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel( | ||
| modelName = "gemini-3.1-flash-image-preview", | ||
| generationConfig = generationConfig { | ||
| responseModalities = listOf(ResponseModality.TEXT, ResponseModality.IMAGE) | ||
| } | ||
| ) | ||
| val image = imageResponse.images.first() | ||
| return image.asBitmap() | ||
| } | ||
|
|
||
| fun combineBitmaps(foreground: Bitmap, background: Bitmap): Bitmap { | ||
| val height = background.height | ||
| val width = background.width | ||
|
|
||
| val resultBitmap = Bitmap.createBitmap(width, height, background.config!!) | ||
| val canvas = Canvas(resultBitmap) | ||
| val paint = Paint() | ||
| canvas.drawBitmap(background, 0f, 0f, paint) | ||
|
|
||
| var foregroundHeight = foreground.height | ||
| var foregroundWidth = foreground.width | ||
| val ratio = foregroundWidth.toFloat() / foregroundHeight.toFloat() | ||
|
|
||
| foregroundHeight = height | ||
| foregroundWidth = (foregroundHeight * ratio).roundToInt() | ||
|
|
||
| val scaledForeground = Bitmap.createScaledBitmap(foreground, foregroundWidth, foregroundHeight, false) | ||
|
|
||
| val left = (width - scaledForeground.width) / 2f | ||
| val top = (height - scaledForeground.height.toFloat()) | ||
| canvas.drawBitmap(scaledForeground, left, top, paint) | ||
|
|
||
| return resultBitmap | ||
| suspend fun generateMagicSelfie(bitmap: Bitmap, prompt: String): Bitmap { | ||
| val multimodalPrompt = content { | ||
| image(bitmap) | ||
| text("Change the background of this image to $prompt") | ||
lethargicpanda marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| val response = generativeModel.generateContent(multimodalPrompt) | ||
| return response.candidates.firstOrNull()?.content?.parts?.firstNotNullOfOrNull { it.asImageOrNull() } | ||
| ?: throw Exception("No image generated") | ||
lethargicpanda marked this conversation as resolved.
Show resolved
Hide resolved
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we pass the error 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.