Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. import sys
  2. import asyncio
  3. import base64
  4. import copy
  5. import pprint
  6. # pip3 install winrt
  7. from winrt.windows.media.ocr import OcrEngine
  8. from winrt.windows.globalization import Language
  9. from winrt.windows.graphics.imaging import *
  10. from winrt.windows.security.cryptography import CryptographicBuffer
  11. from PIL import Image
  12.  
  13.  
  14. class rect:
  15. def __init__(self, x, y, w, h):
  16. self.x = x
  17. self.y = y
  18. self.width = w
  19. self.height = h
  20.  
  21. def __repr__(self):
  22. return 'rect(%d, %d, %d, %d)' % (self.x, self.y, self.width, self.height)
  23.  
  24. def right(self):
  25. return self.x + self.width
  26.  
  27. def bottom(self):
  28. return self.y + self.height
  29.  
  30. def set_right(self, value):
  31. self.width = value - self.x
  32.  
  33. def set_bottom(self, value):
  34. self.height = value - self.y
  35.  
  36.  
  37. def dump_rect(rtrect: winrt.windows.foundation.Rect):
  38. return rect(rtrect.x, rtrect.y, rtrect.width, rtrect.height)
  39.  
  40. def dump_ocrword(word):
  41. return {
  42. 'bounding_rect': dump_rect(word.bounding_rect),
  43. 'text': word.text
  44. }
  45.  
  46. def merge_words(words):
  47. if len(words) == 0:
  48. return words
  49. new_words = [copy.deepcopy(words[0])]
  50. words = words[1:]
  51. for word in words:
  52. lastnewword = new_words[-1]
  53. lastnewwordrect = new_words[-1]['bounding_rect']
  54. wordrect = word['bounding_rect']
  55. if len(word['text']) == 1 and wordrect.x - lastnewwordrect.right() <= wordrect.width * 0.2:
  56. lastnewword['text'] += word['text']
  57. lastnewwordrect.x = min((wordrect.x, lastnewwordrect.x))
  58. lastnewwordrect.y = min((wordrect.y, lastnewwordrect.y))
  59. lastnewwordrect.set_right(max((wordrect.right(), lastnewwordrect.right())))
  60. lastnewwordrect.set_bottom(max((wordrect.bottom(), lastnewwordrect.bottom())))
  61. else:
  62. new_words.append(copy.deepcopy(word))
  63. return new_words
  64.  
  65. def dump_ocrline(line):
  66. words = list(map(dump_ocrword, line.words))
  67. merged = merge_words(words)
  68. return {
  69. 'text': line.text,
  70. 'words': words,
  71. 'merged_words': merged,
  72. 'merged_text': ' '.join(map(lambda x: x['text'], merged))
  73. }
  74.  
  75. def dump_ocrresult(ocrresult):
  76. lines = list(map(dump_ocrline, ocrresult.lines))
  77. return {
  78. 'text': ocrresult.text,
  79. 'text_angle': ocrresult.text_angle.value if ocrresult.text_angle else None,
  80. 'lines': lines,
  81. 'merged_text': ' '.join(map(lambda x: x['merged_text'], lines))
  82. }
  83.  
  84.  
  85. def ibuffer(s):
  86. """create WinRT IBuffer instance from a bytes-like object"""
  87. return CryptographicBuffer.decode_from_base64_string(base64.b64encode(s).decode('ascii'))
  88.  
  89. def swbmp_from_pil_image(img):
  90. if img.mode != "RGBA":
  91. img = img.convert("RGBA")
  92. pybuf = img.tobytes()
  93. rtbuf = ibuffer(pybuf)
  94. return SoftwareBitmap.create_copy_from_buffer(rtbuf, BitmapPixelFormat.RGBA8, img.width, img.height, BitmapAlphaMode.STRAIGHT)
  95.  
  96.  
  97. async def ensure_coroutine(awaitable):
  98. return await awaitable
  99.  
  100. def blocking_wait(awaitable):
  101. return asyncio.run(ensure_coroutine(awaitable))
  102.  
  103. def recognize_pil_image(img, lang):
  104. lang = Language(lang)
  105. assert(OcrEngine.is_language_supported(lang))
  106. eng = OcrEngine.try_create_from_language(lang)
  107. swbmp = swbmp_from_pil_image(img)
  108. return dump_ocrresult(blocking_wait(eng.recognize_async(swbmp)))
  109.  
  110. def recognize_file(filename, lang):
  111. img = Image.open(filename)
  112. return recognize_pil_image(img, lang)
  113.  
  114.  
  115. if __name__ == '__main__':
  116. if 2 <= len(sys.argv) <= 3:
  117. lang = 'zh-hans-cn' if len(sys.argv) == 2 else sys.argv[1]
  118. result = recognize_file(sys.argv[-1], lang)
  119. pprint.pprint(result, width=128)
  120. else:
  121. print('usage: %s [language=zh-hans-cn] filename' % sys.argv[0])
  122. langs = list(map(lambda x: x.language_tag, OcrEngine.get_available_recognizer_languages()))
  123. print('installed languages:', ', '.join(langs))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement