File size: 1,370 Bytes
14b8fef
 
 
 
 
 
 
ad92166
14b8fef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from fastai.vision.all import *
import pathlib
plt = platform.system()
if plt == 'Linux': pathlib.WindowsPath = pathlib.PosixPath

current_dir = os.getcwd()
model = 'resnet_model_17_Sep_1.pkl'
model_path = os.path.join(current_dir, model)

model = load_learner(model_path)


st.title("Defect Classification")
st.write("Upload an image to check whether if it's normal or defective (stain)")

uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])

if uploaded_file is not None:
    image = Image.open(uploaded_file)
    st.image(image, caption="Uploaded Image", use_column_width=True)

    # Perform Predictions
    if st.button("Predict"):
        img = PILImage.create(uploaded_file)
        # Make predictions
        predictions, _ = model.get_preds(dl=model.dls.test_dl([img]))
        # Get the predicted class index
        predicted_class_idx = predictions.argmax(dim=1).item()
        # Get the confidence score for the predicted class
        confidence_score = predictions[0][predicted_class_idx].item()
        # Map the class index to class name
        class_names = model.dls.vocab
        predicted_class = class_names[predicted_class_idx]

        # Display the prediction and confidence score
        st.write(f"Prediction: {predicted_class}")
        st.write(f"Confidence: {confidence_score:.4f}")