diff options
Diffstat (limited to 'megapixels/app/server/api_task.py')
| -rw-r--r-- | megapixels/app/server/api_task.py | 48 |
1 files changed, 19 insertions, 29 deletions
diff --git a/megapixels/app/server/api_task.py b/megapixels/app/server/api_task.py index 23e11454..fb24c154 100644 --- a/megapixels/app/server/api_task.py +++ b/megapixels/app/server/api_task.py @@ -3,6 +3,7 @@ import re import uuid import time import dlib +import tempfile import simplejson as json import numpy as np from flask import Blueprint, request, jsonify @@ -64,62 +65,51 @@ def sleep_test(): Test the Celery system using a task that sleeps. """ async_task = task_lookup['sleep']['task'].apply_async(args=['sleep_test']) - task_url = '/task/{}/{}'.format('sleep', async_task.id) + task_url = '/task/status/{}/{}'.format('sleep', async_task.id) return jsonify({ 'result': True, 'task_url': task_url, }) -@api_task.route('/upload/:style', methods=['POST']) -def upload(style): +@api_task.route('/upload/blur', methods=['POST']) +def upload(): """ Process a images in a particular style """ + style = 'blur' print('style: {}'.format(style)) if style in task_lookup: task = task_lookup[style]['task'] - print('task',task) + print('task', task) else: return jsonify({ 'result': False, 'error': 'Unknown task', }) - file = request.files['user_image'] - ext = request.form['ext'] - if ext is None: - ext = request.files['ext'] + print('get file...') + file = request.files['query_img'] - uuid_name = str(uuid.uuid4()) + uuid_str = str(uuid.uuid4()) - app.logger.info('[+] style: {}'.format(style)) - app.logger.info('[+] ext: {}'.format(ext)) - app.logger.info('[+] uuid_name: {}'.format(uuid_name)) - - # convert PNG to JPG - print('[+] Resizing image') + print('[+] style: {}'.format(style)) + print('[+] uuid_name: {}'.format(uuid_str)) im = Image.open(file.stream).convert('RGB') - im = ImageOps.fit(im, (256, 256)) - - # # Save image to disk - # print('[+] Save image to {}'.format(fpath)) - # im.save(fpath, 'JPEG', quality=100) - # im_pil_256 = im.resize((256,256)) + im = ImageOps.fit(im, (256, 256,)) - # print('[+] ensure_np...') - # im_np = imx.ensure_np(im_pil_256) + tmpfile = tempfile.NamedTemporaryFile(delete=False) - celery_result = { - im: im, - } + # Save image to disk + print('[+] Save image to temporary file') + im.save(tmpfile, 'JPEG', quality=80) print('[+] Start celery') - async_task = task.apply_async(args=[uuid_name, celery_result]) - task_url = '/task/{}/{}'.format(style, async_task.id) + async_task = task.apply_async(args=[uuid_str, tmpfile.name]) + task_url = '/task/status/{}/{}'.format(style, async_task.id) return jsonify({ 'result': True, 'taskURL': task_url, - 'uuid': uuid_name + 'uuid': uuid_str }) |
