PMP0006 Python ile Sesli Asistan
https://youtu.be/DHGcy8xucSQ
adresinde yayınlanan videomda incelediğim Python kodlarını aşağıda paylaşıyorum:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# sr_facebook01.py +U+W+M import speech_recognition as sr # pip install SpeechRecognition from witkeys import * # 32 karakterlik bir string import webbrowser # pip install webbrowser from gtts import gTTS # pip install gtts import pyttsx3 # pip install pyttsx3 import os from datetime import datetime import time uyku = True pyttsx3_kullan=False speech_engine = pyttsx3.init() speech_engine.setProperty('rate', 150) speech_engine.setProperty('language', 'tr') say = 0 def seslendir(mesaj): if pyttsx3_kullan: speech_engine.say(mesaj) speech_engine.runAndWait() return global say say += 1 print("Asistan : ", mesaj) dosya = f"zz{say}.mp3" print(dosya) seslendirilecek = gTTS(text=mesaj, lang='tr') try: seslendirilecek.save(dosya) print(dosya+ " kaydedildi") komut = f"mpg123 -q {dosya}" os.system(komut) time.sleep(1) os.remove(dosya) except Exception as e: print(e) def dingdong(): komut = f"mpg123 -q ding-dong.mp3" os.system(komut) def komutal(uyku): # create a Recognizer() instance and assign it to r r = sr.Recognizer() audio = '' # sr.Microphone() nesnesini oluştur ve source adı ver with sr.Microphone() as source: # enerji eşik değerini otomatik olarak belirle # eğer gürültüsüz bir ortamdaysanız buna gerek olmayabilir r.adjust_for_ambient_noise(source, duration=1) # konuşması için kullanıcıyı uyar if uyku == False: dingdong() # bekleme süresini 5 sn ile sınırlayarak dinlemeye başla # konuşma uzunluğunu 5 sn ile sınırla audio = r.listen(source, timeout=5, phrase_time_limit=5) print('Sesi aldım') simdi = datetime.now() filename=simdi.strftime("cumleler/%Y%m%d_%H%M%S")+".wav" with open(filename,"wb") as f: f.write(audio.get_wav_data()) print('Sesi kaydettim') # try-except bloğu başlat try: # yakalanan sesleri varsayılan ön değerlerle facebook'a gönder # geri dönen stringi mesaj değişkenine ata ve ekrana bas print("Şimdi lütfen bekle... wit.ai çözümleme yapıyor.") mesaj = r.recognize_wit(audio, key=WIT_ACCESS_TOKEN) mesaj = mesaj.lower().strip() if len(mesaj)>0: print("ÇÖZÜM: ",mesaj) print('Ses çözümlendi.') return mesaj # istisnaları denetle except sr.WaitTimeoutError: print("Süre aşımı.") except sr.UnknownValueError: print("Wit.ai Speech Recognition sesli mesajı anlayamadı") except sr.RequestError as e: print(f"Wit.ai Speech Recognition servisi yanıt vermiyor: {e}") except Exception as ee: print(f"Hata: {ee}") if __name__ == '__main__': while True: komut = komutal(uyku) if "stop" in komut: break if "bitti" in komut: break if "bitir" in komut: break if "merhaba" in komut: uyku = False if "günaydın" in komut: uyku = False if "uyu" in komut: uyku = True if "hoşça kal" in komut: uyku = True if uyku: print("uyku modu") continue print("asistan komut bekliyor (sorgula/stop/bitti/bitir/uyu/hoşça kal/facebook/youtube)") if "sorgula" in komut: try: kelimeler = komut.split() endeks = kelimeler.index("sorgula") sorgu="" i = endeks + 1 while i < len(kelimeler): sorgu += " " + kelimeler[i] i += 1 print("sorgu= ", sorgu) webbrowser.open(f"https://yandex.com.tr/search/?text='{sorgu}'") uyku = True except: webbrowser.open("https://yandex.com.tr/") elif "facebook" in komut: webbrowser.open("https://www.facebook.com") uyku = True elif "youtube" in komut: webbrowser.open("https://www.youtube.com") uyku = True |
Kodlara ilişkin açıklamalar video içinde mevcut.
Takıldığınız yerler olursa, yorum alanından bana iletebilirsiniz.
Hashtags: #sesliasistan #pythonilemikroprojeler #ahmetax #python #pmp #mikroproje #sestenyazıya #stt #speechtotext
Ahmet Aksoy – ahmetax