简体   繁体   English

如何在 Java (Android Studio) 中运行 python 语音识别脚本?

[英]How to run python voice recognition script in Java (Android Studio)?

I've searched quite a bit on this topic, looking at options like Jython and Chaquopy, but whatever I do, there is always an error.我在这个主题上搜索了很多,查看了 Jython 和 Chaquopy 之类的选项,但无论我做什么,总是有错误。

What I'm looking to do is, when a button on my Android app is clicked, the python script (voice recognition using gTTS API in python) will run.我想要做的是,当我的 Android 应用程序上的一个按钮被点击时,python 脚本(在 python 中使用 gTTS API 进行语音识别)将运行。

This is what my code looks like (using Chaquopy):这是我的代码的样子(使用 Chaquopy):

MainActivity.java:主活动.java:

import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.content.res.AssetFileDescriptor;
import com.chaquo.python.*;
import com.chaquo.python.android.AndroidPlatform;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnTouchListener{

    ImageButton AutoCruiseButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (!Python.isStarted()) {
            Python.start(new AndroidPlatform(this));
        }

        AutoCruiseButton = (ImageButton) findViewById(R.id.auto); 
        AutoCruiseButton.setOnTouchListener(this);
        final MediaPlayer beep1 = MediaPlayer.create(this, R.raw.beep1);
        final MediaPlayer mp = new MediaPlayer();

        AutoCruiseButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                stopAndPlay(R.raw.beep1, mp);
                Python python = Python.getInstance();
                PyObject file = python.getModule("voice_recognition");
                PyObject command = file.callAttr("myCommand");
            }
        });

Top-level build file: build.gradle顶级构建文件:build.gradle

buildscript {
    
    repositories {
        google()
        jcenter()
        maven { url "https://chaquo.com/maven" }
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.2'
        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
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle (:app): build.gradle (:app):

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

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.myapplication"
        sourceSets{
            main{
                python{
                    srcDirs = ["src/main/python"]
                }
            }
        }
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
        python {
            buildPython "C:/Users/Alya/AppData/Local/Microsoft/WindowsApps/python3"
            buildPython "python3"
        }
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }



    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        targetCompatibility = 1.8
        sourceCompatibility = 1.8
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

voice_recognition.py (located in python folder under the Main folder for Android Studio project): voice_recognition.py(位于 Android Studio 项目主文件夹下的 python 文件夹中):

from gtts import gTTS 
import speech_recognition as sr
import playsound
import sys

counter = 0

def myCommand():
    r = sr.Recognizer()
    global counter
    if counter == 0:
        playsound.playsound('C:/Users/Alya/Desktop/KITT/Audio_Clips/What_Can_I_Do.mp3')
    with sr.Microphone() as source:
        counter += 1
        print('I am ready for your next command')
        r.pause_threshold = 1
        r.adjust_for_ambient_noise(source, duration=1)
        audio = r.listen(source)
    try: 
        command = r.recognize_google(audio)
        print('You said ' + command + '\n')

    #loop back in case not understood
    except sr.UnknownValueError:
        print("I didn't hear that")
        command = ""
        playsound.playsound('C:/Users/Alya/Desktop/KITT/Audio_Clips/Go_Ahead.mp3')
        assistant(myCommand())

    return command

#if statements for executing commands
def assistant(command):
    if 'introduce yourself' in command:
        playsound.playsound('C:/Users/Alya/Desktop/KITT/Audio_Clips/KITT_Intro.mp3')

I know Chaquopy works since I tested a basic python file and my emulator didn't crash.我知道 Chaquopy 可以工作,因为我测试了一个基本的 python 文件并且我的模拟器没有崩溃。 Here is the error I get when clicking the button to run the python script:这是单击按钮运行 python 脚本时得到的错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 26779
    com.chaquo.python.PyException: ModuleNotFoundError: No module named 'gtts'
        at <python>.java.chaquopy.import_override(import.pxi:20)
        at <python>.java.chaquopy.import_override(import.pxi:60)
        at <python>.voice_recognition.<module>(voice_recognition.py:1)
        at <python>.importlib._bootstrap._call_with_frames_removed(<frozen importlib._bootstrap>:219)
        at <python>.importlib._bootstrap_external.exec_module(<frozen importlib._bootstrap_external>:783)
        at <python>.java.android.importer.exec_module(importer.py:521)
        at <python>.importlib._bootstrap._load_unlocked(<frozen importlib._bootstrap>:671)
        at <python>.importlib._bootstrap._find_and_load_unlocked(<frozen importlib._bootstrap>:975)
        at <python>.importlib._bootstrap._find_and_load(<frozen importlib._bootstrap>:991)
        at <python>.importlib._bootstrap._gcd_import(<frozen importlib._bootstrap>:1014)
        at <python>.importlib.import_module(__init__.py:127)
        at <python>.chaquopy_java.Java_com_chaquo_python_Python_getModule(chaquopy_java.pyx:154)
        at com.chaquo.python.Python.getModule(Native Method)
        at com.example.myapplication.MainActivity$9.onClick(MainActivity.java:125)
        at android.view.View.performClick(View.java:6597)
        at android.view.View.performClickInternal(View.java:6574)
        at android.view.View.access$3100(View.java:778)
        at android.view.View$PerformClick.run(View.java:25885)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/Process: Sending signal. PID: 26779 SIG: 9

Any suggestions of how to proceed?有关如何进行的任何建议? I really rather keep my Android app in Java since the interface is already implemented.我真的宁愿将我的 Android 应用程序保留在 Java 中,因为该接口已经实现。 How can I fix my code so it works with Chaquopy?如何修复我的代码以便它与 Chaquopy 一起使用? Or how would I do it in Jython if it is a better alternative?或者,如果 Jython 是更好的选择,我将如何做?

You'll have to install gtts, into your app using pip, as described here .你必须安装这些GTT,到使用PIP您的应用程序,如所描述这里 The same goes for the other third-party modules you're using.您正在使用的其他第三方模块也是如此。

Also, you won't be able to access files on your Windows desktop from an Android app.此外,您将无法从 Android 应用程序访问 Windows 桌面上的文件。 Instead, include them in your Python source directory and then access them relative to __file__ as described here .相反,将它们包含在您的 Python 源目录中,然后按照此处所述相对于__file__访问它们。

Unfortunately this script will probably encounter other, more difficult problems:不幸的是,这个脚本可能会遇到其他更困难的问题:

  • You probably won't be able to access the device's microphone or speakers from Python code, so you'll have to use the Android Java APIs instead.您可能无法通过 Python 代码访问设备的麦克风或扬声器,因此您必须改用 Android Java API。
  • As discussed here , recognize_google requires a FLAC converter, which Chaquopy isn't currently able to support.随着讨论这里recognize_google需要FLAC转换器,Chaquopy目前不能够支持。

Sorry I couldn't be more helpful.抱歉,我帮不上忙了。

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

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