-
Notifications
You must be signed in to change notification settings - Fork 22
/
camera_vision_label.py
42 lines (26 loc) · 1.44 KB
/
camera_vision_label.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
"""
Google Vision API Tutorial with a Raspberry Pi and Raspberry Pi Camera. See more about it here: https://www.dexterindustries.com/howto/use-google-cloud-vision-on-the-raspberry-pi/
Use Google Cloud Vision on the Raspberry Pi to take a picture with the Raspberry Pi Camera and classify it with the Google Cloud Vision API. First, we'll walk you through setting up the Google Cloud Platform. Next, we will use the Raspberry Pi Camera to take a picture of an object, and then use the Raspberry Pi to upload the picture taken to Google Cloud. We can analyze the picture and return labels (what's going on in the picture), logos (company logos that are in the picture) and faces.
This script uses the Vision API's label detection capabilities to find a label
based on an image's content.
"""
import picamera
from google.cloud import vision
client = vision.ImageAnnotatorClient()
def takephoto():
camera = picamera.PiCamera()
camera.capture('image.jpg')
def main():
takephoto() # First take a picture
"""Run a label request on a single image"""
with open('image.jpg', 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
response = client.logo_detection(image=image)
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')
for label in labels:
print(label.description)
if __name__ == '__main__':
main()