summaryrefslogtreecommitdiff
path: root/megapixels/app/server/tasks/blur.py
blob: 3b7e20be4cda743cf60905d16a85d78e383360c8 (plain)
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
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

from app.server.tasks import celery

from celery.utils.log import get_task_logger
celery_logger = get_task_logger(__name__)
import imutils

@celery.task(bind=True)
def blur_task(self, uuid_name, extra):
  """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'
  })

  self.update_state(
    state = 'PROCESSING',
    meta = {
      'percent': 0.25,
      'message': 'Applying blur',
      'uuid': uuid_name
    })

  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(render_dir, fn)
  im_blur_pil.save(fpath, 'JPEG', quality=95)

  files.append({
    'title': 'Blurred image',
    'fn': render_uri + uuid_name + '_blur.jpg'
  })

  time.sleep(3)

  self.update_state(
    state = 'PROCESSING',
    meta = {
      'percent': 0.75,
      'message': 'Sleeping some more',
      'uuid': uuid_name
    })
  time.sleep(2)

  data = {
    'uuid': uuid_name,
    'date': str(datetime.datetime.now()),
    '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)

  celery_logger.debug('ok')
    
  return {
    'percent': 100,
    'state': 'complete',
    'uuid': uuid_name,
  }