Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
|
| 5 |
import tensorflow as tf
|
| 6 |
import cv2
|
|
@@ -11,38 +11,72 @@ warnings.filterwarnings('ignore')
|
|
| 11 |
|
| 12 |
# Set Page Config
|
| 13 |
st.set_page_config(
|
| 14 |
-
page_title
|
| 15 |
-
page_icon
|
| 16 |
-
layout
|
| 17 |
-
initial_sidebar_state
|
| 18 |
)
|
| 19 |
|
|
|
|
| 20 |
@st.cache_resource()
|
| 21 |
def load_model():
|
| 22 |
model = tf.keras.models.load_model('models/model.h5')
|
| 23 |
return model
|
| 24 |
|
|
|
|
| 25 |
def preprocess_image(image):
|
| 26 |
-
img = cv2.resize(image, (224, 224))
|
| 27 |
img = tf.keras.applications.efficientnet.preprocess_input(img)
|
| 28 |
return img
|
| 29 |
|
| 30 |
# Home Page Content
|
| 31 |
def main():
|
| 32 |
model = load_model()
|
| 33 |
-
threshold = 0.33
|
| 34 |
|
| 35 |
with st.sidebar:
|
| 36 |
# Set Sidebar Content
|
| 37 |
st.sidebar.image('media/logo.png', use_column_width=True)
|
| 38 |
with st.container():
|
| 39 |
-
img_uploaded = st.file_uploader(
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
st.caption('Made with ❤️ by [DataFlow](https://dataflow.kz) team.')
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
if img_uploaded is not None:
|
| 44 |
with st.spinner('Processing the image, getting faces...'):
|
| 45 |
-
image = cv2.imdecode(np.frombuffer(
|
|
|
|
| 46 |
face_locations = face_recognition.face_locations(image)
|
| 47 |
if len(face_locations) == 0:
|
| 48 |
st.warning('Faces not found!')
|
|
@@ -55,16 +89,23 @@ def main():
|
|
| 55 |
|
| 56 |
prediction = model.predict(processed_face)
|
| 57 |
|
| 58 |
-
predicted_class = "FAKE" if prediction[0,
|
|
|
|
| 59 |
|
| 60 |
-
st.image(cv2.cvtColor(face_image, cv2.COLOR_BGR2RGB),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
if predicted_class == "FAKE":
|
| 63 |
st.warning('The image is most likely fake!')
|
| 64 |
else:
|
| 65 |
st.success('The image is most likely real!')
|
| 66 |
-
|
| 67 |
|
| 68 |
-
|
| 69 |
-
main()
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import numpy as np # linear algebra
|
| 3 |
+
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv
|
| 4 |
|
| 5 |
import tensorflow as tf
|
| 6 |
import cv2
|
|
|
|
| 11 |
|
| 12 |
# Set Page Config
|
| 13 |
st.set_page_config(
|
| 14 |
+
page_title='ReDeepFake Demo App',
|
| 15 |
+
page_icon='📊',
|
| 16 |
+
layout='wide',
|
| 17 |
+
initial_sidebar_state='auto',
|
| 18 |
)
|
| 19 |
|
| 20 |
+
|
| 21 |
@st.cache_resource()
|
| 22 |
def load_model():
|
| 23 |
model = tf.keras.models.load_model('models/model.h5')
|
| 24 |
return model
|
| 25 |
|
| 26 |
+
|
| 27 |
def preprocess_image(image):
|
| 28 |
+
img = cv2.resize(image, (224, 224))
|
| 29 |
img = tf.keras.applications.efficientnet.preprocess_input(img)
|
| 30 |
return img
|
| 31 |
|
| 32 |
# Home Page Content
|
| 33 |
def main():
|
| 34 |
model = load_model()
|
|
|
|
| 35 |
|
| 36 |
with st.sidebar:
|
| 37 |
# Set Sidebar Content
|
| 38 |
st.sidebar.image('media/logo.png', use_column_width=True)
|
| 39 |
with st.container():
|
| 40 |
+
img_uploaded = st.file_uploader(
|
| 41 |
+
"Choose an image...", type=["jpg", "png"])
|
| 42 |
+
threshold = st.select_slider(
|
| 43 |
+
'Threshold', options=[i/100 for i in range(0, 101, 5)], value=0.5)
|
| 44 |
+
st.info(
|
| 45 |
+
'ReDeepFake is an advanced Deepfake detection model for 2D flat images.')
|
| 46 |
st.caption('Made with ❤️ by [DataFlow](https://dataflow.kz) team.')
|
| 47 |
+
|
| 48 |
+
st.title('🧑 ReDeepFake v1.4')
|
| 49 |
+
st.markdown('''
|
| 50 |
+
This is a demo app for ReDeepFake model - Advanced Deepfake detection model for 2D flat images.
|
| 51 |
+
''')
|
| 52 |
+
colA, colB = st.columns(2)
|
| 53 |
+
colA.markdown('''
|
| 54 |
+
**How to use this app?**
|
| 55 |
+
1. Upload an image.
|
| 56 |
+
2. Adjust the threshold.
|
| 57 |
+
3. Image will be processed and real/fake faces will be detected automatically.
|
| 58 |
+
''')
|
| 59 |
+
colB.markdown('''
|
| 60 |
+
**Resources:**
|
| 61 |
+
* **Kaggle Notebook:** [ReDeepFake](https://www.kaggle.com/code/armanzhalgasbayev/deepfake-detection-efficientnetb4-tf-cnn)
|
| 62 |
+
* **GitHub Repository:** [ReDeepFake](https://github.com/silvermete0r/redeepfake-demo-app)
|
| 63 |
+
* **Hugging Face Demo App:** [ReDeepFake](https://huggingface.co/spaces/dataflow/redeepfake-demo)
|
| 64 |
+
* **Download Model:** [ReDeepFake](https://huggingface.co/dataflow/redeepfake)
|
| 65 |
+
''')
|
| 66 |
+
st.caption('**Note:** Before using the model read about limitations.')
|
| 67 |
+
with st.expander('Limitations', expanded=False):
|
| 68 |
+
st.markdown('''
|
| 69 |
+
1. The model's performance may be influenced by variations in lighting conditions, image quality, and diverse facial expressions.
|
| 70 |
+
|
| 71 |
+
2. It may not be fully robust against emerging deepfake generation techniques: Modern image generation methods use advanced descriptors to assess the quality of the image's realism, so photos of such faces are difficult to distinguish from real people.
|
| 72 |
+
|
| 73 |
+
3. Deepfakes made by using 3D image processing technologies and manually modified images by the authors cannot be recognized correctly by the model.
|
| 74 |
+
''')
|
| 75 |
+
|
| 76 |
if img_uploaded is not None:
|
| 77 |
with st.spinner('Processing the image, getting faces...'):
|
| 78 |
+
image = cv2.imdecode(np.frombuffer(
|
| 79 |
+
img_uploaded.read(), dtype=np.uint8), 1)
|
| 80 |
face_locations = face_recognition.face_locations(image)
|
| 81 |
if len(face_locations) == 0:
|
| 82 |
st.warning('Faces not found!')
|
|
|
|
| 89 |
|
| 90 |
prediction = model.predict(processed_face)
|
| 91 |
|
| 92 |
+
predicted_class = "FAKE" if prediction[0,
|
| 93 |
+
0] > threshold else "REAL"
|
| 94 |
|
| 95 |
+
st.image(cv2.cvtColor(face_image, cv2.COLOR_BGR2RGB),
|
| 96 |
+
caption=f"Face {i+1}: {predicted_class} | Score: {prediction[0, 0]:.2f}", width=350)
|
| 97 |
+
|
| 98 |
+
download_img = cv2.imencode('.png', face_image)[1].tobytes()
|
| 99 |
+
st.download_button(label="Download Image", data=download_img,
|
| 100 |
+
file_name=f"face_{i+1}_{predicted_class}.png", mime="image/png")
|
| 101 |
|
| 102 |
if predicted_class == "FAKE":
|
| 103 |
st.warning('The image is most likely fake!')
|
| 104 |
else:
|
| 105 |
st.success('The image is most likely real!')
|
|
|
|
| 106 |
|
| 107 |
+
st.divider()
|
|
|
|
| 108 |
|
| 109 |
+
|
| 110 |
+
if __name__ == "__main__":
|
| 111 |
+
main()
|