Last active
December 18, 2015 10:29
-
-
Save austinogilvie/5768863 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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