Skip to content

Commit

Permalink
fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulTR committed Oct 24, 2023
1 parent aff6062 commit 9fc15ba
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ android {
}
}

// Downloads the TFLite and Task files used for plugins
project.ext.ASSET_DIR = projectDir.toString() + '/src/main/assets'

dependencies {

implementation 'androidx.core:core-ktx:1.8.0'
Expand All @@ -48,5 +45,6 @@ dependencies {
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation 'com.google.mediapipe:tasks-vision-image-generator:0.10.5.2'

// Step 1 - Add dependency
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,45 +23,23 @@ class ImageGenerationHelper(

lateinit var imageGenerator: ImageGenerator

// Setup image generation model with output size, iteration
fun initializeImageGenerator(modelPath: String) {
val options = ImageGeneratorOptions.builder()
.setImageGeneratorModelDirectory(modelPath)
.build()

imageGenerator = ImageGenerator.createFromOptions(context, options)
// Step 2 - initialize the image generator
}

// Set input prompt, iteration, seed
fun setInput(prompt: String, iteration: Int, seed: Int) {
imageGenerator.setInputs(prompt, iteration, seed)
// Step 3 - accept inputs
}


fun generate(prompt: String, iteration: Int, seed: Int): Bitmap {
val result = imageGenerator.generate(prompt, iteration, seed)
val bitmap = BitmapExtractor.extract(result?.generatedImage())
return bitmap
// Step 4 - generate without showing iterations
return Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888)
}

fun execute(showResult: Boolean): Bitmap {
// execute image generation model
val result = imageGenerator.execute(showResult)

if (result == null || result.generatedImage() == null) {
return Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888)
.apply {
val canvas = Canvas(this)
val paint = Paint()
paint.color = Color.WHITE
canvas.drawPaint(paint)
}
}

val bitmap =
BitmapExtractor.extract(result.generatedImage())

return bitmap
// Step 5 - generate with iterations
return Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888)
}

fun close() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class DiffusionViewModel : ViewModel() {
private val _uiState = MutableStateFlow(UiState())
private var helper: ImageGenerationHelper? = null
val uiState: StateFlow<UiState> = _uiState
private val MODEL_PATH = "/data/local/tmp/image_generator/bins/"

private val MODEL_PATH = ""// Step 6 - set model path

fun updateDisplayIteration(displayIteration: Int?) {
_uiState.update { it.copy(displayIteration = displayIteration) }
Expand Down Expand Up @@ -120,26 +121,9 @@ class DiffusionViewModel : ViewModel() {
val startTime = System.currentTimeMillis()
// if display option is final, use generate method, else use execute method
if (uiState.value.displayOptions == DisplayOptions.FINAL) {
val result = helper?.generate(prompt, iteration, seed)
_uiState.update {
it.copy(outputBitmap = result)
}
// Step 7 - Generate without showing iterations
} else {
helper?.setInput(prompt, iteration, seed)
for (step in 0 until iteration) {
isDisplayStep =
(displayIteration > 0 && ((step + 1) % displayIteration == 0))
val result = helper?.execute(isDisplayStep)

if (isDisplayStep) {
_uiState.update {
it.copy(
outputBitmap = result,
generatingMessage = "Generating... (${step + 1}/$iteration)",
)
}
}
}
// Step 8 - Generate with showing iterations
}
_uiState.update {
it.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ plugins {
id 'com.android.application' version '8.0.0' apply false
id 'com.android.library' version '8.0.0' apply false
id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
id 'de.undercouch.download' version '4.1.2' apply false
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ android {
}
}

// Downloads the TFLite and Task files used for plugins
project.ext.ASSET_DIR = projectDir.toString() + '/src/main/assets'

dependencies {

implementation 'androidx.core:core-ktx:1.8.0'
Expand All @@ -48,5 +45,6 @@ dependencies {
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation 'com.google.mediapipe:tasks-vision-image-generator:0.10.5.2'

// Step 1 - Add dependency
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ class ImageGenerationHelper(
lateinit var imageGenerator: ImageGenerator

fun initializeImageGenerator(modelPath: String) {
// Step 1 - initialize the image generator
// Step 2 - initialize the image generator
}

fun setInput(prompt: String, iteration: Int, seed: Int) {
// Step 2 - accept inputs
// Step 3 - accept inputs
}


fun generate(prompt: String, iteration: Int, seed: Int): Bitmap {
// Step 3 - generate without showing iterations
// Step 4 - generate without showing iterations
return Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888)
}

fun execute(showResult: Boolean): Bitmap {
// Step 4 - generate with iterations
// Step 5 - generate with iterations
return Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DiffusionViewModel : ViewModel() {
private var helper: ImageGenerationHelper? = null
val uiState: StateFlow<UiState> = _uiState

private val MODEL_PATH = ""// Step 5 - set model path
private val MODEL_PATH = ""// Step 6 - set model path

fun updateDisplayIteration(displayIteration: Int?) {
_uiState.update { it.copy(displayIteration = displayIteration) }
Expand Down Expand Up @@ -121,9 +121,9 @@ class DiffusionViewModel : ViewModel() {
val startTime = System.currentTimeMillis()
// if display option is final, use generate method, else use execute method
if (uiState.value.displayOptions == DisplayOptions.FINAL) {
// Step 6 - Generate without showing iterations
// Step 7 - Generate without showing iterations
} else {
// Step 7 - Generate with showing iterations
// Step 8 - Generate with showing iterations
}
_uiState.update {
it.copy(
Expand Down
1 change: 0 additions & 1 deletion codelabs/image_generation_basic/android/start/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ plugins {
id 'com.android.application' version '8.0.0' apply false
id 'com.android.library' version '8.0.0' apply false
id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
id 'de.undercouch.download' version '4.1.2' apply false
}

0 comments on commit 9fc15ba

Please sign in to comment.