/
V13Coral.py
1377 lines (1214 loc) · 48.1 KB
/
V13Coral.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pygame
import pygame.freetype
import cv2
import threading
import multiprocessing as mp
from moviepy.editor import VideoFileClip
import math
import socket
import snowboydecoder
import speech_recognition as sr
from gtts import gTTS
import re
from pydub import AudioSegment as AS
import numpy as np
import random
import matplotlib
matplotlib.use("Agg")
import matplotlib.backends.backend_agg as agg
import matplotlib.pyplot as plt, mpld3
from matplotlib.ticker import MaxNLocator
from pygame.locals import *
import datetime as dt
from pushbullet import Pushbullet
import board
import adafruit_dotstar as dotstar
from edgetpu.detection.engine import DetectionEngine
from edgetpu.utils import dataset_utils
from PIL import Image
from PIL import ImageDraw
pygame.init()
import RPi.GPIO as GPIO
import os
import time
#from threading import Timer
#import sys
########## Settings ##########
WHITE = (255,255,255)
BACKGROUND = WHITE
COLORKEY = (255,170,200)
FPS = 60
LANGUAGE = "en-US" # en-US
isOnline = True
eyeDesign = "normal"
########## GPIO ##########
# Pins
PIN_MOTORDETECT = 24
PIN_IRSENOR = 20
PIN_LED = 25
# SPI Pins
PIN_CLK = 11
PIN_MOSI = 10
PIN_MISO = 9
PIN_CS = 8
#global variables
numberOfActivations = 0
irSensorThreshhold = 0
dispenserEmpty = False
turnOffDispenser = False
dispenserRefilled = False
dispenserEmptyTemp = 0
def init_GPIO():
# Init GPIO pins
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_LED, GPIO.OUT)
GPIO.output(PIN_LED, False)
GPIO.setup(PIN_MOTORDETECT, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# Spi pins Init
GPIO.setup(PIN_CLK, GPIO.OUT)
GPIO.setup(PIN_MISO, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(PIN_MOSI, GPIO.OUT)
GPIO.setup(PIN_CS, GPIO.OUT)
# Read SPI data from ADC
def recvBits(numBits, clkPin, misoPin):
#Receives arbitrary number of bits
retVal = 0
for bit in range(numBits):
# Pulse clock pin
GPIO.output(clkPin, GPIO.HIGH)
GPIO.output(clkPin, GPIO.LOW)
# Read 1 data bit in
if GPIO.input(misoPin):
retVal |= 0x1
# Advance input to next bit
retVal <<= 1
# Divide by two to drop the NULL bit
return (retVal/2)
def readAdc(channel, clkPin, misoPin, mosiPin, csPin):
if (channel < 0) or (channel > 7):
print("Invalid ADC Channel number, must be between [0,7]")
return -1
# Datasheet says chip select must be pulled high between conversions
GPIO.output(csPin, GPIO.HIGH)
# Start the read with both clock and chip select low
GPIO.output(csPin, GPIO.LOW)
GPIO.output(clkPin, GPIO.HIGH)
adc = recvBits(10, clkPin, misoPin)
# Set chip select high to end the read
GPIO.output(csPin, GPIO.HIGH)
return round(adc)
myfont = pygame.freetype.SysFont(pygame.freetype.get_default_font(), 20)
#init_GPIO()
def GPIOfunction():
global numberOfActivations, irSensorThreshhold, dispenserEmpty, turnOffDispenser, dispenserRefilled, dispenserEmptyTemp, activated
ADCvalue = readAdc(0, PIN_CLK, PIN_MISO, PIN_MOSI, PIN_CS)
if(dispenserEmpty == False):
turnOffDispenser = False
if not activated and GPIO.input(PIN_MOTORDETECT):
print("Motor Activated!")
#count number of times used
numberOfActivations += 1
print("Activations: ", numberOfActivations)
activated = True
elif activated and not GPIO.input(PIN_MOTORDETECT):
activated = False
if(dispenserEmpty):
# If dispenser refilled - button pushed in gui - rest all conditions and turn on system/motor again
if(dispenserRefilled):
dispenserEmptyTemp = 0
dispenserEmpty = False
dispenserRefilled = False
turnOffDispenser = False
# Test MOSFET
if(turnOffDispenser == False):
GPIO.output(PIN_LED, True)
else:
GPIO.output(PIN_LED, False)
text_surface, rect = myfont.render(f"Activations: {numberOfActivations}", (0,0,0))
screen.blit(text_surface, (5,5))
########## Frequency and plot ##########
fig = plt.figure(figsize=[8, 4.8], dpi=100)
numberOfActivationsMinute = 0
trailingFiveMinSum = 0
lastMinute = 0
firstMinSkipped = False
hours = "%H"
minutes = "%M"
seconds = "%S"
# Set the time format here
#---------------------------------------------------------------
timeFormat = minutes
#---------------------------------------------------------------
hoursStart = 19
hoursEnd = 25
#hoursStart = int(dt.datetime.now().strftime(timeFormat))
#hoursEnd = (hoursStart + 10) % 60
numberOfActivations = 0
numberOfActivationsHour = 0
lastHour = 0
operatingTime = False
hoursList = []
peopleListPlot = []
fiveMinQue = []
def trailing_five_min_activations():
global lastMinute, fiveMinQue, numberOfActivationsMinute, numberOfActivations, firstMinSkipped, trailingFiveMinSum
dailyMinute = dt.datetime.now()
dailyMinute = dailyMinute.strftime(timeFormat)
minute = int(dailyMinute)
if minute != lastMinute:
if lastMinute != 0 and firstMinSkipped == True:
if len(fiveMinQue) == 5:
fiveMinQue.pop(0)
trailingActivations = numberOfActivations - numberOfActivationsMinute
fiveMinQue.append(trailingActivations)
numberOfActivationsMinute = numberOfActivations
if len(fiveMinQue) == 5:
trailingFiveMinSum = sum(fiveMinQue)
lastMinute = minute
firstMinSkipped = True
PIN_SERVO = 4
PULSE_FREQ = 50
DUTY_CYCLE = 2
GPIO.setmode(GPIO.BCM)
#servo = setup_servo(PIN_SERVO, PULSE_FREQ) # 50 Hz pulse
def setup_servo(pin, pwmFreq):
GPIO.setup(pin, GPIO.OUT)
servo = GPIO.PWM(pin, pwmFreq)
servo.start(0)
return servo
def set_angle(servo, angle):
dutyCycle = 2 + (float(angle) / 18)
servo.ChangeDutyCycle(dutyCycle) #Recalculate with values from datasheet of servo
def move_servo_to_random_position():
global servo
randomPosition = random.randint(40, 140)
set_angle(servo, randomPosition)
print("Position: ", randomPosition)
########## Load images ##########
normalL = pygame.image.load("images/png/normalL.png")
normalR = pygame.transform.flip(normalL, True, False)
normalwhiteL = pygame.image.load("images/png/normalLwhite.png")
normalwhiteR = pygame.transform.flip(normalwhiteL, True, False)
closedL = pygame.image.load("images/png/closedL.png")
closedR = pygame.transform.flip(closedL, True, False)
happyL = pygame.image.load("images/png/happyL.png")
happyR = pygame.transform.flip(happyL, True, False)
confusedL = pygame.image.load("images/png/confusedL.png")
confusedR = pygame.image.load("images/png/confusedR.png")
confusedLwhite = pygame.image.load("images/png/normalAltLwhite.png")
confusedRwhite = pygame.image.load("images/png/confusedRwhite.png")
onlineicon = pygame.image.load("images/png/online.png")
offlineicon = pygame.image.load("images/png/offline.png")
danishbutton = pygame.image.load("images/bg/danishicon.png")
englishbutton = pygame.image.load("images/bg/englishicon.png")
danishbutton = pygame.transform.scale(danishbutton, (120,90))
englishbutton = pygame.transform.scale(englishbutton, (120,90))
nobutton = pygame.image.load("images/png/nobutton.png")
yesbutton = pygame.image.load("images/png/yesbutton.png")
pupil = pygame.image.load("images/png/pupil.png")
colorselector = pygame.image.load("images/bg/ColorPicker.bmp")
colorselector = pygame.transform.flip(colorselector, False, True)
coloricon = pygame.transform.scale(colorselector, (120, 90))
colorselector = pygame.transform.scale(colorselector, (700, 350))
menuicon = pygame.image.load("images/png/menuicon.png")
graphicon = pygame.image.load("images/png/graphicon.png")
graphicon = pygame.transform.scale(graphicon, (90,90))
monster = pygame.image.load("images/png/monstereyes.png")
monsterpupil = pygame.image.load("images/png/monsterpupil.png")
monsterpupilsmall = pygame.image.load("images/png/monsterpupilsmall.png")
monsterblinkL = pygame.image.load("images/png/monsterclosedL.png")
monsterblinkM = pygame.image.load("images/png/monsterclosedM.png")
monsterblinkR = pygame.image.load("images/png/monsterclosedR.png")
benderL = pygame.image.load("images/bg/benderL.png")
benderR = pygame.transform.flip(benderL, True, False)
benderpupil = pygame.image.load("images/bg/benderpupil.png")
########## Alts ##########
happyAltL = pygame.image.load("images/bg/bghappy4L.png")
happyAltL.set_alpha(None)
happyAltL.set_colorkey(COLORKEY)
happyAltR = pygame.transform.flip(happyAltL, True, False)
blinkAltL = pygame.image.load("images/bg/bgclosedgrey3L.png")
blinkAltL.set_alpha(None)
blinkAltL.set_colorkey(COLORKEY)
blinkAltR = pygame.transform.flip(blinkAltL, True, False)
spookyouter = pygame.image.load("images/png/outer.png")
spookyinner = pygame.image.load("images/png/Inner.png")
########## Load sounds ##########
Hi_sanitizer = "sounds/Hi_sanitizer.mp3"
Sorry_sanitizer = "sounds/Sorry_sanitizer.mp3"
Great_dispenser = "sounds/Great_dispenser.mp3"
Nice_day = "sounds/Nice_day.mp3"
Sorry_bye = "sounds/Sorry_bye.mp3"
Sorry_video = "sounds/Sorry_video.mp3"
Video = "sounds/Video.mp3"
Hej_sprit = "sounds/Hej_sprit.mp3"
Undskyld_sprit = "sounds/Undskyld_sprit.mp3"
Under_automaten = "sounds/Under_automaten.mp3"
God_dag = "sounds/God_dag.mp3"
Undskyld_farvel = "sounds/Undskyld_farvel.mp3"
Undskyld_video = "sounds/Undskyld_video.mp3"
Video_da = "sounds/Video_da.mp3"
Okay_video = "sounds/Okay_video.mp3"
thirtysec = "sounds/30sec.mp3"
joke1_1 = "sounds/joke1_1.mp3"
joke1_2 = "sounds/joke1_2.mp3"
joke2_1 = "sounds/joke2_1.mp3"
joke2_2 = "sounds/joke2_2.mp3"
joke3_1 = "sounds/joke3_1.mp3"
joke3_2 = "sounds/joke3_2.mp3"
nudge1 = "sounds/nudge1.mp3"
nudge2 = "sounds/nudge2.mp3"
#sounds = [Hi_sanitizer, Video, Sorry_sanitizer, Sorry_video, Great_dispenser, Nice_day, Sorry_bye,
# Hej_sprit, Video_da, Undskyld_sprit, Undskyld_video, Under_automaten, God_dag, Undskyld_farvel]
sounds = [Hi_sanitizer, Video, Great_dispenser, Okay_video, Sorry_sanitizer, Sorry_video, Nice_day, Sorry_bye,
nudge1, nudge2, thirtysec, joke1_1, joke1_2, joke2_1, joke2_2, joke3_1, joke3_2,
Hej_sprit, Video_da, Undskyld_sprit, Undskyld_video, Under_automaten, God_dag, Undskyld_farvel]
########## Functions ##########
activationsList = []
numberOfPeopleHour = 0
numberOfDispenses = 10
dispensesEmptyThreshold = 0.8
almostEmpty = numberOfDispenses * dispensesEmptyThreshold
notification = "notification"
sms = "sms"
typeOfNotification = notification
msg = "The dispenser is under 20" + "%" + " full."
def update_activations_plot(operatingTimeList):
global lastHour, activationsList, numberOfActivationsHour, numberOfActivations
dailyHour = dt.datetime.now()
dailyHour = dailyHour.strftime(timeFormat)
hour = int(dailyHour)
hourString = ""
if hour < 10:
hourString = "0" + str(hour) + ":00"
else:
hourString = str(hour) + ":00"
if hourString in operatingTimeList:
operatingHours = len(operatingTimeList) - 1
#print("no hours: ", operatingHours)
indexHour = operatingTimeList.index(hourString)
operatingTime = True
else:
#print("no hours: ", operatingHours)
operatingTime = False
if hour != lastHour and operatingTime == True:
trailingActivations = numberOfActivations - numberOfActivationsHour
#print("Activations: ", trailingActivations)
activationsList.insert(indexHour, round(trailingActivations))
activationsList.pop(indexHour + 1)
#print("acti list: ", activationsList)
numberOfActivationsHour = numberOfActivations
lastHour = hour
def update_people_plot(operatingTimeList, numberOfPeople):
global lastHour, peopleListPlot, numberOfPeopleHour, activationsList, numberOfActivationsHour, numberOfActivations
dailyHour = dt.datetime.now()
dailyHour = dailyHour.strftime(timeFormat)
hour = int(dailyHour)
hourString = ""
if hoursEnd - hoursStart > 10:
hourString = str(hour)
else:
if hour < 10:
hourString = "0" + str(hour) + ":00"
else:
hourString = str(hour) + ":00"
if hourString in operatingTimeList:
operatingHours = len(operatingTimeList) - 1
indexHour = operatingTimeList.index(hourString)
operatingTime = True
else:
operatingTime = False
numberOfActivationsHour = numberOfActivations
numberOfPeopleHour = numberOfPeople
if hour != lastHour and operatingTime == True:
trailingPeople = numberOfPeople - numberOfPeopleHour
#print("People: ", trailingPeople)
peopleListPlot.insert(indexHour, round(trailingPeople))
peopleListPlot.pop(indexHour + 1)
#print("People list: ", peopleListPlot)
numberOfPeopleHour = numberOfPeople
trailingActivations = numberOfActivations - numberOfActivationsHour
#print("Activations: ", trailingActivations)
activationsList.insert(indexHour, round(trailingActivations))
activationsList.pop(indexHour + 1)
#print("acti list: ", activationsList)
numberOfActivationsHour = numberOfActivations
lastHour = hour
def list_with_operating_hours(hoursStart, hoursEnd):
global activationsList, peopleListPlot
hours = []
#activationsList = []
for i in range(hoursStart + 1, hoursEnd + 1):
if (hoursEnd - hoursStart) > 10:
hours.append(str(i))
else:
if i < 10:
hours.append("0" + str(i) + ":00")
else:
hours.append(str(i) + ":00")
length = len(hours)
for i in range(length):
activationsList.append(0)
peopleListPlot.append(0)
return hours
def pushbullet_notification(notificationType, message):
pb = Pushbullet("o.xgWW2FkTk3pGt2oQXM9m9TiQXF2GvUPQ")
# Get all devices that the current user has access to.
# Select a device
phone = pb.devices[0]
phone = pb.get_device('OnePlus 7 Pro')
if notificationType == "notification":
push = phone.push_note("Alert!", message)
elif notificationType == "sms":
push = pb.push_sms(phone, "+4521378019", message)
def checkInternet(host="8.8.8.8", port=53, timeout=3):
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
return True
except socket.error as ex:
print(ex)
return False
def showButtons():
screen.blit(yesbutton, (720,0))
screen.blit(nobutton, (0,0))
def wait(ms):
pygame.time.wait(ms)
def blitImages(left, right):
screen.blit(left, (0,0))
screen.blit(right, (400,0))
def signal():
global interrupted
interrupted = True
def interrupt_callback():
global interrupted, timeout
timeout += 0.03
if threadevent.is_set():
return True
if timeout > 5:
print("Timeout happened")
return True
return interrupted
def listen_for_two_cmds(cmd1, cmd2):
global interrupted, timeout
interrupted = False
timeout = 0.
detector = snowboydecoder.HotwordDetector(
[f"resources/models/{cmd1}.pmdl",f"resources/models/{cmd2}.pmdl"], sensitivity=[0.5,0.5])
detector.start(detected_callback=[detected_callback1, detected_callback2],
interrupt_check=interrupt_callback,
sleep_time=0.03)
detector.terminate()
print("Terminated")
#interrupted = True
def detected_callback1():
print("callback 1")
signal()
global yes_detected
yes_detected = True
def detected_callback2():
print("callback 2")
signal()
global no_detected
no_detected = True
def google_in():
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration=0.5)
print("Say something!")
try:
audio = r.listen(source, timeout=5, phrase_time_limit=4)
except sr.WaitTimeoutError:
return "Timeout"
else:
try:
out = r.recognize_google(audio, language=LANGUAGE)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
else:
return out
return "Error"
def speech_in(cmd1, cmd2):
global yes_detected, no_detected
yes_detected = False
no_detected = False
if cmd1 == "yes" and LANGUAGE == "da-DK":
cmd1 = "ja"
cmd2 = "nej"
if not isOnline:
listen_for_two_cmds(cmd1, cmd2)
else:
gin = google_in()
if findWholeWord(cmd1)(gin): yes_detected = True
elif findWholeWord(cmd2)(gin): no_detected = True
#------------ Functions to convert amplitudes to brightness -------
#Normalized Data
def normalize_data(sample, data):
normalizedSample = (sample - min(data)) / (max(data) - min(data))
return normalizedSample
# convert the normalized data to a brightness
def convert_normData_to_brightness(normData):
maxBrightValue = 255
brightValue = int(normData * maxBrightValue)
return brightValue
# Choose color and set the brightness
def set_brightness(color, brightness):
#return (0, 0, 255-brightness)
if color == "red":
return (255-brightness, 0, 0)
elif color == "green":
return (0, 255-brightness, 0)
elif color == "blue":
return (0, 0, 255-brightness)
elif color == "all":
return (brightness, brightness, brightness)
def insideOut():
for dot in range(round(numberOfDots/2)):
color = random_brightness(blue)
#print(color)
dots[round(numberOfDots/2) + dot] = color
dots[round(numberOfDots/2) - dot -1] = color
#----------------LEDS init---------------------
NOCOLOR = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# Using a DotStar Digital LED Strip with 72 LEDs connected to hardware SPI
# 5V - Yellow wire to gpio11 and green wire to gpio10
dots = dotstar.DotStar(board.SCK, board.MOSI, 27, brightness=0.1)
# Using a DotStar Digital LED Strip with 72 LEDs connected to digital pins
# 5V - Yellow wire to gpio6 and green wire to gpio5
#dots = dotstar.DotStar(board.D6, board.D5, 72, brightness=0.1)
red = "red"
green = "green"
blue = "blue"
allColors = "all"
chosenColor = blue
dots.fill(BLUE)
numberOfDots = len(dots)
i = 0
def change_brightness_when_speaking(sample_rate, amp_data):
global i
ms = pygame.time.Clock().tick(30)
samples = int(sample_rate/(1000/ms))
out = np.average(amp_data[i*samples:(i+1)*samples])
#Normalize sample
normalizedSample = normalize_data(out, amp_data)
#convert amp to brightness
brightnessScale = 4
brightnessAmp = convert_normData_to_brightness(normalizedSample) * brightnessScale
if(brightnessAmp > 255):
brightnessAmp = 255
#convert brightness to color
brightValue = set_brightness(chosenColor, brightnessAmp)
lengthOfDots = len(dots)
middleOfDots = round(lengthOfDots/2)
# Fill all LEDs
allLedsFill(brightValue)
#insideOut()
i += 1
def speech_out(index):
if LANGUAGE == "da-DK":
index += 7
pygame.mixer.init()
pygame.mixer.music.load(sounds[index])
sound = AS.from_mp3(sounds[index])
raw_data = sound.raw_data
sample_rate = sound.frame_rate
amp_data = np.frombuffer(raw_data, dtype=np.int16)
amp_data = np.absolute(amp_data)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
if threadevent.is_set():
pygame.mixer.quit()
return
pygame.time.Clock().tick(10)
#change_brightness_when_speaking(sample_rate, amp_data)
def interaction():
global show_buttons
flowState = 0
speech_out(0)
i = 0
while not threadevent.is_set():
speech_in("yes", "no")
if yes_detected:
if flowState == 0:
pygame.event.post(happyevent)
speech_out(4)
flowState += 1
i = 0
#while not GPIO.input(PIN_MOTORDETECT): pygame.time.Clock().tick(10)
wait(1000)
speech_out(1)
else:
global state
state = VIDEOSTATE
break
elif no_detected:
speech_out(5)
break
elif i < 2:
pygame.event.post(questionevent)
speech_out(flowState+2)
show_buttons = True
i += 1
else:
speech_out(6)
break
show_buttons = False
threadevent.clear()
print("Flow ended")
def interaction2(*items):
skip = False
for item in items:
if item == "nudge":
speech_out(8+items[1])
elif item == "30s":
speech_out(10)
elif item == "joke":
speech_out(11+items[1])
wait(2000)
speech_out(12+items[1])
elif item == "sanitizer":
skip = interactionQuestion(0)
elif item == "video" and not skip:
interactionQuestion(1)
elif item == "monster":
global eyeDesign, state
speech_in(items[0], items[1])
if yes_detected:
eyeDesign = items[0]
state = NORMALSTATE
elif no_detected:
eyeDesign = items[1]
state = NORMALSTATE
def interactionQuestion(question):
global show_buttons
skip = False
lastNumberOfActivations = numberOfActivations
speech_out(question)
i = 0
while not threadevent.is_set():
speech_in("yes", "no")
if yes_detected:
pygame.event.post(happyevent)
speech_out(question+2)
if question == 0:
#while not GPIO.input(PIN_MOTORDETECT): pygame.time.Clock().tick(10)
wait(5000)
# add speech if no activation
break
else:
global state
state = VIDEOSTATE
break
elif no_detected:
speech_out(6)
skip = True
break
elif question == 0 and numberOfActivations != lastNumberOfActivations:
pygame.event.post(happyevent)
speech_out(10)
wait(2000)
break
elif i < 2:
pygame.event.post(questionevent) # dont repeat if sound level low?
speech_out(question+4)
show_buttons = True
i += 1
else:
speech_out(7)
#if dispenser used - move on (+remark)
#speech_out() didnt hear - repeat
show_buttons = False
threadevent.clear()
print("Flow ended")
return skip
def findWholeWord(w):
return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search
def playVideo():
#pygame.mixer.quit() File "/home/pi/.local/lib/python3.7/site-packages/moviepy/audio/io/preview.py", line 45, in preview
clip = VideoFileClip(f'videos/{eyeDesign}video.mp4', target_resolution=(480,800))
clip.preview()
res0 = (320,320)
res1 = (320,240)
res2 = (640,480)
res3 = (1280,720)
res = res2
def faceTracking(listproxy, peoplecounter, terminator):
engine = DetectionEngine("ssd_mobilenet_v2_face_quant_postprocess_edgetpu.tflite")
cap = cv2.VideoCapture(-1)
#cap.set(cv2.CAP_PROP_FRAME_WIDTH, res[0])
#cap.set(cv2.CAP_PROP_FRAME_HEIGHT, res[1])
currentID = 1
faceTrackers = {}
term = False
peopleCount = 0
start = time.time()
fps = 0
old = 0.
while not terminator.value:
_, frame = cap.read()
frame = cv2.rotate(frame, cv2.ROTATE_180)
frameRGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
framePIL = Image.fromarray(frameRGB)
faces = engine.detect_with_image(framePIL,
threshold=0.05,
keep_aspect_ratio=True,
relative_coord=False,
top_k=10,
resample=4)
for face in faces:
(x, y, x2, y2) = (int(i) for i in face.bounding_box.flatten().tolist())
w = x2-x
h = y2-y
center = (int(x+w*0.5), int(y+h*0.5))
fidMatch = False
for fid in faceTrackers.keys():
(tx, ty, tw, th, n, u, c) = faceTrackers.get(fid)
if tx <= center[0] <= tx+tw and ty <= center[1] <= ty+th:
if n < 50: n += 1
if n >= 35 and c == False:
c = True
peopleCount += 1
faceTrackers.update({fid:(x,y,w,h,n,True,c)})
fidMatch = True
break
if not fidMatch:
faceTrackers.update({currentID:(x,y,w,h,1,True,False)})
currentID += 1
fidsToDelete = []
for fid in faceTrackers.keys():
(tx, ty, tw, th, n, u, c) = faceTrackers.get(fid)
if not u:
# if center is close to frame edge then decay faster
#if res[0]-tw-20 < tx < 20:
#n-=5
n -= 1
if n < 1: fidsToDelete.append(fid)
else:
faceTrackers.update({fid:(tx,ty,tw,th,n,False,c)})
for fid in fidsToDelete:
faceTrackers.pop(fid, None)
listproxy[0] = faceTrackers
peoplecounter.value = peopleCount
fps += 1
now = time.time()
tid = math.trunc(now-start)
if tid != old:
#print(fps)
fps = 0
old = tid
cap.release()
def calculateAngles(x, y, w, h):
#globals
WIDTH = res[0]/2
HEIGHT = res[1]/2
EYE_DEPTH = 2
hFOV = 62/2
vFOV = 49/2
ppcm = WIDTH*2/15.5# * 1.5
center = (int(x+w*0.5), int(y+h*0.5))
hAngle = (1 - center[0]/WIDTH) * hFOV
vAngle = (1 - center[1]/HEIGHT) * vFOV
c = -0.26*w+103
global pupilL, pupilR, pupilV
# horizontal
b = 4
angleA = (90 - hAngle)*math.pi/180
a = math.sqrt(b*b + c*c - 2*b*c*math.cos(angleA))
angleC = math.acos((a*a + b*b - c*c)/(2*a*b))
pupilL = int((angleC - math.pi/2) * EYE_DEPTH * ppcm)
b_hat = 2*b
c_hat = math.sqrt(a*a + b_hat*b_hat - 2*a*b_hat*math.cos(angleC))
angleA_hat = math.acos((b_hat*b_hat + c_hat*c_hat - a*a)/(2*b_hat*c_hat))
pupilR = int((math.pi/2 - angleA_hat) * EYE_DEPTH * ppcm)
# vertical
b = 6
angleA = (90 - vAngle)*math.pi/180
a = math.sqrt(b*b + c*c - 2*b*c*math.cos(angleA))
angleC = math.acos((a*a + b*b - c*c)/(2*a*b))
pupilV = int((angleC - math.pi/2) * EYE_DEPTH * ppcm)
def drawPupils():
pupilposL = (centerL[0]+pupilL, centerL[1]-pupilV)
pupilposR = (centerR[0]+pupilR, centerR[1]-pupilV)
screen.blit(pupil, pupilposL)
screen.blit(pupil, pupilposR)
def drawMonsterPupils():
centerLmonster = (158-43, 218-50)
centerMmonster = (390-25, 250-27)
centerRmonster = (640-43, 222-50)
pupilposL = (centerLmonster[0]+pupilL, centerLmonster[1]-pupilV)
pupilposR = (centerRmonster[0]+pupilR, centerRmonster[1]-pupilV)
pupilposM = (centerMmonster[0]+int((pupilL+pupilR)/2), centerMmonster[1]-pupilV)
screen.blit(monsterpupil, pupilposL)
screen.blit(monsterpupil, pupilposR)
screen.blit(monsterpupilsmall, pupilposM)
########## Setup face detection ##########
pupilL = 0
pupilR = 0
pupilV = 0
########## Setup misc ##########
interrupted = False
yes_detected = False
no_detected = False
timeout = 0.
show_buttons = False
offset = 800-170-100
eyeL = pygame.Rect(100, 210, 170, 170)
eyeR = pygame.Rect(offset, 210, 170, 170)
centerL = (185-30+10, 295-30)
centerR =(offset+85-30-10,295-30)
buttonL = pygame.Rect(0,0,100,70)
buttonR = pygame.Rect(800-100,0,100,70)
########## Events ##########
BLINKEVENT = pygame.USEREVENT + 1
BLINKEVENT2 = pygame.USEREVENT + 2
NORMALEVENT = pygame.USEREVENT + 3
WINKEVENT = pygame.USEREVENT + 4
HAPPYEVENT = pygame.USEREVENT + 5
LISTENEVENT = pygame.USEREVENT + 6
HAPPYSTARTEVENT = pygame.USEREVENT + 7
QUESTIONEVENT = pygame.USEREVENT + 8
INTERACTIONEVENT = pygame.USEREVENT + 9
GAZEEVENT = pygame.USEREVENT + 10
MONSTERBLINKEVENT = pygame.USEREVENT + 11
MULTIEVENT = pygame.USEREVENT + 12
NECKSERVOEVENT = pygame.USEREVENT + 13
happyevent = pygame.event.Event(HAPPYSTARTEVENT)
questionevent = pygame.event.Event(QUESTIONEVENT)
multiGestureLock = False
########## States ##########
NORMALSTATE = 0
BLINKSTATE = 1
WINKSTATE = 2
HAPPYSTATE = 3
CONFUSEDSTATE = 4
VIDEOSTATE = 5
BLINKSTATE2 = 6
HAPPYSTATE2 = 7
MENUSTATE = 8
COLORSTATE = 9
MONSTERSTATE = 10
MONSTERBLINKSTATE = 11
MONSTERBLINKSTATE2 = 12
MONSTERBLINKSTATE3 = 13
SPOOKYSTATE = 14
state = VIDEOSTATE
if __name__ == '__main__':
#manager = mp.Manager()
#dictionary = manager.dict()
#terminator = manager.Value('i', False)
#peoplecounter = manager.Value('i', 0)
#listproxy = manager.list()
#listproxy.append({})
#mp.set_start_method('spawn',force=True)
#tracking_proc = mp.Process(target=faceTracking, args=(listproxy,peoplecounter,terminator))
#tracking_proc.start()
pygame.time.set_timer(BLINKEVENT, 15000, True)
monsterblinks = [monsterblinkL, monsterblinkM, monsterblinkR]
#pygame.time.set_timer(NECKSERVOEVENT, 5000)
########## init interaction vars ##########
flow = threading.Thread(target=interaction)
threadevent = threading.Event()
trackID = 0
videoKeys = []
prevKeys = []
prevInteraction = 0
interactionWait = False
interactionIndex = 0
lastNumberOfActivations = 0
interactNumberOfActivations = 0
peopleAmount = 1 #len(trackedList)
frequency = 1 #from trailing_five
gazeAtClosest = True
pygame.time.set_timer(GAZEEVENT, 8000, True)
altTrackID = 0
interactionItems = []
runInteraction = False
########## Stats vars ##########
hoursList = list_with_operating_hours(hoursStart, hoursEnd)
oldNumberOfPeople = 0
numberOfPeople = 0
pygame.mouse.set_visible(False)
clock = pygame.time.Clock()
size = (800,480)
screen = pygame.display.set_mode(size) # pygame.NOFRAME
#pygame.display.toggle_fullscreen()
done = False
start = time.time()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
print("ESC")
done = True
elif event.key == pygame.K_s: