File size: 1,530 Bytes
57ce089
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 streamlit as st 
import tensorflow as tf 
import cv2
from PIL import Image, ImageOps
import numpy as np

# st.set_option("deprecation.showfileUploaderEncoding", False)
@st.cache(allow_output_mutation=True)

def load_model():
    model = tf.keras.models.load_model("F:/igebra/internship/ai ready/machine learning/image_classification_cnn/cifar10_model.h5")
    return model

model = load_model()

st.title("CIFAR-10 Image Classification")
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])

import cv2
import numpy as np

def import_and_predict(image_data, model):
    size = (32, 32)
    image = np.array(image_data)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if len(image.shape) > 2 else cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
    image = cv2.resize(image, size, interpolation=cv2.INTER_AREA)
    image = image / 255.0
    img_reshape = np.expand_dims(image, axis=0)
    prediction = model.predict(img_reshape)
    return prediction


if uploaded_file is None:
    st.text("Please upload an image file")
else:
    image = Image.open(uploaded_file)
    st.image(image, use_column_width=True)
    predictions = import_and_predict(image, model)
    print(predictions)
    print(np.argmax(predictions))
    classes = ["airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"] 
    print(classes[np.argmax(predictions)])
    string = ("This image is most likely is :")
    st.success(f"This image most likely contains: {classes[np.argmax(predictions)]}")