-
Notifications
You must be signed in to change notification settings - Fork 0
/
AIArtFrame.py
368 lines (322 loc) · 10.9 KB
/
AIArtFrame.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# the following program is provided by DevMiser - https://github.com/DevMiser
import datetime
import io
import openai
import os
import pvcobra
import pvleopard
import pvporcupine
import pyaudio
import random
import socket
import struct
import schedule
import sys
import threading
import time
import traceback
import urllib.request
from colorama import Fore, Style
from inky.auto import auto
from openai import OpenAI
from PIL import Image,ImageDraw,ImageFont,ImageOps,ImageEnhance
from pvleopard import *
from pvrecorder import PvRecorder
from threading import Thread, Event
from time import sleep
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
led1_pin=4
GPIO.setup(led1_pin, GPIO.OUT)
GPIO.output(led1_pin, GPIO.LOW)
audio_stream = None
cobra = None
pa = None
porcupine = None
recorder = None
wav_file = None
display = auto()
openai.api_key = "put your secret API key between these quotation marks"
pv_access_key= "put your secret access key between these quotation marks"
client = OpenAI(api_key=openai.api_key)
Clear_list = ["Clear",
"Clear the screen",
"Clear the display",
"Clear the canvas",
"Delete",
"Clean",
"Clean the screen",
"Clean the display",
"Clean the canvas",
"Wipe",
"Wipe the screen",
"Wipe the display",
"Wipe the canvas",
"Erase",
"Erase the screen",
"Erase the display",
"Erase the canvas",
"Blank screen",
"Blank Display"]
# Uncomment the following if you want to speicfy the style(s) that will be used
# without including it in the specific request. You can use or modify this list.
"""
style = [
"Leonardo da Vinci",
"Pablo Picasso",
"Mary Cassatt",
"Salvador Dali",
"Frida Kahlo",
"Vincent van Gogh",
"Berthe Morisot",
"Fern Coppedge",
"Roy Lichtenstein",
"Tamara de Lempicka",
"Paul Gauguin",
"Henri Matisse",
"Yayoi Kusama",
"Jean-Michel Baquiat",
"Norman Rockwell",
"Banksy",
"Bauhaus",
"Baroque",
"Pop Art",
"Romanticism",
"Surrealism",
"Impressionism",
"Cubism",
"Steampunk",
"oil painting",
"watercolor",
"Naturalism",
"Retro",
"Flat Art",
"3D Illustration"
]
"""
def clean_screen():
cycles = 2
colours = (display.RED, display.BLACK, display.WHITE, display.CLEAN)
colour_names = (display.colour, "red", "black", "white", "clean")
img = Image.new("P", (display.WIDTH, display.HEIGHT))
for i in range(cycles):
print("Cleaning cycle %i\n" % (i + 1))
for j, c in enumerate(colours):
print("- updating with %s" % colour_names[j+1])
display.set_border(c)
for x in range(display.WIDTH):
for y in range(display.HEIGHT):
img.putpixel((x, y), c)
display.set_image(img)
display.show()
time.sleep(1)
print("\n")
print("Cleaning complete")
def current_time():
time_now = datetime.datetime.now()
formatted_time = time_now.strftime("%m-%d-%Y %I:%M %p\n")
print("The current date and time is:", formatted_time)
def dall_e3(prompt):
try:
response = client.images.generate(
model="dall-e-3",
prompt=prompt,
size="1024x1024",
quality="standard",
n=1,
)
return (response.data[0].url)
except ConnectionResetError:
print("ConnectionResetError")
current_time()
def detect_silence():
cobra = pvcobra.create(access_key=pv_access_key)
silence_pa = pyaudio.PyAudio()
cobra_audio_stream = silence_pa.open(
rate=cobra.sample_rate,
channels=1,
format=pyaudio.paInt16,
input=True,
frames_per_buffer=cobra.frame_length)
last_voice_time = time.time()
while True:
cobra_pcm = cobra_audio_stream.read(cobra.frame_length)
cobra_pcm = struct.unpack_from("h" * cobra.frame_length, cobra_pcm)
if cobra.process(cobra_pcm) > 0.2:
last_voice_time = time.time()
else:
silence_duration = time.time() - last_voice_time
if silence_duration > 1.3:
print("End of request detected\n")
GPIO.output(led1_pin, GPIO.LOW)
cobra_audio_stream.stop_stream
cobra_audio_stream.close()
cobra.delete()
last_voice_time=None
break
def fade_leds(event):
pwm1 = GPIO.PWM(led1_pin, 200)
event.clear()
while not event.is_set():
pwm1.start(0)
for dc in range(0, 101, 5):
pwm1.ChangeDutyCycle(dc)
time.sleep(0.05)
time.sleep(0.75)
for dc in range(100, -1, -5):
pwm1.ChangeDutyCycle(dc)
time.sleep(0.05)
time.sleep(0.75)
def listen():
cobra = pvcobra.create(access_key=pv_access_key)
listen_pa = pyaudio.PyAudio()
listen_audio_stream = listen_pa.open(
rate=cobra.sample_rate,
channels=1,
format=pyaudio.paInt16,
input=True,
frames_per_buffer=cobra.frame_length)
print("Listening...")
while True:
listen_pcm = listen_audio_stream.read(cobra.frame_length)
listen_pcm = struct.unpack_from("h" * cobra.frame_length, listen_pcm)
if cobra.process(listen_pcm) > 0.3:
print("Voice detected")
listen_audio_stream.stop_stream
listen_audio_stream.close()
cobra.delete()
break
def refresh():
print("\nThe screen refreshes every day at midnight to help prevent burn-in\n")
current_time()
clean_screen()
sleep(5)
print("\nRe-rendering")
display.set_image(img_resized)
# display.set_border(display.BLACK)
display.show()
print("\nDone")
def refresh_schedule(event2):
schedule.every().day.at("00:00").do(refresh)
event2.clear()
while not event2.is_set():
schedule.run_pending()
sleep(1)
def wake_word():
porcupine = pvporcupine.create(keywords=["computer", "jarvis", "Art-Frame"],
access_key=pv_access_key,
sensitivities=[0.1, 0.1, 0.1], #from 0 to 1.0 - a higher number reduces the miss rate at the cost on increased false alarms
)
devnull = os.open(os.devnull, os.O_WRONLY)
old_stderr = os.dup(2)
sys.stderr.flush()
os.dup2(devnull, 2)
os.close(devnull)
wake_pa = pyaudio.PyAudio()
porcupine_audio_stream = wake_pa.open(
rate=porcupine.sample_rate,
channels=1,
format=pyaudio.paInt16,
input=True,
frames_per_buffer=porcupine.frame_length)
Detect = True
while Detect:
porcupine_pcm = porcupine_audio_stream.read(porcupine.frame_length)
porcupine_pcm = struct.unpack_from("h" * porcupine.frame_length, porcupine_pcm)
porcupine_keyword_index = porcupine.process(porcupine_pcm)
if porcupine_keyword_index >= 0:
GPIO.output(led1_pin, GPIO.HIGH)
print(Fore.GREEN + "\nWake word detected\n")
current_time()
print("What would you like me to render?\n")
porcupine_audio_stream.stop_stream
porcupine_audio_stream.close()
porcupine.delete()
os.dup2(old_stderr, 2)
os.close(old_stderr)
Detect = False
class Recorder(Thread):
def __init__(self):
super().__init__()
self._pcm = list()
self._is_recording = False
self._stop = False
def is_recording(self):
return self._is_recording
def run(self):
self._is_recording = True
recorder = PvRecorder(device_index=-1, frame_length=512)
recorder.start()
while not self._stop:
self._pcm.extend(recorder.read())
recorder.stop()
self._is_recording = False
def stop(self):
self._stop = True
while self._is_recording:
pass
return self._pcm
try:
o = create(
access_key=pv_access_key,
enable_automatic_punctuation = False,
)
event = threading.Event()
event2 = threading.Event()
while True:
wake_word()
event2.set()
recorder = Recorder()
recorder.start()
listen()
detect_silence()
transcript, words = o.process(recorder.stop())
t_fade = threading.Thread(target=fade_leds, args=(event,))
t_fade.start()
recorder.stop()
if transcript not in Clear_list:
current_time()
prompt_full = (transcript + (" using vibrant colors"))
# this program asks that only bright colors be used because they look more vibrant on e-paper
# if you prefer not to do this, comment out the prompt_full line above and uncomment the floowing line
# prompt_full = transcript
# comment out the prior line and uncomment one of the following lines to try pretermined styles
# prompt_full = (transcript + (", using only shades of the colors blue, green, red, white, yellow, orange and black."))
# Style = random.choice(style)
# prompt_full = (transcript + (" in the style of ") + Style + (", using only shades of the colors blue, green, red, white, yellow, orange and black."))
print("You requested: " + prompt_full)
print("\nCreating...")
image_url = dall_e3(prompt_full)
raw_data = urllib.request.urlopen(image_url).read()
img = Image.open(io.BytesIO(raw_data))
img_bordered = ImageOps.expand(img, border=(152,0), fill='black')
img_resized = img_bordered.resize((600, 448), Image.ANTIALIAS)
# curr_col = ImageEnhance.Color(img_resized)
# new_col = 2.5
# img_enhanced = curr_col.enhance(new_col)
# img_bordered = ImageOps.expand(img_enhanced, border=(0,76), fill='black')
print("\nRendering...")
display.set_image(img_resized)
# display.set_border(display.BLACK)
display.show()
# uncomment the following line if you also want the image to show on a display connected to the RPi
# img.show()
event.set()
GPIO.output(led1_pin, GPIO.LOW)
sleep(2)
t_refresh = threading.Thread(target=refresh_schedule, args=(event2,))
t_refresh.start()
print("\nDone")
else:
print ("Clearing the display...")
clean_screen()
event.set()
event2.set()
GPIO.output(led1_pin, GPIO.LOW)
print("\nDone")
except ConnectionResetError:
print ("Reset Error")
current_time()
except KeyboardInterrupt:
exit()