-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
141 lines (118 loc) · 4.08 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import tkinter as tk
from tkinter import filedialog, messagebox
import speech_recognition as sr
import os
from time import sleep
# Import local modules
from extract_text import extract
from create_document import create
from send_mail import upload
# Function to open the file for verification
def verify(name=None):
# To open files redirected form other function after creation
if name:
p = "F:/Mini Projects Rishi/voice prescription/{}.docx".format(name)
# Manual verification of existing files
else:
filename = filedialog.askopenfilename(initialdir="F:\\Mini Projects Rishi\\voice prescription", title="Select file", filetypes=(("document files", "*.docx"), ("all files", "*.*")))
p = filename
# Split path and file name
a = os.path.split(p)
try:
# Opens file
os.startfile(a[0] + '/' + a[1])
sleep(10)
# Call upload function to send prescription through mail
upload(p)
except:
messagebox.showerror("ERROR", "Could not open file")
# Function for existing audio file
def doc():
# Open existing audio file
filename = filedialog.askopenfilename(initialdir="F:\\Mini Projects Rishi\\voice prescription", title="Select file", filetypes=(("wav files", "*.wav"),("all files", "*.*")))
# split path and filename
a = os.path.split(filename)
a = a[0] + "/" + a[1]
r = sr.Recognizer()
try:
with sr.AudioFile(a) as source:
audio = r.listen(source)
except:
messagebox.showerror("ERROR","Could not open file, please select .wav file")
return
# Convert audio to text
try:
text = r.recognize_google(audio)
messagebox.showinfo("Alert", "Converted Text: {}".format(text))
except:
messagebox.showerror("ERROR","Could not recognize the voice")
# Extract text
try:
name, age, date, tablet = extract(text)
except ValueError:
messagebox.showerror("ERROR","Could not find patient Name")
except:
messagebox.showerror("ERROR","Could not extract text")
# Create document
try:
create(name, age, date, tablet)
except PermissionError:
messagebox.showerror("ERROR","The document is open, please close and try again!")
except:
messagebox.showerror("ERROR","Could not create document")
# Verify document
try:
verify(name)
except:
messagebox.showerror("ERROR","Could not open file")
# Function for live recording
def listen():
r = sr.Recognizer()
with sr.Microphone() as source:
messagebox.showinfo("Alert", "Start Speaking")
audio = r.listen(source)
messagebox.showinfo("Alert", "completed.")
# Convert audio to text
try:
text = r.recognize_google(audio)
messagebox.showinfo("Alert", "Converted Text: {}".format(text))
except:
messagebox.showerror("ERROR","Could not recognize the voice")
# Extract text
try:
name, age, date, tablet = extract(text)
except ValueError:
messagebox.showerror("ERROR","Could not find patient Name")
except:
messagebox.showerror("ERROR","Could not extract text")
# Create document
try:
create(name, age, date, tablet)
except PermissionError:
messagebox.showerror("ERROR","The document is open, please close and try again!")
except:
messagebox.showerror("ERROR","Could not create document")
# Verify document
try:
verify(name)
except:
messagebox.showerror("ERROR","Could not open file")
# Create GUI main window
main_window = tk.Tk()
main_window.geometry('300x300')
main_window.configure(background='#54a8cc')
main_window.title('Voice Prescription')
try:
# Add buttons for every functions
btn1 = tk.Button(main_window, text='Upload Audio File', width=25, command=doc, bg="#eb7e44")
btn2 = tk.Button(main_window, text='Start new recording', width=25, command=listen, bg="#eb7e44")
btn4 = tk.Button(main_window, text='Verify document', width=25, command=verify, bg="#eb7e44")
btn3 = tk.Button(main_window, text='Send Document through mail', width=25, command=upload, bg="#eb7e44")
btn1.pack(pady=20)
btn2.pack(pady=20)
btn4.pack(pady=20)
btn3.pack(pady=20)
except:
messagebox.showinfo("Error", "Something went wrong, please try again")
main_window.mainloop()
main_window.destroy()