简体   繁体   English

在 Java class 活动中运行 Python 脚本与 ChaquCopyZ4

[英]Running Python scripts in Java class activity in Android Studio with Chaquopy

I am trying to utilise some python scripts in an Android Application (Java).我正在尝试在 Android 应用程序(Java)中使用一些 python 脚本。 I have configured Chaquopy following the instructions on their website.我已经按照他们网站上的说明配置了 Chaquopy。 https://chaquo.com/chaquopy/doc/current/android.html https://chaquo.com/chaquopy/doc/current/android.html

I have found an example of how to execute python scripts in a kotlin application https://www.ericdecanini.com/2019/10/21/run-python-scripts-in-the-android-front-end-with-chaquopy/ but I am struggling to figure this out in Java. I have found an example of how to execute python scripts in a kotlin application https://www.ericdecanini.com/2019/10/21/run-python-scripts-in-the-android-front-end-with-chaquopy /但我很难在 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.如果我理解正确,python 脚本 (.py) 存储在 src/main/python 中,可以从另一个活动调用此脚本并在同一调用活动中显示结果。

The example in kotlin: 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:我想执行以下 python 脚本:

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. face_recognition 中的这个命令行工具允许用户在图像目录上运行面部识别,并将它们与已知人脸目录进行比较。 The result is a printout of the files processed with any known or unknown faces being appended to the filename.结果是处理的文件的打印输出,其中任何已知或未知的面都附加到文件名。 在此处输入图像描述

My Goal is to execute this python script and display the output in the activity (ie textView) Does anyone know if it is possible to use face_recognition in Android Studio with Chaquopy?我的目标是执行这个 python 脚本并在活动中显示 output(即 textView)有谁知道是否可以在 Android Studio 中使用 face_recognition 与 Chaquopop87846DZ? It is not listed so I am having some doubts https://chaquo.com/pypi-7.0/ Is it possible to make os.它没有列出,所以我有一些疑问https://chaquo.com/pypi-7.0/是否可以制作操作系统。 type calls in android application?在 android 应用程序中键入调用?

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.另外,如果有人知道在不使用 python 的 face_recognition 的情况下对 Java 中的图像目录执行面部识别的方法,请告诉我。 I have attempted find a library like face_recognition for java but have not been successful.我试图为 java 找到一个类似 face_recognition 的库,但没有成功。 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. Python 的 face_recognition 库对我来说运行良好,但现在我正试图将我的 python 脚本移植到 Android Studio 项目中,事情变得有点棘手。

My code thus far: Android Manifest到目前为止我的代码:Android Manifest

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

app Gradle应用 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项目 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 脚本的活动

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

Thanks in advance for any input.提前感谢您的任何意见。 It is much appreciated.非常感谢。

I've heard from quite a few people who've used face_recognition successfully on Android.我听说很多人在 Android 上成功使用了 face_recognition。 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 .它是纯 Python package,因此可以直接从 PyPI 安装,不需要在Chaquopy 自己的 package 存储库中。

However, Chaquopy doesn't come with a Python executable, so running a command-line script with os.system is unlikely to work.但是,Chaquopy 不附带os.system可执行文件,因此使用 os.system 运行命令行脚本不太可能工作。 Instead, you can just call the face_recognition Python API, which has many examples on its own website .相反,您可以调用face_recognition Python API,它在自己的网站上有很多示例。

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.看起来与运行face_recognition脚本最接近的等效方法是导入face_recognition.face_recognition_cli并调用main function,可能在使用命令行设置sys.argv之后。

Any text printed to standard output will go to Logcat , which you can view in Android Studio.打印到标准 output 的任何文本都将 go 到Logcat ,您可以在 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 .如果您也想在设备屏幕上看到它,最简单的方法是调整控制台应用程序模板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM