Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,46 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import tensorflow as tf
|
3 |
-
from tensorflow.keras.preprocessing import image
|
4 |
-
import numpy as np
|
5 |
-
from PIL import Image
|
6 |
-
|
7 |
-
# Load the trained model
|
8 |
-
model = tf.keras.models.load_model('deepfake_detection.h5')
|
9 |
-
|
10 |
-
# Function to load and preprocess the image
|
11 |
-
def load_and_preprocess_image(uploaded_image):
|
12 |
-
img = Image.open(uploaded_image)
|
13 |
-
img = img.resize((150, 150)) # Resize image to match the input size expected by the model
|
14 |
-
img_array = image.img_to_array(img) # Convert the image to a numpy array
|
15 |
-
img_array = np.expand_dims(img_array, axis=0) # Expand dimensions to match the input shape (1, 150, 150, 3)
|
16 |
-
img_array = img_array / 255.0 # Rescale the image array
|
17 |
-
return img_array
|
18 |
-
|
19 |
-
# Function to predict whether the image is real or fake
|
20 |
-
def predict_image(uploaded_image):
|
21 |
-
img_array = load_and_preprocess_image(uploaded_image)
|
22 |
-
prediction = model.predict(img_array)
|
23 |
-
|
24 |
-
if prediction < 0.5:
|
25 |
-
return "Fake"
|
26 |
-
else:
|
27 |
-
return "Real"
|
28 |
-
|
29 |
-
# Streamlit app layout
|
30 |
-
st.title("Deepfake Image Classification")
|
31 |
-
st.write("Upload an image and the model will predict whether it's Real or Fake.")
|
32 |
-
|
33 |
-
# Image uploader
|
34 |
-
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg"])
|
35 |
-
|
36 |
-
# Prediction button
|
37 |
-
if uploaded_image is not None:
|
38 |
-
st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
|
39 |
-
st.write("")
|
40 |
-
|
41 |
-
if st.button("Predict"):
|
42 |
-
result = predict_image(uploaded_image)
|
43 |
-
if result == "Fake":
|
44 |
-
st.write("The image is **<span style='color:red;'>Fake</span>**", unsafe_allow_html=True)
|
45 |
-
else:
|
46 |
-
st.write("The image is **<span style='color:
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import tensorflow as tf
|
3 |
+
from tensorflow.keras.preprocessing import image
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# Load the trained model
|
8 |
+
model = tf.keras.models.load_model('deepfake_detection.h5')
|
9 |
+
|
10 |
+
# Function to load and preprocess the image
|
11 |
+
def load_and_preprocess_image(uploaded_image):
|
12 |
+
img = Image.open(uploaded_image)
|
13 |
+
img = img.resize((150, 150)) # Resize image to match the input size expected by the model
|
14 |
+
img_array = image.img_to_array(img) # Convert the image to a numpy array
|
15 |
+
img_array = np.expand_dims(img_array, axis=0) # Expand dimensions to match the input shape (1, 150, 150, 3)
|
16 |
+
img_array = img_array / 255.0 # Rescale the image array
|
17 |
+
return img_array
|
18 |
+
|
19 |
+
# Function to predict whether the image is real or fake
|
20 |
+
def predict_image(uploaded_image):
|
21 |
+
img_array = load_and_preprocess_image(uploaded_image)
|
22 |
+
prediction = model.predict(img_array)
|
23 |
+
|
24 |
+
if prediction < 0.5:
|
25 |
+
return "Fake"
|
26 |
+
else:
|
27 |
+
return "Real"
|
28 |
+
|
29 |
+
# Streamlit app layout
|
30 |
+
st.title("Deepfake Image Classification")
|
31 |
+
st.write("Upload an image and the model will predict whether it's Real or Fake.")
|
32 |
+
|
33 |
+
# Image uploader
|
34 |
+
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg"])
|
35 |
+
|
36 |
+
# Prediction button
|
37 |
+
if uploaded_image is not None:
|
38 |
+
st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
|
39 |
+
st.write("")
|
40 |
+
|
41 |
+
if st.button("Predict"):
|
42 |
+
result = predict_image(uploaded_image)
|
43 |
+
if result == "Fake":
|
44 |
+
st.write("The image is **<span style='color:red;'>Fake</span>**", unsafe_allow_html=True)
|
45 |
+
else:
|
46 |
+
st.write("The image is **<span style='color:cyan;'>Real</span>**", unsafe_allow_html=True)
|