termux / termux-api-package

Termux package containing scripts to call functionality in Termux:API.
MIT License
1.02k stars 318 forks source link

How to develop speech to text with Python in termux #95

Open mandiladitya opened 4 years ago

mandiladitya commented 4 years ago

I want to make speech to text with Python in termux so I used SpeechRecognition library of Python it installed correctly but it's can't get the record audio so is there any way or any changes I have to do to #access the microphones of phone using termux python

AbdullahM0hamed commented 4 years ago

You can do this using the termux-microphone-record script.

Something like the following:


import subprocess

def ensure_permission():
    no_permission_text = '''{
  "error": "Please grant the following permission to use this command: android.permission.RECORD_AUDIO"
}'''
    record = subprocess.getoutput("termux-microphone-record -d") 
    while record == no_permission_text:
        record = subprocess.getoutput("termux-microphone-record -d")

#This will start a recording if (or when) there is permission 
ensure_permission() 

#Call this when you want to finish the recording
recording = subprocess.getoutput("termux-microphone-record -q")[20:] 
ashuhar455 commented 4 years ago

Termux has a pkg called termux-api which includes termux-speech-to-text command that can be run from termux to record audio and directly convert it to text.

Install it using:

pkg install termux-api -y

To test, run:

termux-speech-to-text

There will be a beep after which you can speak and it gives output in text.

This can be used along with subprocess module within python to get the output and store it to a string.

import subprocess

inp = subprocess.getoutput("termux-speech-to-text")
print("You said: "+str(inp))

if inp == "Hello":
   print("Termux : Hello I am termux")

It uses Androids inbuit speech recognition feature with o ly downside that if you dont speak after the beep it will terminate with None as output.

Also, termux-tts-speak command can be utilised same way to turn the string into voice. But it needs to be used with

os.system("termux-tts-speak {}".format(text_you_want_in_speech))

Assuming that os module is imported.

(Sorry for so many edits 😅)

ashuhar455 commented 4 years ago

want to make speech to text with Python in termux so I used SpeechRecognition library of Python it installed correctly but it's can't get the record audio so is there any way or any changes I have to do to #access the microphones of phone using termux python

Talking about speech_recognition, termux doesnt have portaudio in its core repository (though you can add Its-pointless's repo to achive it). Even if you get portaudio and pyaudio installed it doesnt support microphone input as of now due to some limitations.

EightNice commented 3 years ago

Termux has a pkg called termux-api which includes termux-speech-to-text command that can be run from termux to record audio and directly convert it to text.

Install it using:

pkg install termux-api -y

To test, run:

termux-speech-to-text

There will be a beep after which you can speak and it gives output in text.

This can be used along with subprocess module within python to get the output and store it to a string.

import subprocess

inp = subprocess.getoutput("termux-speech-to-text")
print("You said: "+str(inp))

if inp == "Hello":
   print("Termux : Hello I am termux")

It uses Androids inbuit speech recognition feature with o ly downside that if you dont speak after the beep it will terminate with None as output.

Hello! Is there any way to configure that to recognize spanish?