-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
73 lines (67 loc) · 2.19 KB
/
Copy pathtasks.py
File metadata and controls
73 lines (67 loc) · 2.19 KB
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
from dbexchanges import Session, engine, Base, OCRImage, OCRSchedule
from celery import Celery
from io import BytesIO
from PIL import Image
import logging
import time
import random
from string import ascii_letters
app = Celery('tasks', broker='redis://127.0.0.1:6666', backend="redis://127.0.0.1:7777")
logging.basicConfig(filename='run.log', level=logging.DEBUG)
session = Session()
def get_image_from_db(image_url: str):
"""
retrieves image from db
:param image_url:
:return:
"""
r = session.query(OCRImage.color).filter(OCRImage.url == image_url).first()
return r[0]
def save_processed_image_to_db(url: str, image):
"""
saves image to db
:param url:
:param image:
:return:
"""
stream = BytesIO()
image.save(stream, format="PNG")
session.query(OCRImage).filter(OCRImage.url == url).update({"bw": stream.getvalue()})
session.commit()
stream.close()
@app.task
def desaturate(image_url: str, image_hash:str):
"""
processes image to b&w
:param image_url:
:param image_hash:
:return:
"""
image = get_image_from_db(image_url)
try:
c = Image.open(BytesIO(image))
l = c.convert('L')
rgb = l.convert("RGB")
save_processed_image_to_db(image_url, rgb)
return image_hash
except OSError as ose:
logging.error("couldn't desaturate image %s, the error was: %s" % (image_url, ose))
@app.task
def ocr_api_call(imhash: str):
"""
placeholder for a network api call
:param imhash:
:return:
"""
r = session.query(OCRImage).filter(OCRImage.imhash==imhash).first()
logging.info("processing image %s" % r.imhash)
session.query(OCRImage).filter(OCRImage.imhash == imhash).update({"ocr_status": OCRSchedule.PENDING})
session.commit()
#### TODO try catch/timeout for error states
time.sleep(random.randrange(0,60))
# generate random string
mock_ocr_text = "".join([random.choice(ascii_letters) for x in range(0,50)])
####
session.query(OCRImage).filter(OCRImage.imhash == imhash).update({"ocr_status": OCRSchedule.DONE})
session.query(OCRImage).filter(OCRImage.imhash == imhash).update({"ocr_result": mock_ocr_text})
session.commit()