-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageclassifier.py
More file actions
271 lines (215 loc) · 8.4 KB
/
imageclassifier.py
File metadata and controls
271 lines (215 loc) · 8.4 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# -*- coding: utf-8 -*-
"""ImageClassifier.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1ng9IvTmcg30h9myEvCcCiyoCaH3UfFGK
"""
from google.colab import drive
drive.mount('/content/gdrive/')
from google.colab import files
files.upload()
!pip install -q kaggle
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/
!ls ~/.kaggle
!chmod 600 /root/.kaggle/kaggle.json
!kaggle datasets download -d ciplab/real-and-fake-face-detection --force
!unzip \*.zip && rm *.zip
# Commented out IPython magic to ensure Python compatibility.
# %mv real_and_fake_face/ input_data
# %ls
import pathlib
data_dir = pathlib.Path('/content/input_data/')
training_fake = pathlib.Path('/content/input_data/training_fake')
training_real = pathlib.Path('/content/input_data/training_real')
!pip install mtcnn
from matplotlib import pyplot
from matplotlib.patches import Rectangle
from mtcnn.mtcnn import MTCNN
from PIL import Image
filename = '/content/input_data/training_fake/mid_137_1011.jpg'
def draw_image_with_boxes(filename, result_list):
# load the image
data = pyplot.imread(filename)
# plot the image
pyplot.imshow(data)
# get the context for drawing boxes
ax = pyplot.gca()
# plot each box
for result in result_list:
# get coordinates
x, y, width, height = result['box']
# create the shape
rect = Rectangle((x, y), width, height, fill=False, color='red')
# draw the box
ax.add_patch(rect)
# show the plot
pyplot.show()
return(x,y,x+width,y+height)
detector = MTCNN()
# detect faces in the image
pixels = pyplot.imread(filename)
print(pixels)
faces = detector.detect_faces(pixels)
# display faces on the original image
draw_image_with_boxes(filename, faces)
# Commented out IPython magic to ensure Python compatibility.
# %mkdir data
# %cd data/
# %mkdir fake
# %mkdir real
# Commented out IPython magic to ensure Python compatibility.
# %cd ..
from matplotlib import pyplot
from matplotlib.patches import Rectangle
from mtcnn.mtcnn import MTCNN
from PIL import Image
import os
## change the folder name accordingly for training and testing
path = '/content/input_data/'
folders = os.listdir(path)
print(folders)
#folders = folders[1:] ## [1:] to remove .ds_store folder if it is made automatically otherwise just use folder
## Iterate over the folder and detect and crop faces and save them in respective folder
for subs in folders:
for files in os.listdir(path+subs):
try:
if 'fake' in path+subs+files and 'jpg' in path+subs+files:
print(path+subs+'/'+files)
pixels = pyplot.imread(path+subs+'/'+files)
faces = detector.detect_faces(pixels)
x, y, width, height = faces[0]['box']
#coordinates = tuple(faces[0]['box'])
Image.fromarray(pixels).crop((x, y, x + width, y + height)).save('/content/data/fake/'+files)
elif 'real' in path+subs+files and 'jpg' in path+subs+files:
print(path+subs+'/'+files)
pixels = pyplot.imread(path+subs+'/'+files)
faces = detector.detect_faces(pixels)
x, y, width, height = faces[0]['box']
#coordinates = tuple(faces[0]['box'])
Image.fromarray(pixels).crop((x, y, x + width, y + height)).save('/content/data/real/'+files)
except (IndexError or SystemError):
print('Face Not Found')
from keras.models import Sequential
from keras.layers import Conv2D,MaxPool2D,Flatten,Dense,Dropout
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import to_categorical
import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
model = Sequential()
model.add(Conv2D(32,(3,3),input_shape = (200,200,3),activation = 'relu',padding='same',))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.2))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(MaxPool2D((2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
model.add(MaxPool2D((2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer = 'adam',loss = 'binary_crossentropy',metrics = ['accuracy'])
datagen = ImageDataGenerator(rescale=1.0/255.0,
validation_split = 0.2
)
train = datagen.flow_from_directory('/content/data/',
classes={
'fake':1,
'real':0
},
class_mode='binary',
batch_size=64,
target_size=(200,200),
subset = 'training')
val = datagen.flow_from_directory('/content/data/',
classes={
'fake':1,
'real':0
},
class_mode='binary',
batch_size=64,
target_size=(200,200),
subset = 'validation')
print(val)
history = model.fit_generator(train,
validation_data=(val),
epochs = 50,
steps_per_epoch=len(train),
validation_steps=val.samples
)
models_path = '/content/gdrive/MyDrive/uclais/dataset/dataset/data/dataset/dataset/test/models/'
model_json = model.to_json()
with open(models_path+'model.json','w') as json_file:
json_file.write(model_json)
model.save_weights(models_path+'model.h5')
# Commented out IPython magic to ensure Python compatibility.
# %mkdir /content/final_test/
# %ls
# Commented out IPython magic to ensure Python compatibility.
# %cd /content/final_test/
# %rm *.jpg
# %ls
from keras.models import load_model, model_from_json
from PIL import Image
import json
import numpy as np
from keras.models import load_model
from keras.preprocessing import image
import numpy as np
import os
import pandas as pd
import shutil
folder_path = '/content/gdrive/MyDrive/uclais/dataset/dataset/data/dataset/dataset/test/test/'
cropped_faces_test_path = '/content/final_test/'
model_json_path = '/content/gdrive/MyDrive/uclais/dataset/dataset/data/dataset/dataset/test/models/model.json'
model_weights_path = '/content/gdrive/MyDrive/uclais/dataset/dataset/data/dataset/dataset/test/models/model.h5'
json_file = open(model_json_path,'r')
loaded_model = json_file.read()
json_file.close()
model = model_from_json(loaded_model)
model.load_weights(model_weights_path)
img_width, img_height = 200,200
# Load Image
images = []
folder_images = os.listdir(folder_path)
for img in folder_images:
try:
print(img)
pixels = pyplot.imread(folder_path + img)
faces = detector.detect_faces(pixels)
x, y, width, height = faces[0]['box']
Image.fromarray(pixels).crop((x, y, x + width, y + height)).save(cropped_faces_test_path+img)
except(IndexError or SystemError):
print('bagi pula in ' + img)
shutil.move(folder_path + img, cropped_faces_test_path)
cropped_img_list = os.listdir(cropped_faces_test_path)
cropped_img_list.sort(key=lambda file_name: int(file_name.split('.')[0]))
print(cropped_img_list)
for img in cropped_img_list:
img = os.path.join(cropped_faces_test_path, img)
img = image.load_img(img, target_size=(img_width, img_height))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
# im = np.asarray(im)
#img = np.reshape(img,(1,img.shape[0],img.shape[1],img.shape[2]))
images.append(img)
# stack up images list to pass for prediction
images = np.vstack(images)
classes = model.predict_classes(images, batch_size=10)
print(classes)
for i,_ in enumerate(classes):
classes[i] = 1 - classes[i]
print(classes)
result_df = pd.DataFrame()
result_df["filename"] = list(cropped_img_list)
result_df["label"] = classes
result_df.head()
result_df.to_excel(models_path+'subms2.xlsx')