'Running Python scripts in Java class activity in Android Studio with Chaquopy

I am trying to utilize some python scripts in an Android Application (Java). I have configured Chaquopy following the instructions on their website. https://chaquo.com/chaquopy/doc/current/android.html

I have found an example of how to execute python scripts in a Kotlin application but I am struggling to figure this out in Java.

If I understand correctly, the python script (.py) is stored in src/main/python and one can call this script from another activity and display the results in that same calling activity.

The example in Kotlin:

val python = Python.getInstance()
val pythonFile = python.getModule("helloworldscript")
val helloWorldString = pythonFile.callAttr("helloworld")
hello_textview.text = helloWorldString.toString()

I want to execute the following python script:

import os
import face_recognition
def cmd2():
    os.system("face_recognition ./event_faces/ ./event_images/")

This commandline tool from face_recognition allows the user to run facial recognition on a directory of images and compares them to a directory of known faces. The result is a printout of the files processed with any known or unknown faces being appended to the filename. enter image description here

My Goal is to execute this python script and display the output in the activity (i.e textView) Does anyone know if it is possible to use face_recognition in Android Studio with Chaquopy? It is not listed so I am having some doubts https://chaquo.com/pypi-7.0/ Is it possible to make os. type calls in android application?

ADDITIONALLY If anyone is aware of a way to perform facial recognition on a directory of images in Java without using python's face_recognition, please let me know. I have attempted find a library like face_recognition for java but have not been successful. The face_recognition library from Python has been working very well for me but now as I am trying to port my python scripts into a Android Studio project things are becoming a little tricky.

My code thus far: Android Manifest

 <application
        android:name="com.chaquo.python.android.PyApplication"

app Gradle

apply plugin: 'com.android.application'
apply plugin: 'com.chaquo.python'

android {
    compileSdkVersion 29

    defaultConfig {
        python{
            staticProxy "bulk.py"
            pip{
                install "dlib"
                install "opencv-python"
                install "opencv-contrib-python"
                install "face_recognition"
                install "pillow"
                install "numpy"
                install "cv2"
                install "os"
            }
        }
        applicationId "com.projectdevelopment.faces"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        ndk {
            abiFilters "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }

project gradle

buildscript {
    repositories {
        google()
        jcenter()
        maven { url "https://chaquo.com/maven" }

    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.0.0"
        classpath 'com.google.gms:google-services:4.3.3'
        classpath "com.chaquo.python:gradle:8.0.0"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

Activity executing the Python script

python.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //call python script and return output to this activity
            }
        });


Solution 1:[1]

I've heard from quite a few people who've used face_recognition successfully on Android. It's a pure-Python package, so it can be installed directly from PyPI and doesn't need to be in Chaquopy's own package repository.

However, Chaquopy doesn't come with a Python executable, so running a command-line script with os.system is unlikely to work. Instead, you can just call the face_recognition Python API, which has many examples on its own website.

It looks like the closest equivalent to running the face_recognition script is to import face_recognition.face_recognition_cli and call the main function, possibly after setting up sys.argv with your command line.

Any text printed to standard output will go to Logcat, which you can view in Android Studio. If you'd like to see it on the device screen as well, the easiest way is to adapt the console app template.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mhsmith