mirror of
https://github.com/MCV-Software/TWBlue.git
synced 2025-07-18 06:06:06 -04:00
Added OCR support via the ocr.space API. Closes #93
This commit is contained in:
37
src/extra/ocr/OCRSpace.py
Normal file
37
src/extra/ocr/OCRSpace.py
Normal 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
|
||||
|
2
src/extra/ocr/__init__.py
Normal file
2
src/extra/ocr/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import OCRSpace
|
Reference in New Issue
Block a user