From 8ed6cdcedf9e8bffc6eaaf173003245a5e6fe227 Mon Sep 17 00:00:00 2001 From: HarryZhu <7harryprince@gmail.com> Date: Wed, 15 Jun 2016 14:11:03 +0800 Subject: [PATCH] Create ocr.space_code_example_chinese --- ocr.space_code_example_chinese | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 ocr.space_code_example_chinese diff --git a/ocr.space_code_example_chinese b/ocr.space_code_example_chinese new file mode 100644 index 0000000..9005910 --- /dev/null +++ b/ocr.space_code_example_chinese @@ -0,0 +1,61 @@ +# in this example, we improve the return value more friendly. + +import requests +import json + + +def ocr_space_file(filename, overlay=False, api_key='helloworld', language='chs'): + """ OCR.space API request with local file. + Python3.5 - not tested on 2.7 + :param filename: Your file path & name. + :param overlay: Is OCR.space overlay required in your response. + Defaults to False. + :param api_key: OCR.space API key. + Defaults to 'helloworld'. + :param language: Language code to be used in OCR. + List of available language codes can be found on https://ocr.space/OCRAPI + Defaults to 'en'. + :return: Result in JSON format. + """ + + payload = {'isOverlayRequired': overlay, + 'apikey': api_key, + 'language': language, + } + with open(filename, 'rb') as f: + r = requests.post('https://api.ocr.space/parse/image', + files={filename: f}, + data=payload, + ) + return json.loads(r.content.decode()) + + +def ocr_space_url(url, overlay=False, api_key='helloworld', language='eng'): + """ OCR.space API request with remote file. + Python3.5 - not tested on 2.7 + :param url: Image url. + :param overlay: Is OCR.space overlay required in your response. + Defaults to False. + :param api_key: OCR.space API key. + Defaults to 'helloworld'. + :param language: Language code to be used in OCR. + List of available language codes can be found on https://ocr.space/OCRAPI + Defaults to 'en'. + :return: Result in JSON format. + """ + + payload = {'url': url, + 'isOverlayRequired': overlay, + 'apikey': api_key, + 'language': language, + } + r = requests.post('https://api.ocr.space/parse/image', + data=payload, + ) + return json.loads(r.content.decode()) + + +# Use examples: +# test_file = ocr_space_file(filename='example_image.png', language='chs') +test_url = ocr_space_url(url='http://ww4.sinaimg.cn/large/0060lm7Tgw1f4vooar90nj30ly106qjk.jpg',language='chs',overlay='true') +print test_url