Skip to content

Commit

Permalink
Merge pull request google-ai-edge#233 from googlesamples/10.5-facesty…
Browse files Browse the repository at this point in the history
…lizer

10.5 facestylizer
  • Loading branch information
PaulTR authored Sep 14, 2023
2 parents d6e020b + 0cf5ea1 commit e33b33c
Show file tree
Hide file tree
Showing 39 changed files with 1,493 additions and 0 deletions.
15 changes: 15 additions & 0 deletions examples/face_stylizer/android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
1 change: 1 addition & 0 deletions examples/face_stylizer/android/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
59 changes: 59 additions & 0 deletions examples/face_stylizer/android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'de.undercouch.download'
}

android {
namespace 'com.google.mediapipe.examples.facestylizer'
compileSdk 33

defaultConfig {
applicationId "com.google.mediapipe.examples.facestylizer"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
}
}


// import DownloadModels task
project.ext.ASSET_DIR = projectDir.toString() + '/src/main/assets'
project.ext.TEST_ASSETS_DIR = projectDir.toString() + '/src/androidTest/assets'

// Download default models; if you wish to use your own models then
// place them in the "assets" directory and comment out this line.
apply from: 'download_models.gradle'

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'

implementation 'com.google.mediapipe:tasks-vision:0.10.5'
}
35 changes: 35 additions & 0 deletions examples/face_stylizer/android/app/download_models.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
*
* 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
*
* http://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.
*/

task downloadColorInkTask(type: Download) {
src 'https://storage.googleapis.com/mediapipe-models/face_stylizer/blaze_face_stylizer/float32/latest/face_stylizer_color_ink.task'
dest project.ext.ASSET_DIR + '/face_stylizer_color_ink.task'
overwrite false
}

task downloadColorSketchTask(type: Download) {
src 'https://storage.googleapis.com/mediapipe-models/face_stylizer/blaze_face_stylizer/float32/latest/face_stylizer_color_sketch.task'
dest project.ext.ASSET_DIR + '/face_stylizer_color_sketch.task'
overwrite false
}

task downloadOilPainting(type: Download) {
src 'https://storage.googleapis.com/mediapipe-models/face_stylizer/blaze_face_stylizer/float32/latest/face_stylizer_oil_painting.task'
dest project.ext.ASSET_DIR + '/face_stylizer_oil_painting.task'
overwrite false
}

preBuild.dependsOn downloadColorInkTask, downloadColorSketchTask, downloadOilPainting
21 changes: 21 additions & 0 deletions examples/face_stylizer/android/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
*
* 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
*
* http://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.google.mediapipe.examples.facestylizer

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.google.mediapipe.examples.facestylizer", appContext.packageName)
}
}
44 changes: 44 additions & 0 deletions examples/face_stylizer/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2023 The TensorFlow Authors. All Rights Reserved.
~
~ 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
~
~ http://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.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
*
* 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
*
* http://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.google.mediapipe.examples.facestylizer

import android.content.Context
import android.graphics.Bitmap
import android.util.Log
import com.google.mediapipe.framework.image.BitmapImageBuilder
import com.google.mediapipe.tasks.core.BaseOptions
import com.google.mediapipe.tasks.vision.core.RunningMode
import com.google.mediapipe.tasks.vision.facestylizer.FaceStylizer
import com.google.mediapipe.tasks.vision.facestylizer.FaceStylizer.FaceStylizerOptions
import com.google.mediapipe.tasks.vision.facestylizer.FaceStylizerResult

class FaceStylizationHelper(
private val modelPosition: Int,
private val context: Context,
var faceStylizerListener: FaceStylizerListener? = null
) {

private var faceStylizer: FaceStylizer? = null

init {
setupFaceStylizer()
}

private fun setupFaceStylizer() {
val baseOptionsBuilder = BaseOptions.builder()
// Sets the model selection.
baseOptionsBuilder.setModelAssetPath(
when (modelPosition) {
0 -> MODEL_PATH_COLOR_SKETCH
1 -> MODEL_PATH_COLOR_INK
2 -> MODEL_PATH_OIL_PAINTING
else -> throw Throwable("Invalid model type position")
}
)

try {
val baseOptions = baseOptionsBuilder.build()
val optionsBuilder = FaceStylizerOptions.builder()
.setBaseOptions(baseOptions)

val options = optionsBuilder.build()
faceStylizer = FaceStylizer.createFromOptions(context, options)
} catch (e: IllegalStateException) {
faceStylizerListener?.onError(
"Face stylizer failed to initialize. See error logs for " +
"details"
)
Log.e(
TAG,
"Face stylizer failed to load model with error: " + e.message
)
} catch (e: RuntimeException) {
// This occurs if the model being used does not support GPU
faceStylizerListener?.onError(
"Face stylizer failed to initialize. See error logs for " +
"details", GPU_ERROR
)
Log.e(
TAG,
"Face stylizer failed to load model with error: " + e.message
)
}
}

fun stylize(bitmap: Bitmap): ResultBundle {
val mpImage = BitmapImageBuilder(bitmap).build()
var timestampMs = System.currentTimeMillis()
val result = faceStylizer?.stylize(mpImage)
timestampMs = System.currentTimeMillis() - timestampMs

return ResultBundle(result, timestampMs)
}

fun close() {
faceStylizer?.close()
}

// Wraps results from inference, the time it takes for inference to be
// performed.
data class ResultBundle(
val stylizedFace: FaceStylizerResult?,
val inferenceTime: Long,
)

companion object {
const val MODEL_PATH_OIL_PAINTING = "face_stylizer_oil_painting.task"
const val MODEL_PATH_COLOR_INK = "face_stylizer_color_ink.task"
const val MODEL_PATH_COLOR_SKETCH = "face_stylizer_color_sketch.task"
const val OTHER_ERROR = 0
const val GPU_ERROR = 1
private const val TAG = "FaceStylizationHelper"
}

interface FaceStylizerListener {
fun onError(error: String, errorCode: Int = OTHER_ERROR)
}
}
Loading

0 comments on commit e33b33c

Please sign in to comment.