-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
188 lines (161 loc) · 4.99 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import sys, getopt
# Import Mocks
from lib.speech.fake_text_to_speech import FakeTextToSpeech
from lib.ocr.mock_ocr import FakeOCR
from lib.camera.fake_img_provider import FakeImageProvider
from lib.io.button.pc_button import PCTriggerButton
def start(
tts,
img_provider,
trigger_button,
ocr,
):
from lib.preprocessing.paper_crop import get_lines_from_img
while True:
# Wait for the user to press the button
trigger_button.wait_for_trigger()
img = []
# Capture the image
try:
img = img_provider.get_img(save_image=True, verbose=True)
except:
message = "Could not capture the image"
print(f"⚠ {message}")
tts.say(message)
pass
# get image lines
img_lines = get_lines_from_img(img, display_img=False)
# logging
line_num = len(img_lines)
print(f"\n\n Found {line_num} lines!")
# Break down the image into paragraph for more performant processing
if len(img_lines) > 0:
for line, line_number in zip(img_lines, range(line_num)):
print(f"Procesing line {line_number + 1} / {line_num}")
text = ocr.get_text(line, verbose=True)
print(text)
tts.say(text)
print("/n/n======== OVER =========/n")
tts.say("\n\nOver")
else:
tts.say("Error, no paper was detected")
def get_ocr(arg=""):
print("Intializing OCR...", end="\t\t")
if arg == "real" or arg == "r":
print("Real", end=" ")
try:
from lib.ocr.tesseract_ocr import OCR
ocr = OCR()
except:
print("❌")
return
else:
print("Fake", end=" ")
ocr = FakeOCR()
print("✅")
return ocr
def get_tts(arg=""):
print("Intializing TTS...", end="\t\t")
if arg == "real" or arg == "r":
print("Real", end=" ")
try:
from lib.speech.text_to_speech import TextToSpeech
tts = TextToSpeech()
except:
print("❌")
return
else:
print("Fake", end=" ")
tts = FakeTextToSpeech()
print("✅")
return tts
def get_img_provider(arg=""):
print("Intializing Image Provider...", end="\t")
if arg == "real" or arg == "r":
print("Real", end=" ")
try:
from lib.camera.img_provider import ImageProvider
img_provider = ImageProvider()
except:
print("❌")
return
else:
print("Real", end=" ")
img_provider = FakeImageProvider()
print("✅")
return img_provider
def get_trigger_button(arg=""):
print("Intializing Trigger Button...", end="\t")
if arg == "real" or arg == "r" or arg == "physical":
print("Real", end=" ")
try:
from lib.io.button.trigger_button import TriggerButton
trigger_button = TriggerButton()
except:
print("❌")
return
else:
print("Fake", end=" ")
trigger_button = PCTriggerButton()
print("✅")
return trigger_button
def main(argv):
tts_arg = ""
img_provider_arg = ""
trigger_button_arg = ""
ocr_arg = ""
try:
opts, args = getopt.getopt(
argv,
"p:o:s:i:b:",
["preset=", "ocr=", "tts=", "img_prov=", "trigger_button="],
)
except getopt.GetoptError:
help_msg = (
"Please use the proper arguments:\n"
"--------------------------------\n"
"-p, --preset\t Uses one preset f\nor options.\n"
"-o\n"
)
print()
# Apply presets first
for opt, arg in opts:
if opt in ("-p", "--preset"):
# The pi_real preset runs everything as it should in the final
# product
if arg == "pi_real":
tts_arg = "r"
img_provider_arg = "r"
trigger_button_arg = "r"
ocr_arg = "r"
# The pc preset runs everything using real components except for
# the camera and the image provider
elif arg == "pc":
tts_arg = "r"
ocr_arg = "r"
elif arg == "demo":
tts_arg = "r"
trigger_button_arg = "r"
ocr_arg = "r"
# Apply individual options then
for opt, arg in opts:
if opt in ("-o", "--ocr"):
ocr_arg = arg
elif opt in ("-s", "--tts"):
tts_arg = arg
elif opt in ("-i", "--img_prov"):
img_provider_arg = arg
elif opt in ("-b", "--trigger_button"):
trigger_button_arg = arg
tts = get_tts(tts_arg)
img_provider = get_img_provider(img_provider_arg)
trigger_button = get_trigger_button(trigger_button_arg)
ocr = get_ocr(ocr_arg)
start(
tts,
img_provider,
trigger_button,
ocr,
)
if __name__ == "__main__":
main(sys.argv[1:])