Skip to content

Commit

Permalink
added final after writing codelab
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulTR committed Oct 24, 2023
1 parent 9fc15ba commit 2935e39
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

// Step 1 - Add dependency
implementation 'com.google.mediapipe:tasks-vision-image-generator:latest.release'
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import com.google.mediapipe.tasks.vision.imagegenerator.ImageGenerator.Condition
import com.google.mediapipe.tasks.vision.imagegenerator.ImageGenerator.ConditionOptions.FaceConditionOptions
import com.google.mediapipe.tasks.vision.imagegenerator.ImageGenerator.ImageGeneratorOptions


class ImageGenerationHelper(
val context: Context
) {
Expand All @@ -25,16 +24,24 @@ class ImageGenerationHelper(

fun initializeImageGenerator(modelPath: String) {
// Step 2 - initialize the image generator
val options = ImageGeneratorOptions.builder()
.setImageGeneratorModelDirectory(modelPath)
.build()

imageGenerator = ImageGenerator.createFromOptions(context, options)
}

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


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

fun execute(showResult: Boolean): Bitmap {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MainActivity : AppCompatActivity() {
}

private lateinit var binding: ActivityMainBinding
private val viewModel: DiffusionViewModel by viewModels()
private val viewModel: MainViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

class DiffusionViewModel : ViewModel() {
class MainViewModel : ViewModel() {
private val _uiState = MutableStateFlow(UiState())
private var helper: ImageGenerationHelper? = null
val uiState: StateFlow<UiState> = _uiState

private val MODEL_PATH = ""// Step 6 - set model path
private val MODEL_PATH = "/data/local/tmp/image_generator/bins/" // Step 6 - set model path

fun updateDisplayIteration(displayIteration: Int?) {
_uiState.update { it.copy(displayIteration = displayIteration) }
Expand Down Expand Up @@ -122,8 +122,27 @@ class DiffusionViewModel : ViewModel() {
// if display option is final, use generate method, else use execute method
if (uiState.value.displayOptions == DisplayOptions.FINAL) {
// Step 7 - Generate without showing iterations
val result = helper?.generate(prompt, iteration, seed)
_uiState.update {
it.copy(outputBitmap = result)
}
} else {
// Step 8 - Generate with showing iterations
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)",
)
}
}
}
}
_uiState.update {
it.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import com.google.mediapipe.tasks.vision.imagegenerator.ImageGenerator.Condition
import com.google.mediapipe.tasks.vision.imagegenerator.ImageGenerator.ConditionOptions.FaceConditionOptions
import com.google.mediapipe.tasks.vision.imagegenerator.ImageGenerator.ImageGeneratorOptions


class ImageGenerationHelper(
val context: Context
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

class DiffusionViewModel : ViewModel() {
class MainViewModel : ViewModel() {
private val _uiState = MutableStateFlow(UiState())
private var helper: ImageGenerationHelper? = null
val uiState: StateFlow<UiState> = _uiState
Expand Down

0 comments on commit 2935e39

Please sign in to comment.