#python #audio #speech-recognition #speech-to-text
Вопрос:
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog as fd
from tkinter.messagebox import showinfo
import tkinter as tk
import speech_recognition as sr
import nltk
# create the root window
root = tk.Tk()
root.title('Tkinter File Dialog')
root.resizable(False, False)
root.geometry('300x150')
def select_files():
filetypes = (
('audio files', '*.wav'),
)
file = fd.askopenfilenames(
title='Open file',
initialdir='/',
filetypes=filetypes)
showinfo(
title='Selected File',
message=file
)
r = sr.Recognizer()
if file is not None:
tk.messagebox.showinfo("result","file added successfully")
with file as source:
audiof = r.record(source, duration=20)
resultf = r.recognize_google(audiof)
contentf = file.read()
with open("runtimefile.txt", "w") as text_file:
text_file.write("%s" % resultf)
with open("runtimefile.txt", "r") as file:
file_content = file.read()
runtokens = nltk.word_tokenize(file_content)
print("tokens for runtime file", runtokens)
text_file.close()
# open button
open_button = ttk.Button(
root,
text='Open Files',
command=select_files
)
open_button.pack(expand=True)
root.mainloop()
Exception in Tkinter callback
Traceback (most recent call last):
File "C:UsersTaniAnaconda3envspythonProjectlibtkinter__init__.py",
line 1892, in __call__
return self.func(*args)
File "C:/Users/Tani/PycharmProjects/pythonProject/microphone.py", line 33, in
select_files
with file as source:
AttributeError: __enter__
я пытаюсь взять аудиофайл в качестве входных данных во время выполнения на python, а затем прочитать этот файл, преобразовав его в текстовый файл и обозначив содержимое файла, который был взят в качестве входных данных, но система выдает мне ошибку, подобную упомянутой выше
пожалуйста, предложите мне решение для этого