Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import json
- def ocr_space_file(filename, overlay=False, api_key='helloworld', language='eng'):
- """ 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: 2fe3ae129e88957
- 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 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: 2fe3ae129e88957
- 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,
- }
- m = r.content.decode()
- jsonstr = json.loads(m)
- print (jsonstr["ParsedResults"][0]["ParsedText"])
- ocr_space_file(filename='example_image.png', language='eng')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement