File size: 1,880 Bytes
863aaff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4266cb
 
 
 
 
 
 
 
863aaff
 
 
 
 
d4266cb
3bac7c4
 
 
 
 
 
61f26cf
863aaff
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
76
77
78
79
80
81
82
83
84
import tensorflow as tf
from tensorflow import keras
import numpy as np
import gradio as gr

model = keras.models.load_model("Model.keras")

classnames = [
    "Acacia",
    "Adenanthera microsperma",
    "Adenium species",
    "Anacardium occidentale",
    "Annona squamosa",
    "Artocarpus altilis",
    "Artocarpus heterophyllus",
    "Barringtonia acutangula",
    "Cananga odorata",
    "Carica papaya",
    "Casuarina equisetifolia",
    "Cedrus",
    "Chrysophyllum cainino",
    "Citrus aurantiifolia",
    "Citrus grandis",
    "Cocos nucifera",
    "Dalbergia oliveri",
    "Delonix regia",
    "Dipterocarpus alatus",
    "Erythrina fusca",
    "Eucalyptus",
    "Ficus microcarpa",
    "Ficus racemosa",
    "Gmelina arborea Roxb",
    "Hevea brasiliensis",
    "Hopea",
    "Khaya senegalensis",
    "Khaya senegalensis A.Juss",
    "Lagerstroemia speciosa",
    "Magnolia alba",
    "Mangifera",
    "Melaleuca",
    "Melia azedarach",
    "Musa",
    "Nephelium lappaceum",
    "Persea",
    "Polyalthia longifolia",
    "Prunnus",
    "Prunus salicina",
    "Psidium guajava",
    "Pterocarpus macrocarpus",
    "Senna siamea",
    "Spondias mombin L",
    "Syzygium nervosum",
    "Tamarindus indica",
    "Tectona grandis",
    "Terminalia catappa",
    "Veitchia merrilli",
    "Wrightia",
    "Wrightia religiosa",
]


def predict(image):
    img = tf.image.resize(image, (224, 224))
    
    img = tf.cast(img, tf.float32) / 255.0
    
    pred = model.predict(tf.expand_dims(img, axis=0))
    
    confidences = {classnames[i]: float(pred[0][i]) for i in range(len(classnames))}
    return confidences


gr.Interface(
    fn=predict,
    inputs=gr.Image(),
    outputs=gr.Label(num_top_classes=5),
    examples=[
        "Dalbergia oliveri.JPG",
        "Eucalyptus.JPG",
        "Khaya senegalensis.JPG",
        "Syzygium nervosum.JPG",
    ],
).launch()