-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (52 loc) · 2.09 KB
/
main.py
File metadata and controls
74 lines (52 loc) · 2.09 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
#Driver program for the image classification on the server
import os
import numpy as np
import cv2
from tensorflow import keras
from preprocessing import preprocess
fldr = 'uploads/'
dest_fldr = 'preprocessedUploads/'
wrong_dest_fldr = 'uploadsWithoutFace/'
Model_g = keras.models.load_model('./model/g_final.h5')
Model_a = keras.models.load_model('./model/a_final.h5')
preprocessing_methods = ['he']
img_shape = (96, 96, 1)
# Detects faces and preprocesses the images
preprocess(fldr, dest_fldr, wrong_dest_fldr, img_shape, preprocessing_methods)
# Creating array with names of the images where faces couldn't be found.
filenames_no_face = []
for filename in os.listdir(wrong_dest_fldr):
filenameWithoutSpaces = filename.replace(" ", "")
filenames_no_face.append(filenameWithoutSpaces)
# Creating list with the preprocessed pictures and list with the corresponding filenames
pictures = []
filenames = []
for filename in os.listdir(dest_fldr):
img = cv2.imread(dest_fldr+'/' + filename, 0)
pictures.append(img)
filenameWithoutSpaces = filename.replace(" ", "")
filenames.append(filenameWithoutSpaces)
pictures_f = np.array(pictures)
pictures_f_2 = pictures_f/255
# Predicts for age and gender
pred_gender = Model_g.predict(pictures_f_2)
pred_age = Model_a.predict(pictures_f_2)
res = ""
for i in range(len(pred_gender)):
sex_f = ['Male','Female']
age = int(pred_age[i])
sex = int(np.argmax(pred_gender[i]))
final_prediction = [str(age),sex_f[sex]]
res = res + filenames[i] + " " + final_prediction[0] + " " + final_prediction[1] + " "
# Creating the string to return
res = res + '*' + " "
for i in range(len(filenames_no_face)):
res = res + filenames_no_face[i] + " "
print(res)
#Removing images
for file in os.listdir('uploads'):
os.remove(os.path.join('uploads', file))
for file in os.listdir('preprocessedUploads'):
os.remove(os.path.join('preprocessedUploads', file))
for file in os.listdir('uploadsWithoutFace'):
os.remove(os.path.join('uploadsWithoutFace', file))