import streamlit as st import numpy as np import tensorflow as tf from PIL import Image import cv2 # Load the model model = tf.keras.models.load_model('cnn_real_fake_4.h5') # Define a function to preprocess the uploaded image def preprocess_image(image): img_size = (256, 256) image = image.resize(img_size) # Resize image image = np.array(image) # Convert to numpy array image = image / 255.0 # Normalize to [0, 1] image = np.expand_dims(image, axis=0) # Add batch dimension return image # Define a function for making predictions def predict_image(image): preprocessed_image = preprocess_image(image) prediction = model.predict(preprocessed_image) if prediction > 0.5: return 'Fake' else: return 'Real' # Streamlit app interface with open("style.css") as f: st.markdown(f"", unsafe_allow_html=True) st.title('Real or Fake Image Classifier') st.write(""" This app allows you to upload an image and classify it as either Real or Fake. """) # Upload an image uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"]) if uploaded_image is not None: # Display the uploaded image image = Image.open(uploaded_image) img_resized = image.resize((100, 100)) st.image(img_resized, caption='Uploaded Image', use_container_width=False) # Predict the image st.write("") st.write("Classifying the image...") result = predict_image(image) # Display the result st.write(f"The uploaded image is classified as: **{result}**")