summaryrefslogtreecommitdiff
path: root/megapixels/app
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2019-01-13 18:08:49 +0100
committerJules Laplace <julescarbon@gmail.com>2019-01-13 18:08:49 +0100
commit6710b9f7f223acd01ac82171d9f9f4eb577f5885 (patch)
treee8328f43f107e5c5dbef2aeb8b2746239a44508a /megapixels/app
parent47b6ae0f8ad2f49692222bb0c800e7ba1eb4b94b (diff)
serializing image failed, writing to tmp file instead
Diffstat (limited to 'megapixels/app')
-rw-r--r--megapixels/app/server/api_task.py48
-rw-r--r--megapixels/app/server/tasks/blur.py27
2 files changed, 30 insertions, 45 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
})
diff --git a/megapixels/app/server/tasks/blur.py b/megapixels/app/server/tasks/blur.py
index 3b7e20be..d1f67f54 100644
--- a/megapixels/app/server/tasks/blur.py
+++ b/megapixels/app/server/tasks/blur.py
@@ -16,18 +16,13 @@ celery_logger = get_task_logger(__name__)
import imutils
@celery.task(bind=True)
-def blur_task(self, uuid_name, extra):
+def blur_task(self, uuid_name, fn):
"""Process image and update during"""
celery_logger.debug('process_image_task, uuid: {}'.format(uuid_name))
files = []
- im = Image.open(os.path.join(upload_dir, uuid_name + '.jpg')).convert('RGB')
- im = im.resize((256,256))
- files.append({
- 'title': 'Original image',
- 'fn': upload_uri + uuid_name + '.jpg'
- })
+ im = Image.open(fn).convert('RGB')
self.update_state(
state = 'PROCESSING',
@@ -42,13 +37,13 @@ def blur_task(self, uuid_name, extra):
im_blur_pil = ensure_pil(im_blur)
fn = uuid_name + '_blur.jpg'
- fpath = os.path.join(render_dir, fn)
- im_blur_pil.save(fpath, 'JPEG', quality=95)
+ # fpath = os.path.join(render_dir, fn)
+ # im_blur_pil.save(fpath, 'JPEG', quality=95)
- files.append({
- 'title': 'Blurred image',
- 'fn': render_uri + uuid_name + '_blur.jpg'
- })
+ # files.append({
+ # 'title': 'Blurred image',
+ # 'fn': render_uri + uuid_name + '_blur.jpg'
+ # })
time.sleep(3)
@@ -67,9 +62,9 @@ def blur_task(self, uuid_name, extra):
'files': files
}
- json_path = os.path.join(json_dir, uuid_name + '.json')
- with open(json_path, 'w') as json_file:
- json.dump(data, json_file)
+ # json_path = os.path.join(json_dir, uuid_name + '.json')
+ # with open(json_path, 'w') as json_file:
+ # json.dump(data, json_file)
celery_logger.debug('ok')