File size: 2,516 Bytes
33eeb70
 
 
d5a8b69
 
33eeb70
 
cb61b97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33eeb70
 
 
 
 
 
2660746
33eeb70
 
2660746
d5a8b69
cb61b97
 
2660746
33eeb70
2660746
 
 
 
33eeb70
2660746
 
 
33eeb70
 
 
cb61b97
 
 
2660746
 
 
 
 
33eeb70
2660746
33eeb70
 
 
cb61b97
2660746
 
33eeb70
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# streamlit_app.py
import streamlit as st
from fastai.vision.all import *
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# Function to get the label from the file name
def GetLabel(fileName):
    return fileName.split('-')[0]

# Function to prepare data (similar to your code)
def prepare_data(food_path, label_a, label_b):
    for img in get_image_files(food_path):
        if label_a in str(img):
            img.rename(f"{img.parent}/{label_a}-{img.name}")
        elif label_b in str(img):
            img.rename(f"{img.parent}/{label_b}-{img.name}")
        else:
            os.remove(img)

# Function to load the pre-trained model
def load_pretrained_model():
    model_path = "export.pkl"  # Update with the correct path to your export.pkl
    return load_learner(model_path)

# Streamlit app
def main():
    st.title("Food Classifier Streamlit App")

    # Sidebar options
    options = ["Upload Image", "Test Random Images", "Confusion Matrix"]
    choice = st.sidebar.selectbox("Choose an option", options)

    if choice == "Upload Image":
        st.subheader("Upload Your Own Images")
        model = load_pretrained_model()

        uploaded_files = st.file_uploader("Choose images", type=["jpg", "jpeg", "png"], accept_multiple_files=True)

        if uploaded_files:
            for img in uploaded_files:
                img = PILImage.create(img)
                label, _, probs = model.predict(img)

                st.image(img, caption=f"This is a {label}.")
                st.write(f"{label}: {probs[1].item():.6f}")
                st.write(f"{label}: {probs[0].item():.6f}")

    elif choice == "Test Random Images":
        st.subheader("Test Using Images in Dataset")
        model = load_pretrained_model()
        food_path = Path("~/.fastai/data/food-101/food-101").expanduser()

        for i in range(0, 5):  # Change 5 to the number of images you want to display
            random_index = random.randint(0, len(get_image_files(food_path)) - 1)
            img_path = get_image_files(food_path)[random_index]
            img = mpimg.imread(img_path)
            label, _, probs = model.predict(img)

            st.image(img, caption=f"Predicted label: {label}")

    elif choice == "Confusion Matrix":
        st.subheader("Confusion Matrix")
        model = load_pretrained_model()
        interp = ClassificationInterpretation.from_learner(model)
        st.pyplot(interp.plot_confusion_matrix())

# Run the Streamlit app
if __name__ == "__main__":
    main()