/
raspdetector.py
66 lines (55 loc) · 1.68 KB
/
raspdetector.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
import RPi.GPIO as GPIO
import time
import picamera
from picamera.array import PiRGBArray
import libdetect
from gtts import gTTS
import vlc
# Set the GPIOS
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO_TRIGGER = 17
GPIO_ECHO = 27
GPIO.setup(25,GPIO.IN, pull_up_down=GPIO.PUD_UP) # Switch da câmera
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
camera = picamera.PiCamera()
rawCapture = PiRGBArray(camera)
def calc_distance():
# set Trigger to HIGH
GPIO.output(GPIO_TRIGGER, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(GPIO_ECHO) == 0:
#print(GPIO.input(GPIO_ECHO))
StartTime = time.time()
# save time of arrival
while GPIO.input(GPIO_ECHO) == 1:
StopTime = time.time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
return distance
def wait_photo(d):
# Check button:
input_state = GPIO.input(25)
if input_state == 0:
camera.capture(rawCapture, format="bgr")
image = rawCapture.array
print("Photo! Distance:",d)
texto = libdetect.detect(image) + " distância: " + str(d) + " centímetros."
tts = gTTS(texto, lang='pt')
tts.save("text.mp3")
p = vlc.MediaPlayer("./text.mp3")
p.play()
time.sleep(0.2)
while (True):
dist = calc_distance()
#print ("Measured Distance = %.1f cm" % dist)
wait_photo(dist)