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
|
import os
import sys
import time
import datetime
import json
from PIL import Image
import cv2 as cv
import numpy as np
from app.utils.im_utils import ensure_np, ensure_pil
from flask import current_app as app
import app.settings.app_cfg as cfg
from app.server.tasks import celery
from celery.utils.log import get_task_logger
log = get_task_logger(__name__)
import imutils
@celery.task(bind=True)
def blur_task(self, uuid_name, fn):
"""Process image and update during"""
log.debug('process_image_task, uuid: {}'.format(uuid_name))
log.debug('fn: {}'.format(fn))
files = []
meta = {
'step': 0,
'total': 3,
'message': 'Starting',
'uuid': uuid_name,
'data': {},
}
self.update_state(state='PROCESSING', meta=meta)
im = Image.open(fn).convert('RGB')
os.remove(fn)
meta['step'] += 1
meta['message'] = 'Applying blur'
self.update_state(state='PROCESSING', meta=meta)
im_np = ensure_np(im)
im_blur = cv.blur(im_np, (5,5), 1.0)
im_blur_pil = ensure_pil(im_blur)
fn = uuid_name + '_blur.jpg'
fpath = os.path.join(cfg.DIR_SITE_USER_CONTENT, fn)
im_blur_pil.save(fpath, 'JPEG', quality=80)
log.debug('fpath: {}'.format(fpath))
print('fpath: {}'.format(fpath))
# files.append({
# 'title': 'Blurred image',
# 'fn': render_uri + uuid_name + '_blur.jpg'
# })
meta['step'] += 1
meta['message'] = 'Applying blur'
meta['data']['blur_fn'] = {
'title': 'Blurred image',
'url': os.path.join('/user_content/', fn)
}
self.update_state(state='PROCESSING', meta=meta)
time.sleep(3)
if os.path.exists(fpath):
os.remove(fpath)
meta['step'] += 1
meta['message'] = 'Securely deleting user content'
self.update_state(state='PROCESSING', meta=meta)
time.sleep(2)
log.debug('done!!')
meta['step'] = meta['total']
meta['state'] = 'complete'
return meta
|