-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
183 lines (146 loc) · 5.9 KB
/
app.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
179
180
181
182
183
from flask import Flask, render_template, request, jsonify, redirect
from flask_uploads import UploadSet, configure_uploads, IMAGES
from PIL import Image, ExifTags, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
import urllib
import requests
import json
from flask_cors import CORS, cross_origin
import os
#import filetype
import pdf2image
import time
import fileinput
import base64
import numpy as np
import cv2
from werkzeug import secure_filename
import string
import re
from pprint import pprint
import sys
import operator
import io
import random
from flask_sslify import SSLify
import cv2
from flask_mysqldb import MySQL
import yaml
import demo
#pages = convert_from_path('bscit.pdf', 500)
#page[0].save('out.jpg', 'JPEG')
#os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/home/ubuntu/.keys/key.json"
def saveImage(imgstring, filename):
#print imgstring
imgdata = base64.b64decode(imgstring.split(",")[1])
#print imgdata
with open(filename, 'wb') as f:
f.write(imgdata)
application = Flask(__name__,static_url_path='/static')
CORS(application, support_credentials=True)
@application.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
return response
db = yaml.load(open('db.yaml'))
application.config['MySQL_HOST'] = db['mysql_host']
application.config['MySQL_USER'] = db['mysql_user']
application.config['MySQL_PASSWORD'] = db['mysql_password']
application.config['MYSQL_DB'] = db['mysql_db']
mysql = MySQL(application)
@application.route('/index', methods=['GET', 'POST'])
def signup():
if request.method == 'POST':
#fetch form data
userDetails = request.form
base64img = userDetails['base64img']
print base64img
filepath = 'static/img/signups/' # I assume you have a way of picking unique filenames
full_filename = filepath+''.join(random.choice(string.ascii_uppercase) for _ in range(5)) + '.jpg'
saveImage(base64img, full_filename)
# cur = mysql.connection.cursor()
# x = cur.execute("SELECT photo FROM users WHERE name =%s", [name])
# if int(x)>0:
# #flash("Username taken")
# print "Username taken"
# return render_template("usernametaken.html")
# else:
# cur.execute("INSERT INTO users(photo) VALUES(%s, %s, %s, %s, %s)",(full_filename))
# mysql.connection.commit()
# cur.close()
return redirect('/users')
return render_template('landing2.html')
@application.route('/login')
def login():
return render_template('login.html')
@application.route('/upload_FID', methods=['GET', 'POST'])
def upload_FID():
if request.method == 'POST':
username = request.form['name']
base64img = request.form['base64img']
cur = mysql.connection.cursor()
filepath = 'static/img/logins/' # I assume you have a way of picking unique filenames
full_filename = filepath+''.join(random.choice(string.ascii_uppercase) for _ in range(5)) + '.jpg'
saveImage(base64img, full_filename)
cur.execute("SELECT photo FROM users WHERE name =%s", [username])
userDetails = cur.fetchone()
try:
oldimg = userDetails[0]
except TypeError:
stmt = 'No such username found'
return json.dumps(stmt)
#oldimg = userDetails[0]
mysql.connection.commit()
cur.close()
verified1 = match.verify(full_filename, oldimg)
#verified = "[" + verified1.replace("}", "},", verified1.count("}")-1) + "]"
json_data = json.loads(verified1)
result = json.dumps({"result": json_data},sort_keys = True, indent = 4, separators = (',', ': '))
#resp = json_data.to_dict()
# print json_data['score']
percent = json_data['score'] *100
if percent>90:
return render_template("index2.html", result = result)
else:
return render_template("matchfailed.html")
@application.route('/viewdatabase', methods=['GET', 'POST'])
def test():
cur = mysql.connection.cursor()
resultValue = cur.execute("SELECT * FROM users")
if resultValue > 0:
userDetails = cur.fetchall()
return render_template('userdatabaselist.html',userDetails = userDetails)
@application.route('/', methods=['GET', 'POST'])
def landing():
return render_template('index.html')
@application.route('/test1', methods=['GET', 'POST'])
def test1():
return render_template('indextest.html')
@application.route('/users')
def users():
return render_template('users.html')
@application.route('/emotion', methods=['GET', 'POST'])
def facemap():
return render_template('clm_emotiondetection.html')
@application.route('/uploader', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
base64img = request.form['base64img']
filepath = 'static/img/signups/' # I assume you have a way of picking unique filenames
full_filename = filepath+''.join(random.choice(string.ascii_uppercase) for _ in range(5)) + '.jpg'
saveImage(base64img, full_filename)
return jsonify(age=demo.getAge(os.getcwd()+'/'+full_filename))
@application.route('/peoplecount', methods=['GET', 'POST'])
def peoplecount():
if request.method == 'POST':
base64img = request.form['base64img']
filepath = 'static/img/signups/' # I assume you have a way of picking unique filenames
full_filename = filepath+''.join(random.choice(string.ascii_uppercase) for _ in range(5)) + '.jpg'
saveImage(base64img, full_filename)
return jsonify(count=demo.getPeopleCount(os.getcwd()+'/'+full_filename))
if __name__ == '__main__':
# context = ('ssl/cert.pem', 'ssl/privkey.pem')
# SSLify(application)
application.run(debug=True, port=8000, host="0.0.0.0")