forked from Hironsan/google-vision-sampler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabel_detection.py
48 lines (36 loc) · 1.29 KB
/
label_detection.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
"""
This script uses the Vision API's label detection capabilities to find a label
based on an image's content.
To run the example, install the necessary libraries by running:
pip install -r requirements.txt
Run the script on an image to get a label, E.g.:
./label_detection.py <path-to-image>
"""
import argparse
import os
from utils import Service, encode_image
def main(photo_file):
"""Run a label request on a single image"""
access_token = os.environ.get('VISION_API')
service = Service('vision', 'v1', access_token=access_token)
with open(photo_file, 'rb') as image:
base64_image = encode_image(image)
body = {
'requests': [{
'image': {
'content': base64_image,
},
'features': [{
'type': 'LABEL_DETECTION',
'maxResults': 1,
}]
}]
}
response = service.execute(body=body)
label = response['responses'][0]['labelAnnotations'][0]['description']
print('Found label: {}'.format(label))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('image_file', help='The image you\'d like to label.')
args = parser.parse_args()
main(args.image_file)