Skip to content

Commit a734170

Browse files
authored
Merge pull request #1566 from makermelissa/master
Adding Voice2JSON Demo
2 parents eb103e9 + 4534436 commit a734170

14 files changed

Lines changed: 124 additions & 0 deletions

File tree

Voice2Json_Edge_Detection/demo.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import os
2+
import subprocess
3+
import random
4+
import json
5+
import re
6+
from datetime import datetime
7+
import board
8+
import displayio
9+
import adafruit_dotstar
10+
from adafruit_st7789 import ST7789
11+
12+
IMAGE_FOLDER = "images"
13+
14+
listen_command = "/usr/bin/voice2json transcribe-stream | /usr/bin/voice2json recognize-intent"
15+
speak_command = "/usr/bin/voice2json speak-sentence '{}'"
16+
pattern = re.compile(r'(?<!^)(?=[A-Z])')
17+
18+
dots = adafruit_dotstar.DotStar(board.D6, board.D5, 3, brightness=0.2, pixel_order=adafruit_dotstar.RBG)
19+
dots.fill(0)
20+
21+
colors = {
22+
'red': 0xff0000,
23+
'green': 0x00ff00,
24+
'blue': 0x0000ff,
25+
'yellow': 0xffff00,
26+
'orange': 0xff8800,
27+
'purple': 0x8800ff,
28+
'white': 0xffffff,
29+
'off': 0
30+
}
31+
32+
lights = ['left', 'middle', 'right']
33+
34+
def get_time():
35+
now = datetime.now()
36+
speak("The time is {}".format(now.strftime("%-I:%M %p")))
37+
38+
def display_picture(category):
39+
path = os.getcwd() + "/" + IMAGE_FOLDER + "/" + category
40+
print("Showing a random image from {}".format(category))
41+
load_image(path + "/" + get_random_file(path))
42+
43+
def get_random_file(folder):
44+
filenames = []
45+
for item in os.listdir(folder):
46+
if os.path.isfile(os.path.join(folder, item)) and item.endswith((".jpg", ".bmp", ".gif")):
47+
filenames.append(item)
48+
if len(filenames):
49+
return filenames[random.randrange(len(filenames))]
50+
return None
51+
52+
def load_image(path):
53+
"Load an image from the path"
54+
if len(splash):
55+
splash.pop()
56+
bitmap = displayio.OnDiskBitmap(open(path, "rb"))
57+
sprite = displayio.TileGrid(bitmap, pixel_shader=displayio.ColorConverter())
58+
splash.append(sprite)
59+
60+
def change_light_color(lightname, color):
61+
dotstar_number = lights.index(lightname)
62+
dots[dotstar_number] = colors[color]
63+
print("Setting Dotstar {} to 0x{:06X}".format(dotstar_number, colors[color]))
64+
65+
def speak(sentence):
66+
for output_line in shell_command(speak_command.format(sentence)):
67+
print(output_line, end='')
68+
69+
def shell_command(cmd):
70+
popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, universal_newlines=True)
71+
for stdout_line in iter(popen.stdout.readline, ""):
72+
yield stdout_line
73+
popen.stdout.close()
74+
return_code = popen.wait()
75+
if return_code:
76+
raise subprocess.CalledProcessError(return_code, cmd)
77+
78+
def process_output(line):
79+
data = json.loads(line)
80+
if not data['timeout'] and data['intent']['name']:
81+
func_name = pattern.sub('_', data['intent']['name']).lower()
82+
if func_name in globals():
83+
globals()[func_name](**data['slots'])
84+
85+
displayio.release_displays()
86+
spi = board.SPI()
87+
tft_cs = board.CE0
88+
tft_dc = board.D25
89+
tft_lite = board.D26
90+
91+
display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs)
92+
93+
display = ST7789(
94+
display_bus,
95+
width=240,
96+
height=240,
97+
rowstart=80,
98+
rotation=180,
99+
backlight_pin=tft_lite,
100+
)
101+
102+
splash = displayio.Group(max_size=10)
103+
display.show(splash)
104+
105+
for output_line in shell_command(listen_command):
106+
process_output(output_line)
47.4 KB
Loading
41.5 KB
Loading
65.7 KB
Loading
39.8 KB
Loading
39.3 KB
Loading
91.4 KB
Loading
39.6 KB
Loading
71.9 KB
Loading
58.9 KB
Loading

0 commit comments

Comments
 (0)