-
Notifications
You must be signed in to change notification settings - Fork 0
/
pumpkinPi.py
131 lines (105 loc) · 3.72 KB
/
pumpkinPi.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
# pumpkinPi.py
# Measure distance using an ultrasonic module in a loop.
# Plays Mp3 and takes picures at certain distance detection
#
# --------------------------------
# Import required Python libraries
# --------------------------------
import time
import datetime
import os
import random
import RPi.GPIO as GPIO
import statistics
import random
import picamera
import json
# --------------------------------
# Declear Global Variables
# --------------------------------
GPIO.setmode(GPIO.BCM)
GPIO_TRIGGER = 18
GPIO_ECHO = 24
# Set pins as output and input
GPIO.setup(GPIO_TRIGGER, GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO, GPIO.IN) # Echo
# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)
time.sleep(0.5) # Allow module to settle
speedSound = 34300
filepath = os.path.dirname(__file__)
dir_path = os.path.join(filepath, "static/target_captured")
soundPathList = [os.path.join(filepath, 'Resources/sounds/Cat_Scream.mp3'),
os.path.join(filepath, 'Resources/sounds/Dark_Laugh.mp3'),
os.path.join(filepath, 'Resources/sounds/Evil_Laugh.mp3'),
os.path.join(
filepath, 'Resources/sounds/I_will_kill_you.mp3'),
os.path.join(filepath, 'Resources/sounds/Witches_Laugh.mp3')
]
# Initialize the camera
camera = picamera.PiCamera()
# --------------------------------
# Declear Functions for ultrasonic sensor
# --------------------------------
def get_distance():
""" This function takes in data and calculate the distance """
GPIO.output(GPIO_TRIGGER, True) # Send 10us pulse to trigger
time.sleep(0.00001) # Wait 10us
GPIO.output(GPIO_TRIGGER, False) # Set trigger to False (Low)
start = time.time()
while GPIO.input(GPIO_ECHO) == 0:
start = time.time()
while GPIO.input(GPIO_ECHO) == 1:
stop = time.time()
elapsed = stop-start
distance = (elapsed * speedSound) / 2
return distance
def get_avgdistance():
""" This function takes 3 measurements and return Avg distance """
distanceList = []
for _ in range(3):
d = get_distance()
time.sleep(0.1)
distanceList.append(d)
return statistics.mean(distanceList)
# --------------------------------
# Declear Functions Handling images
# --------------------------------
def readExistingImgPath():
""" This function Reads in all the imgs in the folder """
dataList = []
for (path, dirList, fileList) in os.walk(dir_path):
for f in fileList:
dataList.append(os.path.join("/static/target_captured", f))
dataList.sort(reverse=True)
return dataList
def dumpInData(imgList):
""" This function accepts data and dumps it into a json file"""
with open(os.path.join(filepath, 'ImgData.json'), 'w') as fh:
json.dump(imgList, fh, indent=3)
def captureTarget():
timestamp = datetime.datetime.now()
relativePath = f"static/target_captured/{timestamp.strftime('%d%b%Y_%H%M%S')}.jpg"
img_filepath = os.path.join(filepath, relativePath)
camera.capture(img_filepath)
updateFile()
def updateFile():
imgList = readExistingImgPath()
dumpInData(imgList)
# --------------------------------
# A while loop keeps taking in data and checking if it should react
# --------------------------------
print("Ultrasonic Measurement")
while True:
try:
distance = get_avgdistance()
updateFile()
print("Distance : %.1f" % distance)
if(distance == 0 or distance < 100):
path = random.choice(soundPathList)
captureTarget()
os.system(f"mpg321 {path}")
time.sleep(3) # time between loop iterations
except KeyboardInterrupt:
print("KeyboardInterrupt")
GPIO.cleanup()