Pushp123 commited on
Commit
0b08e64
·
verified ·
1 Parent(s): 5ed441d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +297 -0
app.py ADDED
@@ -0,0 +1,297 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ''' !pip install seaborn
2
+ !pip install keras
3
+ !pip install tensorflow
4
+
5
+ !python3.10 -m pip install --upgrade pip
6
+
7
+ !pip install scikit-learn scipy matplotlib'''
8
+
9
+ import pandas as pd
10
+ import numpy as np
11
+ import matplotlib.pyplot as plt
12
+ import matplotlib.image as mpimg
13
+ import seaborn as sns
14
+ %matplotlib inline
15
+
16
+ np.random.seed(2)
17
+
18
+ from sklearn.model_selection import train_test_split
19
+ from sklearn.metrics import confusion_matrix
20
+ import itertools
21
+
22
+ from tensorflow.keras.utils import to_categorical
23
+ from keras.models import Sequential
24
+ from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
25
+ from keras.optimizers import RMSprop
26
+
27
+ from tensorflow.keras.preprocessing.image import ImageDataGenerator
28
+
29
+ from keras.callbacks import ReduceLROnPlateau, EarlyStopping
30
+
31
+ sns.set(style='white', context='notebook', palette='deep')
32
+
33
+
34
+ from PIL import Image
35
+ import os
36
+ from pylab import *
37
+ import re
38
+ from PIL import Image, ImageChops, ImageEnhance
39
+
40
+ def get_imlist(path):
41
+ return [os.path.join(path,f) for f in os.listdir(path) if f.endswith('.jpg') or f.endswith('.png')]
42
+
43
+ def convert_to_ela_image(path, quality):
44
+ filename = path
45
+ resaved_filename = filename.split('.')[0] + '.resaved.jpg'
46
+ ELA_filename = filename.split('.')[0] + '.ela.png'
47
+
48
+ im = Image.open(filename).convert('RGB')
49
+ im.save(resaved_filename, 'JPEG', quality=quality)
50
+ resaved_im = Image.open(resaved_filename)
51
+
52
+ ela_im = ImageChops.difference(im, resaved_im)
53
+
54
+ extrema = ela_im.getextrema()
55
+ max_diff = max([ex[1] for ex in extrema])
56
+ if max_diff == 0:
57
+ max_diff = 1
58
+ scale = 255.0 / max_diff
59
+
60
+ ela_im = ImageEnhance.Brightness(ela_im).enhance(scale)
61
+
62
+ return ela_im
63
+
64
+
65
+
66
+ Image.open('Deep Fake Dataset/real_images/6401_0.jpg')
67
+
68
+ convert_to_ela_image('Deep Fake Dataset/real_images/6401_0.jpg',90)
69
+
70
+ Image.open('Deep Fake Dataset/fake_images/1601_0.jpg')
71
+
72
+ convert_to_ela_image('Deep Fake Dataset/fake_images/1601_0.jpg',90)
73
+
74
+ import os
75
+ import csv
76
+ from PIL import Image # Use PIL for image processing
77
+
78
+ def create_image_dataset_csv(fake_folder, real_folder, output_csv):
79
+ # Initialize an empty list to store image information
80
+ image_data = []
81
+
82
+ # Process fake images
83
+ fake_files = os.listdir(fake_folder)
84
+ for filename in fake_files:
85
+ if filename.endswith('.jpg') or filename.endswith('.png'): # Adjust based on your image formats
86
+ file_path = os.path.join(fake_folder, filename)
87
+ label = 0 # Assign label 0 for fake
88
+ image_data.append((file_path, label))
89
+
90
+ # Process real images
91
+ real_files = os.listdir(real_folder)
92
+ for filename in real_files:
93
+ if filename.endswith('.jpg') or filename.endswith('.png'): # Adjust based on your image formats
94
+ file_path = os.path.join(real_folder, filename)
95
+ label = 1 # Assign label 1 for real
96
+ image_data.append((file_path, label))
97
+
98
+ # Write image data to CSV file
99
+ with open(output_csv, 'w', newline='') as csvfile:
100
+ csv_writer = csv.writer(csvfile)
101
+ csv_writer.writerow(['file_path', 'label']) # Header row
102
+ csv_writer.writerows(image_data)
103
+
104
+ print(f"CSV file '{output_csv}' has been created successfully with {len(image_data)} entries.")
105
+
106
+ # Example usage:
107
+ fake_images_folder = 'Deep Fake Dataset/fake_images'
108
+ real_images_folder = 'Deep Fake Dataset/real_images'
109
+ output_csv_file = 'image_dataset.csv'
110
+
111
+ create_image_dataset_csv(fake_images_folder, real_images_folder, output_csv_file)
112
+
113
+ import pandas as pd
114
+
115
+
116
+ # dataset = pd.read_csv('datasets/dataset.csv')
117
+ dataset = pd.read_csv('image_dataset.csv')
118
+
119
+ dataset.head()
120
+
121
+ X = []
122
+ Y = []
123
+
124
+ X
125
+
126
+ for index, row in dataset.iterrows():
127
+ X.append(array(convert_to_ela_image(row[0], 90).resize((128, 128))).flatten() / 255.0)
128
+ Y.append(row[1])
129
+
130
+ X = np.array(X)
131
+ Y = to_categorical(Y, 2)
132
+
133
+ X = X.reshape(-1, 128, 128, 3)
134
+
135
+ X_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size = 0.2, random_state=5)
136
+
137
+ model = Sequential()
138
+
139
+ model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'valid',
140
+ activation ='relu', input_shape = (128,128,3)))
141
+ print("Input: ", model.input_shape)
142
+ print("Output: ", model.output_shape)
143
+
144
+ model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'valid',
145
+ activation ='relu'))
146
+ print("Input: ", model.input_shape)
147
+ print("Output: ", model.output_shape)
148
+
149
+ model.add(MaxPool2D(pool_size=(2,2)))
150
+
151
+ model.add(Dropout(0.25))
152
+ print("Input: ", model.input_shape)
153
+ print("Output: ", model.output_shape)
154
+
155
+ model.add(Flatten())
156
+ model.add(Dense(256, activation = "relu"))
157
+ model.add(Dropout(0.5))
158
+ model.add(Dense(2, activation = "softmax"))
159
+
160
+
161
+ model.summary()
162
+
163
+ optimizer = RMSprop(learning_rate=0.0005, rho=0.9, epsilon=1e-08, decay=0.0)
164
+
165
+ model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
166
+
167
+ early_stopping = EarlyStopping(monitor='val_acc',
168
+ min_delta=0,
169
+ patience=2,
170
+ verbose=0, mode='max')
171
+
172
+ epochs = 10
173
+ batch_size = 100
174
+
175
+ history = model.fit(X_train, Y_train, batch_size = batch_size, epochs = epochs,
176
+ validation_data = (X_val, Y_val), verbose = 2, callbacks=[early_stopping])
177
+
178
+ # Plot the loss and accuracy curves for training and validation
179
+ fig, ax = plt.subplots(2,1)
180
+ ax[0].plot(history.history['loss'], color='b', label="Training loss")
181
+ ax[0].plot(history.history['val_loss'], color='r', label="validation loss")
182
+ legend = ax[0].legend(loc='best', shadow=True)
183
+
184
+ ax[1].plot(history.history['accuracy'], color='b', label="Training accuracy")
185
+ ax[1].plot(history.history['val_accuracy'], color='r',label="Validation accuracy")
186
+ legend = ax[1].legend(loc='best', shadow=True)
187
+
188
+
189
+ from sklearn.metrics import confusion_matrix
190
+
191
+ def plot_confusion_matrix(cm, classes,
192
+ normalize=False,
193
+ title='Confusion matrix',
194
+ cmap=plt.cm.Blues):
195
+ """
196
+ This function prints and plots the confusion matrix.
197
+ Normalization can be applied by setting `normalize=True`.
198
+ """
199
+ plt.imshow(cm, interpolation='nearest', cmap=cmap)
200
+ plt.title(title)
201
+ plt.colorbar()
202
+ tick_marks = np.arange(len(classes))
203
+ plt.xticks(tick_marks, classes, rotation=45)
204
+ plt.yticks(tick_marks, classes)
205
+
206
+ if normalize:
207
+ cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
208
+
209
+ thresh = cm.max() / 2.
210
+ for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
211
+ plt.text(j, i, cm[i, j],
212
+ horizontalalignment="center",
213
+ color="white" if cm[i, j] > thresh else "black")
214
+
215
+ plt.tight_layout()
216
+ plt.ylabel('True label')
217
+ plt.xlabel('Predicted label')
218
+
219
+
220
+ # Predict the values from the validation dataset
221
+ Y_pred = model.predict(X_val)
222
+ # Convert predictions classes to one hot vectors
223
+ Y_pred_classes = np.argmax(Y_pred,axis = 1)
224
+ # Convert validation observations to one hot vectors
225
+ Y_true = np.argmax(Y_val,axis = 1)
226
+
227
+ # compute the confusion matrix
228
+ confusion_mtx = confusion_matrix(Y_true, Y_pred_classes)
229
+ plt.xlabel('Predicted')
230
+ plt.ylabel('True')
231
+ plt.title('Confusion Matrix')
232
+ sns.heatmap(confusion_mtx/np.sum(confusion_mtx), annot=True,
233
+ fmt='.2%', cmap='Blues')
234
+
235
+ from sklearn.metrics import classification_report
236
+
237
+ print(classification_report(Y_true, Y_pred_classes))
238
+
239
+ #saving the trained cnn model
240
+ model.save("fake-image-detection.h5")
241
+
242
+
243
+ #!pip install gradio
244
+
245
+ import gradio as gr
246
+ import numpy as np
247
+ from PIL import Image, ImageChops, ImageEnhance
248
+ from keras.models import load_model
249
+ import tensorflow as tf
250
+
251
+ # Load the trained model
252
+ model = load_model("fake-image-detection.h5")
253
+
254
+ # Function to convert an image to its ELA form
255
+ def convert_to_ela_image(image, quality=90):
256
+ resaved_image = image.convert('RGB')
257
+ resaved_image.save("resaved_image.jpg", 'JPEG', quality=quality)
258
+ resaved_image = Image.open("resaved_image.jpg")
259
+
260
+ ela_image = ImageChops.difference(image, resaved_image)
261
+
262
+ extrema = ela_image.getextrema()
263
+ max_diff = max([ex[1] for ex in extrema])
264
+ if max_diff == 0:
265
+ max_diff = 1
266
+ scale = 255.0 / max_diff
267
+
268
+ ela_image = ImageEnhance.Brightness(ela_image).enhance(scale)
269
+ return ela_image
270
+
271
+ # Prediction function
272
+ def predict(image):
273
+ # Convert the input image to an ELA image
274
+ ela_image = convert_to_ela_image(image)
275
+ ela_image = ela_image.resize((128, 128)) # Resize to match the input size of the model
276
+ ela_array = np.array(ela_image).astype('float32') / 255.0
277
+ ela_array = ela_array.reshape(1, 128, 128, 3) # Reshape for model input
278
+
279
+ # Make a prediction
280
+ prediction = model.predict(ela_array)
281
+ class_idx = np.argmax(prediction, axis=1)[0]
282
+
283
+ # Map the prediction to labels
284
+ labels = {0: "Fake", 1: "Real"}
285
+ return labels[class_idx]
286
+
287
+ # Gradio interface
288
+ interface = gr.Interface(
289
+ fn=predict, # Prediction function
290
+ inputs=gr.Image(type="pil"), # Image input (PIL format)
291
+ outputs="label", # Output a label
292
+ title="Deep Fake Detector",
293
+ description="Upload an image to detect if it's a real or fake image using ELA and a trained CNN model."
294
+ )
295
+
296
+ # Launch the interface
297
+ interface.launch()