Newer
Older
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from flask import Blueprint
from flask import current_app, abort, send_file, jsonify
import base64
from cStringIO import StringIO
from uuid import uuid4
from PIL import Image
import utils
from utils.decorator import login_required
from utils.images import create_thumbnail
from functions import do_decrypt_dek, do_encrypt_dek
from functions import no_preview_image
from views.tp_template import get_tenprint_template_zones
import config
image_view = Blueprint( "image", __name__, template_folder = "template" )
def get_submission_uuid_for_file( file_uuid ):
"""
Get the related submission uuid for a file uuid.
"""
sql = """
SELECT submissions.uuid
FROM submissions
LEFT JOIN files ON submissions.id = files.folder
WHERE files.uuid = %s
"""
return config.db.query_fetchone( sql, ( file_uuid, ) )[ "uuid" ]
@image_view.route( config.baseurl + "/image/file/<file_id>/preview" )
@login_required
def image_file_serve( file_id ):
"""
Function to get an image from the database and return it as PNG preview image.
"""
current_app.logger.info( "Serve a preview for the file '{}'".format( file_id ) )
try:
submission_id = get_submission_uuid_for_file( file_id )
current_app.logger.debug( "submission id: '{}'".format( submission_id ) )
img, _ = image_serve( "thumbnails", file_id, submission_id )
if img == None:
current_app.logger.debug( "No image in the 'thumnnails' database. Recreating the thumbnail" )
img, _ = image_serve( "files", file_id, submission_id )
if img == None:
return abort( 404 )
current_app.logger.debug( "Image from the 'files' table: {}".format( img ) )
img = create_thumbnail( file_id, img, submission_id )
current_app.logger.debug( "Thumbnail image: {}".format( img ) )
buff = utils.images.pil2buffer( img, "PNG" )
return send_file( buff, mimetype = "image/png" )
except:
current_app.logger.error( "Error while creating the thumbnail. Serving a 'no preview' image" )
return send_file( no_preview_image(), mimetype = "image/png" )
@image_view.route( config.baseurl + "/image/segment/<tenprint_id>/<pc>" )
@login_required
def image_segment_serve( tenprint_id, pc ):
"""
Serve a preview for a segment image.
"""
current_app.logger.info( "Serving a segment image for the tenprint '{}', pc '{}'".format( tenprint_id, pc ) )
try:
submission_id = get_submission_uuid_for_file( tenprint_id )
current_app.logger.debug( "submission id: {}".format( submission_id ) )
img, file_segment_id = image_serve( "files_segments", ( tenprint_id, pc ), submission_id )
img = create_thumbnail( file_segment_id, img, submission_id )
current_app.logger.debug( "image: {}".format( img ) )
buff = utils.images.pil2buffer( img, "PNG" )
return send_file( buff, mimetype = "image/png" )
except:
current_app.logger.error( "Error while creating the thumbnail. Serving a 'no preview' image" )
return send_file( no_preview_image(), mimetype = "image/png" )
@image_view.route( config.baseurl + "/image/template/<tenprint_id>/<side>" )
@image_view.route( config.baseurl + "/image/template/<tenprint_id>/<side>/<action>" )
@login_required
def image_tp_template( tenprint_id, side, action = "full" ):
"""
Serve a template image, full-resolution or preview.
"""
current_app.logger.info( "Serve the tenprint template image for {}, {}".format( tenprint_id, side ) )
if side in [ "front", "back" ]:
img, _ = image_serve( "tenprint_cards", ( tenprint_id, side ), None )
if img == None:
return send_file( no_preview_image(), mimetype = "image/png" )
if action == "preview":
img.thumbnail( ( 500, 500 ) )
current_app.logger.debug( "image: {}".format( img ) )
buff = utils.images.pil2buffer( img, "PNG" )
return send_file( buff, mimetype = "image/png" )
else:
return abort( 403 )
def image_serve( table, image_id, submission_id ):
"""
Backend function to get the image from the database.
"""
current_app.logger.info( "Serve the image '{}' for submission '{}' from table '{}'".format( image_id, submission_id, table ) )
need_to_decrypt = True
if table == "files_segments":
if isinstance( image_id, tuple ):
tp, pc = image_id
sql = "SELECT data, uuid FROM {} WHERE tenprint = %s AND pc = %s".format( table )
p = ( tp, pc, )
else:
sql = "SELECT data, uuid FROM {} WHERE uuid = %s".format( table )
p = ( image_id, )
elif table in [ "files", "thumbnails" ]:
sql = "SELECT data, uuid FROM {} WHERE uuid = %s".format( table )
p = ( image_id, )
elif table == "tenprint_cards":
image_id, t = image_id
sql = "SELECT image_{}, id FROM {} WHERE id = %s".format( t, table )
p = ( image_id, )
need_to_decrypt = False
else:
current_app.logger.error( "table '{}' not authorized".format( table ) )
raise Exception( "table not authorized" )
current_app.logger.debug( "sql: {}".format( sql ) )
current_app.logger.debug( "params: {}".format( p ) )
data = config.db.query_fetchone( sql, p )
if data == None:
return None, None
else:
img, rid = data
if img == None:
return None, None
current_app.logger.debug( "image: {}...".format( img[ 0:20 ] ) )
current_app.logger.debug( "need_to_decrypt: {}".format( need_to_decrypt ) )
if need_to_decrypt:
img = do_decrypt_dek( img, submission_id )
img = str2img( img )
current_app.logger.debug( "image: {}".format( img ) )
return img, rid
def str2img( data ):
"""
Convert a base64 string image to a PIL image.
"""
current_app.logger.info( "Convert string image to PIL format" )
if data == None:
return None
else:
img = base64.b64decode( data )
buff = StringIO()
buff.write( img )
buff.seek( 0 )
img = Image.open( buff )
current_app.logger.debug( "string: {}".format( data[ 0:20 ] ) )
current_app.logger.debug( "image: {}".format( img ) )
return img
@image_view.route( config.baseurl + "/image/file/<image_id>/info" )
@login_required
def img_info( image_id ):
"""
Get and return the metadata for a particular image.
See do_img_info() for more informations.
"""
current_app.logger.info( "Serve image informations for image '{}'".format( image_id ) )
d = do_img_info( image_id )
if d != None:
return jsonify( d )
else:
return abort( 404 )
def do_img_info( image_id ):
"""
Retrieve the metadata for a particular image from the database.
"""
current_app.logger.debug( "Get image information from database for '{}'".format( image_id ) )
sql = "SELECT size, width, height, resolution, format FROM files WHERE uuid = %s"
d = config.db.query_fetchone( sql, ( image_id, ) )
for key, value in d.iteritems():
current_app.logger.debug( "{}: {}".format( key, value ) )
if d != None:
return dict( d )
else:
return None
@image_view.route( config.baseurl + "/image/segment/<tenprint_id>/start" )
@login_required
def image_tenprint_segmentation( tenprint_id ):
"""
Route to start the segmentation of a tenprint image into segments (fingers or palm images).
"""
current_app.logger.info( "Start segmentations for '{}'".format( tenprint_id ) )
ret = do_image_tenprint_segmentation( tenprint_id )
return jsonify( {
"error": False,
"data": ret
} )
def do_image_tenprint_segmentation( tenprint_id ):
"""
Backend function to create all the segments images for a tenprint souce image.
"""
sql = "SELECT size, resolution, type, format, data FROM files WHERE uuid = %s"
img = config.db.query_fetchone( sql, ( tenprint_id, ) )
for key, value in img.iteritems():
if isinstance( value, str ) and len( value ) > 20:
value = "{}...".format( value[ 0:20 ] )
current_app.logger.debug( "{}: {}".format( key, value ) )
res = img[ "resolution" ]
img_format = img[ "format" ]
side = {
1: "front",
2: "back"
}[ img[ "type" ] ]
current_app.logger.debug( "side: {}".format( side ) )
submission_id = get_submission_uuid_for_file( tenprint_id )
current_app.logger.debug( "Decrypt data with DEK" )
img = do_decrypt_dek( img[ "data" ], submission_id )
img = base64.b64decode( img )
buff = StringIO()
buff.write( img )
buff.seek( 0 )
img = Image.open( buff )
current_app.logger.debug( "image: {}".format( img ) )
sql = "SELECT template FROM file_template WHERE file = %s"
template_id = config.db.query_fetchone( sql, ( tenprint_id, ) )[ "template" ]
zones = get_tenprint_template_zones( template_id, side )
current_app.logger.debug( "Use '{}' as tenprint template".format( template_id ) )
for z in zones:
current_app.logger.debug( "Segmenting fpc '{}' ({})".format( z[ "pc" ], z[ "pc_name" ] ) )
tl_x, tl_y, br_x, br_y = map( lambda v: v * res / 2.54 , [ z[ "tl_x" ], z[ "tl_y" ], z[ "br_x" ], z[ "br_y" ] ] )
tmp = img.crop( ( tl_x, tl_y, br_x, br_y ) )
buff = StringIO()
tmp.save( buff, format = img_format )
buff.seek( 0 )
current_app.logger.debug( "Encrypting segment image with DEK" )
file_data = buff.getvalue()
file_data = base64.b64encode( file_data )
file_data = do_encrypt_dek( file_data, submission_id )
sql = "SELECT id FROM files_segments WHERE tenprint = %s AND pc = %s"
q = config.db.query_fetchone( sql, ( tenprint_id, z[ "pc" ], ) )
if q == None:
current_app.logger.debug( "Inserting to the database" )
sql = utils.sql.sql_insert_generate( "files_segments", [ "tenprint", "uuid", "pc", "data" ] )
data = ( tenprint_id, str( uuid4() ), z[ "pc" ], file_data )
config.db.query( sql, data )
else:
current_app.logger.debug( "Updating the database" )
sql = "UPDATE files_segments SET data = %s WHERE tenprint = %s AND pc = %s"
data = ( file_data, tenprint_id, z[ "pc" ] )
config.db.query( sql, data )
config.db.commit()
return True