-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkitt.py
More file actions
209 lines (175 loc) · 5.86 KB
/
kitt.py
File metadata and controls
209 lines (175 loc) · 5.86 KB
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
#!/usr/bin/env python
import re
import os, sys, subprocess
from os import path
import time
import RPi.GPIO as GPIO
from pocketsphinx import *
from sphinxbase import *
from pydub import AudioSegment as am
modeldir = "/usr/local/share/pocketsphinx/model/"
datadir = "/home/pi/kitt/"
filename = "transcribe.wav"
new_filename = "transcribe_export.wav"
config = Decoder.default_config()
config.set_string('-hmm', os.path.join(modeldir, 'en-us/en-us'))
config.set_string('-dict', os.path.join(modeldir, 'en-us/cmudict-en-us.dict'))
config.set_string('-kws', os.path.join(datadir, 'keyword.list'))
#config.set_string('-keyphrase', 'morning')
#config.set_float('-kws_threshold', 1e-30)
def start():
print ("Starting")
#time_to_wait = 9999
time_counter = 0
while not os.path.exists(filename):
time.sleep(1)
time_counter += 1
#if time_counter > time_to_wait:break
# Convert transcibe.wav to text
subprocess.run(["sox", filename, "-r 16000", new_filename])
stream = open(os.path.join(datadir, new_filename), "rb")
decoder = Decoder(config)
decoder.start_utt()
while True:
buf = stream.read(1024)
if buf:
decoder.process_raw(buf, False, False)
else:
omxprocess = subprocess.Popen(['omxplayer', '--no-keys', '-olocal', 'understand.mp3'], stdin=subprocess.PIPE, stdout=None, stderr=None, bufsize=0)
os.remove(filename)
start()
break
if decoder.hyp() is not None:
#print([(seg.word, seg.prob, seg.start_frame, seg.end_frame) for seg in decoder.seg()])
#print([(seg.word) for seg in decoder.seg()])
keyword = ([(seg.word) for seg in decoder.seg()])
decoder.end_utt()
# PERFORM COOL TASK
#Match keyword to flat file input.txt and get audio MP3
keyword = ([(seg.word) for seg in decoder.seg()])
found = (keyword)
name = re.sub('\W+','', str(found))
print (name)
with open('input.txt') as fd:
print (name)
#Do stuff based on the audio match
for line in fd:
match = re.search(rf'.*{name}.* : (\S+)', line)
print (match)
if (name == "cereal"):
omxprocess = subprocess.Popen(['omxplayer', '--no-keys', '-olocal', 'fx-scanner.mp3'], stdin=subprocess.PIPE, stdout=None, stderr=None, bufsize=0)
os.remove(filename)
time.sleep(1)
print ("GOing to chaser")
chaser()
elif (name == "skinny"):
omxprocess = subprocess.Popen(['omxplayer', '--no-keys', '-olocal', 'fx-scanner.mp3'], stdin=subprocess.PIPE, stdout=None, stderr=None, bufsize=0)
os.remove(filename)
time.sleep(1)
print ("GOing to chaser")
chaser()
elif (name == "scan"):
omxprocess = subprocess.Popen(['omxplayer', '--no-keys', '-olocal', 'fx-scanner.mp3'], stdin=subprocess.PIPE, stdout=None, stderr=None, bufsize=0)
os.remove(filename)
time.sleep(1)
print ("GOing to chaser")
chaser()
elif match:
music = match.group(1)
print('{}'.format(music))
omxprocess = subprocess.Popen(['omxplayer', '--no-keys', '-olocal', music], stdin=subprocess.PIPE, stdout=None, stderr=None, bufsize=0)
os.remove(filename)
start()
def chaser():
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
# Define GPIO signals to use
# that are connected to 10 LEDs
# Pins 7,11,15,21,23,16,18,22,24,26
# GPIO4,GPIO17,GPIO22,GPIO9,GPIO11
# GPIO23,GPIO24,GPIO25,GPIO8,GPIO7
RpiGPIO = [4,17,22,9,11,23,24,25,8,7]
# Set all pins as output
for pinref in RpiGPIO:
# print("Setup pins")
GPIO.setup(pinref,GPIO.OUT)
# Define some settings
StepCounter = 0
StepDir = 1
WaitTime = 0.2
# Define some sequences
# One LED
StepCount1 = 10
Seq1 = []
Seq1 = list(range(0,StepCount1))
Seq1[0] =[1,0,0,0,0,0,0,0,0,0]
Seq1[1] =[0,1,0,0,0,0,0,0,0,0]
Seq1[2] =[0,0,1,0,0,0,0,0,0,0]
Seq1[3] =[0,0,0,1,0,0,0,0,0,0]
Seq1[4] =[0,0,0,0,1,0,0,0,0,0]
Seq1[5] =[0,0,0,0,0,1,0,0,0,0]
Seq1[6] =[0,0,0,0,0,0,1,0,0,0]
Seq1[7] =[0,0,0,0,0,0,0,1,0,0]
Seq1[8] =[0,0,0,0,0,0,0,0,1,0]
Seq1[9] =[0,0,0,0,0,0,0,0,0,1]
# Double LEDs
StepCount2 = 11
Seq2 = []
Seq2 = list(range(0,StepCount2))
Seq2[0] =[1,0,0,0,0,0,0,0,0,0]
Seq2[1] =[1,1,0,0,0,0,0,0,0,0]
Seq2[2] =[0,1,1,0,0,0,0,0,0,0]
Seq2[3] =[0,0,1,1,0,0,0,0,0,0]
Seq2[4] =[0,0,0,1,1,0,0,0,0,0]
Seq2[5] =[0,0,0,0,1,1,0,0,0,0]
Seq2[6] =[0,0,0,0,0,1,1,0,0,0]
Seq2[7] =[0,0,0,0,0,0,1,1,0,0]
Seq2[8] =[0,0,0,0,0,0,0,1,1,0]
Seq2[9] =[0,0,0,0,0,0,0,0,1,1]
Seq2[10]=[0,0,0,0,0,0,0,0,0,1]
# Two LEDs from opposite ends
StepCount3 = 9
Seq3 = []
Seq3 = list(range(0,StepCount3))
Seq3[0] =[1,0,0,0,0,0,0,0,0,1]
Seq3[1] =[0,1,0,0,0,0,0,0,1,0]
Seq3[2] =[0,0,1,0,0,0,0,1,0,0]
Seq3[3] =[0,0,0,1,0,0,1,0,0,0]
Seq3[4] =[0,0,0,0,1,1,0,0,0,0]
Seq3[5] =[0,0,0,1,0,0,1,0,0,0]
Seq3[6] =[0,0,1,0,0,0,0,1,0,0]
Seq3[7] =[0,1,0,0,0,0,0,0,1,0]
Seq3[8] =[1,0,0,0,0,0,0,0,0,1]
count = 1
# Choose a sequence to use
Seq = Seq3
StepCount = StepCount3
i=0
# Start main loop
while True:
# print("-- Step : "+ str(StepCounter) +" --")
for pinref in range(0, 10):
xpin=RpiGPIO[pinref]#
# Check if LED should be on or off
if Seq[StepCounter][pinref]!=0:
# print(" Enable " + str(xpin))
GPIO.output(xpin, True)
i += 1
else:
# print(" Disable " + str(xpin))
GPIO.output(xpin, False)
StepCounter += StepDir
# If we reach the end of the sequence reverse
# the direction and step the other way
if (StepCounter==StepCount) or (StepCounter<0):
StepDir = StepDir * -1
StepCounter = StepCounter + StepDir + StepDir
print(i)
if (i > 51):
print ("i = 8")
GPIO.cleanup()
start()
# Wait before moving on
time.sleep(0.2)
start()