-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·181 lines (147 loc) · 4.35 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
#!/usr/bin/python
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 = 100 # Audio volume
# OTHER SETTINGS
SOUNDS = "/home/pi/eScanor/sounds/" # Directory for sound effect(s)
CAMERA = "raspistill -cfx 128:128 --awb auto -rot 180 -t 500 -o /tmp/image.jpg"
# GPIO BUTTONS
BTN1 = 24 # The button!
### FUNCTIONS
# Thread controls for background processing
class RaspberryThread(threading.Thread):
# Takes in a threading obj and defines which func to execute
def __init__(self, function):
self.running = False
self.function = function
super(RaspberryThread, self).__init__()
def start(self):
# Start the thread execution
self.running = True
super(RaspberryThread, self).start()
def run(self):
# The run() method will run in the background
# until the application exits.
while self.running:
self.function()
def stop(self):
self.running = False
# 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 slt --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','slt','-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():
#logger.info('getData()')
# 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)
cmd = "cat /tmp/text.txt"
os.system(cmd)
# Cleanup text
# Start reading text
playTTS()
cleanText()
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)
# Threaded audio player
rt = RaspberryThread( function = stopTTS ) # Stop Speaking text
volume(VOLUME)
speak("OK, ready for scanning...")
while True:
if GPIO.input(BTN1) == GPIO.LOW:
# Btn 1 pressed
getData()
rt.stop()
rt = RaspberryThread( function = stopTTS ) # Stop Speaking text
time.sleep(0.5)
speak("OK, ready for scanning...")
time.sleep(0.2)
except KeyboardInterrupt:
#logger.info("exiting")
print("exiting")
GPIO.cleanup() #Reset GPIOs
sys.exit(0)