123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- '''Created Feb 28, 2023 deeejas'''
- import re
- import os
- import platform
- import subprocess as sp
- from collections import deque
- import cv2
- import keyboard as kb
- import numpy as np
- import pywinctl as gw
- import pytesseract
- import mss
- from plyer import notification
- container = deque(maxlen=500)
- if platform.system() == 'Windows':
- pytesseract.pytesseract.tesseract_cmd = '{}/tesseract.exe'.format(os.getenv('TESSERACT'))
- # Noise Removal
- def noise_removal(image):
- kernel = np.ones((1, 1), np.uint8)
- image = cv2.dilate(image, kernel, iterations=1)
- kernel = np.ones((1, 1), np.uint8)
- image = cv2.erode(image, kernel, iterations=1)
- image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
- image = cv2.medianBlur(image, 3)
- return (image)
- # Dilation and Erosion
- def thin_font(image):
- image = cv2.bitwise_not(image)
- kernel = np.ones((2, 2), np.uint8)
- image = cv2.erode(image, kernel, iterations=1)
- image = cv2.bitwise_not(image)
- return (image)
- def thick_font(image):
- image = cv2.bitwise_not(image)
- kernel = np.ones((2, 2), np.uint8)
- image = cv2.dilate(image, kernel, iterations=1)
- image = cv2.bitwise_not(image)
- return (image)
- def copy_rec():
- # wh = gw.getScreenSize()._asdict()
- # mon = {'top': 0, 'left': 0, **wh}
- title = 'ANAF depunere declaratii'
- timeout = 3 # seconds
- app_name = 'e-guvernare'
- app_icon = 'not_icon.ico'
- try:
- with mss.mss() as sct:
- monitor = sct.monitors[1]
- left = monitor['left'] + monitor['width'] * 35 // 100 # 50% from the left
- top = monitor['top'] + monitor['height'] * 25 // 100 # 50% from the top
- right = left + 800 # 400px width
- lower = top + 100 # 400px height
- bbox = (left, top, right, lower)
- chrome_win = gw.getWindowsWithTitle('decl.anaf.mfinante.gov.ro/WAS6DUS/displayFile.do - Brave')[0]
- active_win = gw.getActiveWindow()
- if chrome_win.title in active_win.title:
- sct_img = sct.grab(bbox)
- # mss.tools.to_png(sct_img.rgb, sct_img.size, output='screenshot.png')
- im = np.asarray(sct_img) # -> good approch with cv2
- # im = Image.open('recipisa.png').convert('RGB')
- # im = np.array(im)
- # img = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
- # gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
- im_rgb = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
- _thresh, im_bw = cv2.threshold(im_rgb, 210, 230, cv2.THRESH_BINARY)
- # df = pytesseract.image_to_data(gray, output_type=pytesseract.Output.DATAFRAME)
- text = pytesseract.image_to_string(im_bw)
- receipt_index = re.search(r'(?<=Indexul este )[0-9]+', text)
- if receipt_index:
- receipt_index = receipt_index.group()
- # filtered_df = df.loc[(df['word_num'] == 14) & (df['block_num'] == 11)]
- # receipt_index = filtered_df.iloc[0]['text']
- if receipt_index not in container:
- item1 = sp.run(['copyq', 'add', receipt_index], capture_output=True, text=True)
- print(item1.stdout)
- container.appendleft(receipt_index)
- titleb = f'{receipt_index}'
- print(len(container), f'index <{receipt_index}> appended')
- notification.notify(title=titleb,
- message='index',
- app_name=app_name,
- app_icon=app_icon,
- timeout=timeout)
- else:
- notification.notify(title=title,
- message=f'index <{receipt_index}> already in container',
- app_name=app_name,
- app_icon=app_icon,
- timeout=timeout)
- else:
- notification.notify(title=title,
- message='index not found in re.search pattern',
- app_name=app_name,
- app_icon=app_icon,
- timeout=timeout)
- else:
- notification.notify(title=title,
- message='chrome window not found',
- app_name=app_name,
- app_icon=app_icon,
- timeout=timeout)
- except Exception as exc_:
- raise exc_(str(exc_))
- print('wndow not active')
- def main():
- kb.add_hotkey('`+1', copy_rec, args=())
- kb.wait()
- if __name__ == '__main__':
- main()
- # print(gw.getAllTitles())
|