-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
190 lines (140 loc) · 6.12 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
184
185
186
187
188
189
190
from flask import Flask, request, render_template, redirect, url_for
from flask_ngrok import run_with_ngrok
from werkzeug.utils import secure_filename
import argparse
import cv2
import os
import hashlib
import numpy as np
from src import ImageDeepHash
from src import ImageDeepCompare
app = Flask(__name__, template_folder='templates', static_folder='static')
parser = argparse.ArgumentParser('Image Deep Hash')
parser.add_argument('--ngrok', action='store_true',
default=False, help="Run on local or ngrok")
parser.add_argument('--host', type=str,
default='localhost:4000', help="Local IP")
parser.add_argument('--debug', action='store_true',
default=False, help="Run app in debug mode")
UPLOAD_FOLDER = './static/assets/uploads'
BLEND_FOLDER = './static/assets/blends'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['BLEND_FOLDER'] = BLEND_FOLDER
M_HASH = None
M_COMPARE = None
HASH_MODEL_TYPES = None
HASH_HEX_LEN = None
COMPARE_MODEL_TYPES = None
@app.route('/')
def hash_page():
return render_template("hash.html")
@app.route('/compare')
def compare_page():
return render_template("compare.html")
@app.route('/analyze', methods=['POST', 'GET'])
def analyze():
if request.method == 'POST':
# Get input in form
model_types = request.form.get('model-types')
model_types = str(model_types)
print("model: ", model_types)
if 'compare-button' in request.form:
global M_COMPARE, COMPARE_MODEL_TYPES
COMPARE_MODEL_TYPES = model_types
f = request.files['file']
f2 = request.files['file2']
ori_file_name = secure_filename(f.filename)
_, ext = os.path.splitext(ori_file_name)
ori_file_name2 = secure_filename(f2.filename)
_, ext2 = os.path.splitext(ori_file_name2)
# Get cache name by hashing image
data = f.read()
print('ext: ', ext)
filename = hashlib.sha256(data).hexdigest() + f'{ext}'
data2 = f2.read()
filename2 = hashlib.sha256(data2).hexdigest() + f'{ext2}'
# save file to /static/uploads
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
np_img = np.fromstring(data, np.uint8)
img = cv2.imdecode(np_img, cv2.IMREAD_COLOR)
filepath2 = os.path.join(app.config['UPLOAD_FOLDER'], filename2)
np_img2 = np.fromstring(data2, np.uint8)
img2 = cv2.imdecode(np_img2, cv2.IMREAD_COLOR)
cv2.imwrite(filepath, img)
cv2.imwrite(filepath2, img2)
# Resize for blending 2 images
img = cv2.resize(img, (480, 600))
img2 = cv2.resize(img2, (480, 600))
filename_blend = hashlib.sha256(data + data2).hexdigest() + '.jpg'
filepath_blend = os.path.join(
app.config['BLEND_FOLDER'], filename_blend)
# Blending
img_blend = cv2.addWeighted(img, 0.5, img2, 0.5, 0)
cv2.imwrite(filepath_blend, img_blend)
# Comparing
if M_COMPARE is None or model_types != COMPARE_MODEL_TYPES:
M_COMPARE = ImageDeepCompare.ImageDeepCompare(
weight=COMPARE_MODEL_TYPES)
metric_types = request.form.get('metric-types')
metric_types = str(metric_types)
print("metric: ", metric_types)
# Evaluating
compare_result = str(M_COMPARE.compare(
filepath, filepath2, metric_types))
filename = os.path.basename(filename)
filename2 = os.path.basename(filename2)
filename_blend = os.path.basename(filename_blend)
return render_template('analyze-compare.html', metric=metric_types, result=compare_result, fname=filename, fname2=filename2, fname_blend=filename_blend)
if 'hash-button' in request.form:
global M_HASH, HASH_MODEL_TYPES, HASH_HEX_LEN
HASH_MODEL_TYPES = model_types
f = request.files['file']
ori_file_name = secure_filename(f.filename)
_, ext = os.path.splitext(ori_file_name)
# Get cache name by hashing image
data = f.read()
filename = hashlib.sha256(data).hexdigest() + f'{ext}'
# save file to /static/uploads
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
np_img = np.fromstring(data, np.uint8)
img = cv2.imdecode(np_img, cv2.IMREAD_COLOR)
cv2.imwrite(filepath, img)
# Hashing
hex_len = request.form.get('length-range')
hex_len = int(hex_len)
HASH_HEX_LEN = hex_len
print("hex len: ", hex_len)
if M_HASH is None or (hex_len != HASH_HEX_LEN and model_types != HASH_MODEL_TYPES):
M_HASH = ImageDeepHash.ImageDeepHash(
weight=HASH_MODEL_TYPES, hex_len=HASH_HEX_LEN)
M_HASH.reset()
M_HASH.hash(filepath)
hash_seq = M_HASH.hexdigest()
filename = os.path.basename(filename)
return render_template('analyze-hash.html', hash_seq=hash_seq, fname=filename)
return redirect('/')
@app.after_request
def add_header(response):
if 'Cache-Control' not in response.headers:
response.headers['Cache-Control'] = 'public, no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '-1'
return response
if __name__ == '__main__':
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
if not os.path.exists(BLEND_FOLDER):
os.makedirs(BLEND_FOLDER, exist_ok=True)
args = parser.parse_args()
if args.ngrok:
run_with_ngrok(app)
app.run()
else:
hostname = str.split(args.host, ':')
if len(hostname) == 1:
port = 4000
else:
port = hostname[1]
host = hostname[0]
app.run(host=host, port=port, debug=args.debug, use_reloader=False)
# python app.py --host localhost:8000