-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
119 lines (90 loc) · 3.49 KB
/
main.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
import falcon
import json
import numpy as np
import random
import string
import os
from urllib import request
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
from keras.models import load_model
from keras.preprocessing.image import img_to_array, load_img
class TestResource(object):
def on_get(self, req, res):
"""Handles all GET requests."""
res.status = falcon.HTTP_200
sauce = [random.choice(string.ascii_letters) for i in range(3)]
res.body = ("".join(sauce))
class KittyResource(object):
def load_model(self):
img_width, img_height = 300, 300
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.load_weights('weights/weights.h5')
return model
def request_image(self, kitty_id):
base_url = "https://img.cryptokitties.co/0x06012c8cf97bead5deae237070f9587f8e7a266d/"
fh = kitty_id + ".png"
endpoint = base_url + fh
request.urlretrieve(endpoint, "images/{}".format(fh))
def load_image(self, kitty_id):
# NB. just repeat this because class variables suck
img_width, img_height = 300, 300
fh = kitty_id + ".png"
if not os.path.isfile('images/{}'.format(fh)):
print('<<<<<<+++++++ CACHE MISS: REQUEST IMAGE ++++++>>>>>>')
self.request_image(kitty_id)
# TODO Get the kitty!
# NB. Insecure as fuck!
img = load_img("images/{}".format(fh), False, target_size=(img_width,img_height))
return img
def on_get(self, req, res):
img_width, img_height = 300, 300
if "ids" not in req.params:
res.status = falcon.HTTP_200
res.body = json.dumps({})
return
kitty_ids = req.params["ids"]
print(kitty_ids)
if kitty_ids is None or len(kitty_ids) is 0:
res.status = falcon.HTTP_200
res.body = json.dumps({})
return
model = self.load_model()
predictions = { "_nonce": random.random() }
for kitty_id in kitty_ids.split(","):
print("kitty_id: " + kitty_id)
img = self.load_image(kitty_id)
x = img_to_array(img)
x = np.expand_dims(x, axis=0)
preds = model.predict_classes(x)
is_criminal = True if (int(preds) is 0) else False
predictions[kitty_id] = { "criminal": is_criminal }
res.status = falcon.HTTP_200
res.body = json.dumps(predictions)
app = falcon.API()
test_resource = TestResource()
kitty_resource = KittyResource()
app.add_route('/test', test_resource)
app.add_route('/kitty', kitty_resource)