Speech recognition using SL4A with Tasker

791 views
Skip to first unread message

baudi

unread,
Oct 17, 2010, 6:59:49 PM10/17/10
to Tasker
Here's a demonstration of using SL4A with Tasker. To make it work,
you'll have to install SL4A and the Python interpreter as described
here: http://code.google.com/p/android-scripting/wiki/UserGuide

Here's how the demo works: press the Camera button to bring up a
speech recognition dialog. Speak. Wait 5 seconds. Listen to your phone
read back to you what you said in its own "voice".

The demo consists of two profiles and a Python script. Here they are:
(case matters)

Profile: Recognize
Event: Button: Camera
Enter: 38
A1: Run Script [ Name:Recognize.py Terminal:Off Pass Variables: ]

Profile: RecognizeResult
Event: File Closed
Enter: 95
A1: Read Paragraph [ File:Tasker/tmp/Recognize.result Para: To Var:
%RecognizeText ]
A2: Say [ Text:%RecognizeText Engine:Voice:com.svox.pico:eng-USA
Stream:3 Pitch:5 Speed:5 Continue Task Immediately:Off ]

Save the following 6 lines of Python code as Recognize.py in the sl4a/
scripts folder:
import android
droid = android.Android()
Speech = droid.recognizeSpeech()[1]
f = open('/sdcard/Tasker/tmp/Recognize.result', 'w')
f.write(Speech)
f.close

baudi

unread,
Oct 17, 2010, 7:19:49 PM10/17/10
to Tasker
There's actually a little bug in the second Tasker profile,
RecognizeResult. The way it's written now, it will sometimes trigger
when the Python script erases the previous version of the result file
and say "EOF" instead of what you said. Easy to fix. Use the following
version of the profile.

Profile: RecognizeResult
Event: File Modified
Enter: 95
A1: Read Paragraph [ File:Tasker/tmp/Recognize.result Para: To Var:
%RecognizeText ]
A2: If If [ %RecognizeText ~ eof ]
A3: Wait [ MS:0 Seconds:4 Minutes:0 Hours:0 Days:0 ]
A4: Goto Action [ Number:1 ]
A5: End If
A6: Say [ Text:%RecognizeText Engine:Voice:com.svox.pico:eng-USA
Message has been deleted
Message has been deleted

Filmmakerc14

unread,
Oct 17, 2010, 8:57:13 PM10/17/10
to Tasker
Hello,
Thanks for the quick reply. I accidentally deleted my post from before
but I got everything setup. Right after I posted I realized how to
find all the parts and set it up.
Just a question. For the File Modified part, what do I put as the
file?

On Oct 17, 8:21 pm, baudi <dch...@gmail.com> wrote:
> If you take a look at the user guide I referenced above, you find a
> link to "Installing Interpreters". I'd suggest you read and study both
> the guide and that link. SL4A is not for the faint hearted, so you'll
> need perseverance to make it work. If you are not a programmer, I
> wouldn't recommend attempting this.

baudi

unread,
Oct 17, 2010, 9:01:23 PM10/17/10
to Tasker
You put: Tasker/tmp/Recognize.result

Looks like Pent's export to Clipboard doesn't copy that part of the
profile.

Filmmakerc14

unread,
Oct 17, 2010, 9:15:12 PM10/17/10
to Tasker
Ok, I tested it and it works well. Just a simple question, when you
say "Enter: 95" Is there a special reason for this? Or can it be named
anything?
Also, since you set %RECOGNIZETEXT to what you said, you could make a
variable that says like if %RECOGNIZETEXT matches "Something" then do
an action?
Thanks for the help.

baudi

unread,
Oct 17, 2010, 9:21:18 PM10/17/10
to Tasker
> Ok, I tested it and it works well.

Congratulations!

>Just a simple question, when you
> say "Enter: 95" Is there a special reason for this? Or can it be named
> anything?

Actually, it's an anonymous task in my version. Name it anything you
like. "Enter: 95" is just what Tasker showed for an anonymous task. I
guess "Enter" means it's the Profile Entry task, and "95" is just
Tasker's internal code for the Task ID.


> Also, since you set %RECOGNIZETEXT to what you said, you could make a
> variable that says like if %RECOGNIZETEXT matches "Something" then do
> an action?

Exactly. That's the whole idea: to use it to trigger other tasks. The
demo is just to prove the principle.

Filmmakerc14

unread,
Oct 17, 2010, 9:29:35 PM10/17/10
to Tasker
Cool, I'm surprised no one has thought of it until now!

As for another one of your posts:
http://groups.google.com/group/tasker/browse_thread/thread/406485d52ec25819/8b86d49f000a067f?lnk=gst&q=speech+recognition#8b86d49f000a067f
I tried this out and it now works.
However, since Tasker lets you run a script already, it probably
doesn't matter anymore.

baudi

unread,
Oct 17, 2010, 9:34:54 PM10/17/10
to Tasker
Until last week, Tasker could not run a script with release 2 of SL4A
because of a bug in SL4A. That other post was advising people how to
register their interest in getting that bug fixed.

But, as of last week, the SL4A bug was fixed. Tasker is now able to
run SL4A scripts and pass parameters to the scripts.


On Oct 17, 9:29 pm, Filmmakerc14 <filmmaker...@gmail.com> wrote:
> Cool, I'm surprised no one has thought of it until now!
>
> As for another one of your posts:http://groups.google.com/group/tasker/browse_thread/thread/406485d52e...

fubaya

unread,
Oct 18, 2010, 11:33:31 AM10/18/10
to Tasker
Holy smokes, this is cool!

I'm on a Nexus One and don't have a camera button so I set it up as a
widget. It gets hung sometimes but otherwise works pretty well.

I was a little disappointed that it just brings up the Google voice
search API thing-a-ma-bob, but after playing around with it, I
realized how much more powerful it can be with Tasker.

I've been searching the market for a month for a voice app that would
launch shortcuts and execute commands. There are some that launch
apps, but nothing that could run a script, perform a Tasker task, etc,
but since this takes the speech and puts it in a file and/or a
variable, the sky is the limit when using this with Tasker.

For example, I have a script to copy the phones clipboard to my
computer's clipboard over ssh. I already had it set up as a Tasker
task called "n1clip", so I added this to the baudi's voice demo.

Perform Task n1clip
if %RECOGNIZETEXT matches paste

I copied the URL to this discussion on the phone, clicked the voice
demo widget, said "paste" and voila!, the link was in my computer's
clipboard when I got home.

Niiice.

baudi

unread,
Oct 18, 2010, 12:07:41 PM10/18/10
to Tasker
How does it get hung up?

baudi

unread,
Oct 18, 2010, 12:10:34 PM10/18/10
to Tasker
By the way, you can also just use an SL4A shortcut to the Recognize.py
script. It works fine that way, too. Just long-press on a home screen,
choose Shortcut|Scripts|Recognize.py.

baudi

unread,
Oct 18, 2010, 1:38:17 PM10/18/10
to Tasker
>For example, I have a script to copy the phones clipboard to my
>computer's clipboard over ssh.

Could you share your code? That sounds useful.

fubaya

unread,
Oct 18, 2010, 7:25:03 PM10/18/10
to Tasker
Yeah, I wrote it up here: http://forum.xda-developers.com/showpost.php?p=8446531&postcount=224

I may put it on the Tasker wiki after Pent adds a "read file" action,
which is on his todo. Until then, it's not as perfect as I'd like. The
phone to pc part is perfect but the pc to phone is limited at the
moment. Anyway, it's explained at the link. If you decide to try it,
let me know how it works, and let me know if you have any trouble.

As for the demo hanging, I took out the "say" action and it quit
hanging so I guess it was something on my end. I did notice it will
hang if you tap "cancel" when the microphone is on screen, then you
have to tap "kill" in Tasker 4-5 times.

When using shell scripts like this with Tasker, I usually use:

command > file || echo "failed" > file

so if it fails, at least something gets into the file. I bet that's
not possible here though, as the API probably doesn't tell python if
something went wrong. (I don't know python or how this API business
works)

I know it's just a demo, but unless someone else writes something,
I'll probably use the python part for all my voice stuff.

baudi

unread,
Oct 18, 2010, 8:18:31 PM10/18/10
to Tasker
This Task and python script should do what you want in terms of
reading an entire file into the clipboard.

Task: ToClip
A1: Variable Set [ Name:%FNAME To:/sdcard/.clipboard Do Maths:Off
Append:Off ]
A2: Run Script [ Name:toclip.py Terminal:On Pass Variables:%FNAME ]

Save the following lines as toclip.py in the sl4a/scripts folder:

import android
droid = android.Android()
fname = droid.getIntent().result[u'extras'][u'%FNAME']
print fname
f = open(fname, 'r')
droid.setClipboard(f.read())
f.close

baudi

unread,
Oct 18, 2010, 8:22:00 PM10/18/10
to Tasker
Oh, you might want to turn Terminal:0ff in A2. I had it on for
testing. And remove print fname from the python script. Again for
testing. I forgot to take it out.

fubaya

unread,
Oct 19, 2010, 12:24:59 AM10/19/10
to Tasker
I have a little blog that I post to a couple times a month and just
posted this, you, or someone else, might like it:

http://a-more-common-hades.blogspot.com/2010/10/speech-recognition-with-tasker.html

Summary: I have a short shell command/script to grab the 7-day
forecast. Using Tasker, it grabs it hourly and sticks it in a file.
With a voice action, I can say "forecast Xday" and it triggers another
shell command/script to extract that day from the weather file and a
task to speak the forecast for that day. Needs to be perfected but it
tested great.

This can actually be done with most voice apps, but there's no fun in
that.
Reply all
Reply to author
Forward
0 new messages