-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_api-servo_move
More file actions
70 lines (45 loc) · 1.65 KB
/
google_api-servo_move
File metadata and controls
70 lines (45 loc) · 1.65 KB
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
""" if picture input detects bird with google api, triggers servo movement. """
import io
import os
import google
# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types
#Run following line in script or command line on pi before calling function to authorize API call
#export GOOGLE_APPLICATION_CREDENTIALS = 'PATH'
import RPi.GPIO as GPIO
import time
# Instantiates a client
client = vision.ImageAnnotatorClient()
# The name of the image file to annotate
file_name = os.path.join(
os.path.dirname(__file__),
'bird.jpg')
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations
for label in labels:
#if you want to see al the label descriptions run following line
#print(label.description)
if 'bird'==str(label.description):
print('found bird')
GPIO.setmode(GPIO.BOARD)
GPIO.setup(10, GPIO.OUT)
p = GPIO.PWM(10, 50)
p.start(7.5)
try:
while True:
p.ChangeDutyCycle(7.5) # turn towards 90 degree
time.sleep(1) # sleep 1 second
p.ChangeDutyCycle(2.5) # turn towards 0 degree
time.sleep(1) # sleep 1 second
p.ChangeDutyCycle(12.5) # turn towards 180 degree
time.sleep(1) # sleep 1 second
except KeyboardInterrupt:
p.stop()
GPIO.cleanup()
break