File size: 1,212 Bytes
aaff7e3 8f378e3 2532cd8 8f378e3 2532cd8 aaff7e3 59f025f 09aa75f 2532cd8 bd88d49 59f025f aaff7e3 2532cd8 59f025f 09aa75f aaff7e3 2532cd8 4915f06 2532cd8 4915f06 2532cd8 f802131 4915f06 8f378e3 59f025f | 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 | from fastai.vision.all import *
import gradio as gr
from PIL import Image
import numpy as np
# Load the model
learn = load_learner('export.pkl')
categories = learn.dls.vocab
# Create a mapping from original label → display label
pretty_labels = {cat: cat.replace('_', ' ').title() for cat in categories}
# Display-friendly veggie list
veggie_list = ', '.join(pretty_labels[cat] for cat in categories)
description = f"Upload a vegetable image to classify it.\n\n The model can recognize: {veggie_list}."
# Prediction function
def classify_image(img):
if isinstance(img, str): # If it's a filepath
img = Image.open(img)
elif isinstance(img, np.ndarray): # If it's a NumPy array
img = Image.fromarray(img)
# If it's already a PIL Image, no conversion needed
pred, idx, probs = learn.predict(img)
return {
pretty_labels[c]: float(probs[i])
for i, c in enumerate(categories)
}
# Gradio UI
image = gr.Image(type="pil", label="Upload a vegetable image")
label = gr.Label(label="Prediction")
intf = gr.Interface(
fn=classify_image,
inputs=image,
outputs=label,
title="Vegetable Classifier",
description=description
)
intf.launch()
|