mg297bgyy commited on
Commit
11e2123
·
1 Parent(s): cb281b1
Files changed (1) hide show
  1. app.py +57 -28
app.py CHANGED
@@ -3,45 +3,74 @@
3
 
4
  # Voila runs jpnb but hides the code cells and only displays the output (including the ipywidgets) as well as the markdown cells.
5
 
6
- import fastbook
7
- fastbook.setup_book()
8
- from fastbook import *
9
  from fastai.vision.widgets import *
10
 
11
  # Load the model
12
  path = Path()
13
- path.ls(file_exts='.pkl')
14
-
15
  learn_inf = load_learner(path/'export.pkl')
16
 
17
- # Create a button widget
18
- btn_upload = widgets.FileUpload()
19
- btn_upload
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- # Create a clear_output widget
22
- out_pl = widgets.Output()
23
- out_pl.clear_output()
24
 
25
- # Create a label widget
26
- lbl_pred = widgets.Label()
27
 
28
- # Create a run button widget
29
- btn_run = widgets.Button(description='Classify')
30
- btn_run
31
 
32
- # Function to classify the image
33
- def on_click_classify(change):
34
- img = PILImage.create(btn_upload.data[-1])
35
- out_pl.clear_output()
36
- with out_pl: display(img.to_thumb(128,128))
37
- pred,pred_idx,probs = learn_inf.predict(img)
38
- lbl_pred.value = f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}'
39
 
40
- btn_run.on_click(on_click_classify)
41
 
42
- # Create a Vertical Box (VBox) to hold the widgets
43
- btn_upload = widgets.FileUpload()
44
- VBox([widgets.Label('Select your forest!'),
45
- btn_upload, btn_run, out_pl, lbl_pred])
46
 
47
 
 
3
 
4
  # Voila runs jpnb but hides the code cells and only displays the output (including the ipywidgets) as well as the markdown cells.
5
 
6
+ import streamlit as st
7
+ from fastai.vision.all import *
 
8
  from fastai.vision.widgets import *
9
 
10
  # Load the model
11
  path = Path()
 
 
12
  learn_inf = load_learner(path/'export.pkl')
13
 
14
+ # Title of the app
15
+ st.title('Forest Image Classifier')
16
+
17
+ # File uploader
18
+ uploaded_file = st.file_uploader("Select your forest image", type=['jpg', 'jpeg', 'png'])
19
+
20
+ # If an image has been uploaded
21
+ if uploaded_file is not None:
22
+ # Display the uploaded image
23
+ img = PILImage.create(uploaded_file)
24
+ st.image(img.to_thumb(128, 128), caption='Uploaded Image', use_column_width=True)
25
+
26
+ # Classify the image
27
+ pred, pred_idx, probs = learn_inf.predict(img)
28
+
29
+ # Display the prediction and probability
30
+ st.write(f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}')
31
+
32
+
33
+ #
34
+ #
35
+ # import fastbook
36
+ # fastbook.setup_book()
37
+ # from fastbook import *
38
+ # from fastai.vision.widgets import *
39
+
40
+ # # Load the model
41
+ # path = Path()
42
+ # path.ls(file_exts='.pkl')
43
+
44
+ # learn_inf = load_learner(path/'export.pkl')
45
+
46
+ # # Create a button widget
47
+ # btn_upload = widgets.FileUpload()
48
+ # btn_upload
49
 
50
+ # # Create a clear_output widget
51
+ # out_pl = widgets.Output()
52
+ # out_pl.clear_output()
53
 
54
+ # # Create a label widget
55
+ # lbl_pred = widgets.Label()
56
 
57
+ # # Create a run button widget
58
+ # btn_run = widgets.Button(description='Classify')
59
+ # btn_run
60
 
61
+ # # Function to classify the image
62
+ # def on_click_classify(change):
63
+ # img = PILImage.create(btn_upload.data[-1])
64
+ # out_pl.clear_output()
65
+ # with out_pl: display(img.to_thumb(128,128))
66
+ # pred,pred_idx,probs = learn_inf.predict(img)
67
+ # lbl_pred.value = f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}'
68
 
69
+ # btn_run.on_click(on_click_classify)
70
 
71
+ # # Create a Vertical Box (VBox) to hold the widgets
72
+ # btn_upload = widgets.FileUpload()
73
+ # VBox([widgets.Label('Select your forest!'),
74
+ # btn_upload, btn_run, out_pl, lbl_pred])
75
 
76