Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
src/01_genreating_image_pickle.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from utils.all_utils import read_yaml, create_dir
|
2 |
+
import os
|
3 |
+
import pickle
|
4 |
+
from mtcnn import MTCNN
|
5 |
+
from tensorflow.keras.preprocessing import image
|
6 |
+
from tqdm import tqdm
|
7 |
+
import cv2
|
8 |
+
|
9 |
+
|
10 |
+
def extract_face_from_image(image_path, required_size=(224, 224)):
|
11 |
+
img = cv2.imread(image_path)
|
12 |
+
|
13 |
+
detector = MTCNN()
|
14 |
+
faces = detector.detect_faces(img)
|
15 |
+
if len(faces)>0:
|
16 |
+
|
17 |
+
x, y, width, height = faces[0]['box']
|
18 |
+
face_boundary = img[y:y+ height, x:x+width]
|
19 |
+
|
20 |
+
image = cv2.resize(face_boundary, required_size)
|
21 |
+
return image
|
22 |
+
|
23 |
+
|
24 |
+
def generate_image_pickle_file(config_path, params_path):
|
25 |
+
config = read_yaml(config_path)
|
26 |
+
params = read_yaml(params_path)
|
27 |
+
|
28 |
+
artifacts = config['artifacts']
|
29 |
+
artifacts_dir = artifacts['artifacts_dir']
|
30 |
+
pickle_format_dir = artifacts['pickle_format_data_dir']
|
31 |
+
img_pickle_filename = artifacts['img_pickle_file_name']
|
32 |
+
pickle_actors_name = artifacts['pickle_actor_names']
|
33 |
+
cropped_dir = artifacts['cropped_dir']
|
34 |
+
|
35 |
+
raw_local_dir_path = os.path.join(artifacts_dir,pickle_format_dir)
|
36 |
+
|
37 |
+
create_dir([raw_local_dir_path])
|
38 |
+
|
39 |
+
|
40 |
+
pickle_file = os.path.join(raw_local_dir_path, img_pickle_filename)
|
41 |
+
pickle_actor = os.path.join(raw_local_dir_path,pickle_actors_name)
|
42 |
+
|
43 |
+
data = params['base']['data_path']
|
44 |
+
create_dir([os.path.join(data,cropped_dir)])
|
45 |
+
|
46 |
+
actors = os.listdir(data)
|
47 |
+
filenames = []
|
48 |
+
|
49 |
+
for actor in tqdm(actors):
|
50 |
+
count = 0
|
51 |
+
actor_crop_dir = os.path.join(data, cropped_dir, actor)
|
52 |
+
create_dir([actor_crop_dir])
|
53 |
+
|
54 |
+
for file in os.listdir(os.path.join(data, actor)):
|
55 |
+
file_dir = os.path.join(data,actor, file)
|
56 |
+
|
57 |
+
try:
|
58 |
+
detected_face = extract_face_from_image(file_dir)
|
59 |
+
cropped_file_name = actor+ "_" + str(count) + ".jpg"
|
60 |
+
|
61 |
+
cv2.imwrite(os.path.join(actor_crop_dir,cropped_file_name), detected_face)
|
62 |
+
count+=1
|
63 |
+
except:
|
64 |
+
pass
|
65 |
+
|
66 |
+
|
67 |
+
for file in os.listdir(actor_crop_dir):
|
68 |
+
filenames.append(os.path.join(data,cropped_dir,actor,file))
|
69 |
+
|
70 |
+
|
71 |
+
print(f'Total celeb are: {len(actors)}')
|
72 |
+
print(f'Total celeb images: {len(filenames)}')
|
73 |
+
|
74 |
+
pickle.dump(filenames, open(pickle_file, 'wb'))
|
75 |
+
pickle.dump(actors, open(pickle_actor, 'wb'))
|
76 |
+
|
77 |
+
|
78 |
+
if __name__ == '__main__':
|
79 |
+
generate_image_pickle_file('config/config.yaml', 'params.yaml')
|
src/02_feature_extraction.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fileinput import filename
|
2 |
+
from sklearn import preprocessing
|
3 |
+
from utils.all_utils import read_yaml, create_dir
|
4 |
+
import os
|
5 |
+
import pickle
|
6 |
+
from tensorflow.keras.preprocessing import image
|
7 |
+
from keras_vggface.utils import preprocess_input
|
8 |
+
from keras_vggface.vggface import VGGFace
|
9 |
+
import numpy as np
|
10 |
+
from tqdm import tqdm
|
11 |
+
from PIL import Image
|
12 |
+
|
13 |
+
|
14 |
+
def extractor(img_path, model):
|
15 |
+
img = Image.open(img_path)
|
16 |
+
resized_img = img.resize((244, 244), Image.ANTIALIAS)
|
17 |
+
img_array = image.img_to_array(resized_img)
|
18 |
+
|
19 |
+
expanded_img = np.expand_dims(img_array, axis=0)
|
20 |
+
preproecess_img = preprocess_input(expanded_img)
|
21 |
+
|
22 |
+
reselt = model.predict(preproecess_img).flatten()
|
23 |
+
return reselt
|
24 |
+
|
25 |
+
def feature_extraction(config_path, params_path):
|
26 |
+
config = read_yaml(config_path)
|
27 |
+
params = read_yaml(params_path)
|
28 |
+
|
29 |
+
artifacts = config['artifacts']
|
30 |
+
artifacts_dirs = artifacts['artifacts_dir']
|
31 |
+
pickle_format_dirs = artifacts['pickle_format_data_dir']
|
32 |
+
img_pickle_filename = artifacts['img_pickle_file_name']
|
33 |
+
|
34 |
+
|
35 |
+
img_pickle_filename = os.path.join(artifacts_dirs, pickle_format_dirs, img_pickle_filename)
|
36 |
+
filename = pickle.load(open(img_pickle_filename, 'rb'))
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
feature_dir = artifacts['feature_extraction_dir']
|
41 |
+
feature_pickle_filename = artifacts['extracted_features_name']
|
42 |
+
|
43 |
+
create_dir([os.path.join(artifacts_dirs, feature_dir)])
|
44 |
+
|
45 |
+
|
46 |
+
model_name = params['base']['BASE_MODEL']
|
47 |
+
include_top = params['base']['include_top']
|
48 |
+
pooling = params['base']['pooling']
|
49 |
+
|
50 |
+
model = VGGFace(model= model_name, include_top= include_top,input_shape= (244,244,3), pooling = pooling)
|
51 |
+
|
52 |
+
features = []
|
53 |
+
|
54 |
+
for file in tqdm(filename):
|
55 |
+
features.append(extractor(file, model))
|
56 |
+
|
57 |
+
|
58 |
+
feature_path = os.path.join(artifacts_dirs, feature_dir, feature_pickle_filename)
|
59 |
+
pickle.dump(features, open(feature_path, 'wb'))
|
60 |
+
|
61 |
+
|
62 |
+
if __name__ == '__main__':
|
63 |
+
feature_extraction('config/config.yaml', 'params.yaml')
|