Spaces:
Sleeping
Sleeping
File size: 1,327 Bytes
cfdcae4 |
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 |
import streamlit as st
import torch
from PIL import Image
from torchvision import transforms
from model import ResNet50 # Assuming your model architecture is defined in a separate file called model.py
# Load the model
model = ResNet50()
model.load_state_dict(torch.load('best_modelv2.pth', map_location=torch.device('cpu')))
model.eval()
# Define transform for input images
data_transforms = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# Function to predict image label
def predict_image_label(image):
# Preprocess the image
image = data_transforms(image).unsqueeze(0)
# Make prediction
with torch.no_grad():
output = model(image)
_, predicted = torch.max(output, 1)
return predicted.item()
# Streamlit app
st.title("Leaf or Plant Classifier")
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Display the uploaded image
image = Image.open(uploaded_file)
st.image(image, caption='Uploaded Image', use_column_width=True)
# Classify the image
prediction = predict_image_label(image)
label = 'Leaf' if prediction == 0 else 'Plant'
st.write(f"Prediction: {label}")
|