Spaces:
Sleeping
Sleeping
| # -*- 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() | |