Repository: Kristina-Simakova/arfaces Branch: master Commit: ccdc12669a98 Files: 50 Total size: 64.0 KB Directory structure: gitextract_rgusjmyx/ ├── .gitignore ├── LICENSE ├── README.md ├── app/ │ ├── .gitignore │ ├── build.gradle │ ├── gradle/ │ │ └── wrapper/ │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties │ ├── gradle.properties │ ├── gradlew │ ├── gradlew.bat │ ├── proguard-rules.pro │ ├── sampledata/ │ │ ├── sunglasses.fbx │ │ ├── sunglasses.sfa │ │ ├── yellow_sunglasses.fbx │ │ └── yellow_sunglasses.sfa │ └── src/ │ ├── androidTest/ │ │ └── java/ │ │ └── no/ │ │ └── realitylab/ │ │ └── arface/ │ │ └── ExampleInstrumentedTest.kt │ ├── main/ │ │ ├── AndroidManifest.xml │ │ ├── assets/ │ │ │ ├── sunglasses.sfb │ │ │ └── yellow_sunglasses.sfb │ │ ├── java/ │ │ │ └── no/ │ │ │ └── realitylab/ │ │ │ └── arface/ │ │ │ ├── CustomFaceNode.kt │ │ │ ├── FaceArFragment.kt │ │ │ ├── FaceLandmarksActivity.kt │ │ │ ├── FaceRegionsActivity.kt │ │ │ ├── FilterFace.kt │ │ │ ├── GlassesActivity.kt │ │ │ ├── MainActivity.kt │ │ │ └── MakeupActivity.kt │ │ └── res/ │ │ ├── drawable/ │ │ │ ├── ic_autorenew_black_24dp.xml │ │ │ ├── ic_launcher_background.xml │ │ │ └── rounded_bg.xml │ │ ├── drawable-v24/ │ │ │ └── ic_launcher_foreground.xml │ │ ├── layout/ │ │ │ ├── activity_glasses.xml │ │ │ ├── activity_main.xml │ │ │ ├── activity_makeup.xml │ │ │ ├── activity_regions.xml │ │ │ ├── card_layout.xml │ │ │ └── element_layout.xml │ │ ├── mipmap-anydpi-v26/ │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ └── values/ │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test/ │ └── java/ │ └── no/ │ └── realitylab/ │ └── arface/ │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle/ │ └── wrapper/ │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradle.properties ├── gradlew ├── gradlew.bat └── settings.gradle ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Built application files *.apk *.ap_ # Files for the ART/Dalvik VM *.dex # Java class files *.class # Generated files bin/ gen/ out/ # Gradle files .gradle/ build/ # Local configuration file (sdk path, etc) local.properties # Proguard folder generated by Eclipse proguard/ # Log Files *.log # Android Studio Navigation editor temp files .navigation/ # Android Studio captures folder captures/ # IntelliJ *.iml .idea/ .idea/workspace.xml .idea/tasks.xml .idea/gradle.xml .idea/assetWizardSettings.xml .idea/dictionaries .idea/libraries .idea/caches # Keystore files # Uncomment the following line if you do not want to check your keystore files in. #*.jks # External native build folder generated in Android Studio 2.2 and later .externalNativeBuild # Google Services (e.g. APIs or Firebase) google-services.json # Freeline freeline.py freeline/ freeline_project_description.json # fastlane fastlane/report.xml fastlane/Preview.html fastlane/screenshots fastlane/test_output fastlane/readme.md ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2019 Kristina Simakova Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # Augmented Faces ARCore tutorials Augmented Faces for Android Try-On makeup with Augmented Faces
Blog post -> https://kristisimakova.medium.com/try-on-makeup-with-augmented-faces-d2ade1906f90

Try-On glasses with Augmented Faces
Blog post -> https://www.droid-girl.dev/blog/06gzoflzz25nqobt6ucp8rjzmlj5h6

Instagram - like filter "Which ... are you?" with Augmented Faces
Blog post -> https://www.droid-girl.dev/blog/y3ml70veq0xtpmjjwsf00t1mclhau8

Face Landmarks for ARCore Augmented Faces
Blog post -> https://www.droid-girl.dev/blog/hhgda2z2j6u27pdh95om0xggx42p7x ================================================ FILE: app/.gitignore ================================================ /build ================================================ FILE: app/build.gradle ================================================ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 29 defaultConfig { applicationId "no.realitylab.arface" minSdkVersion 24 targetSdkVersion 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation "com.google.ar.sceneform.ux:sceneform-ux:1.14.0" testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' } apply plugin: 'com.google.ar.sceneform.plugin' sceneform.asset('sampledata/sunglasses.fbx', 'default', 'sampledata/sunglasses.sfa', 'src/main/assets/sunglasses') sceneform.asset('sampledata/yellow_sunglasses.fbx', 'default', 'sampledata/yellow_sunglasses.sfa', 'src/main/assets/yellow_sunglasses') ================================================ FILE: app/gradle/wrapper/gradle-wrapper.properties ================================================ #Fri Dec 27 19:32:10 CET 2019 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.1-all.zip ================================================ FILE: app/gradle.properties ================================================ android.enableJetifier=true android.useAndroidX=true ================================================ FILE: app/gradlew ================================================ #!/usr/bin/env sh ############################################################################## ## ## Gradle start up script for UN*X ## ############################################################################## # Attempt to set APP_HOME # Resolve links: $0 may be a link PRG="$0" # Need this for relative symlinks. while [ -h "$PRG" ] ; do ls=`ls -ld "$PRG"` link=`expr "$ls" : '.*-> \(.*\)$'` if expr "$link" : '/.*' > /dev/null; then PRG="$link" else PRG=`dirname "$PRG"`"/$link" fi done SAVED="`pwd`" cd "`dirname \"$PRG\"`/" >/dev/null APP_HOME="`pwd -P`" cd "$SAVED" >/dev/null APP_NAME="Gradle" APP_BASE_NAME=`basename "$0"` # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. DEFAULT_JVM_OPTS="" # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD="maximum" warn () { echo "$*" } die () { echo echo "$*" echo exit 1 } # OS specific support (must be 'true' or 'false'). cygwin=false msys=false darwin=false nonstop=false case "`uname`" in CYGWIN* ) cygwin=true ;; Darwin* ) darwin=true ;; MINGW* ) msys=true ;; NONSTOP* ) nonstop=true ;; esac CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar # Determine the Java command to use to start the JVM. if [ -n "$JAVA_HOME" ] ; then if [ -x "$JAVA_HOME/jre/sh/java" ] ; then # IBM's JDK on AIX uses strange locations for the executables JAVACMD="$JAVA_HOME/jre/sh/java" else JAVACMD="$JAVA_HOME/bin/java" fi if [ ! -x "$JAVACMD" ] ; then die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME Please set the JAVA_HOME variable in your environment to match the location of your Java installation." fi else JAVACMD="java" which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. Please set the JAVA_HOME variable in your environment to match the location of your Java installation." fi # Increase the maximum file descriptors if we can. if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then MAX_FD_LIMIT=`ulimit -H -n` if [ $? -eq 0 ] ; then if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then MAX_FD="$MAX_FD_LIMIT" fi ulimit -n $MAX_FD if [ $? -ne 0 ] ; then warn "Could not set maximum file descriptor limit: $MAX_FD" fi else warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" fi fi # For Darwin, add options to specify how the application appears in the dock if $darwin; then GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" fi # For Cygwin, switch paths to Windows format before running java if $cygwin ; then APP_HOME=`cygpath --path --mixed "$APP_HOME"` CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` JAVACMD=`cygpath --unix "$JAVACMD"` # We build the pattern for arguments to be converted via cygpath ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` SEP="" for dir in $ROOTDIRSRAW ; do ROOTDIRS="$ROOTDIRS$SEP$dir" SEP="|" done OURCYGPATTERN="(^($ROOTDIRS))" # Add a user-defined pattern to the cygpath arguments if [ "$GRADLE_CYGPATTERN" != "" ] ; then OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" fi # Now convert the arguments - kludge to limit ourselves to /bin/sh i=0 for arg in "$@" ; do CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` else eval `echo args$i`="\"$arg\"" fi i=$((i+1)) done case $i in (0) set -- ;; (1) set -- "$args0" ;; (2) set -- "$args0" "$args1" ;; (3) set -- "$args0" "$args1" "$args2" ;; (4) set -- "$args0" "$args1" "$args2" "$args3" ;; (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; esac fi # Escape application args save () { for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done echo " " } APP_ARGS=$(save "$@") # Collect all arguments for the java command, following the shell quoting and substitution rules eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then cd "$(dirname "$0")" fi exec "$JAVACMD" "$@" ================================================ FILE: app/gradlew.bat ================================================ @if "%DEBUG%" == "" @echo off @rem ########################################################################## @rem @rem Gradle startup script for Windows @rem @rem ########################################################################## @rem Set local scope for the variables with windows NT shell if "%OS%"=="Windows_NT" setlocal set DIRNAME=%~dp0 if "%DIRNAME%" == "" set DIRNAME=. set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. set DEFAULT_JVM_OPTS= @rem Find java.exe if defined JAVA_HOME goto findJavaFromJavaHome set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 if "%ERRORLEVEL%" == "0" goto init echo. echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. echo. echo Please set the JAVA_HOME variable in your environment to match the echo location of your Java installation. goto fail :findJavaFromJavaHome set JAVA_HOME=%JAVA_HOME:"=% set JAVA_EXE=%JAVA_HOME%/bin/java.exe if exist "%JAVA_EXE%" goto init echo. echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% echo. echo Please set the JAVA_HOME variable in your environment to match the echo location of your Java installation. goto fail :init @rem Get command-line arguments, handling Windows variants if not "%OS%" == "Windows_NT" goto win9xME_args :win9xME_args @rem Slurp the command line arguments. set CMD_LINE_ARGS= set _SKIP=2 :win9xME_args_slurp if "x%~1" == "x" goto execute set CMD_LINE_ARGS=%* :execute @rem Setup the command line set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar @rem Execute Gradle "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% :end @rem End local scope for the variables with windows NT shell if "%ERRORLEVEL%"=="0" goto mainEnd :fail rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of rem the _cmd.exe /c_ return code! if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 exit /b 1 :mainEnd if "%OS%"=="Windows_NT" endlocal :omega ================================================ FILE: app/proguard-rules.pro ================================================ # 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 ================================================ FILE: app/sampledata/sunglasses.sfa ================================================ { animations: [ { path: 'sampledata/sunglasses.fbx', }, ], materials: [ { name: '1A1A1A.001', parameters: [ { baseColor: [ 0.101961, 0.101961, 0.101961, 1, ], }, { baseColorMap: null, }, { normalMap: null, }, { interpolatedColor: null, }, { metallic: 0, }, { metallicMap: null, }, { roughness: 1, }, { roughnessMap: null, }, { opacity: null, }, ], source: 'build/sceneform_sdk/default_materials/fbx_material.sfm', }, ], model: { attributes: [ 'Position', 'Orientation', 'BoneIndices', 'BoneWeights', ], collision: {}, file: 'sampledata/sunglasses.fbx', name: 'sunglasses', recenter: 'root', }, version: '0.54:2', } ================================================ FILE: app/sampledata/yellow_sunglasses.sfa ================================================ { animations: [ { path: 'sampledata/yellow_sunglasses.fbx', }, ], materials: [ { name: 'body', parameters: [ { baseColor: [ 0.98101300000000002, 0.67214600000000002, 0, 1, ], }, { baseColorMap: null, }, { normalMap: null, }, { interpolatedColor: null, }, { metallic: 0, }, { metallicMap: null, }, { roughness: 1, }, { roughnessMap: null, }, { opacity: null, }, ], source: 'build/sceneform_sdk/default_materials/fbx_material.sfm', }, ], model: { attributes: [ 'Position', 'TexCoord', 'Orientation', 'BoneIndices', 'BoneWeights', ], collision: {}, file: 'sampledata/yellow_sunglasses.fbx', name: 'yellow_sunglasses', recenter: 'root', }, version: '0.54:2', } ================================================ FILE: app/src/androidTest/java/no/realitylab/arface/ExampleInstrumentedTest.kt ================================================ package no.realitylab.arface 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.getTargetContext() assertEquals("no.realitylab.arface", appContext.packageName) } } ================================================ FILE: app/src/main/AndroidManifest.xml ================================================ ================================================ FILE: app/src/main/java/no/realitylab/arface/CustomFaceNode.kt ================================================ package no.realitylab.arface import android.content.Context import android.widget.ImageView import com.google.ar.core.AugmentedFace import com.google.ar.sceneform.FrameTime import com.google.ar.sceneform.Node import com.google.ar.sceneform.math.Quaternion import com.google.ar.sceneform.math.Vector3 import com.google.ar.sceneform.rendering.ViewRenderable import com.google.ar.sceneform.ux.AugmentedFaceNode class CustomFaceNode(augmentedFace: AugmentedFace?, val context: Context ): AugmentedFaceNode(augmentedFace) { private var eyeNodeLeft: Node? = null private var eyeNodeRight: Node? = null private var mustacheNode: Node? = null companion object { enum class FaceRegion { LEFT_EYE, RIGHT_EYE, MUSTACHE } } override fun onActivate() { super.onActivate() eyeNodeLeft = Node() eyeNodeLeft?.setParent(this) eyeNodeRight = Node() eyeNodeRight?.setParent(this) mustacheNode = Node() mustacheNode?.setParent(this) ViewRenderable.builder() .setView(context, R.layout.element_layout) .build() .thenAccept { uiRenderable: ViewRenderable -> uiRenderable.isShadowCaster = false uiRenderable.isShadowReceiver = false eyeNodeLeft?.renderable = uiRenderable eyeNodeRight?.renderable = uiRenderable } .exceptionally { throwable: Throwable? -> throw AssertionError( "Could not create ui element", throwable ) } ViewRenderable.builder() .setView(context, R.layout.element_layout) .build() .thenAccept { uiRenderable: ViewRenderable -> uiRenderable.isShadowCaster = false uiRenderable.isShadowReceiver = false mustacheNode?.renderable = uiRenderable uiRenderable.view.findViewById(R.id.element_image).setImageResource(R.drawable.mustache) } .exceptionally { throwable: Throwable? -> throw AssertionError( "Could not create ui element", throwable ) } } private fun getRegionPose(region: FaceRegion) : Vector3? { val buffer = augmentedFace?.meshVertices if (buffer != null) { return when (region) { FaceRegion.LEFT_EYE -> Vector3(buffer.get(374 * 3),buffer.get(374 * 3 + 1), buffer.get(374 * 3 + 2)) FaceRegion.RIGHT_EYE -> Vector3(buffer.get(145 * 3),buffer.get(145 * 3 + 1), buffer.get(145 * 3 + 2)) FaceRegion.MUSTACHE -> Vector3(buffer.get(11 * 3), buffer.get(11 * 3 + 1), buffer.get(11 * 3 + 2)) } } return null } override fun onUpdate(frameTime: FrameTime?) { super.onUpdate(frameTime) augmentedFace?.let {face -> getRegionPose(FaceRegion.LEFT_EYE)?.let { eyeNodeLeft?.localPosition = Vector3(it.x, it.y - 0.035f, it.z + 0.015f) eyeNodeLeft?.localScale = Vector3(0.055f, 0.055f, 0.055f) eyeNodeLeft?.localRotation = Quaternion.axisAngle(Vector3(0.0f, 0.0f, 1.0f), -10f) } getRegionPose(FaceRegion.RIGHT_EYE)?.let { eyeNodeRight?.localPosition = Vector3(it.x, it.y - 0.035f, it.z + 0.015f) eyeNodeRight?.localScale = Vector3(0.055f, 0.055f, 0.055f) eyeNodeRight?.localRotation = Quaternion.axisAngle(Vector3(0.0f, 0.0f, 1.0f), 10f) } getRegionPose(FaceRegion.MUSTACHE)?.let { mustacheNode?.localPosition = Vector3(it.x, it.y - 0.035f, it.z + 0.015f) mustacheNode?.localScale = Vector3(0.07f, 0.07f, 0.07f) } } } } ================================================ FILE: app/src/main/java/no/realitylab/arface/FaceArFragment.kt ================================================ package no.realitylab.arface import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.FrameLayout import com.google.ar.core.Config import com.google.ar.core.Session import com.google.ar.sceneform.ux.ArFragment import java.util.* public class FaceArFragment : ArFragment() { override fun getSessionConfiguration(session: Session?): Config { val config = Config(session) config.augmentedFaceMode = Config.AugmentedFaceMode.MESH3D return config } override fun getSessionFeatures(): MutableSet { return EnumSet.of(Session.Feature.FRONT_CAMERA) } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val frameLayout = super.onCreateView(inflater, container, savedInstanceState) as? FrameLayout planeDiscoveryController.hide() planeDiscoveryController.setInstructionView(null) return frameLayout } } ================================================ FILE: app/src/main/java/no/realitylab/arface/FaceLandmarksActivity.kt ================================================ package no.realitylab.arface import android.app.ActivityManager import android.content.Context import android.os.Bundle import android.view.View import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.google.ar.core.ArCoreApk import com.google.ar.core.AugmentedFace import com.google.ar.core.TrackingState import com.google.ar.sceneform.rendering.Renderable import kotlinx.android.synthetic.main.activity_regions.* class FaceLandmarksActivity : AppCompatActivity() { companion object { const val MIN_OPENGL_VERSION = 3.0 } lateinit var arFragment: FaceArFragment var faceNodeMap = HashMap() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) if (!checkIsSupportedDeviceOrFinish()) { return } setContentView(R.layout.activity_regions) arFragment = face_fragment as FaceArFragment button_refresh.visibility = View.GONE val sceneView = arFragment.arSceneView sceneView.cameraStreamRenderPriority = Renderable.RENDER_PRIORITY_FIRST val scene = sceneView.scene scene.addOnUpdateListener { sceneView.session ?.getAllTrackables(AugmentedFace::class.java)?.let { for (f in it) { if (!faceNodeMap.containsKey(f)) { val faceNode = CustomFaceNode(f, this) faceNode.setParent(scene) faceNodeMap.put(f, faceNode) } } // Remove any AugmentedFaceNodes associated with an AugmentedFace that stopped tracking. val iter = faceNodeMap.entries.iterator() while (iter.hasNext()) { val entry = iter.next() val face = entry.key if (face.trackingState == TrackingState.STOPPED) { val faceNode = entry.value faceNode.setParent(null) iter.remove() } } } } } private fun checkIsSupportedDeviceOrFinish() : Boolean { if (ArCoreApk.getInstance().checkAvailability(this) == ArCoreApk.Availability.UNSUPPORTED_DEVICE_NOT_CAPABLE) { Toast.makeText(this, "Augmented Faces requires ARCore", Toast.LENGTH_LONG).show() finish() return false } val openGlVersionString = (getSystemService(Context.ACTIVITY_SERVICE) as? ActivityManager) ?.deviceConfigurationInfo ?.glEsVersion openGlVersionString?.let { s -> if (java.lang.Double.parseDouble(openGlVersionString) < MIN_OPENGL_VERSION) { Toast.makeText(this, "Sceneform requires OpenGL ES 3.0 or later", Toast.LENGTH_LONG) .show() finish() return false } } return true } } ================================================ FILE: app/src/main/java/no/realitylab/arface/FaceRegionsActivity.kt ================================================ package no.realitylab.arface import android.app.ActivityManager import android.content.Context import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.google.ar.core.ArCoreApk import com.google.ar.core.AugmentedFace import com.google.ar.core.TrackingState import com.google.ar.sceneform.rendering.Renderable import kotlinx.android.synthetic.main.activity_regions.* class FaceRegionsActivity : AppCompatActivity() { companion object { const val MIN_OPENGL_VERSION = 3.0 } lateinit var arFragment: FaceArFragment var faceNodeMap = HashMap() var refresh: Boolean = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) if (!checkIsSupportedDeviceOrFinish()) { return } setContentView(R.layout.activity_regions) arFragment = face_fragment as FaceArFragment val sceneView = arFragment.arSceneView sceneView.cameraStreamRenderPriority = Renderable.RENDER_PRIORITY_FIRST val scene = sceneView.scene scene.addOnUpdateListener { sceneView.session ?.getAllTrackables(AugmentedFace::class.java)?.let { for (f in it) { if (!faceNodeMap.containsKey(f)) { val faceNode = FilterFace(f, this) faceNode.setParent(scene) faceNodeMap.put(f, faceNode) } } // Remove any AugmentedFaceNodes associated with an AugmentedFace that stopped tracking. val iter = faceNodeMap.entries.iterator() while (iter.hasNext()) { val entry = iter.next() val face = entry.key if (face.trackingState == TrackingState.STOPPED) { val faceNode = entry.value faceNode.setParent(null) iter.remove() } } } } button_refresh.setOnClickListener { if (!refresh) { startQuiz() } else { refresh() } refresh = !refresh } } private fun startQuiz() { for (face in faceNodeMap.values) { face.animate() } } private fun refresh() { for (face in faceNodeMap.values) { face.refresh() } } private fun checkIsSupportedDeviceOrFinish() : Boolean { if (ArCoreApk.getInstance().checkAvailability(this) == ArCoreApk.Availability.UNSUPPORTED_DEVICE_NOT_CAPABLE) { Toast.makeText(this, "Augmented Faces requires ARCore", Toast.LENGTH_LONG).show() finish() return false } val openGlVersionString = (getSystemService(Context.ACTIVITY_SERVICE) as? ActivityManager) ?.deviceConfigurationInfo ?.glEsVersion openGlVersionString?.let { s -> if (java.lang.Double.parseDouble(openGlVersionString) < MIN_OPENGL_VERSION) { Toast.makeText(this, "Sceneform requires OpenGL ES 3.0 or later", Toast.LENGTH_LONG) .show() finish() return false } } return true } } ================================================ FILE: app/src/main/java/no/realitylab/arface/FilterFace.kt ================================================ package no.realitylab.arface import android.content.Context import android.os.Handler import android.widget.TextView import com.google.ar.core.AugmentedFace import com.google.ar.sceneform.FrameTime import com.google.ar.sceneform.Node import com.google.ar.sceneform.math.Vector3 import com.google.ar.sceneform.rendering.* import com.google.ar.sceneform.ux.AugmentedFaceNode class FilterFace(augmentedFace: AugmentedFace?, val context: Context): AugmentedFaceNode(augmentedFace) { private var cardNode: Node? = null private var textView: TextView? = null private lateinit var mHandler: Handler private lateinit var mRunnable:Runnable val animals = arrayOf("Dog", "Cat", "Tiger", "Frog", "Zebra", "Monkey", "Lion") override fun onActivate() { super.onActivate() cardNode = Node() cardNode?.setParent(this) mHandler = Handler() ViewRenderable.builder() .setView(context, R.layout.card_layout) .build() .thenAccept { uiRenderable: ViewRenderable -> uiRenderable.isShadowCaster = false uiRenderable.isShadowReceiver = false cardNode?.renderable = uiRenderable textView = uiRenderable.view.findViewById(R.id.title) } .exceptionally { throwable: Throwable? -> throw AssertionError( "Could not create ui element", throwable ) } } override fun onUpdate(frameTime: FrameTime?) { super.onUpdate(frameTime) augmentedFace?.let {face -> val rightForehead = face.getRegionPose(AugmentedFace.RegionType.FOREHEAD_RIGHT) val leftForehead = face.getRegionPose(AugmentedFace.RegionType.FOREHEAD_LEFT) val center = face.centerPose cardNode?.worldPosition = Vector3((leftForehead.tx() + rightForehead.tx()) / 2, (leftForehead.ty() + rightForehead.ty()) / 2 + 0.05f , center.tz()) } } fun animate() { val index = (animals.indices).random() val rounds = (2..4).random() var currentIndex = 0 var currentRound = 0 mRunnable = Runnable { textView?.text = animals[currentIndex] currentIndex ++ if (currentIndex == animals.size) { currentIndex = 0 currentRound ++ } if (currentRound == rounds) { textView?.text = animals[index] } else { // Schedule the task to repeat mHandler.postDelayed( mRunnable, // Runnable 100 // Delay in milliseconds ) } } // Schedule the task to repeat mHandler.postDelayed( mRunnable, // Runnable 100 // Delay in milliseconds ) } fun refresh() { textView?.text = context.getText(R.string.quiz_title) } } ================================================ FILE: app/src/main/java/no/realitylab/arface/GlassesActivity.kt ================================================ package no.realitylab.arface import android.app.ActivityManager import android.content.Context import android.net.Uri import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.google.ar.core.ArCoreApk import com.google.ar.core.AugmentedFace import com.google.ar.core.TrackingState import com.google.ar.sceneform.rendering.ModelRenderable import com.google.ar.sceneform.rendering.Renderable import com.google.ar.sceneform.rendering.Texture import com.google.ar.sceneform.ux.AugmentedFaceNode import kotlinx.android.synthetic.main.activity_glasses.* import java.util.ArrayList class GlassesActivity : AppCompatActivity() { companion object { const val MIN_OPENGL_VERSION = 3.0 } lateinit var arFragment: FaceArFragment private var faceMeshTexture: Texture? = null private var glasses: ArrayList = ArrayList() private var faceRegionsRenderable: ModelRenderable? = null var faceNodeMap = HashMap() private var index: Int = 0 private var changeModel: Boolean = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) if (!checkIsSupportedDeviceOrFinish()) { return } setContentView(R.layout.activity_glasses) button_next.setOnClickListener { changeModel = !changeModel index++ if (index > glasses.size - 1) { index = 0 } faceRegionsRenderable = glasses.get(index) } arFragment = face_fragment as FaceArFragment Texture.builder() .setSource(this, R.drawable.makeup) .build() .thenAccept { texture -> faceMeshTexture = texture } ModelRenderable.builder() .setSource(this, Uri.parse("yellow_sunglasses.sfb")) .build() .thenAccept { modelRenderable -> glasses.add(modelRenderable) faceRegionsRenderable = modelRenderable modelRenderable.isShadowCaster = false modelRenderable.isShadowReceiver = false } ModelRenderable.builder() .setSource(this, Uri.parse("sunglasses.sfb")) .build() .thenAccept { modelRenderable -> glasses.add(modelRenderable) modelRenderable.isShadowCaster = false modelRenderable.isShadowReceiver = false } val sceneView = arFragment.arSceneView sceneView.cameraStreamRenderPriority = Renderable.RENDER_PRIORITY_FIRST val scene = sceneView.scene scene.addOnUpdateListener { if (faceRegionsRenderable != null) { sceneView.session ?.getAllTrackables(AugmentedFace::class.java)?.let { for (f in it) { if (!faceNodeMap.containsKey(f)) { val faceNode = AugmentedFaceNode(f) faceNode.setParent(scene) faceNode.faceRegionsRenderable = faceRegionsRenderable faceNodeMap.put(f, faceNode) } else if (changeModel) { faceNodeMap.getValue(f).faceRegionsRenderable = faceRegionsRenderable } } changeModel = false // Remove any AugmentedFaceNodes associated with an AugmentedFace that stopped tracking. val iter = faceNodeMap.entries.iterator() while (iter.hasNext()) { val entry = iter.next() val face = entry.key if (face.trackingState == TrackingState.STOPPED) { val faceNode = entry.value faceNode.setParent(null) iter.remove() } } } } } } fun checkIsSupportedDeviceOrFinish() : Boolean { if (ArCoreApk.getInstance().checkAvailability(this) == ArCoreApk.Availability.UNSUPPORTED_DEVICE_NOT_CAPABLE) { Toast.makeText(this, "Augmented Faces requires ARCore", Toast.LENGTH_LONG).show() finish() return false } val openGlVersionString = (getSystemService(Context.ACTIVITY_SERVICE) as? ActivityManager) ?.deviceConfigurationInfo ?.glEsVersion openGlVersionString?.let { s -> if (java.lang.Double.parseDouble(openGlVersionString) < MIN_OPENGL_VERSION) { Toast.makeText(this, "Sceneform requires OpenGL ES 3.0 or later", Toast.LENGTH_LONG) .show() finish() return false } } return true } } ================================================ FILE: app/src/main/java/no/realitylab/arface/MainActivity.kt ================================================ package no.realitylab.arface import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) button_makeup.setOnClickListener { startActivity(Intent(this, MakeupActivity::class.java)) } button_glasses.setOnClickListener { startActivity(Intent(this, GlassesActivity::class.java)) } button_regions.setOnClickListener { startActivity(Intent(this, FaceRegionsActivity::class.java)) } button_face_landmarks.setOnClickListener { startActivity(Intent(this, FaceLandmarksActivity::class.java)) } } } ================================================ FILE: app/src/main/java/no/realitylab/arface/MakeupActivity.kt ================================================ package no.realitylab.arface import android.app.ActivityManager import android.content.Context import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.google.ar.core.ArCoreApk import com.google.ar.core.AugmentedFace import com.google.ar.core.TrackingState import com.google.ar.sceneform.rendering.Renderable import com.google.ar.sceneform.rendering.Texture import com.google.ar.sceneform.ux.AugmentedFaceNode import kotlinx.android.synthetic.main.activity_makeup.* class MakeupActivity : AppCompatActivity() { companion object { const val MIN_OPENGL_VERSION = 3.0 } lateinit var arFragment: FaceArFragment private var faceMeshTexture: Texture? = null var faceNodeMap = HashMap() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) if (!checkIsSupportedDeviceOrFinish()) { return } setContentView(R.layout.activity_makeup) arFragment = face_fragment as FaceArFragment Texture.builder() .setSource(this, R.drawable.makeup) .build() .thenAccept { texture -> faceMeshTexture = texture } val sceneView = arFragment.arSceneView sceneView.cameraStreamRenderPriority = Renderable.RENDER_PRIORITY_FIRST val scene = sceneView.scene scene.addOnUpdateListener { faceMeshTexture.let { sceneView.session ?.getAllTrackables(AugmentedFace::class.java)?.let { for (f in it) { if (!faceNodeMap.containsKey(f)) { val faceNode = AugmentedFaceNode(f) faceNode.setParent(scene) faceNode.faceMeshTexture = faceMeshTexture faceNodeMap.put(f, faceNode) } } // Remove any AugmentedFaceNodes associated with an AugmentedFace that stopped tracking. val iter = faceNodeMap.entries.iterator() while (iter.hasNext()) { val entry = iter.next() val face = entry.key if (face.trackingState == TrackingState.STOPPED) { val faceNode = entry.value faceNode.setParent(null) iter.remove() } } } } } } fun checkIsSupportedDeviceOrFinish() : Boolean { if (ArCoreApk.getInstance().checkAvailability(this) == ArCoreApk.Availability.UNSUPPORTED_DEVICE_NOT_CAPABLE) { Toast.makeText(this, "Augmented Faces requires ARCore", Toast.LENGTH_LONG).show() finish() return false } val openGlVersionString = (getSystemService(Context.ACTIVITY_SERVICE) as? ActivityManager) ?.deviceConfigurationInfo ?.glEsVersion openGlVersionString?.let { s -> if (java.lang.Double.parseDouble(openGlVersionString) < MIN_OPENGL_VERSION) { Toast.makeText(this, "Sceneform requires OpenGL ES 3.0 or later", Toast.LENGTH_LONG) .show() finish() return false } } return true } } ================================================ FILE: app/src/main/res/drawable/ic_autorenew_black_24dp.xml ================================================ ================================================ FILE: app/src/main/res/drawable/ic_launcher_background.xml ================================================ ================================================ FILE: app/src/main/res/drawable/rounded_bg.xml ================================================ ================================================ FILE: app/src/main/res/drawable-v24/ic_launcher_foreground.xml ================================================ ================================================ FILE: app/src/main/res/layout/activity_glasses.xml ================================================ ================================================ FILE: app/src/main/res/layout/activity_main.xml ================================================