语音转文本、文本转语音和笑脸

问题描述

我正在编写一个代码,用于接收语音输入,然后取决于所说的内容;它应该提供音频输出。在我的代码中,我一直保持简单,并使其只重复所说的内容

我的主要问题是我必须将音频输出与嘴巴运动联系起来。基本上,当它提供音频输出时,它应该切换嘴巴来移动。

我正在使用线程来同时运行两者。如何将语音部分与人脸联系起来?

import speech_recognition
import pyttsx3
import speech_recognition as sr
from tkinter import Tk,HIDDEN,norMAL,Canvas
import threading


def smile_func():
    def toggle_eyes():
        current_color = c.itemcget(eye_left,'fill')
        new_color= c.body_color if current_color == "white" else "white"
        #new_color = 'white' if current_color == 'white' else 'white'
        current_state = c.itemcget(pupil_left,'state')
        new_state = norMAL if current_state == HIDDEN else HIDDEN
        c.itemconfigure(pupil_left,state=new_state)
        c.itemconfigure(pupil_right,state=new_state)
        c.itemconfigure(eye_left,fill=new_color)
        c.itemconfigure(eye_right,fill=new_color)

    def blink():
        toggle_eyes()
        win.after(250,toggle_eyes) 
        win.after(3000,blink)


    def toggle_tongue():
        if not c.tongue_out:
            c.itemconfigure(tongue_tip,state=norMAL)
            c.itemconfigure(tongue_main,state=norMAL)
            c.tongue_out=True
        else:
            c.itemconfigure(tongue_tip,state=HIDDEN)
            c.itemconfigure(tongue_main,state=HIDDEN)
            c.tongue_out = False
        
    
    def toggle_tongue1():
        print("ouch")
        if not c.tongue_out:
            c.itemconfigure(tongue_tip,state=norMAL)
            c.tongue_out=True
            
        else:
            c.itemconfigure(tongue_tip,state=HIDDEN)
            c.tongue_out = False
        

    def cheek(event):
        global cheek
        toggle_tongue()
      
        win.after(500,toggle_tongue1)
    
        return 
    
    win = Tk()
    c= Canvas(win,width=400,height=400)
    c.configure(bg="black",highlightthickness=0)

    c.body_color= "yellow"

    body = c.create_rectangle(35,20,365,350,outline=c.body_color,fill = "yellow")

    eye_left= c.create_oval(130,110,160,170,outline="black",fill="white")
    pupil_left= c.create_oval(140,145,150,155,fill="black")

    eye_right= c.create_oval(230,260,fill="white")
    pupil_right= c.create_oval(240,250,fill="black")

    mouth_normal= c.create_line(170,200,272,230,smooth=1,width=2,state=norMAL)
    mouth_happy= c.create_line(170,282,state=norMAL)


    tongue_main= c.create_rectangle(170,290,outline='red',fill='red',state=HIDDEN)
    tongue_tip= c.create_oval(170,285,300,state=HIDDEN)

    c.pack()



    c.bind('<Button-1>',cheek)

    c.crossed_eyes= False
    c.tongue_out=False
    #c.happy_level = 0
    c.happy_level = 1

    win.after(1000,blink)
    win.mainloop()
    


#speech and text xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
engine= pyttsx3.init()
def record_audio():

      r = sr.Recognizer()

      with sr.Microphone() as source:
        #r.phrase_threshold=1
        print("adjusting ambient noise")
        r.adjust_for_ambient_noise(source)#,duration=5)
        #r.energy_threshold=350 
        #r.pause_threshold=1 
        print("listening ....")
        audio = r.listen(source)
        print("got it")
        voice_data=''
      try:
          voice_data= r.recognize_google(audio,language = "en")
          voice_data=voice_data.lower()
      except Exception as e:
                print(e)
                return "None"
      except sr.RequestError:
                alexis_speak('sorry my speech server is down')  #print 
      array=[voice_data]
      return array


def alexis_speak(text):
    engine.say(text)
    engine.runAndWait()
    print(text)


def speech_func():
    while 1:
        voice_data=record_audio()
        alexis_speak(voice_data)

if __name__ == "__main__":
    # creating thread
    t1 = threading.Thread(target=speech_func )
    t2 = threading.Thread(target=smile_func )
  
    # starting thread 1
    t1.start()
    # starting thread 2
    t2.start()
  
    # wait until thread 1 is completely executed
    t1.join()
    # wait until thread 2 is completely executed
    t2.join()
  
    # both threads completely executed
    print("Done!")

#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)