李根赢 commited on
Commit
13b99bb
1 Parent(s): 6a40026
Files changed (3) hide show
  1. app.py +31 -17
  2. images/black.jpg +0 -0
  3. images/teddy.jpg +0 -0
app.py CHANGED
@@ -1,30 +1,44 @@
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: gdrive/MyDrive/Colab Notebooks/Untitled8.ipynb.
2
 
3
  # %% auto 0
4
- __all__ = ['path', 'learn_inf', 'btn_upload', 'img', 'out_pl']
5
 
6
  # %% gdrive/MyDrive/Colab Notebooks/Untitled8.ipynb 25
7
- from fastai.vision.widgets import *
8
  from fastai.vision.all import *
9
  import gradio as gr
10
- from IPython.display import display
11
 
12
- # %% gdrive/MyDrive/Colab Notebooks/Untitled8.ipynb 26
13
- path = Path('bears')
14
- learn_inf = load_learner(path/'export.pkl')
15
 
16
  # %% gdrive/MyDrive/Colab Notebooks/Untitled8.ipynb 27
17
- btn_upload = widgets.FileUpload()
18
- btn_upload
 
 
 
 
 
 
 
19
 
20
- # %% gdrive/MyDrive/Colab Notebooks/Untitled8.ipynb 28
21
- btn_upload = SimpleNamespace(data = ['images/grizzly.jpg'])
 
 
 
 
22
 
23
- # %% gdrive/MyDrive/Colab Notebooks/Untitled8.ipynb 29
24
- img = PILImage.create(btn_upload.data[-1])
 
 
 
 
 
 
 
 
 
 
25
 
26
- # %% gdrive/MyDrive/Colab Notebooks/Untitled8.ipynb 30
27
- out_pl = widgets.Output()
28
- out_pl.clear_output()
29
- with out_pl: display(img.to_thumb(128,128))
30
- out_pl
 
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: gdrive/MyDrive/Colab Notebooks/Untitled8.ipynb.
2
 
3
  # %% auto 0
4
+ __all__ = ['learn_inf', 'lbl_pred', 'image', 'label', 'examples', 'intf', 'btn_upload', 'classify_image', 'on_click_classify']
5
 
6
  # %% gdrive/MyDrive/Colab Notebooks/Untitled8.ipynb 25
 
7
  from fastai.vision.all import *
8
  import gradio as gr
 
9
 
 
 
 
10
 
11
  # %% gdrive/MyDrive/Colab Notebooks/Untitled8.ipynb 27
12
+ learn_inf = load_learner('bears/export.pkl')
13
+
14
+ # %% gdrive/MyDrive/Colab Notebooks/Untitled8.ipynb 33
15
+ lbl_pred = learn_inf.dls.vocab
16
+
17
+ def classify_image(img):
18
+ img = img.to_thumb(128,128)
19
+ pred,pred_idx,probs = learn_inf.predict(img)
20
+ return dict(zip(lbl_pred, map(float, probs)))
21
 
22
+ # %% gdrive/MyDrive/Colab Notebooks/Untitled8.ipynb 34
23
+ image = gr.Image(type="pil")
24
+ label = gr.Label()
25
+ examples = ['images/black.jpg', 'images/grizzly.jpg', 'images/teddy.jpg']
26
+ intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
27
+ intf.launch(inline=False, debug=True)
28
 
29
+ # %% gdrive/MyDrive/Colab Notebooks/Untitled8.ipynb 35
30
+ def on_click_classify(change):
31
+ img = PILImage.create(btn_upload.data[-1])
32
+ out_pl.clear_output()
33
+ with out_pl: display(img.to_thumb(128,128))
34
+ pred,pred_idx,probs = learn_inf.predict(img)
35
+ lbl_pred.value = f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}'
36
+
37
+ btn_run.on_click(on_click_classify)
38
+
39
+ # %% gdrive/MyDrive/Colab Notebooks/Untitled8.ipynb 36
40
+ btn_upload = widgets.FileUpload()
41
 
42
+ # %% gdrive/MyDrive/Colab Notebooks/Untitled8.ipynb 37
43
+ VBox([widgets.Label('Select your bear!'),
44
+ btn_upload, btn_run, out_pl, lbl_pred])
 
 
images/black.jpg ADDED
images/teddy.jpg ADDED