File size: 1,219 Bytes
06bf0a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
import subprocess
import sys

def install(package):
    subprocess.check_call([sys.executable, "-m", "pip", "install", package])

install('fastai')
install('torch')

import streamlit as st
from fastai.vision.all import *
from PIL import Image

# Load the model (ensure model file is in the same directory or provide correct path)
@st.cache(allow_output_mutation=True)
def load_model():
    # Replace 'model.pkl' with your model file
    model = load_learner('modelmarvel.pkl')
    return model

# Load the model
model = load_model()

# Streamlit application
st.title("Image Classification with fastai")

# Image upload
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])

if uploaded_file:
    # Open and display the image
    image = Image.open(uploaded_file).convert("RGB")
    st.image(image, caption="Uploaded Image", use_column_width=True)
    
    # Prepare image for the model
    img = PILImage.create(uploaded_file)
    
    # Predict
    pred, pred_idx, probs = model.predict(img)
    
    # Display results
    st.write(f"Prediction: {pred}")
    st.write(f"Probability: {probs[pred_idx]:.4f}")

    # Optionally, display confidence as a bar chart
    st.bar_chart(probs.numpy())