forked from Sanketdhami/Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_basic.py
More file actions
284 lines (240 loc) · 8.71 KB
/
app_basic.py
File metadata and controls
284 lines (240 loc) · 8.71 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#import MySQLdb as mdb
import pymongo
import sys
import os
import pprint
import unicodedata
from pymongo import MongoClient
from flask import jsonify
from flask import Flask, render_template, request,redirect, url_for ,session,escape,json
import cv2, os
import numpy as np
from PIL import Image
import algorithm
from algorithm import predict_confidence
# Connection with MongoDB database :
client = MongoClient('mongodb://akhilesh_123:cmpe273@ds123361.mlab.com:23361/cmpe273')
db = client['cmpe273']
collection = db['photorecog']
filename = '';
originalImage = ''
tobeCompared = ''
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
print "app path : " + APP_ROOT
app = Flask(__name__,static_url_path='/static', template_folder="templates")
#secret key
app.secret_key = 'dsjksdh88989djj'
@app.after_request
def add_header(r):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
@app.route("/",methods=["GET"])
def defaultPage():
print "Arrived in index - ROOT"
return render_template("Index.html")
@app.route("/home",methods=["GET"])
def homePage():
print "Arrived in index - home"
return render_template("Home.html")
@app.route("/compare",methods=["GET"])
def comparePage():
print "Arrived in index - Compare"
if 'userId' in session:
print session['userId']
print "found"
return render_template("compare.html")
else:
print "not found"
return redirect(url_for('loginPage'))
@app.route("/login",methods=["GET"])
def loginPage():
print "Arrived in index - Login"
return render_template("login.html")
@app.route("/signUp",methods=["GET"])
def signUpPage():
print "Arrived in index - signUp"
return render_template("signUp.html")
@app.route("/complete",methods=["GET"])
def completePage():
print "Arrived in index - complete"
if 'userId' in session:
print session['userId']
return render_template("complete.html")
else:
print 'user is not logged in'
return redirect(url_for('loginPage'))
@app.route("/upload",methods=["GET"])
def uploadPage():
print "Arrived in index - upload"
respons = {'status':'','msg':''}
if 'userId' in session:
print session['userId']
return render_template("upload.html")
else:
print 'user is not logged in'
return redirect(url_for('loginPage'))
response['status'] = '400'
@app.route("/logout",methods=["GET"])
def logout():
print "Arrived in index - Logout"
response = {'status':'','msg':{}}
if 'userId' in session:
print session['userId']
# remove the userId from the session if it is there
print "clearing sesssion"
session.clear()
print "session cleared"
response['status'] = "200"
response['msg'] ='user is not logged in error'
return jsonify(response)
else:
response['status'] = 404;
response['msg'] ='user is not logged in error'
return jsonify(response)
@app.route("/login",methods=["POST"])
def index():
studentId = request.get_json()
print studentId
result = db.photorecog.find({"_id" : studentId});
response = {'status' : '','msg' : {}}
if result.count() == 0:
print "NO record found"
response['status'] = "404"
response['msg'] = "student Id not found"
print response
else:
print "found user...."
response['status'] = "200"
response['msg'] = result.next()
session['userId']= studentId
print studentId;
print session['userId']
print response
return jsonify(response)
@app.route("/signUp",methods=["POST"])
def signUp():
message = request.data
dataDict = json.loads(message)
print dataDict['studentId']
print dataDict['firstName']
print dataDict['lastName']
studentId = dataDict['studentId']
result = db.photorecog.find({"_id" : studentId});
response = {'status' : '','msg' : {}}
if result.count() != 0:
print "User Already exists"
response['status'] = "400"
response['msg'] = "User Already exists"
print response
else:
result = db.photorecog.insert_one({
"_id" : dataDict['studentId'],
"firstName" : dataDict['firstName'],
"lastName" : dataDict['lastName'],
"path" : {
"originalImage" : "",
"tobeComparedImage" : ""
}
})
print result
response['status'] = "200"
response['msg'] = "successfully registered"
session['userId']= dataDict['studentId']
print session['userId']
return jsonify(response)
@app.route("/uploadfile/<studentId>", methods=['POST'])
def upload(studentId):
print "in Upload file"
print request.files;
target = os.path.join(APP_ROOT, 'static')
if not os.path.isdir(target):
os.mkdir(target)
target = os.path.join(APP_ROOT, 'static/images')
if not os.path.isdir(target):
os.mkdir(target)
target = os.path.join(APP_ROOT, 'static/images/' + studentId)
print(target)
if not os.path.isdir(target):
os.mkdir(target)
for file in request.files.getlist("file"):
# print "filename : "
print(file)
filename = "subject01"
# print type(filename)
destination = "/".join([target, filename])
file.save(destination)
# print destination
destination = unicodedata.normalize('NFKD', destination).encode('ascii','ignore')
result = db.photorecog.update_one({"_id" : studentId},{"$set" : {"path.originalImage" : destination}})
# print result
return render_template("complete.html")
@app.route("/index1/<studentId>")
def index1(studentId):
return render_template("upload.html",studentId=studentId)
# The file location will stored across the Student Id, so while fetching the file from database, he needs to query wrt to StudentId
# and get the image and compare with the new image.
#
#
@app.route("/compare/<studentId>", methods=['POST'])
def compare(studentId):
print "in Compare file"
print request.files;
target = os.path.join(APP_ROOT, 'static')
if not os.path.isdir(target):
os.mkdir(target)
target = os.path.join(APP_ROOT, 'static/images')
if not os.path.isdir(target):
os.mkdir(target)
target = os.path.join(APP_ROOT, 'static/images/' + studentId)
print(target)
if not os.path.isdir(target):
os.mkdir(target)
for file in request.files.getlist("file"):
# print "filename : "
print(file)
filename = "subject02"
# print type(filename)
destination = "/".join([target, filename])
file.save(destination)
# print destination
destination = unicodedata.normalize('NFKD', destination).encode('ascii','ignore')
result = db.photorecog.update_one({"_id" : studentId},{"$set" : {"path.tobeComparedImage" : destination}})
# print result
result = db.photorecog.find({"_id" : studentId},{"path" : "1"});
print "result is : "
for r in result:
originalImagePath = r["path"]["originalImage"]
tobeComparedImagePath = r["path"]["tobeComparedImage"]
confidences = predict_confidence(originalImagePath,tobeComparedImagePath,studentId)
originalImagePathToSend = 'http://ec2-34-208-241-189.us-west-2.compute.amazonaws.com:5000/static' + originalImagePath.split('static')[1]
tobeComparedImagePathToSend = 'http://ec2-34-208-241-189.us-west-2.compute.amazonaws.com:5000/static' + tobeComparedImagePath.split('static')[1]
if not confidences:
print "len is zero"
response = {'status' : '','msg' : {}}
response['status'] = "400"
return jsonify(response)
else:
confidenceToSend = round(min(confidences)/150,2)
print "Done"
print "found confidence " + str(confidenceToSend)
msg = {'originalImagePathToSend' : '','tobeComparedImagePathToSend' : '','confidenceToSend' : ''}
msg['originalImagePathToSend'] = originalImagePathToSend
msg['tobeComparedImagePathToSend'] = tobeComparedImagePathToSend
msg['confidenceToSend'] = confidenceToSend
response = {'status' : '','msg' : {}}
response['status'] = "200"
response['msg']= msg
return jsonify(response)
if __name__ == "__main__":
# print "in main : creating connection"
# curr = collection.find();
# for document in curr:
# print(document);
app.run(port=5000, debug=True,host='0.0.0.0')