-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
118 lines (100 loc) · 4.11 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
import os
import json
from flask import Flask, request, redirect,\
url_for, render_template, flash, jsonify, make_response, send_from_directory
from werkzeug.utils import secure_filename
from settings import config
from core import face_recognizer as fr
from core import linear_regressor as lr
from core import thermal_grader as tg
import httplib2
app = Flask(__name__)
app.config.from_object(__name__)
auth_token = config.AUTH_TOKEN
IMAGE_FOLDER = os.path.join(config.BASE_DIR, 'data/image')
TRAIN_FOLDER = os.path.join(config.BASE_DIR, 'data/train')
ALLOWED_EXTENSIONS = set(['jpeg', 'JPEG', 'jpg', 'JPG', 'png', 'PNG', 'bmp','BMP', 'bin', 'txt'])
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route("/")
def home():
return jsonify(result="success")
@app.route("/image/", methods=['GET', 'POST'])
def image():
if request.method == 'GET':
response = make_response(
render_template('upload.html')
)
return response
if request.method == 'POST':
if request.form.get('_auth_token') != auth_token:
response = make_response(
json.dumps("Token is not valid"), 401
)
response.headers['Content-Type'] = 'application/json'
return response
image_file_url = request.form.get('justimage', None)
image_file_path = None
if image_file_url and allowed_file(image_file_url):
h = httplib2.Http('.cache')
response, content = h.request(image_file_url, "GET")
ext = image_file_url.rsplit('.', 1)[1]
if response.status == '200' or '304':
filename="image." + ext
# filename = secure_filename(image_file.filename)
image_file_path = os.path.join(IMAGE_FOLDER, filename)
with open(image_file_path, 'wb') as f:
f.write(content)
#image_file.save(image_file_path)
temp_file_url = request.form.get('temperature', None)
if temp_file_url and allowed_file(temp_file_url):
h = httplib2.Http('.cache')
response, content = h.request(temp_file_url, "GET")
filename = "temp.bin"
temperature_path = os.path.join(TRAIN_FOLDER, filename)
with open(temperature_path, 'wb') as f:
f.write(content)
rate = request.form.get('rate')
if rate:
has_user_rate = True
else:
has_user_rate = False
rate = 2.5
upload_result = {
"result": "success",
"rate": rate,
"image_file": image_file_url if image_file_url else None,
"temperature_file": temp_file_url if temp_file_url else None,
"result": dict()
}
if image_file_url or temp_file_url:
try:
result, res = fr.face_recognizer(image_file_path, int(rate))
if result:
predicted_rate =lr.linear_regressor(res, int(rate))
upload_result['result'] = {"how_other_user_say": predicted_rate}
else:
upload_result['result'] = {"how_other_user_say": "no face found"}
except:
pass
if image_file_path:
thermal_rate = tg.thermal_grader(image_file_path, ext)
upload_result['result']['thermal_rate'] = thermal_rate if thermal_rate else 2.5
upload_result['result']['images'] = {
"orig": '/pic/image.'+ext,
"base": '/pic/base.'+ext,
"point1": '/pic/point1.'+ext,
"point2": '/pic/point2.'+ext
}
print upload_result
else:
upload_result['result']['error'] = "Can not get image from link"
print "Error"
return jsonify(upload_result)
@app.route('/pic/<path:filename>')
def send_pic(filename):
return send_from_directory(IMAGE_FOLDER, filename)
if __name__ == "__main__":
app.debug = True
app.run(host='0.0.0.0', port=8000)