Forum Archive

Speech Recognition failed

Python567

Hello guys, I have a code where every 5 seconds the skript check if there is any keyword. But I get the error: Speech Recognition failed. I don‘t know why? My Code:
````

s = ui.Switch()
s.frame = (970,1,600,40)

def s_action(sender):
global timestamp
recorder = sound.Recorder('speech.m4a')
recorder.record()

if s.value == True:
    while True:
        if s.value == False:
            break

        elif clock2.uhrgedruckt == True:
            clock2.uhrgedruckt = False
            print('ok')


        elif timestamp - time.time() <= -5:
            recorder.stop()
            result = speech.recognize('speech.m4a', 'de_DE')

            if result[0][0] == 'Hallo':
                print('Du hast hallo gesagt')
                timestamp = time.time()
                recorder.record()


            else:
                print('Fehler')
                timestamp = time.time()
                recorder.record()


else:
    pass

s.action = s_action

cvp

@Python567 3 remarks:
1) try always to include in your post the error message
2) never do a while loop without waiting either an input, either a time.sleep(), see other topic
3) do you initialize your timestamp?

Python567

@cvp
1. The error message: Speech Recognition failed: Retry
2. ok, I include it in my code
3. yes I do

Edit: The problem is with this line: result = speech.recognize('speech.m4a', 'de_DE')

cvp

@Python567 said:

The error message: Speech Recognition failed: Retry

Sorry, badly read your first post

cvp

@Python567 said:

ok, I include it in my code

Same problem after you did it?

Python567

@cvp yes, but there is something I don‘t understand: The recorder starts, the recorder stops and print the result, and after the print from the result, there comes the error! I don‘t understand that

cvp

@Python567 try, I think that the problem occurs when you don't speak

def s_action(sender):
    global timestamp
    recorder = sound.Recorder('speech.m4a')
    recorder.record()
    timestamp = time.time()
    if s.value == True:
        while True:
            time.sleep(1)
            if s.value == False:
                break

            elif clock2.uhrgedruckt == True:
                clock2.uhrgedruckt = False
                print('ok')

            elif timestamp - time.time() <= -5:
                recorder.stop()
                try:
                    result = speech.recognize('speech.m4a', 'de_DE')
                    if result[0][0] == 'Hallo':
                        print('Du hast hallo gesagt')
                    else:
                        print('Fehler')
                except Exception as e:
                    print(e)
                timestamp = time.time()
                recorder.record()

    else:
        pass
cvp

@Python567 and, please, don't forget the time.sleep between two successive tests

Python567

@cvp ok, now my app crashed, but the skript isn‘t long

cvp

@Python567 if not long, post it

Python567

@cvp I think I have the problem why the app crashed. I have while True:

cvp

@Python567 you have to foresee a way to leave (break) this infinite loop.

Python567

@cvp two questions:
1. If I open a shortcut on ios with the shortcuts module the app shortcut is open, can I close it from the skript or can I bring Pythonista to the front?
2. How can I change the language for this code:
```
t = f"{datetime.datetime.now():%A}"

cvp

@Python567 said:

If I open a shortcut on ios with the shortcuts module the app shortcut is open, can I close it from the skript or can I bring Pythonista to the front?

It depends: how do you open the shortcuts module?
Anyway, you can't never close an app from another app

Python567

@cvp I open it with
```
shortcuts.open_shortcuts_app(name='', shortcut_input='')
````
And how can I change the language on the code which I posted before?

cvp

@Python567 said:

How can I change the language for this code:

Normally, Python allows the locale.setlocale function to do that, but it has never worked in the past.
I know I did it viaObjectiveC but I don't find my script, let me some time, not free this morning.

Of course, if somebody knows a way, don't hesitate to help.

cvp

@Python567 said:

If I open a shortcut on ios with the shortcuts module the app shortcut is open, can I close it from the skript or can I bring Pythonista to the front?

I think you can ask shortcut to go back to Pythonista...

Python567

@cvp Yes that‘s a good idea. I found following in the documentation, but I don‘t know how the set it in german

Weekday as locale’s abbreviated name.
Sun, Mon, ..., Sat (en_US);
So, Mo, ..., Sa (de_DE)
(1)
%A Weekday as locale’s full name.
Sunday, Monday, ..., Saturday (en_US);
Sonntag, Montag, ..., Samstag (de_DE)

cvp

@Python567 the doc = Python doc but locale did never function in Pythonista

Python567

@cvp it‘s the pythonista doc

cvp

@Python567 yes but this Pythonista doc, for Python keywords, points to standard doc. And this locale function has never worked.

cvp

About the shortcut, do you want to open Shortuts app, execute a shortcut and come back in your script or only open Pythonista

Python567

@cvp I want to execute a shortcut and come back to my skript

cvp

Even if I drunk too much 🍷

# https://forum.omz-software.com/topic/4480/handling-locale-date-string-conversion

import datetime
from objc_util import *

NSDateFormatter = ObjCClass('NSDateFormatter')
dateFormatter = NSDateFormatter.alloc().init()

NSLocale = ObjCClass('NSLocale')
deLocale = NSLocale.alloc().initWithLocaleIdentifier_(ns('de'))
dateFormatter.setLocale_(deLocale)

NSDate = ObjCClass('NSDate')
date = NSDate.alloc().init()

now = f"{datetime.datetime.now():%d/%m/%Y}"

# see https://coderwall.com/p/jj6k_a/quick-guide-to-objective-c-dateformatting
dateFormatter.setDateFormat_('dd/MM/yyyy')
date = dateFormatter.dateFromString_(now)

dateFormatter.setDateFormat_('EEEE')
now_day_in_German = str(dateFormatter.stringFromDate_(date))
print(now_day_in_German)

If you want an abbreviated name, use

dateFormatter.setDateFormat_('EE')
cvp

@Python567 said:

I want to execute a shortcut and come back to my skript

Please, read carefully this topic and try with short script and shortcut.

I didn't retry....

cvp

You can also use, for month name

dateFormatter.setDateFormat_('MMM')
now_day_in_German = str(dateFormatter.stringFromDate_(date))
print(now_day_in_German)

dateFormatter.setDateFormat_('MMMM')
now_day_in_German = str(dateFormatter.stringFromDate_(date))
print(now_day_in_German)
cvp

Or

dateFormatter.setDateFormat_('EEEE dd MMMM yyyy')
now_day_in_German = str(dateFormatter.stringFromDate_(date))
print(now_day_in_German)
# ==> Sonntag 14 Februar 2021
Python567

@cvp ok, but what have I to import for that?

cvp

@Python567 some posts above,(4 hours ago), you have a full script with the imports....

Python567

@cvp that?:
```
import datetime
from objc_util import *

cvp

@Python567 yes, plus these lines once at begin of script

NSDateFormatter = ObjCClass('NSDateFormatter')
dateFormatter = NSDateFormatter.alloc().init()

NSLocale = ObjCClass('NSLocale')
deLocale = NSLocale.alloc().initWithLocaleIdentifier_(ns('de'))
dateFormatter.setLocale_(deLocale)

NSDate = ObjCClass('NSDate')
date = NSDate.alloc().init()
Python567

@cvp ok. I found a code with the name email, is it possible to send an email with pythonista?

cvp

@Python567 yes, the doc shows à functional example

Python567

@cvp really cool, can you send me the doc?

cvp

@Python567 the doc is internal to Pythonista. Search on smtplib

Swipe to left to display console, you see above a ? in a circle, tap it.
You get the doc and you have a search field that appears. Type smtplib and enter....
And read

Python567

@cvp great, thanks

Python567

@cvp by the way, can I change my Pythonista skript into a microsoft teams bot?

cvp

@Python567 no idea at all

Python567

@cvp ok, maybe an other guy can help

Python567

@cvp if that‘s not possible, can I create an apk file?

cvp

@Python567 I suppose you can't but, sincerely, I don't have any skill about Android.

Python567

@cvp ok, and for ios?

cvp

@Python567 if your question is still relative to a teams bot, I can't help at all.

Python567

@cvp No that‘s an other question, nothing with teams bots. I ask because I found a video on youtube who somebody have his own app on the home screen (but it‘s not already in the Apple Store!) and I don‘t know how to do that, I found something in the internet but that‘s only with apk, but the youtuber has ios.

cvp

@Python567 there are not so much solutions in iOS.
Either you develop your own app on your Mac and you can put it on the Home Screen, even without putting it on the App Store.
Either you develop, thanks to an app like Pythonista (or Pyto), and you can put your own icon on the Home Screen, and you tap the icon, for instance, you launch a shortcut which launch Pythonista and runs your Python script.

Python567

@cvp Do I really need a Mac for that, because I haven‘t got one

cvp

@Python567 if you want to write a real app, yes you need a Mac.
But, I think you can develop almost all with Pythonista.

Python567

@cvp and if I want my app on my homw screen without doing it in the App Store

cvp

@Python567 if it is a real app, you need a Mac.

Python567

@cvp but I don‘t want a real app for the app store, only for my home screen

cvp

@Python567 😢real app = Mac, App Store or not

7upser

Maybe he means a Homescreen Symbol.
@Python567, see here, if yes:

shortcuts
But you need to install Pythonista for each User.

cvp

@7upser said:

Maybe he means a Homescreen Symbol.

Yes, I had explained that some posts above...

Python567

Ok, than I do it with shortcuts

Python567

@cvp can I install the pythonista modules with pip?

cvp

@Python567 if modules are yours, then no.
if modules are standard ones, they are already included in Pythonista.
Thus, which modules?

Python567

@cvp sorry, I mean: Can I install pythonista modules with pip on my pc? For example: ui, scene, etc.?

cvp

@Python567 no. What do you want to do with Pythonista on your pc?

Python567

@cvp I want to run my skript on my pc. what do you mean before?

cvp

@Python567 You can't run Pythonista on a PC, even not on a Mac, except the last M1.

Python567

@cvp ok, what do you mean with M1?

cvp

@Python567 see here

Python567

@cvp ok, so for start the skript from my pc, I have to code it from my pc

Python567

@cvp the problem why I ask, is that I don't know how to create the clock with tkinker

cvp

@Python567 sorry, I can't help, never seen tkinker

ccc

https://www.google.com/search?q=clock+in+tkinter

7upser

@cvp
uuups, sorry. I was too busy today 😇

anisa

Hey i am anisa simth. I am technical advisor. I am providing direct technical assistance to installation, uninstall, updates, virus removal, router and ect. antivirus customer support team is 24x7 available to any antivirus users. If you needed more information visit here: Magellan GPS Update | Mywifiext

ashley999

I usually convert speech to text, using automatic transcription. It is very helpful and you can get your text files in a few minutes. It takes only 10 minutes to transcribe 1 hour of audio or video.