forked from emcgill03/Facial-Recognition-FYP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pitextreader.py
211 lines (175 loc) · 5.08 KB
/
pitextreader.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
#!/usr/bin/python
#
# PiTextReader - Raspberry Pi Printed Text-to-Speech Reader
#
# Allows sight impaired person to have printed text read using
# OCR and text-to-speech.
#
# Normally run by pi crontab at bootup
# Turn off by commenting out @reboot... using $ crontab -e; sudo reboot
# Manually run using $ python pitextreader.py
#
# This is a simplistic (i.e. not pretty) python program
# Just runs cmd-line pgms raspistill, tesseract-ocr, flite to do all the work
#
# Version 1.0 2018.02.10 - initial release - rgrokett
# v1.1 - added some text cleanup to improve reading
# v1.2 - removed tabs
#
# http://kd.grokett.com/
#
# License: GPLv3, see: www.gnu.org/licenses/gpl-3.0.html
#
import RPi.GPIO as GPIO
import os, sys
import logging
import subprocess
import threading
import time
##### USER VARIABLES
DEBUG = 0 # Debug 0/1 off/on (writes to debug.log)
SPEED = 1.0 # Speech speed, 0.5 - 2.0
VOLUME = 90 # Audio volume
# OTHER SETTINGS
SOUNDS = "/home/pi/PiTextReader/sounds/" # Directory for sound effect(s)
CAMERA = "raspistill -cfx 128:128 --awb auto -rot 0 -t 500 -o /tmp/image.jpg"
# GPIO BUTTONS
BTN1 = 24 # The button!
LED = 18 # The button's LED!
### FUNCTIONS
# Thread controls for background processing
class RaspberryThread(threading.Thread):
def __init__(self, function):
self.running = False
self.function = function
super(RaspberryThread, self).__init__()
def start(self):
self.running = True
super(RaspberryThread, self).start()
def run(self):
while self.running:
self.function()
def stop(self):
self.running = False
# LED ON/OFF
def led(val):
logger.info('led('+str(val)+')')
if val:
GPIO.output(LED,GPIO.HIGH)
else:
GPIO.output(LED,GPIO.LOW)
# PLAY SOUND
def sound(val): # Play a sound
logger.info('sound()')
time.sleep(0.2)
cmd = "/usr/bin/aplay -q "+str(val)
logger.info(cmd)
os.system(cmd)
return
# SPEAK STATUS
def speak(val): # TTS Speak
logger.info('speak()')
cmd = "/usr/bin/flite -voice awb --setf duration_stretch="+str(SPEED)+" -t \""+str(val)+"\""
logger.info(cmd)
os.system(cmd)
return
# SET VOLUME
def volume(val): # Set Volume for Launch
logger.info('volume('+str(val)+')')
vol = int(val)
cmd = "sudo amixer -q sset PCM,0 "+str(vol)+"%"
logger.info(cmd)
os.system(cmd)
return
# TEXT CLEANUP
def cleanText():
logger.info('cleanText()')
cmd = "sed -e 's/\([0-9]\)/& /g' -e 's/[[:punct:]]/ /g' -e 'G' -i /tmp/text.txt"
logger.info(cmd)
os.system(cmd)
return
# Play TTS (Allow Interrupt)
def playTTS():
logger.info('playTTS()')
global current_tts
current_tts=subprocess.Popen(['/usr/bin/flite','-voice','awb','-f', '/tmp/text.txt'],
stdin=subprocess.PIPE,stdout=subprocess.PIPE,
stderr=subprocess.PIPE,close_fds=True)
# Kick off stop audio thread
rt.start()
# Wait until finished speaking (unless interrupted)
current_tts.communicate()
return
# Stop TTS (with Interrupt)
def stopTTS():
global current_tts
# If button pressed, then stop audio
if GPIO.input(BTN1) == GPIO.LOW:
logger.info('stopTTS()')
#current_tts.terminate()
current_tts.kill()
time.sleep(0.5)
return
# GRAB IMAGE AND CONVERT
def getData():
GPIO.input(BTN1) == GPIO.LOW
logger.info('getData()')
led(0) # Turn off Button LED
# Take photo
sound(SOUNDS+"camera-shutter.wav")
cmd = CAMERA
logger.info(cmd)
os.system(cmd)
# OCR to text
speak("now working. please wait.")
cmd = "/usr/bin/tesseract /tmp/image.jpg /tmp/text"
logger.info(cmd)
os.system(cmd)
# Cleanup text
cleanText()
# Start reading text
playTTS()
return
######
# MAIN
######
try:
global rt
# Setup Logging
logger = logging.getLogger()
handler = logging.FileHandler('debug.log')
if DEBUG:
logger.setLevel(logging.INFO)
handler.setLevel(logging.INFO)
else:
logger.setLevel(logging.ERROR)
handler.setLevel(logging.ERROR)
log_format = '%(asctime)-6s: %(name)s - %(levelname)s - %(message)s'
handler.setFormatter(logging.Formatter(log_format))
logger.addHandler(handler)
logger.info('Starting')
# Setup GPIO buttons
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings (False)
GPIO.setup(BTN1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LED, GPIO.OUT)
# Threaded audio player
#rt = RaspberryThread( function = repeatTTS ) # Repeat Speak text
rt = RaspberryThread( function = stopTTS ) # Stop Speaking text
volume(VOLUME)
speak("OK, ready")
led(1)
while True:
if GPIO.input(BTN1) == GPIO.HIGH:
# Btn 1
getData()
rt.stop()
rt = RaspberryThread( function = stopTTS ) # Stop Speaking text
led(1)
time.sleep(0.5)
speak("OK, ready")
time.sleep(0.2)
except KeyboardInterrupt:
logger.info("exiting")
GPIO.cleanup() #Reset GPIOs
sys.exit(0)