-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_vision_lambda.py
44 lines (37 loc) · 1.39 KB
/
google_vision_lambda.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
"""
Author: Ryan Liang
This is the lambda function that will be used to call the Google Vision API.
This function will be triggered by the S3 bucket when a new json of image is uploaded,
and the output will be stored in another S3 bucket.
The image parquets was preprocessed into format of {label: label, image_url: image_url}
from the original parquet.
"""
import os
import boto3
import json
import requests
from google.cloud import vision
API_KEY = 'AIzaSyCTtwcZvAV4y-gE3I6gaz61h1ziLhDxQu8'
s3 = boto3.client('s3')
client = vision.ImageAnnotatorClient(client_options={"api_key": API_KEY})
def lambda_handler(event, context):
image_label = str(event['label'])
image_url = str(event['image_url'])
try:
response = requests.get(image_url)
if response.status_code == 200:
byte_image = response.content
image = vision.Image(content=byte_image)
response = client.label_detection(image=image, max_results=50)
key = image_label + '.json'
output = dict()
output['id'] = image_label
output['response'] = [label.description for label in response.label_annotations]
output_json = json.dumps(output)
s3.put_object(
Body=output_json,
Bucket='image-annotate-output',
Key=key
)
except Exception as e:
print(e)