Skip to content

Instantly share code, notes, and snippets.

@austinogilvie
Last active December 18, 2015 10:29
Show Gist options
  • Select an option

  • Save austinogilvie/5768863 to your computer and use it in GitHub Desktop.

Select an option

Save austinogilvie/5768863 to your computer and use it in GitHub Desktop.
class ImageClassifier(BaseModel):
def require(self):
from StringIO import StringIO
from PIL import Image
import base64
def transform(self, image_string):
#we need to decode the image from base64
image_string = base64.decodestring(image_string)
#since we're seing this as a JSON string, we use StringIO so it acts like a file
img = StringIO(image_string)
img = Image.open(img)
img = img.resize(self.STANDARD_SIZE)
img = list(img.getdata())
img = map(list, img)
img = np.array(img)
s = img.shape[0] * img.shape[1]
img_wide = img.reshape(1, s)
return self.pca.transform(img_wide[0])
def predict(self, data):
preds = self.knn.predict(data)
preds = np.where(preds==1, "check", "drivers_license")
pred = preds[0]
return {"image_label": pred}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment