sct_to_text.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. '''Created Feb 28, 2023 deeejas'''
  2. import re
  3. import os
  4. import platform
  5. import subprocess as sp
  6. from collections import deque
  7. import cv2
  8. import keyboard as kb
  9. import numpy as np
  10. import pywinctl as gw
  11. import pytesseract
  12. import mss
  13. from plyer import notification
  14. container = deque(maxlen=500)
  15. if platform.system() == 'Windows':
  16. pytesseract.pytesseract.tesseract_cmd = '{}/tesseract.exe'.format(os.getenv('TESSERACT'))
  17. # Noise Removal
  18. def noise_removal(image):
  19. kernel = np.ones((1, 1), np.uint8)
  20. image = cv2.dilate(image, kernel, iterations=1)
  21. kernel = np.ones((1, 1), np.uint8)
  22. image = cv2.erode(image, kernel, iterations=1)
  23. image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
  24. image = cv2.medianBlur(image, 3)
  25. return (image)
  26. # Dilation and Erosion
  27. def thin_font(image):
  28. image = cv2.bitwise_not(image)
  29. kernel = np.ones((2, 2), np.uint8)
  30. image = cv2.erode(image, kernel, iterations=1)
  31. image = cv2.bitwise_not(image)
  32. return (image)
  33. def thick_font(image):
  34. image = cv2.bitwise_not(image)
  35. kernel = np.ones((2, 2), np.uint8)
  36. image = cv2.dilate(image, kernel, iterations=1)
  37. image = cv2.bitwise_not(image)
  38. return (image)
  39. def copy_rec():
  40. # wh = gw.getScreenSize()._asdict()
  41. # mon = {'top': 0, 'left': 0, **wh}
  42. title = 'ANAF depunere declaratii'
  43. timeout = 3 # seconds
  44. app_name = 'e-guvernare'
  45. app_icon = 'not_icon.ico'
  46. try:
  47. with mss.mss() as sct:
  48. monitor = sct.monitors[1]
  49. left = monitor['left'] + monitor['width'] * 35 // 100 # 50% from the left
  50. top = monitor['top'] + monitor['height'] * 25 // 100 # 50% from the top
  51. right = left + 800 # 400px width
  52. lower = top + 100 # 400px height
  53. bbox = (left, top, right, lower)
  54. chrome_win = gw.getWindowsWithTitle('decl.anaf.mfinante.gov.ro/WAS6DUS/displayFile.do - Brave')[0]
  55. active_win = gw.getActiveWindow()
  56. if chrome_win.title in active_win.title:
  57. sct_img = sct.grab(bbox)
  58. # mss.tools.to_png(sct_img.rgb, sct_img.size, output='screenshot.png')
  59. im = np.asarray(sct_img) # -> good approch with cv2
  60. # im = Image.open('recipisa.png').convert('RGB')
  61. # im = np.array(im)
  62. # img = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
  63. # gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
  64. im_rgb = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
  65. _thresh, im_bw = cv2.threshold(im_rgb, 210, 230, cv2.THRESH_BINARY)
  66. # df = pytesseract.image_to_data(gray, output_type=pytesseract.Output.DATAFRAME)
  67. text = pytesseract.image_to_string(im_bw)
  68. receipt_index = re.search(r'(?<=Indexul este )[0-9]+', text)
  69. if receipt_index:
  70. receipt_index = receipt_index.group()
  71. # filtered_df = df.loc[(df['word_num'] == 14) & (df['block_num'] == 11)]
  72. # receipt_index = filtered_df.iloc[0]['text']
  73. if receipt_index not in container:
  74. item1 = sp.run(['copyq', 'add', receipt_index], capture_output=True, text=True)
  75. print(item1.stdout)
  76. container.appendleft(receipt_index)
  77. titleb = f'{receipt_index}'
  78. print(len(container), f'index <{receipt_index}> appended')
  79. notification.notify(title=titleb,
  80. message='index',
  81. app_name=app_name,
  82. app_icon=app_icon,
  83. timeout=timeout)
  84. else:
  85. notification.notify(title=title,
  86. message=f'index <{receipt_index}> already in container',
  87. app_name=app_name,
  88. app_icon=app_icon,
  89. timeout=timeout)
  90. else:
  91. notification.notify(title=title,
  92. message='index not found in re.search pattern',
  93. app_name=app_name,
  94. app_icon=app_icon,
  95. timeout=timeout)
  96. else:
  97. notification.notify(title=title,
  98. message='chrome window not found',
  99. app_name=app_name,
  100. app_icon=app_icon,
  101. timeout=timeout)
  102. except Exception as exc_:
  103. raise exc_(str(exc_))
  104. print('wndow not active')
  105. def main():
  106. kb.add_hotkey('`+1', copy_rec, args=())
  107. kb.wait()
  108. if __name__ == '__main__':
  109. main()
  110. # print(gw.getAllTitles())