Open In App

How to make a voice unlock system in Python?

Last Updated : 18 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A voice-based unlock system for our computer will be the application that will take our voice as input and process that voice converts it into a text-based instruction for our computer and then perform an action based on that instruction. This process uses The State-of-the-art process in a speech to text, Natural Language Understanding, and Deep Learning in the text to speech.

The first step to build a voice-based application in our application is to listen to the user’s voice constantly and then transcribe that voice into text-based instruction.

This is difficult to create a voice to text transcription engine with the higher accuracy as we need to train our data model for that also there are a lot of industry leaders are available as Google, Microsoft, Apple, and some others are providing their API based services which can be easily integrated with the applications. Google also offers voice action which is an API-based service to perform an action within the app seamlessly using voice.

If you are interested to develop your own speech-to-text application, please go through it. Now for building your own python based application please do follow these steps :

1. Import libraries

Python3




import sys
import ctypes
import speech_recognition as speech


  • Library ctypes is a foreign function library for Python. Which provides C compatible data types and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.
  • Speech Recognition is an important feature in several applications used such as home automation, artificial intelligence, etc.

2. Audio by the Microphone

Now obtain the audio from your microphone using the Recognizer() function of python’s speech_recognition module. In this way, we have captured the voice command from our system.

Python3




voice = speech.Recognizer()
with speech.Microphone() as source:
    print("Say something!")
    voice_command = voice.listen(source)


3. Check command

Now the time is to use the Google speech recognition engine note this point instead of using this API you can use your own voice recognition system. And you need to handle some exceptions via your code for errors coming from the Google engine.

Python3




try:
    command=voice.recognize_google(voice_command)
     
# handle the exceptions
except speech.UnknownValueError:
    print("Google Speech Recognition system could not understand your \
    instructions please give instructions carefully")
 
except speech.RequestError as e:
    print("Could not request results from Google Speech Recognition\
    service; {0}".format(e))


4. Validate Command

Now the time is to match and compare the pattern of the voice command. And load and execute the command to lock the PC. This Function for locking the pc is available in the default library (user32.dll) of windows. And you can use Linux also but the command will be different for this operation and the default library will be available on (cell.user32) as well.

Python3




if command == "lock my PC":
    ctypes.windll.user32.LockWorkStation()
     
elif command == "stop":
    sys.exit(0)
     
else:
    print("Not a command.")
    fxn()


Below is the Implementation.

Here, we will form a function (method) that repeatedly calls itself until the required commands.

  • If the command is to lock pc then it locks pc.
  • If the command is a stop the program then it stops the program.
  • Otherwise, It says not a coded command and runs again.

Python3




# importing packages
import sys
import ctypes
import speech_recognition as speech
 
# define function
def fxn():
   
    # voice recognizer object
    voice = speech.Recognizer()
 
    # use microphone
    with speech.Microphone() as source:
        print("Say something!")
        voice_command = voice.listen(source)
 
    # check input
    try:
        command = voice.recognize_google(voice_command)
        print(command)
         
    # handle the exceptions
    except speech.UnknownValueError:
        print("Google Speech Recognition system could not\
        understand your instructions please give instructions carefully")
         
    except speech.RequestError as e:
        print(
            "Could not request results from Google Speech Recognition \
            service; {0}".format(e))
 
    # validate input
    if command == "lock my PC":
        ctypes.windll.user32.LockWorkStation()
         
    elif command == "stop":
        sys.exit(0)
         
    else:
        print("Not a command.")
        fxn()
 
# execute
fxn()


Output: 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads