Advanced Python Technologies I

qrcode, Speech Recognition in Python, Google Speech Recognition #PurePythonSeries — Episode #12

J3
Jungletronics
4 min readOct 18, 2022

--

Tech 1 # qrcode

visit: https://pypi.org/project/qrcode/

What is a QR Code?

A Quick Response code is a two-dimensional pictographic code used for its fast readability and comparatively large storage capacity

#### In the code below:First we install qrcode directly from jupyter notebook.Then, after importing the lib, we generate a qrcode, and save it as PNG - Portable Network Graphics

Install the lib:

!pip install qrcodeCollecting qrcode
Downloading qrcode-7.3.1.tar.gz (43 kB)
-------------------------------------- 43.5/43.5 kB 353.8 kB/s eta 0:00:00
Preparing metadata (setup.py): started
Preparing metadata (setup.py): finished with status 'done'
Requirement already satisfied: colorama in c:\users\giljr\appdata\roaming\python\python39\site-packages (from qrcode) (0.4.4)
Building wheels for collected packages: qrcode
Building wheel for qrcode (setup.py): started
Building wheel for qrcode (setup.py): finished with status 'done'
Created wheel for qrcode: filename=qrcode-7.3.1-py3-none-any.whl size=40401 sha256=21157d8580f2ede48dc47666675f5a87ae0c15d4aeb7b102d281040a35194f18
Stored in directory: c:\users\giljr\appdata\local\pip\cache\wheels\72\8d\d4\18b60cd6cda7fd6832229ded41aa505cee22e22f7f47ea97ea
Successfully built qrcode
Installing collected packages: qrcode
Successfully installed qrcode-7.3.1

Run this code:

import os
import qrcode
# Generate QR code
img = qrcode.make("https://medium.com/jungletronics")
# Save as file
img.save("qr.png", "PNG")
# Open file
os.system("open qr.png")
1
Please visit jungletronics on media dot com 😉

Tech 2 # Simplified Answering Machine

To test it please run as much as you like!

Three words or sets of words must exist in the sentence spoken and captured by the microphone:

“HELLO”
“HOW ARE YOU”
“GOODBYE”

And wait for the answer 😉

The code:

# Recognizes a greeting
# Get input
words = input("Say something!\n").lower()
# Respond to speech
if "hello" in words:
print("Hello to you too!")
elif "how are you" in words:
print("I am well, thanks!")
elif "goodbye" in words:
print("Goodbye to you too!")
else:
print("Huh?")
Say something!
hello
Hello to you too!

Tech 3 # Speech Recognition in Python

There are several technologies available on the market. Let's bring the google solution, shall we?How to Convert Speech to Text in PythonCMU Sphinx (offline)
Google Speech Recognition
Google Cloud Speech API
Wit.ai
Microsoft Bing Voice Recognition
Houndify API
IBM Speech To Text
Snowboy Hotword Detection (offline)

In the code below:

First we install pyaudio and SpeechRecognitions from google.

Then, obtain audio from the microphone.

And finaly, Recognize speech using Google Speech Recognition

Say something and wait the answer 👈

!pip install pyaudio
!pip install SpeechRecognition
Collecting pyaudio
Using cached PyAudio-0.2.12-cp39-cp39-win_amd64.whl (163 kB)
Installing collected packages: pyaudio
Successfully installed pyaudio-0.2.12
Collecting SpeechRecognition
Using cached SpeechRecognition-3.8.1-py2.py3-none-any.whl (32.8 MB)
Installing collected packages: SpeechRecognition
Successfully installed SpeechRecognition-3.8.1

The code:

import speech_recognition
# Obtain audio from the microphone
recognizer = speech_recognition.Recognizer()
with speech_recognition.Microphone() as source:
print("Say something:")
audio = recognizer.listen(source)
# Recognize speech using Google Speech Recognition
print("You said:")
print(recognizer.recognize_google(audio))
Say something:
You said:
Shoe Stop

Here we mixed the two codes above and see what happened!

To test it please run as much as you like!

Three words or sets of words must exist in the sentence spoken and captured by the microphone:

“HELLO”
“HOW ARE YOU”
“GOODBYE”

The code:

import speech_recognition
# Obtain audio from the microphone
recognizer = speech_recognition.Recognizer()
with speech_recognition.Microphone() as source:
print("Say something:")
audio = recognizer.listen(source)
# Recognize speech using Google Speech Recognitionwords = recognizer.recognize_google(audio)# Respond to speech
if "hello" in words:
print("Hello to you too!")
elif "how are you" in words:
print("I am well, thanks!")
elif "goodbye" in words:
print("Goodbye to you too!")
else:
print("Huh?")
Say something:
Goodbye to you too!

Now say what’s your name

import re
import speech_recognition
# Obtain audio from the microphone
recognizer = speech_recognition.Recognizer()
with speech_recognition.Microphone() as source:
print("Say something:")
audio = recognizer.listen(source)
# Recognize speech using Google Speech Recognition
words = recognizer.recognize_google(audio)
# Respond to speech
matches = re.search("my name is (.*)", words)
if matches:
print(f"Hey, {matches[1]}.")
else:
print("Hey, you.")
Say something:
Hey, J-3.

Tech 4 # Speech Recognition in Python

visit: https://pypi.org/project/pyttsx3/

It Supports multiple TTS engines, including Sapi5, nsss, and espeak.

!pip install pyttsx3Requirement already satisfied: pyttsx3 in c:\users\giljr\anaconda3\lib\site-packages (2.90)
Requirement already satisfied: comtypes in c:\users\giljr\anaconda3\lib\site-packages (from pyttsx3) (1.1.10)
Requirement already satisfied: pypiwin32 in c:\users\giljr\anaconda3\lib\site-packages (from pyttsx3) (223)
Requirement already satisfied: pywin32 in c:\users\giljr\appdata\roaming\python\python39\site-packages (from pyttsx3) (303)

The code:

import pyttsx3
engine = pyttsx3.init()
engine.say("hello, world")
engine.runAndWait()

You will hear the sentence written above on your computer speakers.

import pyttsx3
engine = pyttsx3.init()
name = input("What's your name? ")
engine.say(f"hello, {name}")
engine.runAndWait()
What's your name? j3

And finally:

import pyttsx3
engine = pyttsx3.init()
engine.say("This was CS50")
engine.say("That's it!")
engine.say("Thanks to: DAVID MALAN from HAVARD CS50 COURSE")
engine.runAndWait()

The credits goes to:

print("Credits Goes To: HAVARD CS50 COURSE - DAVID MALAN - https://youtu.be/d6ZcOxZYh4Y")Credits Goes To: HAVARD CS50 COURSE - DAVID MALAN - https://youtu.be/d6ZcOxZYh4Y

👉Github (PPY-12)

Related Posts

00#Episode#PurePythonSeries — Lambda in Python — Python Lambda Desmistification

01#Episode#PurePythonSeries — Send Email in Python — Using Jupyter Notebook — How To Send Gmail In Python

02#Episode#PurePythonSeries — Automate Your Email With Python & Outlook — How To Create An Email Trigger System in Python

03#Episode#PurePythonSeries — Manipulating Files With Python — Manage Your Lovely Photos With Python!

04#Episode#PurePythonSeries — Pandas DataFrame Advanced — A Complete Notebook Review

05#Episode#PurePythonSeries — Is This Leap Year? Python Calendar — How To Calculate If The Year Is Leap Year and How Many Days Are In The Month

06#Episode#PurePythonSeries — List Comprehension In Python — Locked-in Secrets About List Comprehension

07#Episode#PurePythonSeries — Graphs — In Python — Extremely Simple Algorithms in Python

08#Episode#PurePythonSeries — Decorator in Python — How To Simplifying Your Code And Boost Your Function

12#Episode#PurePythonSeries — Advanced Python Technologies — qrcode, Speech Recognition in Python, Google Speech Recognition #PurePythonSeries (this one)

--

--

J3
Jungletronics

Hi, Guys o/ I am J3! I am just a hobby-dev, playing around with Python, Django, Ruby, Rails, Lego, Arduino, Raspy, PIC, AI… Welcome! Join us!