-
Notifications
You must be signed in to change notification settings - Fork 1
/
bats.py
279 lines (254 loc) · 9.89 KB
/
bats.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
import io
import json
import logging
import os
import pathlib
import random
import sys
import requests
from dateutil import parser
from flickrapi import FlickrAPI, shorturl
from google.api_core import exceptions
from google.cloud import datastore
from google.cloud import vision
from PIL import Image
from twython import Twython
import flickr_to_datastore
import utils
### LOGGING ####################################################################
logger = logging.getLogger(__name__)
utils.configure_logger(logger, console_output=True)
### HELPERS ####################################################################
def is_a(lst, labels):
return any(x in labels for x in lst)
def show_photos(entities):
"""What the heck did we get back from Flickr?"""
for entity in entities:
name = entity.key.name
try:
filepath = download_image(url=entity.get("download_url"),
name=name)
with Image.open(filepath) as im:
im.show()
except requests.exceptions.HTTPError as e:
logger.exception(e)
continue
def classify_as_resp_only(terms, entities):
for entity in entities:
logger.debug(entity)
name = entity.key.name
try:
filepath = download_image(url=entity.get("download_url"),
name=name)
except requests.exceptions.HTTPError as e:
logger.exception(e)
continue
logger.debug(f"Opening {name}...")
try:
with io.open(filepath, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
except Exception as e:
logger.exception(e)
continue
logger.info(f"Starting classification for {name}...")
try:
response = v_client.annotate_image({
"image": image,
"features": [{'type': vision.enums.Feature.Type.LABEL_DETECTION},
{'type': vision.enums.Feature.Type.OBJECT_LOCALIZATION}]
})
logger.debug(f"Response for {name} annotation request: {response}")
except exceptions.GoogleAPIError as e:
logger.exception(e)
continue
################################################################################
# Instantiate clients.
flickr = FlickrAPI(
os.environ['FLICKR_KEY'],
os.environ['FLICKR_SECRET'],
format="parsed-json"
)
twitter = Twython(
os.environ['TWITTER_CONSUMER_KEY'],
os.environ['TWITTER_CONSUMER_SECRET'],
os.environ['TWITTER_ACCESS_TOKEN'],
os.environ['TWITTER_ACCESS_SECRET']
)
ds_client = datastore.Client()
v_client = vision.ImageAnnotatorClient()
# Search Flickr for bats!
def search_flickr(search_string):
params = {"text": search_string,
"license": "1,2,3,4,5,6,8,9,10",
"media": "photos",
"content_type": "1", # Photos only
"safe_search": "1",
"extras": "tags,license,date_upload,owner_name,url_z,url_c,url_l,url_o",
"min_upload_date": "2018-10-01",
"max_upload_date": "2018-10-07",
"sort": "relevance",
"per_page": "8"}
resp = flickr.photos.search(**params)
photos = resp["photos"]["photo"]
return photos
# Turn Flickr results into entities.
def photos_to_entities(photos):
entities = list()
for photo in photos:
kind = "Photo"
name = "Flickr-" + photo.get("id")
key = ds_client.key(kind, name)
entity = datastore.Entity(key=key)
for k, v in photo.items():
if not k == "dateupload":
entity.update({k: v})
else:
entity.update({k: datetime.datetime.utcfromtimestamp(int(v))})
entity.update({
"source": "Flickr",
"search_terms": "bat",
"is_classified": False,
"download_url": flickr_to_datastore.get_download_url(entity)
})
entities.append(entity)
return entities
# Classify photos!
def classify_as(terms, entities):
bat_counter = 0
for entity in entities:
logger.debug(entity)
name = entity.key.name
# Download image. (You can use Cloud Vision API with a remote image, but
# it's flaky.)
try:
filepath = utils.download_image(url=entity.get("download_url"),
name=name)
except requests.exceptions.HTTPError as e:
logger.exception(e)
continue
# Load image.
logger.debug(f"Opening {name}...")
try:
with io.open(filepath, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
except Exception as e:
logger.exception(e)
continue
# Label image as a whole and find objects in image.
logger.info(f"Starting classification for {name}...")
try:
response = v_client.annotate_image({
"image": image,
"features": [{'type': vision.enums.Feature.Type.LABEL_DETECTION},
{'type': vision.enums.Feature.Type.OBJECT_LOCALIZATION}]
})
logger.debug(f"Response for {name} annotation request: {response}")
except exceptions.GoogleAPIError as e:
logger.exception(e)
continue
labels = set()
crop_boxes = set()
if response.label_annotations:
labels.update([a.description for a in response.label_annotations])
if response.localized_object_annotations:
labels.update([a.name.lower() for a in response.localized_object_annotations])
# While we're here, let's save crop boxes.
with Image.open(filepath) as im:
width, height = im.size
for a in response.localized_object_annotations:
verts = a.bounding_poly.normalized_vertices
crop_boxes.add(( round(verts[0].x * width),
round(verts[0].y * height),
round(verts[2].x * width),
round(verts[2].y * height) ))
# If it's not a bat but there are crop boxes, let's crop and get new labels.
if not is_a(["bat"], labels) and crop_boxes:
logger.debug(f"Cropping {name}...")
im = Image.open(filepath)
for cb in crop_boxes:
with im.crop(box=cb) as im2:
# Convert PIL.Image.Image to bytes.
with io.BytesIO() as buffer:
im2.save(buffer, format='JPEG')
byts = buffer.getvalue()
img = vision.types.Image(content=byts)
logger.debug(f"Requesting label detection for crop...")
try:
response = v_client.label_detection(image=img)
logger.debug(f"Response for crop label detection request: {response}")
except exceptions.GoogleAPIError as e:
logger.exception(e)
continue
if response.label_annotations:
labels.update([a.description for a in response.label_annotations])
if is_a(terms, labels):
# We found a bat, let's stop cropping and labeling.
break
im.close()
logger.debug(f"Done cropping {name}.")
logger.debug(f"{name}'s labels: {labels}")
entity.update({
"is_bat": is_a(terms, labels),
"vision_labels": json.dumps(list(labels)),
"is_classified": True
})
if entity.get("is_bat"):
bat_counter += 1
logger.info(f"{name} is_bat = {entity.get('is_bat')}!\n")
logger.debug(f"Processed {len(entities)} entities. Found {bat_counter} bats.")
return
def tweet_photo_entity(entity):
name = entity.key.name
logger.info(f"Tweeting {name}...")
logger.debug(entity)
# Write a message.
title = entity.get("title")
photographer = entity.get("ownername")
shortlink = shorturl.url(entity.get("id"))
message = f"{title} by {photographer} {shortlink} #HappyHalloween"
logger.debug(message)
# Download image if we somehow don't already have it.
filepath = os.path.join(os.path.dirname(__file__), f'assets/{name}.jpg')
if not pathlib.Path(filepath).exists():
filepath = download_image(url=entity.get("download_url"),
name=name)
# Load image and tweet.
try:
with io.open(filepath, 'rb') as img:
upload_resp = twitter.upload_media(media=img)
logger.debug(upload_resp)
tweet_resp = twitter.update_status(status=message,
media_ids=[upload_resp["media_id"]])
logger.debug(tweet_resp)
except Exception as e:
logger.exception(e)
sys.exit()
# Finally, let's remember when we tweeted.
try:
entity.update({
"last_tweeted": parser.parse(tweet_resp.get("created_at"))
})
ds_client.put(entity)
except Exception as e:
logger.exception(e)
################################################################################
if __name__ == "__main__":
photos = search_flickr("bat")
entities = photos_to_entities(photos)
show_photos(entities)
classify_as_resp_only(["bat"], entities)
# classify_as(["bat"], entities)
# with ds_client.batch():
# ds_client.put_multi(entities)
# Get bats from Cloud Datastore.
# query = ds_client.query(kind="Photo")
# query.add_filter("is_bat", "=", True)
# bats = list(query.fetch())
# Time to tweet!
# bat = random.choice(bats)
# tweet_photo_entity(bat)