File size: 2,601 Bytes
fe18fa2
 
 
 
 
 
 
 
304bdbc
fe18fa2
 
 
 
 
 
 
8229bca
443298c
76b160c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ddeea0
443298c
 
 
9184f1b
443298c
173d8f8
443298c
 
8229bca
 
fe18fa2
 
 
758d3f9
 
 
fe18fa2
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# AUTOGENERATED! DO NOT EDIT! File to edit: ../app.ipynb.

# %% auto 0
__all__ = ['path_to_pkl_model', 'learn', 'categories', 'image', 'label', 'intf', 'classify_image']

# %% ../app.ipynb 3
from fastai.vision.all import *
import gradio as gr
path_to_pkl_model = 'model.pkl'

# %% ../app.ipynb 6
learn = load_learner(path_to_pkl_model)

# %% ../app.ipynb 9
categories = learn.dls.vocab # learn.dls.vocab provides the categories from our trained model

new_categories = []

name_map = {"Yucca": "Hupȟéstola",
            "Prairie_Turnip": "Timspila",
            "Prairie_Turnip_Root": "Timpsila",
            "Juniper": "Hanté",
            "Poison_Hemlock": "Yažópi-hú",
            "Spruce": "wazíȟčaka",
            "Flax": "Haȟúntahu",
            "Plantain":"Wihúta-hú-iyéčhata",
            #"Scarlet_Gaura": "tȟatȟáwabluška",
            "Scarlet_Guara": "tȟatȟáwabluška",
            "Stone_Seed": "sunkačanka huipiye",
            "Juneberry": "wípazutkȟaŋ",
            "Wild_Rose_Bush": "uŋžíŋžiŋtka hú",
            "Red_Willow": "čhaŋšáša",
            "Cow_Parsnip": "pangi tȟáŋka",
            "Harebell": "waȟpé tȟó",
            "Yarrow": "tȟaópi pȟežúta",
            "Silver_Leaf_Scurfpea": "matȟó tȟathíŋpsila",
            "Poison_Ivy": "wikȟóška pȟežúta",
            "Burr_Oak_Tree": "útahu čháŋ",
            "Sochan": "wahpe zizicha sake",
            "Sego_Lily": "pšíŋ tȟáŋka",
            "Box_Elder_Mushroom": "čhaŋnákpa",
            "Box_Elder_Maple_Tree": "čhaŋšúška",
            "Pine_Tree": "wazí čháŋ",
            "Chokecherry": "čhaŋpȟá",
            "Smooth_Brome": "pezhi wasicun",
            "Burdock": "waȟpé tȟáŋka",
            "Yellow_Sweet_Clover": "waȟpé swúla",
            "Goatsbeard": "waȟčá zí iyéčheča",
            "Dog_Bane": "napéoilekiyapi",
            "Black_Hills_Spruce": "wazíȟčaka",
            "Raspberry_Shrub": "tȟakȟáŋhečala hú", }

for category in categories:
    if category in name_map.keys():
        new_categories.append(name_map[category] + f"_({category})")
    else:
        new_categories.append(category)
        print(category)

categories = new_categories

def classify_image(img):
    img = PILImage.create(img).resize((192, 192))
    pred,idx,probs = learn.predict(img)

    print(pred, idx, probs)

    return dict(zip(categories, map(float,probs)))

# %% ../app.ipynb 12
image = gr.Image()
label = gr.Label()

intf = gr.Interface(fn=classify_image, inputs=image, outputs=label)
intf.launch(inline=False)