Added OCR support via the ocr.space API. Closes #93

This commit is contained in:
2017-01-03 08:30:58 -06:00
parent 516acb501a
commit ec58d02bb3
10 changed files with 78 additions and 6 deletions

37
src/extra/ocr/OCRSpace.py Normal file
View File

@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
""" original module taken and modified from https://github.com/ctoth/cloudOCR"""
import requests
class APIError(Exception):
pass
class OCRSpaceAPI(object):
def __init__(self, key="4e72ae996f88957", url='https://api.ocr.space/parse/image'):
self.key = key
self.url = url
def OCR_URL(self, url, overlay=False):
payload = {
'url': url,
'isOverlayRequired': overlay,
'apikey': self.key,
}
r = requests.post(self.url, data=payload)
result = r.json()['ParsedResults'][0]
if result['ErrorMessage']:
raise APIError(result['ErrorMessage'])
return result
def OCR_file(self, fileobj, overlay=False):
payload = {
'isOverlayRequired': overlay,
'apikey': self.key,
'lang': 'es',
}
r = requests.post(self.url, data=payload, files={'file': fileobj})
results = r.json()['ParsedResults']
if results[0]['ErrorMessage']:
raise APIError(results[0]['ErrorMessage'])
return results

View File

@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
import OCRSpace