-
Notifications
You must be signed in to change notification settings - Fork 0
/
anmol_work.py
178 lines (133 loc) · 5.52 KB
/
anmol_work.py
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
from flask import Flask,render_template,request,redirect
from werkzeug.utils import secure_filename
import cv2
import numpy as np
import dlib
import tensorflow as tf
import os
import time
import datetime
from imutils import face_utils
import argparse
FaceFinderModelPath = 'Models/haarcascade_frontalface_default.xml'
FaceAlignmentModelPath = 'Models/shape_predictor_68_face_landmarks.dat'
apps = Flask(__name__)
@apps.route('/')
def homepage():
return render_template('homepage.html')
@apps.route('/uploader',methods=['GET','POST'])
def uploader():
f = request.files['file']
f.save('static'+'/'+secure_filename(f.filename))
result = face_finder('static/'+str(f.filename))
#name_of_file = f.filename
return render_template('face.html',result=result)
def find_faces_and_rects(path):
#This Function will return the path to the image with or without boxes and array of rectangles(empty list for no face)
if not(os.path.exists('picrectangles')):
os.makedir('picrectangles')
if not(os.path.exists('Faces Detected')):
os.makedir('Faces Detected')
face_cascade = cv2.CascadeClassifier(FaceFinderModelPath) # Trained Model for facial classification
sub_count = 0
image = cv2.imread(path)
try:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #Changing to grayscale
except:
gray = image
pass
faces = face_cascade.detectMultiScale(gray, 1.1, 5) #Detecting faces
# Image with face rectangles
for (x,y,w,h) in faces:
cv2.rectangle(image,(x,y),(x+w,y+h),(255,255,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = image[y:y+h, x:x+w]
cv2.imwrite('Faces Detected/'+str(sub_count+1)+'.jpg',image)
cv2.imwrite('static/'+str(sub_count+1)+'.jpg',image)
sub_count+=1
if sub_count==0:
cv2.imwrite('Faces Detected/'+str(sub_count+1)+'.jpg',image)
Rectangle_Image_Data = []
for (x,y,w,h) in faces:
sub_count+=1
cv2.imwrite('picrectangles/'+str(sub_count)+".jpg",image[y:y+h,x:x+h,:])
Rectangle_Image_Data.append(image[y:y+h,x:x+h,:])
return 'Faces Detected/', 'picrectangles/'
def align_face(images_dir):
# Takes image_dir containing rectangular face images as input
# Returns aligned images path
if not(os.path.exists('aligned picrectangles')):
os.makedir('aligned picrectangles')
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
# the facial landmark predictor
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('Models/shape_predictor_68_face_landmarks.dat')
fa = face_utils.FaceAligner(predictor, desiredFaceWidth=100)
source_path = images_dir
destination_path = 'aligned picrectangles/'
Records = []
count=0
for img in os.listdir(source_path):
image_path = os.path.join(source_path, img)
image = cv2.imread(image_path)
image = cv2.pyrUp(image)
image = cv2.resize(image, (250,250))
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
rects = detector(gray,0)
if len(rects) == 0:
Records.append(img)
for rect in rects:
count+=1
faceAligned = fa.align(image, gray, rect)
cv2.imwrite(os.path.join(destination_path,str(count)+".jpg"), faceAligned)
for i in Records:
count+=1
image = cv2.imread(os.path.join(source_path,i))
image = cv2.resize(image,(100,100))
cv2.imwrite(os.path.join(destination_path,str(count)+".jpg"),image)
return destination_path
CNNModelChkptPath = 'Models/runs/Models/checkpoints/'
CNNModel = 'model-180.meta'
def cnn_model_results(path):
X = []
for img in os.listdir(path):
new_data = cv2.imread(os.path.join(path,img))
new_data = cv2.resize(new_data, (100,100))
gray_scale = cv2.cvtColor(new_data, cv2.COLOR_BGR2GRAY)
gray_scale = gray_scale.reshape((100,100,1))
X.append(gray_scale)
X = np.array(X,dtype=np.float32)
checkpoint_file = tf.train.latest_checkpoint(CNNModelChkptPath)
graph=tf.Graph()
with graph.as_default():
print(checkpoint_file)
session_conf = tf.ConfigProto(log_device_placement =False)
sess = tf.Session(config = session_conf)
with sess.as_default():
saver = tf.train.import_meta_graph(os.path.join(CNNModelChkptPath,CNNModel))
saver.restore(sess,checkpoint_file)
input = graph.get_operation_by_name("input_x").outputs[0]
prediction=graph.get_operation_by_name("output/predictions").outputs[0]
probabilities = graph.get_operation_by_name("output/softmax_outputs").outputs
newdata=X
my_probabilities = sess.run(probabilities,feed_dict={input:newdata})
my_predictions = sess.run(prediction, feed_dict={input:newdata})
return my_probabilities, my_predictions
def face_finder(path):
Narendra_Modi ="No"
Arvind_Kejriwal = "No"
Faces = "No"
#results path of processed image
main_image_path, rects_path = find_faces_and_rects(path)
aligned_image_path = align_face(rects_path)
probabilities, predictions = cnn_model_results(aligned_image_path)
if len(os.listdir(rects_path)) >0:
Faces = "Yes"
if 0 in predictions:
Arvind_Kejriwal = "Yes"
if 1 in predictions:
Narendra_Modi = "Yes"
filename = os.listdir(main_image_path)
return {"name":'1.jpg',"Namo":Narendra_Modi,"Arke":Arvind_Kejriwal,"Face":Faces}
apps.run(debug=True)