File size: 1,358 Bytes
b0ed203
e4a6f06
 
b0ed203
e4a6f06
 
 
 
 
6be9476
 
 
 
5a8e087
 
 
e4a6f06
6be9476
b0ed203
 
 
 
5a8e087
b0ed203
 
5a8e087
b0ed203
 
 
 
 
e4a6f06
 
 
 
 
5a8e087
b0ed203
 
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
import streamlit as st
import tensorflow as tf
import numpy as np
from PIL import Image

# Load the model
model = tf.keras.models.load_model("vgg19_binary_nonbinary.h5")

def preprocess_image(image):
    # Convert RGBA to RGB if the image has an alpha channel
    if image.mode == "RGBA":
        image = image.convert("RGB")
    # Resize and normalize the image
    image = image.resize((224, 224))  # Resize to match model input size
    image = np.array(image) / 255.0   # Normalize pixel values
    image = np.expand_dims(image, axis=0)  # Add batch dimension
    return image
    
# Streamlit app
st.title("Binary vs Non-Binary Image Classification")
st.write("Upload an image to classify it as 'binary' or 'non-binary'.")

# File uploader
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
    # Display the uploaded image
    image = Image.open(uploaded_file)
    st.image(image, caption="Uploaded Image", use_column_width=True)
    st.write("Classifying...")

    # Preprocess and predict
    processed_image = preprocess_image(image)
    predictions = model.predict(processed_image)
    class_names = ["binary", "non-binary"]
    confidence = {class_names[i]: float(predictions[0][i]) for i in range(2)}

    # Display the prediction
    st.write("Prediction:")
    st.write(confidence)