Skip to content

Commit 267559e

Browse files
committed
initial commit
update to annif mobile client
1 parent 16fd749 commit 267559e

File tree

8 files changed

+907
-0
lines changed

8 files changed

+907
-0
lines changed
Lines changed: 7 additions & 0 deletions
Loading

Annif-mobile/cgi-bin/ocr.cgi

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
import cgi
5+
import json
6+
import requests
7+
import os.path
8+
import io
9+
import functools
10+
import configparser
11+
12+
from PIL import Image
13+
14+
config = configparser.ConfigParser()
15+
config.read('/etc/annif/ocr.ini')
16+
17+
api_options = config['api']
18+
19+
OCRAPIKEY = api_options['apikey']
20+
DEFAULT_LANGUAGE = api_options['default_language']
21+
22+
MAXSIZE_BYTES = 1000000;
23+
MAXSIZE_PIXELS = (2600, 2600)
24+
25+
# map ISO 639-1 language codes into the ISO 639-3 codes that ocr.space uses
26+
LANGMAP = {
27+
'fi': 'fin',
28+
'sv': 'swe',
29+
'en': 'eng'
30+
}
31+
32+
sys.stdout.buffer.write(b"Content-Type: text/plain; charset=utf-8\r\n")
33+
sys.stdout.buffer.write(b"\r\n")
34+
35+
# Use EXIF information to flip and/or transpose the image as necessary
36+
def image_transpose_exif(im):
37+
exif_orientation_tag = 0x0112 # contains an integer, 1 through 8
38+
exif_transpose_sequences = [ # corresponding to the following
39+
[],
40+
[Image.FLIP_LEFT_RIGHT],
41+
[Image.ROTATE_180],
42+
[Image.FLIP_TOP_BOTTOM],
43+
[Image.FLIP_LEFT_RIGHT, Image.ROTATE_90],
44+
[Image.ROTATE_270],
45+
[Image.FLIP_TOP_BOTTOM, Image.ROTATE_90],
46+
[Image.ROTATE_90],
47+
]
48+
49+
try:
50+
seq = exif_transpose_sequences[im._getexif()[exif_orientation_tag] - 1]
51+
except Exception:
52+
return im
53+
else:
54+
return functools.reduce(lambda im, op: im.transpose(op), seq, im)
55+
56+
form = cgi.FieldStorage()
57+
if 'imagefile' in form:
58+
fileitem = form['imagefile']
59+
if fileitem.file:
60+
f = fileitem.file
61+
fn = fileitem.filename
62+
lang = form.getfirst('language')
63+
if lang is None:
64+
lang = DEFAULT_LANGUAGE
65+
if lang in LANGMAP:
66+
# map to ISO 639-3 code
67+
lang = LANGMAP[lang]
68+
else:
69+
# take the language and filename as command line parameters - for testing
70+
lang = sys.argv[1]
71+
fn = sys.argv[2]
72+
f = open(fn, 'rb')
73+
74+
image = image_transpose_exif(Image.open(f))
75+
76+
if image.size[0] > MAXSIZE_PIXELS[0] or image.size[1] > MAXSIZE_PIXELS[1]:
77+
# need to scale it
78+
image.thumbnail(MAXSIZE_PIXELS)
79+
f = io.BytesIO()
80+
image.save(f, 'JPEG')
81+
82+
f.seek(0)
83+
84+
payload = {
85+
'isOverlayRequired': False,
86+
'apikey': OCRAPIKEY,
87+
'language': lang
88+
}
89+
90+
r = requests.post('https://api.ocr.space/parse/image', files={fn: f}, data=payload)
91+
92+
results = r.json()
93+
94+
try:
95+
text = results['ParsedResults'][0]['ParsedText']
96+
sys.stdout.buffer.write(text.encode('UTF-8') + b"\r\n")
97+
except:
98+
sys.stdout.buffer.write(b"error\r\n")

Annif-mobile/compressor.min.js

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Annif-mobile/export-to-notes.png

7.94 KB
Loading

0 commit comments

Comments
 (0)