File size: 3,792 Bytes
5e033e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd70b1f
 
 
 
 
 
 
 
 
5e033e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01a84fb
 
5e033e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 28 18:48:07 2024

@author: liewchooichin
"""
import os
import pathlib
import gradio as gr
import pandas as pd
# my own py to make predictions
import image_pretrained

# global variables
# predictions from:
pred_eff = pd.DataFrame()  # Efficient Net
pred_mob = pd.DataFrame()  # Mobile Net
pred_xcept = pd.DataFrame()  # Xception


def get_prediction(img_path):
    pred_eff, pred_mob, pred_xcept = \
        image_pretrained.predict(img_path)
    print(pred_eff)
    return pred_eff, pred_mob, pred_xcept


def clear_image(img):
    # Clear the previous output result
    return pred_eff, pred_mob, pred_xcept


with gr.Blocks() as demo:
    image_width = 256
    image_height = 256

    gr.Markdown(
        """
        # Image classfication

        Predict the class of the image with pretrained model.

        Models: Xception, MobileNet V3 Small, \
        EfficientNet V2 Small.

        These models were trained on ImageNet 1000. \
        Go to [IMAGENET 1000 Class List]\
        (https://deeplearning.cms.waikato.ac.nz/user-guide/class-maps/IMAGENET/)\
        to see what objects the models can recognize.

        Tip: The ImageNet does not contain classes for people or faces.\
        Input some image of people and human faces and the model will give \
        interesting predictions!

        Top three predictions of classes are shown for each \
        of the model.

        Upload an image for predictions of its class and \
        its probabilities.
        """
    )
    with gr.Row():
        with gr.Column():
            img = gr.Image(height=image_height,
                           width=image_width,
                           sources=["upload", "clipboard"],
                           interactive=True,
                           type="filepath")

        # label_1 = gr.Label(label="Efficient net")
        # label_2 = gr.Label(label="Mobile net")
        # label_3 = gr.Label(label="Xception")
        with gr.Column():
            text_1 = gr.Text(label="Efficient net v2")
            text_2 = gr.Text(label="Mobile net v3")
            text_3 = gr.Text(label="Xception")

    # load the images directory
    data_dir = "images"
    img_path = pathlib.Path(data_dir)
    image_list = [[i] for i in list(img_path.glob("*.jpg"))]
    print(f"List of examples: {image_list}")
    examples = gr.Examples(
        examples=[
            os.path.join(os.path.dirname(__file__), "images",
                         "cat.jpg"),
            os.path.join(os.path.dirname(__file__), "images",
                         "mrt_train.jpg"),
            os.path.join(os.path.dirname(__file__), "images",
                         "duck.jpg"),
            os.path.join(os.path.dirname(__file__), "images",
                         "daisy.jpg"),
            os.path.join(os.path.dirname(__file__), "images",
                         "apples.jpg"),
            os.path.join(os.path.dirname(__file__), "images",
                         "bus.jpg"),
            os.path.join(os.path.dirname(__file__), "images",
                         "butterfly.jpg"),
            os.path.join(os.path.dirname(__file__), "images",
                         "me_small.jpg"),
        ],
        inputs=[img],
        outputs=[text_1, text_2, text_3],
        run_on_click=True, 
        fn=get_prediction
    )
    # prediction when a file is uploaded
    img.upload(fn=get_prediction, inputs=[img],
               outputs=[text_1, text_2, text_3])
    # when an example is clicked
    img.change(fn=get_prediction, inputs=[img],
               outputs=[text_1, text_2, text_3])
    # when an image is cleared
    img.clear(fn=clear_image, inputs=[img],
              outputs=[text_1, text_2, text_3])

if __name__ == "__main__":
    demo.launch()