Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. import requests
  2. import json
  3.  
  4. def ocr_space_file(filename, overlay=False, api_key='helloworld', language='eng'):
  5. """ OCR.space API request with local file.
  6. Python3.5 - not tested on 2.7
  7. :param filename: Your file path & name.
  8. :param overlay: Is OCR.space overlay required in your response.
  9. Defaults to False.
  10. :param api_key: 2fe3ae129e88957
  11. Defaults to 'helloworld'.
  12. :param language: Language code to be used in OCR.
  13. List of available language codes can be found on https://ocr.space/OCRAPI
  14. Defaults to 'en'.
  15. :return: Result in JSON format.
  16. """
  17.  
  18. payload = {'isOverlayRequired': overlay,
  19. 'apikey': api_key,
  20. 'language': language,
  21. }
  22. with open(filename, 'rb') as f:
  23. r = requests.post('https://api.ocr.space/parse/image',
  24. files={filename: f},
  25. data=payload,
  26. )
  27. return r.content.decode()
  28.  
  29.  
  30. def ocr_space_url(url, overlay=False, api_key='helloworld', language='eng'):
  31. """ OCR.space API request with remote file.
  32. Python3.5 - not tested on 2.7
  33. :param url: Image url.
  34. :param overlay: Is OCR.space overlay required in your response.
  35. Defaults to False.
  36. :param api_key: 2fe3ae129e88957
  37. Defaults to 'helloworld'.
  38. :param language: Language code to be used in OCR.
  39. List of available language codes can be found on https://ocr.space/OCRAPI
  40. Defaults to 'en'.
  41. :return: Result in JSON format.
  42. """
  43.  
  44. payload = {'url': url,
  45. 'isOverlayRequired': overlay,
  46. 'apikey': api_key,
  47. 'language': language,
  48. }
  49. m = r.content.decode()
  50. jsonstr = json.loads(m)
  51. print (jsonstr["ParsedResults"][0]["ParsedText"])
  52.  
  53. ocr_space_file(filename='example_image.png', language='eng')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement