File size: 1,666 Bytes
2dfbde4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import tensorflow as tf
import numpy as np
from PIL import Image
import json

# Load the trained CNN model
@st.cache_resource
def load_model():
    return tf.keras.models.load_model("model.h5")

model = load_model()

# Function to preprocess a single image
def preprocess_single_image(pil_img):
    """
    Preprocesses a Pillow image for model inference.

    Args:
        pil_img (PIL.Image.Image): A Pillow image object.

    Returns:
        preprocessed_img (tf.Tensor): Preprocessed image tensor.
    """
    img = pil_img.convert("RGB")  # Convert to RGB
    img = img.resize((224, 224))  # Resize
    img = np.array(img)  # Convert to NumPy array
    img = tf.keras.applications.efficientnet.preprocess_input(img)  # Apply EfficientNet preprocessing
    img = tf.expand_dims(img, axis=0)  # Add batch dimension
    return img

# Load class labels
CLASS_NAMES = json.load(open("class.json", "r"))

st.title("๐Ÿƒ Card Classification with CNN")
st.write("Upload an image to classify and visualize the top predictions.")

# Upload image
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_container_width=True)

    # Preprocess image
    img = preprocess_single_image(image)

    # Predict
    predictions = model.predict(img)
    predicted_class_index = np.argmax(predictions)  # Get highest probability index
    predicted_class = CLASS_NAMES[str(predicted_class_index)]  # Get class label

    # Display predictions
    st.write(f"Predictions Card : { predicted_class }")