#!/usr/bin/env python # coding: utf-8 # In[1]: from fastai.vision.all import * from fastai.vision.widgets import * # # The Amazing Bear Classifier! # You need to know whether you're being chased by a dangerous grizzly, or a sweet teddy bear, and you need an answer *fast*? Then you've come to the right place. Take a pic of the potentially vicious killer, and click 'upload' to classify it. (Important: this only handles grizzly bears, black bears, and teddy bears. It will **not** give a sensible answer for polar bears, a bear market, a bear of a man, or hot dogs. # # ---- # In[3]: path = Path() learn_inf = load_learner(path/'export.pkl', cpu=True) btn_upload = widgets.FileUpload() out_pl = widgets.Output() lbl_pred = widgets.Label() # In[4]: def on_data_change(change): lbl_pred.value = '' img = PILImage.create(btn_upload.data[-1]) out_pl.clear_output() with out_pl: display(img.to_thumb(128,128)) pred,pred_idx,probs = learn_inf.predict(img) lbl_pred.value = f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}' # In[5]: btn_upload.observe(on_data_change, names=['data']) # In[6]: display(VBox([widgets.Label('Select your bear!'), btn_upload, out_pl, lbl_pred])) # In[ ]: