-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGoogle_Vision.py
88 lines (74 loc) · 2.69 KB
/
Google_Vision.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from __future__ import print_function
import boto3
import base64
import uuid, hashlib, pprint, logging, json
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
DISCOVERY_URL='https://{api}.googleapis.com/$discovery/rest?version={apiVersion}'
def handler(event, context):
logging.getLogger().setLevel(logging.INFO)
logging.info('Google_Vision.handler')
logging.info('event {}'.format(event))
if 'Records' in event:
message = extract_sns_message(event, context)
else:
# else called directly
message = event
download_path = fetch_photo(message['bucket'], message['key'])
logging.info("downloaded {}/{} to {}".format(message['bucket'], message['key'], download_path))
response = google_all_seeing_eye_dwim(download_path)
logging.info("detect face response: {}".format(response))
def google_all_seeing_eye_dwim(download_path, max_results=10):
try:
image = open(download_path, 'rb')
image_content = image.read()
except IOError as err:
logging.error("Failed to read image {}: {}").format(download_path, err)
return
batch_request = [{
'image': {
'content': base64.b64encode(image_content).decode('UTF-8')
},
'features': [{
'type': 'FACE_DETECTION',
'maxResults': max_results,
},
{
'type': 'LABEL_DETECTION',
'maxResults': max_results,
},
{
'type': 'IMAGE_PROPERTIES',
'maxResults': max_results,
},
{
'type': 'SAFE_SEARCH_DETECTION',
'maxResults': max_results,
}]
}]
service = get_vision_service()
request = service.images().annotate(body={
'requests': batch_request,
})
response = request.execute()
return response
def fetch_photo(bucket, key):
s3_client = boto3.client('s3')
download_path = '/tmp/{}{}'.format(uuid.uuid4(), key)
data = {}
try:
s3_client.download_file(bucket, key, download_path)
except IOError as err:
logging.error("Failed to download image {}/{} with {}").format(bucket, key, err)
return
return download_path
def get_vision_service():
credentials = GoogleCredentials.from_stream('./application_default_credentials.json')
return discovery.build('vision', 'v1', credentials=credentials,
discoveryServiceUrl=DISCOVERY_URL)
def extract_sns_message(event, context):
for record in event['Records']:
logging.info('looking at {}'.format(record))
if 'aws:sns' == record['EventSource']:
message = json.loads(record['Sns']['Message'])
return message