使用Python 的电影语音助手
在本文中,我们将看到如何制作用于搜索电影或电影的语音助手。在以音频格式输入电影名称后,它也会以音频格式提供有关该电影的信息。
众所周知,最大的电影搜索网站是 IMDb。 IMDb 是一个在线数据库,包含与电影、电视节目、家庭视频、视频游戏和在线流媒体内容相关的信息,包括演员、制作人员、个人传记、情节摘要、琐事、收视率、粉丝和评论。
要求:
- IMDbPY:它是一个Python包,用于检索和管理 IMDb 电影数据库中有关电影、人物、字符和公司的数据。可以使用以下命令安装它:
pip install IMDbPY
- pyttsx3:它是Python的文本到语音转换库。与替代库不同,它可以离线工作并且与Python 2 和 3 兼容。可以使用以下命令安装它:
pip install pyttsx3
- SpeechRecognition:用于执行语音识别的库,支持多个引擎和 API,在线和离线。可以使用以下命令安装它:
pip install SpeechRecognition
- 日期时间:日期/时间值的封装。可以使用以下命令安装它:
pip install DateTime
方法:
- 导入所需的模块。
- 创建以下函数:
- speak():这个函数将帮助我们的助手说话。
- get_audio():该函数将帮助助手获取用户的输入。
- get_movies():这个函数将帮助助手搜索作为输入给出的电影。
- 创建一个新函数search_movie( )以使用上述函数调用来搜索电影。
- 调用上面创建的函数。
下面是实现。
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()
# serach 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()
上面的代码将在用户提供输入时说出有关电影的信息,并打印有关它的信息。
输出:
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.