Wednesday, December 25, 2024
Google search engine
HomeLanguagesVoice Assistant for Movies using Python

Voice Assistant for Movies using Python

In this article, we will see how a voice assistant can be made for searching for movies or films. After giving input as movie name in audio format and it will give the information about that movie in audio format as well.

As we know the greatest searching website for movies is IMDb. IMDb is an online database of information related to films, television programs, home videos, video games, and streaming content online including cast, production crew, and personal biographies, plot summaries, trivia, ratings, and fan, and critical reviews.

Requirements:

  • IMDbPY: It is a Python package useful to retrieve and manage the data of the IMDb movie database about movies, people, characters, and companies. It can be installed using the below command:
pip install IMDbPY
  • pyttsx3: It is a text-to-speech conversion library in Python. Unlike alternative libraries, it works offline and is compatible with both Python 2 and 3. It can be installed using the below command:
pip install pyttsx3
  • SpeechRecognition: Library for performing speech recognition, with support for several engines and APIs, online and offline. It can be installed using the below command:
pip install SpeechRecognition
  • Datetime: Encapsulation of date/time values. It can be installed using the below command:
pip install DateTime

Approach:

  1. Import required modules.
  2. Create the below functions:
    • speak( ): This function will help our assistant to speak up.
    • get_audio( ): This function will help the assistant to get input from the user.
    • get_movies( ): This function will help the assistant to search for the movie that is given as input.
  3. Create a new function search_movie( ) to search a movie by using the above function calls.
  4. Call the above-created function.

Below is the Implementation.

Python3




# importing all required libraries
import imdb
import pyttsx3
import speech_recognition as sr
import datetime
 
 
# Function for speaking
def speak(text):
    engine = pyttsx3.init()
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[1].id)
    rate = engine.getProperty('rate')
 
    engine.setProperty('rate', rate-20)
 
    engine.say(text)
    engine.runAndWait()
 
 
# calling the speak() function
speak("Say the movie name")
 
 
# Function to get input in the audio format
def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        r.pause_threshold = 1
        r.adjust_for_ambient_noise(source, duration=1)
        audio = r.listen(source)
        said = ""
 
    try:
 
        # will recognize the input
        said = r.recognize_google(audio)
        print(said)
 
    except:
        speak("Didn't get that")
    # will return the input in lowercase
    return said.lower()
 
 
# Function for searching movie
def search_movie():
   
    # gathering information from IMDb
    moviesdb = imdb.IMDb()
 
    # search for title
    text = get_audio()
 
    # passing input for searching movie
    movies = moviesdb.search_movie(text)
 
    speak("Searching for " + text)
    if len(movies) == 0:
        speak("No result found")
    else:
 
        speak("I found these:")
 
        for movie in movies:
 
            title = movie['title']
            year = movie['year']
            # speaking title with releasing year
            speak(f'{title}-{year}')
 
            info = movie.getID()
            movie = moviesdb.get_movie(info)
 
            title = movie['title']
            year = movie['year']
            rating = movie['rating']
            plot = movie['plot outline']
 
            # the below if-else is for past and future release
            if year < int(datetime.datetime.now().strftime("%Y")):
                speak(
                    f'{title}was released in {year} has IMDB rating of {rating}.\
                    The plot summary of movie is{plot}')
                print(
                    f'{title}was released in {year} has IMDB rating of {rating}.\
                    The plot summary of movie is{plot}')
                break
 
            else:
                speak(
                    f'{title}will release in {year} has IMDB rating of {rating}.\
                    The plot summary of movie is{plot}')
                print(
                    f'{title}will release in {year} has IMDB rating of {rating}.\
                    The plot summary of movie is{plot}')
                break
 
 
search_movie()


The above code will speak the information about the film as the user gave the input and also print about it.

Output:

parasite

Parasitewas released in 2019 has IMDB rating of 8.6.

The plot summary of movie isThe Kims – mother and father Chung-sook and Ki-taek, and their young adult offspring, son Ki-woo and daughter Ki-jung – are a poor family living in a shabby and cramped half basement apartment in a busy lower working class commercial district of Seoul. 

Without even knowing it, they, especially Mr. and Mrs. Kim, literally smell of poverty. Often as a collective, they perpetrate minor scams to get by, and even when they have jobs, they do the minimum work required. Ki-woo is the one who has dreams of getting out of poverty by one day going to university. 

Despite not having that university education, Ki-woo is chosen by his university student friend Min, who is leaving to go to school, to take over his tutoring job to Park Da-hye, who Min plans to date once he returns to Seoul and she herself is in university.

The Parks are a wealthy family who for four years have lived in their modernistic house designed by and the former residence of famed architect Namgoong. While Mr. and Mrs. Park are all about status, Mrs. Park has a flighty, simpleminded mentality and temperament,

which Min tells Ki-woo to feel comfortable in lying to her about his education to get the job. In getting the job, Ki-woo further learns that Mrs. Park is looking for an art therapist for the Parks’ adolescent son, Da-song, Ki-woo quickly recommending his professional art therapist friend “Jessica”, 

really Ki-jung who he knows can pull off the scam in being the easiest liar of the four Kims. In Ki-woo also falling for Da-hye, he begins to envision himself in that house, and thus the Kims as a collective start a plan for all the Kims, like Ki-jung using assumed names, to replace existing servants in the Parks’ employ in orchestrating reasons for them to be fired.

The most difficult to get rid of may be Moon-gwang, the Parks’ housekeeper who literally came with the house – she Namgoong’s housekeeper when he lived there – and thus knows all the little nooks and crannies of it better than the Parks themselves. The question then becomes how far the Kims can take this scam in their quest to become their version of the Parks.

Code Explanation:

  1. The code first creates a new instance of the sr.Recognizer class.
  2. This class allows the programmer to access all of the features of the Google Speech Recognition engine.
  3. Next, the code sets up some basic parameters for the microphone.
  4. The first parameter is called pause_threshold and it tells the recognizer how much noise to ignore before pausing to listen for speech.
  5. The second parameter is called adjust_for_ambient_noise and it helps to compensate for background noise in the room.
  6. The next step is to get input from the microphone into an audio object.
  7. The code uses the sr.listen method to capture any sound that comes out of the microphone.
  8. Once a sound has been captured, it can be stored in a variable named audio .
  9. The next line of code will recognize any spoken words that are captured by the microphone using Google’s speech recognition engine.
  10. If everything goes according to plan, then said will contain a string containing recognized text from Google’s speech recognition engine.
  11. If something goes wrong during this process, then speak() will be called with an error message as its only argument ( Didnt get that ).
  12. The code will open a Recognizer object and set the pause_threshold to 1.
  13. This will allow the user to hear any audio they are inputting in, but it won’t stop until they hit the pause_threshold.
  14. Next, the code will adjust for ambient noise and listen for input from the microphone.
  15. Once it has detected an audio signal, it will print out what was recognized.
  16. If there is an error, the code will speak an error message instead.
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments