Voice Assistant for Movie Search Using Python

In this guide, we will explore how to create a voice-activated assistant that can search for movie details by taking audio input from the user and providing the movie information as audio output.

The assistant utilizes IMDb, a popular online movie database, to fetch detailed information about films. When a user speaks the name of a movie, the assistant will process the input and return relevant information like the movie’s rating, release year, and plot summary through speech output.

About IMDb

IMDb (Internet Movie Database) is one of the largest online repositories of information about films, TV shows, video games, and other entertainment media. It includes details on the cast, crew, plot summaries, ratings, reviews, and even trivia about various productions. By leveraging the IMDb API through Python, we can create an automated system that interacts with this vast database.

Requirements

To build the voice assistant, you’ll need the following Python libraries:

  1. IMDbPY: A Python package that allows access to the IMDb movie database, enabling the retrieval of information on movies, actors, production teams, and more.

Install it with:

pip install IMDbPY

pyttsx3: A text-to-speech library in Python that works offline and is compatible with both Python 2 and Python 3. It converts text input into speech, enabling the assistant to “speak” to the user.

  • Install it with:
pip install pyttsx3

SpeechRecognition: This library allows the assistant to interpret speech input using various speech recognition engines. It can process audio input both online and offline.

  • Install it with:
pip install SpeechRecognition

Datetime: A standard Python library that handles date and time operations, useful for comparing movie release dates with the current date.

  • Install it with:
pip install DateTime

Approach

The overall approach to building this voice assistant involves the following steps:

  1. Import Required Libraries: Start by importing all the necessary Python modules for handling speech recognition, text-to-speech conversion, interaction with IMDb, and date-time operations.
  2. Create Essential Functions: Several functions need to be defined to make the assistant work:
    • speak(text): This function will handle text-to-speech conversion. It takes a string input and uses pyttsx3 to output it as speech.
    • get_audio(): This function will capture the user’s voice input through a microphone, convert it to text using speech recognition, and return it as a string.
    • search_movie(): This function ties everything together. It uses get_audio() to take the movie name as input, searches for the movie on IMDb, and provides information such as title, release year, IMDb rating, and plot summary. The information is then relayed to the user using the speak() function.
  3. Implementation: The core logic is implemented in the search_movie() function. This function:
    • Listens for the movie name using get_audio().
    • Searches for the movie on IMDb.
    • Retrieves detailed information such as the title, release year, IMDb rating, and plot summary.
    • Speaks the movie details using speak().
  4. Run the Assistant: Finally, after defining the required functions, we call the search_movie() function to activate the assistant, allowing it to take audio input and respond with movie details.

Full Source Code Below:

# 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 = "Hey Baby"

    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()

Output:

Explanation of the above code:

  1. Importing Required Libraries:
    • imdb: A Python package that allows interaction with the IMDb database to search for movies, retrieve information, etc.
    • pyttsx3: A text-to-speech conversion library, which will enable the assistant to speak the results to the user.
    • speech_recognition: A library that allows the program to capture voice input from the user and convert it into text.
    • datetime: The standard Python module for working with dates and times, used here to compare movie release years with the current year.
  2. Function for Speaking (speak function):
    • The function initializes a text-to-speech engine using pyttsx3.init().
    • It retrieves available voices using engine.getProperty('voices'), then selects the second voice (voices[1]), which is often a female voice depending on the system setup.
    • The speech rate is adjusted by reducing it by 20 using engine.setProperty('rate', rate-20) to make the speech slower.
    • The engine.say(text) command converts the input text into speech.
    • The engine.runAndWait() ensures that the speech is executed before continuing to the next command.
  3. Calling speak() to Prompt User:
    • The speak() function is called with the text "Say the movie name", prompting the user to say the name of a movie. The assistant speaks this text.
  4. Function to Get Audio Input (get_audio function):
    • An instance of the speech recognizer (sr.Recognizer()) is created to process audio input.
    • Using a microphone (sr.Microphone()), the function listens to the user’s voice, with r.pause_threshold set to 1 second of silence to signal the end of speech. r.adjust_for_ambient_noise() adjusts for any background noise to improve the accuracy of recognition.
    • The r.listen() method listens for the audio input.
    • A variable said is initialized with a default value of "Hey Baby" (this will be overridden by recognized speech).
    • The program tries to recognize the spoken input using r.recognize_google(audio), which utilizes Google’s speech recognition engine.
    • If successful, the recognized text is printed. If not, an exception occurs, and the assistant says “Didn’t get that” using the speak() function.
    • Finally, the function returns the recognized input in lowercase format.
  5. Function to Search Movies (search_movie function):
    • The imdb.IMDb() object is initialized to access the IMDb database.
    • The get_audio() function is called to capture the movie name from the user, which is stored in the text variable.
    • The search_movie(text) method of IMDb is used to search for the movie by title, and the search results are stored in the movies variable.
    • The assistant speaks, “Searching for [movie name]” to confirm the search action.
    • If no results are found (i.e., len(movies) == 0), the assistant says, “No result found.”
    • If results are found, the assistant says, “I found these:” and then iterates through the list of search results:
      • For each movie in the search results, it retrieves the movie’s title and release year.
      • The assistant speaks the movie title and release year.
      • The movie’s unique IMDb ID is fetched using movie.getID(), and detailed information about the movie is retrieved using moviesdb.get_movie(info).
      • The movie’s title, release year, IMDb rating, and plot summary are retrieved from the database.
  6. Handling Past and Future Releases:
    • If the release year of the movie is earlier than the current year (year < int(datetime.datetime.now().strftime("%Y"))), the assistant speaks the release information, IMDb rating, and plot summary, indicating the movie was released in the past.
    • If the movie is set to release in the future, the assistant speaks the upcoming release information and rating.
    • The results are also printed to the console.
    • The loop breaks after finding and speaking details for the first movie in the search results.
  7. Calling the search_movie() Function:
    • Finally, the search_movie() function is called to execute the full process: capturing the user’s speech, searching IMDb, and delivering movie details as output.

Key Flow:

  1. Import libraries.
  2. Use speak() to prompt the user to say a movie name.
  3. Capture the audio input using get_audio().
  4. Search IMDb for the movie using search_movie().
  5. Provide movie details as speech output and print them to the console.

The voice assistant for movie search using Python is an excellent example of how modern technologies like speech recognition, text-to-speech conversion, and movie databases can work together to create an interactive and user-friendly experience. By leveraging libraries such as IMDbPY, pyttsx3, and SpeechRecognition, this assistant allows users to simply speak the name of a movie and instantly receive key details such as release date, IMDb rating, and plot summary.

The project highlights how Python’s flexibility and rich ecosystem of libraries can simplify complex tasks like interacting with online databases, converting speech to text, and providing output in natural language. The combination of voice interaction and movie data retrieval from IMDb enables a seamless experience, particularly for users who prefer auditory responses over traditional typing and reading.

Additionally, this project offers significant learning opportunities for developers, from understanding how to handle voice input and output to working with APIs and databases. The use of error handling to manage unrecognized speech ensures a smooth interaction, making the application robust and practical. Whether applied to home automation systems, personal assistants, or entertainment solutions, such voice assistants have the potential to enhance user interaction in various contexts.

In conclusion, this voice assistant not only provides practical utility but also demonstrates the powerful capabilities of Python in integrating multiple technologies. It serves as a solid foundation for future enhancements, such as adding support for more complex queries, multiple language recognition, or broader datasets beyond movies, making it a versatile tool for various applications in the evolving field of voice-driven software.

Author

Sona Avatar

Written by

Leave a Reply

Trending

CodeMagnet

Your Magnetic Resource, For Coding Brilliance

Programming Languages

Web Development

Data Science and Visualization

Career Section

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4205364944170772"
     crossorigin="anonymous"></script>