|
import streamlit as st |
|
from PIL import Image |
|
from transformers import AutoModelForImageClassification, ViTImageProcessor |
|
import torch |
|
|
|
|
|
@st.cache_resource |
|
def load_model(): |
|
model = AutoModelForImageClassification.from_pretrained("google/vit-base-patch16-224-in21k") |
|
processor = ViTImageProcessor.from_pretrained("google/vit-base-patch16-224-in21k") |
|
return model, processor |
|
|
|
model, processor = load_model() |
|
|
|
|
|
st.title("π± Plant Identification App π±") |
|
st.write("Upload a plant image and let the app identify its species!") |
|
|
|
|
|
uploaded_file = st.file_uploader("Choose a plant image...", type=["jpg", "jpeg", "png"]) |
|
|
|
if uploaded_file is not None: |
|
|
|
image = Image.open(uploaded_file) |
|
st.image(image, caption="Uploaded Plant Image.", use_column_width=True) |
|
|
|
|
|
inputs = processor(images=image, return_tensors="pt", padding=True) |
|
|
|
|
|
with st.spinner('Classifying plant species...'): |
|
outputs = model(**inputs) |
|
logits = outputs.logits |
|
predicted_class_idx = logits.argmax(-1).item() |
|
|
|
|
|
label = model.config.id2label[predicted_class_idx] |
|
|
|
|
|
st.write(f"Predicted Species: {label}") |
|
st.write(f"Confidence: {torch.softmax(logits, dim=-1)[0][predicted_class_idx]*100:.2f}%") |
|
|