Python‎ > ‎

Python Voice

Pip to bypass St Pius X SSL Self Signed - Security Restriction 

> pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org pip setuptools --upgrade

Option to install into pip.ini file:

  • Open Command Prompt (Admin Mode if possible)
    • press "windows button" "C"
  • navigate to %APPDATA%\pip
    • cd %APPDATA%pip
  • create/edit pip.ini file
    • notepad pip.ini
  • Add following configuration into file:
[install]
trusted-host = 
pypi.org
files.pythonhosted.org

  • Save pip.ini file


Python Text To Speech

Installation

The following instructions are for Python3 and Win32 64bit

To install Python Text To Speech for version 3+ of Python (pyttsx3):

1. CMD to go to command prompt - RUN AS ADMINISTRATOR

2. Navigate to your Python3 installed folder,  e.g. 
    c:
    cd \Python36

3. First, Install Pywin32 extensions
    pip install pywin32

4. Install the additional scripts (Administrator mode only)    
    python Scripts/pywin32_postinstall.py -install

5. Finally, install pyttsx3
    pip install pyttsx3
 
For examples and documentation for pyttsx3



Visual C++ Libraries

Note: As of Python 3.9 onwards - you should not need to do this......

These are needed for performance improvements and installation of some Python Packages that rely on Windows libraries.

This will be needed for installing PyAudio

First Install appropriate Visual C++ modules:

Choose the version of Visual C++ Build Tools for your version of Python and Operating System





Speech Recognition - Speech to Text

For more information

NOTE: July 2020 - currently only supporting up to Python 3.6 - see website for latest port
NOTE2: From Python 3.9 onwards, SpeechRecognition is supported again.


From command prompt:
a) for using microphone:
pip install PyAudio

Note: If you have Python 3.7 see below in Files Attached section for the unofficial WHL file for the PyAudio for Python 3.7
(Obtained from this website:


Move your WHL files into a suitable folder in your Python libraries
such as in folder below pip.exe
create a folder: c:\....\python38\scripts\wheels
 
to install a whl file:
pip install wheels\some-whl-file.whl

Also see StackOverflow post...


The "here" address is....
https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio



b) Speech Recognition
    pip install SpeechRecognition

    This will install the appropriate package for Speech Recognition



How to use Speech Recognition in Python?

Using Google API: 
Note: this means you need to be connected to Internet to access Google
Note: FREE access to this API is limited to calling routing 50 times a day - so be careful in testing it.

(If not, research offline modes for SpeechRecognition)

How Speech Recognition works...

1. Capture sound from microphone.

a) You say something into your microphone - this is captured as a sound file by the Speech Recognition object/class Recognizer() via the listen() method from your microphone as the source, and stored in a python variable audio 

2. The audio file is translated into ASCII text through the Google API..

a) The Recognizer object (r) has a method called "recognize_google"
b) You pass the audio variable/data to this method
c) Google on the internet converts the spoken sound to written text. (essentially using the Google Translate tool API) and returns it from the "recognize_google" call.

Sample Code:

#!/usr/bin/env python3
# Requires PyAudio and PySpeech.

import speech_recognition as sr

# Record Audio
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)

# Speech recognition using Google Speech Recognition
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`     speech = r.recognize_google(audio)
print("You said: " + speech)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))





Alternatives:

Using Web Based Speech Recognition


The Web Speech API enables you to incorporate voice data into web apps. The Web Speech API has two parts: SpeechSynthesis (Text-to-Speech), and SpeechRecognition (Asynchronous Speech Recognition.)