Python 3 PyAudio Script to Build a Voice Recognition Arithmetic Calculator Using Google Speech API Full Project For Beginners

 

Welcome folks today in this blog post we will be building a voice recognition arithmetic calculator using google speech api. All the full source code of the application is shown below.

 

 

 

Get Started

 

 

In order to get started you need to make an main.py file and copy paste the following code

 

 

main.py

 

 

import operator
import speech_recognition as s_r
print("Your speech_recognition version is: "+s_r.__version__)
r = s_r.Recognizer()
my_mic_device = s_r.Microphone(device_index=1)
with my_mic_device as source:
    print("Say what you want to calculate, example: 3 plus 3")
    r.adjust_for_ambient_noise(source)
    audio = r.listen(source)
my_string=r.recognize_google(audio)
print(my_string)
def get_operator_fn(op):
    return {
        '+' : operator.add,
        '-' : operator.sub,
        'x' : operator.mul,
        'divided' :operator.__truediv__,
        'Mod' : operator.mod,
        'mod' : operator.mod,
        '^' : operator.xor,
        }[op]

def eval_binary_expr(op1, oper, op2):
    op1,op2 = int(op1), int(op2)
    return get_operator_fn(oper)(op1, op2)

print(eval_binary_expr(*(my_string.split())))

Leave a Reply