Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import pyaudio
- import wave
- import speech_recognition as sr
- import os
- import time
- # Global variables
- FORMAT = pyaudio.paInt16
- CHANNELS = 2
- RATE = 44100
- CHUNK = 1024
- WAVE_OUTPUT_FILENAME = "file.wav"
- waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
- frames = []
- audio = pyaudio.PyAudio()
- # Creating file for statistics
- ctime = time.strftime('%d %B %y %H:%M:%S', time.gmtime())
- with open('speech_recogn_stats.txt', 'a+') as myFile:
- myFile.write(ctime + '\n')
- #Functions
- def callback(in_data, frame_count, time_info, status):
- global frames
- frames.append(in_data)
- return (in_data, pyaudio.paContinue)
- def record():
- global stream
- stream = audio.open(format=FORMAT,
- channels=CHANNELS,
- rate=RATE,
- input=True,
- stream_callback=callback)
- stream.start_stream()
- statusLab.config(text="Recording")
- def stop():
- stream.stop_stream()
- stream.close()
- audio.terminate()
- statusLab.config(text='Finished recording')
- def createAudioFile():
- waveFile.setnchannels(CHANNELS)
- waveFile.setsampwidth(audio.get_sample_size(FORMAT))
- waveFile.setframerate(RATE)
- waveFile.writeframes(b''.join(frames))
- waveFile.close()
- def recongnize():
- statusLab.config(text='Recognizing the speech')
- createAudioFile()
- r = sr.Recognizer()
- with sr.AudioFile('file.wav') as source:
- audio = r.listen(source)
- try:
- with open('speech_recogn_stats.txt', 'a+') as myFile:
- myFile.write("Command: " + commandEnt.get()+'\n')
- # Google Speech
- str_google = r.recognize_google(audio)
- myFile.write("Google: " + str_google + '\n')
- # Houndify
- str_hound = r.recognize_houndify(audio, client_id='9_wAj1R52ID30A6QdmQOeg==',
- client_key='o12nzb2eS5SggLYu5zuO5c08yUxlDN2vf86eAZtpyFKoe1dVENKGlpQgoMDeaBVNVUZLm2v8EJRuM_7EfMHT1g==')
- myFile.write("Houndify: " + str_hound + '\n')
- # IBM
- str_ibm = r.recognize_ibm(audio, username = "ff9dc8ce-0abe-4636-bc89-21529a2744aa", password = "L0B6ZxQMVTRU")
- myFile.write("IBM: " + str_ibm + '\n')
- #Wit.ai
- str_wit = r.recognize_wit(audio, key='JONVYPARJNOG76XSJRIWN2BIQHDC2AM3')
- myFile.write("WIt.ai: " + str_wit + '\n')
- except sr.UnknownValueError:
- print("Engine did not understand the command")
- except sr.RequestError as e:
- print("Request Error; {0}".format(e))
- os.remove('file.wav')
- # This is an interface
- root = Tk()
- commandEnt = Entry(root, font=(22))
- commandLab = Label(root, font=(20), text="Type your command and press Record")
- recognBut = Button(root, font=(20), text="Recognize", fg='blue', width=9, command=recongnize)
- recordBut = Button(root, font=(20), text="Record", fg='blue', width=9, command=record)
- stopBut = Button(root, font=(20), text="Stop", fg='blue', width=9, command=stop)
- statusLab = Label(root, font=(20), text="Status", fg="red", )
- recognBut.grid(row=0, column=0, sticky=W)
- recordBut.grid(row=0, column=1, sticky=W+E+N+S)
- stopBut.grid(row=0, column=3)
- commandEnt.grid(row=3, column=0, columnspan=3, sticky=W)
- commandLab.grid(row=2, column=0, columnspan=3)
- statusLab.grid(row=4, column=0, sticky=W)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement