File size: 1,716 Bytes
3490b21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import tensorflow as tf
from tensorflow.keras.preprocessing import image
import numpy as np
from PIL import Image

# Load the trained model
model = tf.keras.models.load_model('deepfake_detection.h5')

# Function to load and preprocess the image
def load_and_preprocess_image(uploaded_image):
    img = Image.open(uploaded_image)
    img = img.resize((150, 150))  # Resize image to match the input size expected by the model
    img_array = image.img_to_array(img)  # Convert the image to a numpy array
    img_array = np.expand_dims(img_array, axis=0)  # Expand dimensions to match the input shape (1, 150, 150, 3)
    img_array = img_array / 255.0  # Rescale the image array
    return img_array

# Function to predict whether the image is real or fake
def predict_image(uploaded_image):
    img_array = load_and_preprocess_image(uploaded_image)
    prediction = model.predict(img_array)
    
    if prediction < 0.5:
        return "Fake"
    else:
        return "Real"

# Streamlit app layout
st.title("Deepfake Image Classification")
st.write("Upload an image and the model will predict whether it's Real or Fake.")

# Image uploader
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg"])

# Prediction button
if uploaded_image is not None:
    st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
    st.write("")
    
    if st.button("Predict"):
        result = predict_image(uploaded_image)
        if result == "Fake":
            st.write("The image is **<span style='color:red;'>Fake</span>**", unsafe_allow_html=True)
        else:
            st.write("The image is **<span style='color:cyan;'>Real</span>**", unsafe_allow_html=True)