File size: 1,973 Bytes
b724048
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87dd939
b724048
ac35614
b724048
 
 
a8378ec
b724048
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3cd4e98
b724048
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Import library
import streamlit as st
import pandas as pd
import numpy as np
from PIL import Image
import pickle
import json
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.efficientnet import preprocess_input

# Load trained model
model = load_model('cnn_model.keras', compile=False)
# Define class labels
class_labels = ['Gasoline Can','Hammer', 'Pebbles','Pliers', 'Rope', 'Screwdriver', 'Toolbox', 'Wrench or Spanner']
  
def predict_and_display(uploaded_file, model, class_labels):
    img = Image.open(uploaded_file)
    img = img.resize((224, 224))
    img_array = np.array(img)
    img_array = np.expand_dims(img_array, axis=0)
    img_array = preprocess_input(img_array)

    prediction = model.predict(img_array)
    predicted_class_index = np.argmax(prediction)
    predicted_class_label = class_labels[predicted_class_index]

    st.image(img, use_column_width=True)
    st.write(f"Predicted Mechanical Part: {predicted_class_label}")

def run():
    st.write('##### Form Mechanical Parts Classifyer')
    # Making Form
    # Create a Streamlit form
    with st.form(key='Form Mechanical Parts Classifyer'):
        # Add a file uploader to the form
        uploaded_files = st.file_uploader("Upload a file of one of these format .JPEG/.JPG file", accept_multiple_files=True)

        # Check if any file is uploaded
        if uploaded_files:
            for uploaded_file in uploaded_files:
                st.write("filename:", uploaded_file.name)
        # Close the form
        submitted = st.form_submit_button('Predict')
    
    if submitted:
        for uploaded_file in uploaded_files:
            # Use the predict_and_display function with the uploaded image data
            predict_and_display(uploaded_file, model, class_labels)

        
if __name__ == '__main__':
    run()