forked from google-ai-edge/mediapipe-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request google-ai-edge#233 from googlesamples/10.5-facesty…
…lizer 10.5 facestylizer
- Loading branch information
Showing
39 changed files
with
1,493 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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 |
This file contains 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 @@ | ||
/build |
This file contains 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,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' | ||
} |
This file contains 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,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 |
This file contains 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,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 |
39 changes: 39 additions & 0 deletions
39
...rc/androidTest/java/com/google/mediapipe/examples/facestylizer/ExampleInstrumentedTest.kt
This file contains 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,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
44
examples/face_stylizer/android/app/src/main/AndroidManifest.xml
This file contains 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,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> |
113 changes: 113 additions & 0 deletions
113
...oid/app/src/main/java/com/google/mediapipe/examples/facestylizer/FaceStylizationHelper.kt
This file contains 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,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) | ||
} | ||
} |
Oops, something went wrong.