-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelpBlind.py
220 lines (152 loc) · 5.3 KB
/
HelpBlind.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import streamlit as st
import os
from width_control import *
import easyocr
import cv2
from gtts import gTTS
#import time
#configuration
st.set_page_config(
page_title=("Help blind people"),
page_icon=":sunglasses:",
layout="centered",
initial_sidebar_state="collapsed"
)
select_block_container_style()
cache = open("cache.txt",'r')
startNum = cache.read()
cache.close()
language = 'en'
#make cache file which store text image
if not os.path.isdir("image_text_detection"):
os.mkdir("image_text_detection")
imgList = (os.listdir("image_text_detection"))
tempForStatus = []
for img in imgList:
tempForStatus.append(int((img.split('.jpg'))[0]))
#find the largest num in tempForStatus as temp_status
temp_status = max(tempForStatus)
#header
st.info('Hope this application can help the blinds :smile:')
st.markdown('# Help the blind application :sunglasses:')
st.write('Blind Life Matter')
st.image('images/blind.jpg', use_column_width=False, channels='RGB',output_formats=True)
st.markdown("<br>",unsafe_allow_html=True)
#create cache file with startNum = 0
#if not os.path.exists("cache.txt"):
# os.mkdir("cache.txt")
if st.button("Clear Cache"):
cache = open("cache.txt","w+")
cache.write('0')
cache.close()
cache = open("cache.txt",'r')
startNum = cache.read()
cache.close()
cache = open("detectedWord.txt","w+")
cache.close()
st.header("Camera for text detetction :camera:")
#place WebCam
vedioStart = False
vedioMessage = st.empty()
capturedImg = st.empty()
image = st.empty()
image_placeholder = st.empty()
video = cv2.VideoCapture(0)
col1,col2,col3,temp1,temp2,temp3 = st.beta_columns(6)
with col1:
startButton = st.button(' Start ',key='start-btn')
with col2:
captureButton = st.button('capture',key='capture-btn')
with col3:
stopButton = st.button(' stop ',key='stop-btn')
if stopButton:
#change start num = 0 in txt
cache = open("cache.txt","w+")
cache.write('0')
cache.close()
cache = open("cache.txt",'r')
startNum = cache.read()
cache.close()
video.release()
cv2.destroyAllWindows()
vedioStart = False
if captureButton:
if startNum == '1':
vedioStart = True
if (vedioStart):
vedioMessage = st.empty()
boolean, image = video.read()
capturedImg = image
st.image(capturedImg)
temp_status += 1
img_name = '{}.jpg'.format(str(temp_status))
filelocation = "image_text_detection/{}".format(img_name)
cv2.imwrite(filelocation, capturedImg)
st.success('Image Captured!')
st.info("Press STOP to do text detection")
if vedioStart == False:
vedioMessage = st.error("Haven't turn on camera")
if startButton or startNum=='1':
#change startNum = 1 in txt
cache = open("cache.txt","w+")
cache.write('1')
cache.close()
cache = open("cache.txt",'r')
startNum = cache.read()
cache.close()
vedioStart = True
vedioMessage = st.empty()
while True:
success, image = video.read()
image_placeholder.image(image,use_column_width=False)
st.markdown("<br><br>",unsafe_allow_html=True)
st.header(" Detected Texts :abc:")
detectedText = st.button('detect latest image', key='detect_text-btn')
#display recognised text
if detectedText:
#Activate Model
reader = easyocr.Reader(['en'],gpu=False) # need to run only once to load model into memory
st.success('Model Activated')
imgList = (os.listdir("image_text_detection"))
tempForSort = []
for img in imgList:
tempForSort.append(int((img.split('.jpg'))[0]))
#find the largest num in tempForSort then find the index number
largestNum = max(tempForSort)
indexOfLargest = tempForSort.index(largestNum)
imgList = (os.listdir("image_text_detection"))
last_img = "image_text_detection/"+imgList[indexOfLargest]
detectedWord = reader.readtext(last_img, detail = 0)
wordNum = len(detectedWord)
if wordNum == 1:
st.info("1 word detected")
elif wordNum > 1:
st.info(str(wordNum)+" words detected")
elif wordNum == 0:
st.info("No word detected")
for word in detectedWord:
st.text(word)
cache = open("detectedWord.txt","w+")
for word in detectedWord:
cache.write(str(word))
cache.write(" ")
cache.close()
#text to speech
st.markdown("<br>",unsafe_allow_html=True)
st.header(" Text to speech :loudspeaker:")
text_to_speech = st.button("Latest text to speech")
voice = st.empty()
if text_to_speech:
cache = open("detectedWord.txt","r")
text = cache.read()
cache.close()
converter = gTTS(text=text, lang=language, slow=False)
converter.save("detectedText_to_speech.mp3")
song = "detectedText_to_speech.mp3"
voice = st.audio(song)
st.markdown("<br><br><hr><br>",unsafe_allow_html=True)
###############################################################
# Hew code
###############################################################
st.header("Live Object Detection")
#%%