Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if else repeating
#13
I want to introduce a message saying no voice command detected if nothing is said.
any tips?



# https://stackoverflow.com/questions/71236203/setting-a-threshold-for-fuzzywuzzy-process-extractone


from speech_recognition import Recognizer, Microphone
from gtts import gTTS
import subprocess
import time
from fuzzywuzzy import fuzz
from fuzzywuzzy import process
from playsound import playsound
import os
from datetime import datetime
from pyowm import OWM
from pyowm.utils.config import get_default_config
currentDateAndTime = datetime.now()
currentTime = currentDateAndTime.strftime("%H:%M:%S")

recognizer = Recognizer()

config_dict = get_default_config()
config_dict['language'] = 'fr'

owm = OWM('22b7aaf6688a49eaa3cc518ab1b76841', config_dict)
mgr = owm.weather_manager()

observation = mgr.weather_at_place('Montreal,CA')
w = observation.weather




def listen_audio():
    global text
    with Microphone() as source:
        print("Dite une commande dans le microphone")
        print("Réglage du bruit ambiant... Patientez...")
        recognizer.adjust_for_ambient_noise(source)
        print("Vous pouvez parler...")
        recorded_audio = recognizer.listen(source)
        print("Enregistrement terminé !")

    

    try:
        print("Reconnaissance du texte...")
        text = recognizer.recognize_google(
            recorded_audio,
            language="fr-FR"
        )
        print("Vous avez dit : {}".format(text))
        
    except Exception as ex:
        print(ex)
    
    return text 

def Auto_speak(response):
    tts = gTTS(text=response, lang='fr')
    tts.save('out.mp3')
    playsound('out.mp3')
    os.remove('out.mp3')



def respond(text):
    respond.has_been_called = False
    maPhrase = text
    phrase_A = ["ouvrir la lumière de l'entrée", "allumer la lumière de l'entrée"]

    phrase_B = ["fermer la lumière de l'entrée", "éteindre la lumière de l'entrée"]

    phrase_C = ["ouvrir la lumière du salon", "allumer la lumière du salon"]

    phrase_D = ["fermer la lumière du salon", "éteindre la lumière du salon"]
    
    phrase_E = ["quelle heure est-il?", "il est quelle heure?"]
    
    phrase_F = ["quel temps fait-il?"]

    
    (modele, score1) = process.extractOne(maPhrase, phrase_A)
    (modele, score2) = process.extractOne(maPhrase, phrase_B)
    (modele, score3) = process.extractOne(maPhrase, phrase_C)
    (modele, score4) = process.extractOne(maPhrase, phrase_D)
    (modele, score5) = process.extractOne(maPhrase, phrase_E)
    (modele, score6) = process.extractOne(maPhrase, phrase_F)
    print(modele)

    if score1 == 100:
        Auto_speak("La lumière de l’entrée est allumée")

    if score2 == 100:
        Auto_speak("La lumière de l’entrée est éteinte")

    if score3 == 100:
        Auto_speak("La lumière du salon est allumé")

    if score4 == 100:
        Auto_speak("La lumière du salon est éteinte")
        
    if score5 == 100:
        Auto_speak("Il est: {}".format(currentTime))
        
    if score6 == 100:
        Auto_speak(f"Il fait: {w.temperature('celsius')['temp']} degrés.")

respond.has_been_called = True
while True:
    text = listen_audio()
    if respond.has_been_called:
        respond(text)
    else:
        print("aucune commande détectée")
    #print(bool(respond(text)))
Reply


Messages In This Thread
if else repeating - by Frankduc - Jul-09-2022, 03:39 PM
RE: if else repeating - by Gribouillis - Jul-09-2022, 04:06 PM
RE: if else repeating - by Frankduc - Jul-09-2022, 04:29 PM
RE: if else repeating - by deanhystad - Jul-09-2022, 08:55 PM
RE: if else repeating - by Frankduc - Jul-10-2022, 02:43 PM
RE: if else repeating - by deanhystad - Jul-10-2022, 05:57 PM
RE: if else repeating - by Frankduc - Jul-11-2022, 02:26 PM
RE: if else repeating - by ndc85430 - Jul-11-2022, 02:47 PM
RE: if else repeating - by Frankduc - Jul-11-2022, 08:28 PM
RE: if else repeating - by deanhystad - Jul-12-2022, 01:57 AM
RE: if else repeating - by Frankduc - Jul-12-2022, 07:50 PM
RE: if else repeating - by deanhystad - Jul-13-2022, 09:47 AM
RE: if else repeating - by Frankduc - Jul-14-2022, 12:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is 2/3 not just .666 repeating? DocFro 4 751 Dec-12-2023, 09:09 AM
Last Post: buran
  repeating a user_input astral_travel 17 2,396 Oct-26-2022, 04:15 PM
Last Post: astral_travel
  matching a repeating string Skaperen 2 1,295 Jun-23-2022, 10:34 PM
Last Post: Skaperen
  Random Number Repeating Tzenesh 5 4,100 Jan-13-2021, 10:00 PM
Last Post: deanhystad
  factorial, repeating Aldiyar 4 2,850 Sep-01-2020, 05:22 PM
Last Post: DPaul
  number repeating twice in loop JonnyEnglish 3 3,344 Nov-24-2019, 09:23 AM
Last Post: ThomasL
  Repeating equations Tbot100 2 3,301 May-29-2019, 02:38 AM
Last Post: heiner55
  First non-repeating char and some errors. blackknite 1 2,312 Jan-06-2019, 02:19 PM
Last Post: stullis
  repeating for loop Kaldesyvon 5 3,885 Dec-06-2018, 08:00 PM
Last Post: ichabod801
  Repeating same number in sequence Rudinirudini 6 4,307 Oct-28-2018, 07:44 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020