Spaces:
Sleeping
Sleeping
# -*- coding: utf-8 -*- | |
"""Flower-recognition-app.ipynb | |
Automatically generated by Colaboratory. | |
Original file is located at | |
https://colab.research.google.com/drive/1Oxv1S-3SmLY0i0vKtlsduK8RVcj4EccJ | |
""" | |
# Commented out IPython magic to ensure Python compatibility. | |
# %reload_ext autoreload | |
# %autoreload 2 | |
# %matplotlib inline | |
bs = 32 # batch size | |
version = 2 | |
# Installing all 3 libraries in one go: fastai, fastbook, nbdev | |
#!pip install -Uqq fastai fastbook nbdev | |
#!pip install gradio==3.50.0 | |
from fastai import * | |
#from fastbook import * | |
from fastai.vision.all import * | |
# Commented out IPython magic to ensure Python compatibility. | |
# %cd /content/drive/My Drive/Mastercourse/Flower recognizer | |
flower_labels = [ | |
'Beli flower', | |
'Gada flower', | |
'Joba flower', | |
'Kamini flower', | |
'Kodom flower', | |
'Palash flower', | |
'Rose flower', | |
'Sheuli flower', | |
'Sunflower flower', | |
'Water Lily' | |
] | |
img_path = 'test_images' | |
model_path = f'models/flower-recognizer-v{version}.pkl' | |
#load the model from the pickle file | |
model = load_learner(model_path) | |
# Defining a function to recognize an image | |
def recognize_image(image): | |
pred, idx, probs = model.predict(image) | |
return dict(zip(flower_labels, map(float, probs))) | |
img = PILImage.create(f'test_images/unknown_flower_00.jpg') | |
img.thumbnail((192,192)) | |
img | |
recognize_image(img) | |
import gradio as gr | |
image = gr.inputs.Image(shape=(192,192)) | |
label = gr.outputs.Label() | |
examples = [ | |
'test_images/unknown_flower_00.jpg', | |
'test_images/unknown_flower_01.jpg', | |
'test_images/unknown_flower_02.jpg', | |
'test_images/unknown_flower_03.jpg', | |
'test_images/unknown_flower_04.jpg', | |
'test_images/unknown_flower_05.jpg', | |
] | |
iface = gr.Interface(fn=recognize_image, inputs=image, outputs=label, examples=examples) | |
iface.launch(share=True) |