A Tkinter application that lets a user enter text, select an Amazon Polly voice, and hear the text spoken by the selected voice while an animated face lip-syncs along with it.
| 34 | |
| 35 | |
| 36 | class PollyMouth: |
| 37 | """ |
| 38 | A Tkinter application that lets a user enter text, select an Amazon Polly voice, |
| 39 | and hear the text spoken by the selected voice while an animated face lip-syncs |
| 40 | along with it. |
| 41 | """ |
| 42 | |
| 43 | # A dictionary of visemes mapped to image file names. |
| 44 | lips = { |
| 45 | "p": {"name": ".media/lips_m.png"}, |
| 46 | "t": {"name": ".media/lips_c.png"}, |
| 47 | "S": {"name": ".media/lips_ch.png"}, |
| 48 | "T": {"name": ".media/lips_th.png"}, |
| 49 | "f": {"name": ".media/lips_f.png"}, |
| 50 | "k": {"name": ".media/lips_c.png"}, |
| 51 | "i": {"name": ".media/lips_e.png"}, |
| 52 | "r": {"name": ".media/lips_r.png"}, |
| 53 | "s": {"name": ".media/lips_c.png"}, |
| 54 | "u": {"name": ".media/lips_w.png"}, |
| 55 | "@": {"name": ".media/lips_u.png"}, |
| 56 | "a": {"name": ".media/lips_a.png"}, |
| 57 | "e": {"name": ".media/lips_a.png"}, |
| 58 | "E": {"name": ".media/lips_u.png"}, |
| 59 | "o": {"name": ".media/lips_o.png"}, |
| 60 | "O": {"name": ".media/lips_u.png"}, |
| 61 | "sil": {"name": ".media/lips_sil.png"}, |
| 62 | } |
| 63 | |
| 64 | def __init__(self, polly_wrapper): |
| 65 | """ |
| 66 | Initializes the main Tkinter window and adds all of the widgets needed for |
| 67 | the application. |
| 68 | |
| 69 | :param polly_wrapper: An object that can call Amazon Polly API functions. |
| 70 | """ |
| 71 | self.polly_wrapper = polly_wrapper |
| 72 | self.app = tkinter.Tk() |
| 73 | self.app.title("Amazon Polly Lip Sync") |
| 74 | self.app.resizable(False, False) |
| 75 | |
| 76 | self.load_lips() |
| 77 | |
| 78 | choices_frame = tkinter.Frame(self.app) |
| 79 | |
| 80 | self.sayit_label = tkinter.Label( |
| 81 | self.app, |
| 82 | wraplength=410, |
| 83 | text="Write some text in the box below, then click 'Say it!' " |
| 84 | "to hear and see your text.", |
| 85 | ) |
| 86 | self.sayit_txt = tkinter.Text(self.app, width=50, height=16) |
| 87 | |
| 88 | self.engine_label = tkinter.Label(choices_frame, text="Engine:") |
| 89 | self.engine_var = tkinter.StringVar(choices_frame, "neural") |
| 90 | self.engine_options = tkinter.OptionMenu( |
| 91 | choices_frame, |
| 92 | self.engine_var, |
| 93 | *sorted(polly_wrapper.get_voice_engines()), |