Guest User

Untitled

a guest
Nov 17th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. #!/bin/python
  2. import locale
  3.  
  4. locale.setlocale(locale.LC_ALL, 'C')
  5. from tesserocr import PyTessBaseAPI, RIL, iterate_level
  6. import sys
  7. import cv2
  8. import numpy as np
  9. from PIL import Image
  10. from crop import cropa
  11. import unidecode
  12. import os
  13. import time
  14. locale.setlocale(locale.LC_ALL, 'pt_BR.UTF-8')
  15.  
  16. def processa(path='imagem.jpg'):
  17.  
  18. locale.setlocale(locale.LC_ALL, 'C')
  19. with PyTessBaseAPI(lang='por') as api:
  20. start_time = time.time()
  21. c = cropa(path)
  22. if '.png' in path:
  23. api.SetImageFile('tmp.png')
  24. elif '.jpeg' in path:
  25. api.SetImageFile('tmp.jpeg')
  26. else:
  27. api.SetImageFile('tmp.jpg')
  28.  
  29. api.SetVariable("save_blob_choices", "T")
  30.  
  31. """"
  32. #ima = cv2.imread(path)
  33. #ima = cv2.resize(ima, (1000,900))
  34. #api.SetImage(Image.fromarray(ima))
  35. lines = api.GetTextlines()
  36. print(list(lines))
  37. for im in lines:
  38. #ia= cv2.rectangle(ima,(im[1]['x'], im[1]['y']),(im[1]['x'] + im[1]['w'],im[1]['y'] + im[1]['h']),(0,255,0),3)
  39. #cv2.imshow("kk", ia)
  40. #cv2.waitKey(0)
  41. api.SetRectangle(im[1]['x'], im[1]['y'], im[1]['w'], im[1]['h'])
  42. api.Recognize()
  43. print(api.GetUTF8Text())
  44. """
  45. api.Recognize()
  46. ri = api.GetIterator()
  47. level = RIL.TEXTLINE
  48.  
  49. #print(' '.join(word for word in api.AllWords()))
  50. for r in iterate_level(ri, level):
  51. symbol = r.GetUTF8Text(level) # r == ri
  52. conf = r.Confidence(level)
  53. #print(symbol, end='')
  54. #print(api.GetUTF8Text())
  55.  
  56. text = api.GetUTF8Text()
  57.  
  58. locale.setlocale(locale.LC_ALL, 'pt_BR.UTF-8')
  59. if text != None:
  60. text = unidecode.unidecode(text)
  61. file = open('textscanner.txt', 'w')
  62. file.write(text)
  63. file.close()
  64. os.system('python2 translator.py textscanner.txt')
  65. print("Elapsed time: {}".format(time.time() - start_time))
  66.  
  67. if __name__ == '__main__':
  68. processa()
Add Comment
Please, Sign In to add comment