File size: 1,248 Bytes
76c91ec
 
 
 
 
984c660
 
76c91ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44bf6d0
1d2df2b
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
# Hide - Connects voila to current notebook
#!jupyter serverextension enable --sys-prefix voila

# Voila runs jpnb but hides the code cells and only displays the output (including the ipywidgets) as well as the markdown cells.

import fastbook
fastbook.setup_book()
from fastbook import *
from fastai.vision.widgets import *

# Load the model
path = Path()
path.ls(file_exts='.pkl')

learn_inf = load_learner(path/'export.pkl')

# Create a button widget
btn_upload = widgets.FileUpload()
btn_upload

# Create a clear_output widget
out_pl = widgets.Output()
out_pl.clear_output()

# Create a label widget
lbl_pred = widgets.Label()

# Create a run button widget
btn_run = widgets.Button(description='Classify')
btn_run

# Function to classify the image
def on_click_classify(change):
    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}'

btn_run.on_click(on_click_classify)

# Create a Vertical Box (VBox) to hold the widgets
btn_upload = widgets.FileUpload()
VBox([widgets.Label('Select your forest!'),
      btn_upload, btn_run, out_pl, lbl_pred])