-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbraille.py
203 lines (188 loc) · 6.91 KB
/
braille.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/local/bin/python3
# -*- coding: utf8 -*-
# Written by Aadit Trivedi
# June 6, 2018
# Braille Library
# # Dependencies
# 1) sudo apt-get install pyaudio
# 3) sudo apt-get install espeak
# 2) pip3 install SpeechRecognition
# 3) pip3 install numpy
# 4) pip3 install pillow
# 5) sudo apt-get install pytesseract
# 6) sudo apt-get install opencv-python
# 7) pip3 install opencv
# 8) pip3 install matplotlib
# Upload to GitHub
import numpy as np
import os
from PIL import Image
from pytesseract import image_to_string
import matplotlib.pyplot as plt
import PIL
from model import charToArray, asciicodes, brailles
ascii_braille = {}
arrayLength = len(asciicodes)
counter = 0
while counter < arrayLength:
ascii_braille[asciicodes[counter]] = brailles[counter]
counter = counter + 1
letterToImgPath = {
"a": "images/a.png",
"b": "images/b.png",
"c": "images/c.png",
"d": "images/d.png",
"e": "images/e.png",
"f": "images/f.png",
"g": "images/g.png",
"h": "images/h.png",
"i": "images/i.png",
"j": "images/j.png",
"k": "images/k.png",
"l": "images/l.png",
"m": "images/m.png",
"n": "images/n.png",
"o": "images/o.png",
"p": "images/p.png",
"q": "images/q.png",
"r": "images/r.png",
"s": "images/s.png",
"t": "images/t.png",
"u": "images/u.png",
"v": "images/v.png",
"w": "images/w.png",
"x": "images/x.png",
"y": "images/y.png",
"z": "images/z.png",
" ": "images/void.png",
}
def addImages(list_im):
imgs = [ PIL.Image.open(i) for i in list_im ]
min_shape = sorted( [(np.sum(i.size), i.size ) for i in imgs])[0][1]
imgs_comb = np.hstack( (np.asarray( i.resize(min_shape) ) for i in imgs ) )
imgs_comb = PIL.Image.fromarray(imgs_comb)
imgs_comb.save('output.jpg')
def writeImage(b_string):
images = []
for letter in b_string:
images.append(letterToImgPath[letter])
addImages(images)
img = Image.open('output.jpg')
img.show()
def writeText(b_string):
final_string = ''
for letters in b_string:
final_string = final_string + ascii_braille[letters.lower()]
print(final_string)
def textToBraille(text):
final_string = ''
for char in text:
char = char.lower()
if char == "a":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["a"]))
elif char == "b":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["b"]))
elif char == "c":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["c"]))
elif char == "d":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["d"]))
elif char == "e":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["e"]))
elif char == "f":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["f"]))
elif char == "g":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["g"]))
elif char == "h":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["h"]))
elif char == "i":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["i"]))
elif char == "j":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["j"]))
elif char == "k":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["k"]))
elif char == "l":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["l"]))
elif char == "m":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["m"]))
elif char == "n":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["n"]))
elif char == "o":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["o"]))
elif char == "p":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["p"]))
elif char == "q":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["q"]))
elif char == "r":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["r"]))
elif char == "s":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["s"]))
elif char == "t":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["t"]))
elif char == "u":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["u"]))
elif char == "v":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["v"]))
elif char == "w":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["w"]))
elif char == "x":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["x"]))
elif char == "y":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["y"]))
elif char == "z":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray["z"]))
elif char == " ":
final_string = final_string + ascii_braille[char]
print(char + " " + str(charToArray[" "]))
print(final_string)
return final_string
def textToSpeech(text):
if ((os.system("espeak '" + str(text) + "'")) != 0):
print("Client error: system does not support text-to-audio // may need to download [espeak] command line package")
def brailleToTextArray(array):
new_chars = ''
for key in array:
for a_key in charToArray:
if charToArray[a_key] == key:
new_chars = new_chars + str(a_key)
print(new_chars)
return new_chars
def brailleToSpeechArray(array):
textToSpeech(brailleToTextArray(array))
def brailleToSpeechImg(imgs):
for img in imgs:
for chars in letterToImgPath:
if img == letterToImgPath[chars]:
print(chars)
def imageToText(img):
return image_to_string(Image.open(img))
def imageToSpeech(img):
textToSpeech(imageToText(img))
def imageToBraille(img):
textToBraille(imageToText(img))
imageToText("puretext.jpeg")